示例#1
0
        public async Task <SourceServerResponseDTO> GetPackages(
            string sourceName,
            string url,
            string arch,
            string model,
            VersionDTO versionDto,
            bool isBeta,
            string customUserAgent,
            bool isSearch,
            string keyword    = null,
            bool useGetMethod = false)
        {
            Stopwatch stopwatch = new Stopwatch();

            string            errorMessage;
            ResultFrom        resultFrom;
            double?           cacheOld   = null;
            ParametersDTO     parameters = new ParametersDTO(sourceName, model, versionDto, isBeta, keyword);
            SearchLogEntryDTO logEntry   = new SearchLogEntryDTO(parameters);

            logEntry.RequestType = isSearch ? RequestType.Search : RequestType.Browse;
            logEntry.LogType     = LogType.Parameters;
            logger.LogInformation(Utils.GetSearchLogEntryString(logEntry));
            logEntry.LogType = LogType.Result;
            stopwatch.Start();
            var cacheResult = await cacheService.GetSpkResponseFromCache(sourceName, arch, model, versionDto.Build.ToString(), isBeta);

            SpkResult result;

            if (!cacheResult.HasValidCache)
            {
                string unique            = $"synology_{arch}_{model}"; //TODO: DSM provide model without leading "DS" or "RS", so we should do the same some day.
                var    parametersRequest = PrepareParametersForRequest(arch, unique, versionDto, isBeta, customUserAgent, out var userAgent);

                IDownloadService downloadService = downloadFactory.GetDefaultDownloadService();
                var response = await downloadService.Execute(url, parametersRequest, userAgent, useGetMethod);

                if (response.Success)
                {
                    try
                    {
                        result     = ParseResponse(sourceName, url, arch, model, versionDto, isBeta, response.Content);
                        resultFrom = ResultFrom.Server;
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, $"Unable to parse response from {url}: {response.Content}");
                        if (cacheResult.Cache != null)
                        {
                            logger.LogInformation("Returning data from cache");
                            result     = cacheResult.Cache.SpkResult;
                            resultFrom = ResultFrom.Cache;
                            cacheOld   = cacheResult.Cache.CacheOld;
                        }
                        else
                        {
                            logger.LogError($"Error getting response for url: {url}. No cache available.");
                            return(new SourceServerResponseDTO(false, "No data from source server. No cache available", parameters, null, ResultFrom.NotSpecified, null));
                        }
                    }
                }
                else if (cacheResult.Cache != null)
                {
                    logger.LogError($"Error getting response for url: {url}: {response.ErrorMessage}");
                    result     = cacheResult.Cache.SpkResult;
                    resultFrom = ResultFrom.ExpiredCache;
                    cacheOld   = cacheResult.Cache.CacheOld;
                }
                else //no cache && no server response
                {
                    errorMessage = $"{response.ErrorMessage}";
                    logger.LogError($"Error getting response for url: {url}: {errorMessage}");
                    return(new SourceServerResponseDTO(false, errorMessage, parameters, null, ResultFrom.NotSpecified, null));
                }
            }
            else // get data from Valid cache
            {
                result     = cacheResult.Cache.SpkResult;
                resultFrom = ResultFrom.Cache;
                cacheOld   = cacheResult.Cache.CacheOld;
            }

            if (result != null)
            {
                var finalResult = await GenerateResult(sourceName, keyword, parameters, result, resultFrom, cacheOld);

                stopwatch.Stop();
                logEntry.ResultFrom    = resultFrom;
                logEntry.CacheOld      = cacheOld;
                logEntry.ExecutionTime = stopwatch.ElapsedMilliseconds;
                logger.LogInformation(Utils.GetSearchLogEntryString(logEntry));
                return(finalResult);
            }
            else
            {
                errorMessage = "Spk result is empty";
                stopwatch.Stop();
                logEntry.ResultFrom    = ResultFrom.NotSpecified;
                logEntry.CacheOld      = null;
                logEntry.ExecutionTime = stopwatch.ElapsedMilliseconds;
                logger.LogWarning("Spk result is empty {0}", Utils.GetSearchLogEntryString(logEntry));
                return(new SourceServerResponseDTO(false, errorMessage, parameters, null, resultFrom, cacheOld));
            }
        }
示例#2
0
        public async Task <RawSpkResultDto> GetRawPackages(
            string sourceName,
            string url,
            string arch,
            string unique,
            VersionDTO versionDto,
            bool isBeta,
            string customUserAgent,
            bool isSearch,
            string keyword    = null,
            bool useGetMethod = false
            )
        {
            RawSpkResultDto   result;
            ParametersDTO     parameters = new ParametersDTO(sourceName, unique, versionDto, isBeta, keyword);
            SearchLogEntryDTO logEntry   = new SearchLogEntryDTO(parameters);

            logEntry.RequestType = isSearch ? RequestType.Search : RequestType.Browse;
            logEntry.LogType     = LogType.Parameters;
            logger.LogInformation(Utils.GetSearchLogEntryString(logEntry));
            logEntry.LogType = LogType.Result;
            var stopwatch   = Stopwatch.StartNew();
            var cacheResult = await cacheService.GetSpkResponseForRepositoryFromCache(sourceName, arch, versionDto.Build.ToString(), isBeta);

            if (!cacheResult.HasValidCache)
            {
                var parametersRequest = PrepareParametersForRequest(arch, unique, versionDto, isBeta, customUserAgent, out var userAgent);

                IDownloadService downloadService = downloadFactory.GetDefaultDownloadService();
                var response = await downloadService.Execute(url, parametersRequest, userAgent, useGetMethod);

                if (response.Success)
                {
                    logEntry.ResultFrom = ResultFrom.Server;
                    try
                    {
                        var rawResponse = ParseResponse(sourceName, url, arch, unique, versionDto, isBeta, response.Content);
                        result = new RawSpkResultDto(rawResponse, null);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, $"Could not parse response from server {url}");
                        result = new RawSpkResultDto(null, ex.Message);
                    }
                }
                else
                {
                    logger.LogError($"Could not get any data from the server {url}");
                    result = new RawSpkResultDto(null, $"Could not get any data from the server {url}");
                }
            }
            else //return response from valid cache
            {
                result              = new RawSpkResultDto(cacheResult.Cache?.SpkResult, null);
                logEntry.CacheOld   = cacheResult.Cache.CacheOld;
                logEntry.LogType    = LogType.Result;
                logEntry.ResultFrom = ResultFrom.Cache;
            }

            stopwatch.Stop();
            logEntry.ExecutionTime = stopwatch.ElapsedMilliseconds;
            logger.LogInformation(Utils.GetSearchLogEntryString(logEntry));
            return(result);
        }