public void Generate(string reportFolder)
        {
            _log.Info("ReportGenerator started");

            var dates = _dateCalculator.Calculate();
            _log.InfoFormat("Report ExtractDateTime: {0}, PowerService request date: {1}", dates.ExtractDateTime, dates.RequestDate);

            // added a retry to the powerservice as this is an external call and not something we have control over
            // retry could be changed to catch specific exceptions
            var trades = new RetryBlock<IList<PowerTrade>>(() => _powerService.GetTrades(dates.RequestDate).ToList())
                .WithMaxRetries(3)
                .WithWaitBetweenRetries(1000)
                .WithActionBetweenRetries(() => _log.Warn(("Retrying after error during GetTrades")))
                .Execute();

            _log.InfoFormat("{0} trade returned", trades.Count);

            var position = _positionAggregator.Aggregate(dates.RequestDate, trades);

            var fileName = _fileNameGenerator.Generate(dates.ExtractDateTime);
            var content = _reportContentWriter.Write(position);
            var fullFilePath = Path.Combine(reportFolder, fileName);
            _file.WriteAllText(fullFilePath, content);
            _log.InfoFormat("ReportGenerator complete: {0}", fullFilePath);
        }
예제 #2
0
        public void Generate(string reportFolder)
        {
            _log.Info("ReportGenerator started");

            var dates = _dateCalculator.Calculate();

            _log.InfoFormat("Report ExtractDateTime: {0}, PowerService request date: {1}", dates.ExtractDateTime, dates.RequestDate);

            // added a retry to the power service as this is an external call and not something we have control over
            // retry could be changed to catch specific exceptions
            var trades = new RetryBlock <IList <PowerTrade> >(() => _powerService.GetTrades(dates.RequestDate).ToList())
                         .WithMaxRetries(3)
                         .WithWaitBetweenRetries(1000)
                         .WithActionBetweenRetries(ex => _log.Warn(($"Retrying after error during GetTrades, exception: {ex}")))
                         .Execute();

            _log.InfoFormat("{0} trade returned", trades.Count);

            var position = _positionAggregator.Aggregate(dates.RequestDate, trades);

            var fileName     = _fileNameGenerator.Generate(dates.ExtractDateTime);
            var content      = _reportContentWriter.Write(position);
            var fullFilePath = Path.Combine(reportFolder, fileName);

            _file.WriteAllText(fullFilePath, content);
            _log.InfoFormat("ReportGenerator complete: {0}", fullFilePath);
        }
예제 #3
0
        public async Task ExecuteAsync(CancellationToken cancel)
        {
            // data center
            var existingDcNames = (await dcTargetRepo.GetKeys(cancel)).ToList();

            logger.LogInformation($"total of {existingDcNames.Count} dc already exist in target db");

            Func <IList <DataCenter>, CancellationToken, Task> onDataCentersReceived = async(list, ct) =>
            {
                var newDcRecords = list.Where(item => !existingDcNames.Contains(item.GetKeyValue())).ToList();
                await dcTargetRepo.Ingest(newDcRecords, IngestMode.Refresh, ct);
            };
            var totalDcSynced = await dcSourceRepo.QueryInBatches("", onDataCentersReceived, 1000, cancel);

            logger.LogInformation($"total of {totalDcSynced} data centers synchronized");

            var allDCs = await dcTargetRepo.GetAll(cancel);

            logger.LogInformation($"total of {allDCs.Count()} data centers found in target db");

            // devices
            var existingDeviceNames = (await deviceTargetRepo.GetKeys(cancel)).ToList();

            logger.LogInformation($"total of {existingDeviceNames.Count} devices already exist in target db");

            Func <IList <PowerDevice>, CancellationToken, Task> onDevicesReceived = async(list, ct) =>
            {
                var newRecords = list.Where(item => !existingDeviceNames.Contains(item.GetKeyValue())).ToList();
                await deviceTargetRepo.Ingest(newRecords, IngestMode.Refresh, ct);
            };

            var totalSynced = 0;

            foreach (var dc in allDCs)
            {
                await RetryBlock.RetryOnThrottling(3, TimeSpan.FromSeconds(1), async() =>
                {
                    var deviceQuery   = $"c.dcName='{dc.DcName}'";
                    var devicesSynced = await deviceSourceRepo.QueryInBatches(deviceQuery, onDevicesReceived, 5000, cancel);
                    totalSynced      += devicesSynced;
                    logger.LogInformation($"Synced {devicesSynced} devices for {dc.DcName}, total={totalSynced}");
                }, logger, ex => (ex as NpgsqlException)?.IsTransient == true);
            }

            logger.LogInformation("Done");
        }