public async Task <SumoSearchJobStatusResponse> GetJobStatus(SumoCreateSearchJobResponse createSearchJobResponse)
        {
            var request  = new HttpRequestMessage(HttpMethod.Get, createSearchJobResponse.Location);
            var response = await SendAsync(request, createSearchJobResponse.CookieContainer);

            response.EnsureSuccessStatusCode();

            var body = await response.Content.ReadAsStringAsync();

            return(JsonConvert.DeserializeObject <SumoSearchJobStatusResponse>(body));
        }
        public async Task <List <Log> > GetMessages(Guid requestUuid, SumoCreateSearchJobResponse createSearchJobResponse, int offset, int limit)
        {
            var request  = new HttpRequestMessage(HttpMethod.Get, new Uri($"{createSearchJobResponse.Location}/messages?offset={offset}&limit={limit}"));
            var response = await SendAsync(request, createSearchJobResponse.CookieContainer);

            response.EnsureSuccessStatusCode();

            var body = await response.Content.ReadAsStringAsync();

            var rawSumoMessages = JsonConvert.DeserializeObject <SumoLogMessageResponse>(body);

            return(ExtractLogs(requestUuid, rawSumoMessages));
        }
        private async Task <SumoSurveySearchJobResult> SurveyLogs(Request request)
        {
            SumoCreateSearchJobResponse sumoCreateSearchJobResponse = null;
            var sumoSearchJobStatusResponse = new SumoSearchJobStatusResponse {
                MessageCount = 0
            };
            var previousMessageCountCount = -1;
            var loopTimes = 0;

            while (MaximumSearchLoopTimes > loopTimes &&
                   (sumoSearchJobStatusResponse.MessageCount != previousMessageCountCount))
            {
                previousMessageCountCount = sumoSearchJobStatusResponse.MessageCount;
                if (loopTimes > 0)
                {
                    await Task.Delay(_appSettings.SumoLogIntervalWaitTimeBeforeSearchQueryMs);
                }

                // delete previous sumoCreateSearchJobResponse search jobid
                sumoCreateSearchJobResponse =
                    await _sumoLogicLogRepository.CreateSearchJob(request.Query, request.From, request.To);

                sumoSearchJobStatusResponse.State = null;
                while (
                    sumoSearchJobStatusResponse.State == null ||
                    sumoSearchJobStatusResponse.State == SumoSearchJobStatusConstants.NotStarted ||
                    sumoSearchJobStatusResponse.State == SumoSearchJobStatusConstants.GatheringResults)
                {
                    await Task.Delay(1000);

                    sumoSearchJobStatusResponse =
                        await _sumoLogicLogRepository.GetJobStatus(sumoCreateSearchJobResponse);
                }

                if (sumoSearchJobStatusResponse.State == SumoSearchJobStatusConstants.ForcePaused)
                {
                    break;
                }

                loopTimes++;
            }

            return(new SumoSurveySearchJobResult
            {
                SumoCreateSearchJobResponse = sumoCreateSearchJobResponse,
                SumoSearchJobStatusResponse = sumoSearchJobStatusResponse
            });
        }
        private async Task FetchAndSaveLogs(Request request, SumoCreateSearchJobResponse sumoCreateSearchJobResponse, int expectedMessageCount)
        {
            _dataAccess.DeleteLogs(request.RequestUuid);

            var offset             = 0;
            var batchLimit         = 300;
            var actualMessageCount = 0;
            var apps          = new List <string>();
            var exceptionApps = new List <string>();

            while (expectedMessageCount > actualMessageCount)
            {
                var partialMessages = await _sumoLogicLogRepository.GetMessages(
                    request.RequestUuid, sumoCreateSearchJobResponse, offset, batchLimit);

                if (!partialMessages.Any())
                {
                    break;
                }

                _dataAccess.AddLogs(request.RequestUuid, partialMessages);
                actualMessageCount += partialMessages.Count;

                foreach (var app in partialMessages.Where(log => !string.IsNullOrWhiteSpace(log.ComponentName)).Select(log => log.ComponentName).Distinct())
                {
                    if (!apps.Contains(app))
                    {
                        apps.Add(app);
                    }
                }
                foreach (var exceptionApp in partialMessages.Where(log => log.HasException && !string.IsNullOrWhiteSpace(log.ComponentName)).Select(log => log.ComponentName).Distinct())
                {
                    if (!exceptionApps.Contains(exceptionApp))
                    {
                        exceptionApps.Add(exceptionApp);
                    }
                }

                offset += batchLimit;
            }

            _dataAccess.UpdateRequest(request.RequestUuid, apps, exceptionApps);
        }