<commit_before>﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

using SFA.DAS.NLog.Logger;
using SFA.DAS.PAS.ContractAgreements.WebJob.ContractFeed;
using SFA.DAS.ProviderApprenticeshipsService.Domain.Interfaces;

namespace SFA.DAS.PAS.ContractAgreements.WebJob
{
    public class ProviderAgreementStatusService : IProviderAgreementStatusService
    {
        private readonly IContractDataProvider _dataProvider;
        private readonly IProviderAgreementStatusRepository _repository;

        private readonly ILog _logger;

        public ProviderAgreementStatusService(
            IContractDataProvider dataProvider, 
            IProviderAgreementStatusRepository repository,
            ILog logger)
        {
            _dataProvider = dataProvider;
            _repository = repository;
            _logger = logger;
        }

        public async Task UpdateProviderAgreementStatuses()
        {
            var time = new Stopwatch();
            time.Start();

            var lastContract = await _repository.GetMostRecentContractFeedEvent();
            var mostRecentPageNumber = await _repository.GetMostRecentPageNumber() +1;
            if (lastContract == null) mostRecentPageNumber = 1;

            _logger.Info($"Bookmark: {lastContract?.Id}, Latest page read: {mostRecentPageNumber}");

            var lastRun = _dataProvider.ReadEvents(mostRecentPageNumber, lastContract?.Id ?? Guid.Empty, (eventPageNumber, events) =>
                {
                    var contractFeedEvents = events.ToList();

                    foreach (var contractFeedEvent in contractFeedEvents)
                    {
                        contractFeedEvent.PageNumber = eventPageNumber;
                        _repository.AddContractEvent(contractFeedEvent);
                    }
                });

            time.Stop();
            lastRun.ExecutionTimeMs = time.ElapsedMilliseconds;
            await _repository.SaveLastRun(lastRun);
        }
    }

    public interface IProviderAgreementStatusService
    {
        Task UpdateProviderAgreementStatuses();
    }
}<commit_msg>Added logging when no contracts in db but have ready numerous pages. (#126)<commit_after>﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

using SFA.DAS.NLog.Logger;
using SFA.DAS.PAS.ContractAgreements.WebJob.ContractFeed;
using SFA.DAS.ProviderApprenticeshipsService.Domain.Interfaces;

namespace SFA.DAS.PAS.ContractAgreements.WebJob
{
    public class ProviderAgreementStatusService : IProviderAgreementStatusService
    {
        private readonly IContractDataProvider _dataProvider;
        private readonly IProviderAgreementStatusRepository _repository;

        private readonly ILog _logger;

        public ProviderAgreementStatusService(
            IContractDataProvider dataProvider, 
            IProviderAgreementStatusRepository repository,
            ILog logger)
        {
            _dataProvider = dataProvider;
            _repository = repository;
            _logger = logger;
        }

        public async Task UpdateProviderAgreementStatuses()
        {
            var time = new Stopwatch();
            time.Start();

            var lastContract = await _repository.GetMostRecentContractFeedEvent();
            var lastFullPageNumberRead = await _repository.GetMostRecentPageNumber();
            var nextPageNumberToRead = lastFullPageNumberRead + 1;

            if (lastContract == null && nextPageNumberToRead > 1)
            {
                _logger.Warn($"The database doesn't currently contain any valid contracts from the feed after {lastFullPageNumberRead} pages have been read.");
            }

            _logger.Info($"Bookmark: {lastContract?.Id}, Latest page read: {lastFullPageNumberRead}");

            var lastRun = _dataProvider.ReadEvents(nextPageNumberToRead, lastContract?.Id ?? Guid.Empty, (eventPageNumber, events) =>
                {
                    var contractFeedEvents = events.ToList();

                    foreach (var contractFeedEvent in contractFeedEvents)
                    {
                        contractFeedEvent.PageNumber = eventPageNumber;
                        _repository.AddContractEvent(contractFeedEvent);
                    }
                });

            if (lastRun.ContractCount > 0)
            {
                _logger.Info($"Inserted {lastRun.ContractCount} contracts into the database. LastPageRead: {lastRun.NewLastReadPageNumber}");
            }

            time.Stop();
            lastRun.ExecutionTimeMs = time.ElapsedMilliseconds;
            await _repository.SaveLastRun(lastRun);
        }
    }

    public interface IProviderAgreementStatusService
    {
        Task UpdateProviderAgreementStatuses();
    }
}
{'fraction_non_alphanumeric': 0.07004780710870921, 'fraction_numerical': 0.0016628559551028891, 'mean_word_length': 2.3556485355648538, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3022956', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1142, 'n_words': 249}