public void WillReturnsExtractTimeAdjustedToGmt()
        {
            _clock.UtcNow().Returns(new DateTime(2015, 10, 5, 13, 30, 0));

            var dates = _dateCalculator.Calculate();

            Assert.AreEqual(new DateTime(2015, 10, 5, 14, 30, 0), dates.ExtractDateTime);
        }
        public void Init()
        {
            _log = Substitute.For<ILog>();
            _dateCalculator = Substitute.For<IDateCalculator>();
            _powerService = Substitute.For<IPowerService>();
            _positionAggregator = Substitute.For<IPositionAggregator>();
            _fileNameGenerator = Substitute.For<IFileNameGenerator>();
            _reportContentWriter = Substitute.For<IReportContentWriter>();
            _file = Substitute.For<IFile>();
            _reportGenerator = new ReportGenerator(_log, _dateCalculator, _powerService, _positionAggregator, _fileNameGenerator, _reportContentWriter, _file);

            _reportFolder = @"C:\Temp\";
            _dates = new DateResult { ExtractDateTime = new DateTime(2015, 10, 5, 23, 34, 0), RequestDate = new DateTime(2015, 10, 6) };

            _powerTradeOne = new PowerTrade();
            _powerTradeTwo = new PowerTrade();
            _powerTrades = new[] { _powerTradeOne, _powerTradeTwo };
            _powerPosition = new PowerPosition();
            _fileName = "PowerPositions.csv";
            _content = "Local time, Volume etc";

            _dateCalculator.Calculate().Returns(_dates);
            _powerService.GetTrades(_dates.RequestDate).Returns(_powerTrades);
            _positionAggregator.Aggregate(_dates.RequestDate, Arg.Is<List<PowerTrade>>(x => x[0] == _powerTradeOne && x[1] == _powerTradeTwo)).Returns(_powerPosition);
            _fileNameGenerator.Generate(_dates.ExtractDateTime).Returns(_fileName);
            _reportContentWriter.Write(_powerPosition).Returns(_content);
        }
        public void Init()
        {
            _log                 = Substitute.For <ILog>();
            _dateCalculator      = Substitute.For <IDateCalculator>();
            _powerService        = Substitute.For <IPowerService>();
            _positionAggregator  = Substitute.For <IPositionAggregator>();
            _fileNameGenerator   = Substitute.For <IFileNameGenerator>();
            _reportContentWriter = Substitute.For <IReportContentWriter>();
            _file                = Substitute.For <IFile>();
            _reportGenerator     = new ReportGenerator(_log, _dateCalculator, _powerService, _positionAggregator, _fileNameGenerator, _reportContentWriter, _file);

            _reportFolder = @"C:\Temp\";
            _dates        = new DateResult {
                ExtractDateTime = new DateTime(2015, 10, 5, 23, 34, 0), RequestDate = new DateTime(2015, 10, 6)
            };

            _powerTradeOne = new PowerTrade();
            _powerTradeTwo = new PowerTrade();
            _powerTrades   = new[] { _powerTradeOne, _powerTradeTwo };
            _powerPosition = new PowerPosition(_dates.RequestDate);
            _fileName      = "PowerPositions.csv";
            _content       = "Local time, Volume etc";

            _dateCalculator.Calculate().Returns(_dates);
            _powerService.GetTrades(_dates.RequestDate).Returns(_powerTrades);
            _positionAggregator.Aggregate(_dates.RequestDate, Arg.Is <List <PowerTrade> >(x => x[0] == _powerTradeOne && x[1] == _powerTradeTwo)).Returns(_powerPosition);
            _fileNameGenerator.Generate(_dates.ExtractDateTime).Returns(_fileName);
            _reportContentWriter.Write(_powerPosition).Returns(_content);
        }
예제 #4
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);
        }
예제 #5
0
        public IActionResult Calculate(InputModel model)
        {
            //adding random string to prevent session id from changing on each request
            HttpContext.Session.SetString("temp", "tempstring");

            PersistenceModel output = new PersistenceModel(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"),
                                                           model.Date,
                                                           model.ParamDays,
                                                           model.ParamWeeks,
                                                           model.ParamMonths,
                                                           model.ParamYears,
                                                           model.Operation);

            output.Result    = _dateCalculator.Calculate(model);
            output.SessionID = HttpContext.Session.Id;
            _logger.LogInformation("Session ID :" + output.SessionID);
            _calculationsRepository.AddCalculation(output);
            TempData["result"] = output.Result;
            return(RedirectToAction("Index"));
        }
예제 #6
0
        static void Main(string[] args)
        {
            //set config file
            var configuration = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                .Build();


            //debug
            // System.Console.WriteLine(configuration.GetConnectionString("PostgresConnectionString"));

            //Dependencies and services setup
            ServiceProvider serviceProvider = new ServiceCollection()
                                              .AddDbContext <RecordsDbContext>(option => option.UseNpgsql(configuration.GetConnectionString("PostgresConnectionString")))
                                              .AddSingleton <IDateCalculator, DateCalculator>()
                                              .AddSingleton <IInputReader, ConsoleInputReader>()
                                              .AddScoped <IRepository, RecordsRepository>()
                                              .AddScoped <DbContext, RecordsDbContext>()
                                              .BuildServiceProvider();

            IInputReader    reader     = serviceProvider.GetService <IInputReader>();
            IDateCalculator calculator = serviceProvider.GetService <IDateCalculator>();
            IRepository     repository = serviceProvider.GetService <IRepository>();
            DbContext       dbContext  = serviceProvider.GetService <DbContext>();

            dbContext.Database.EnsureCreated();


            // Dictionary<int, Delegate> methods = new Dictionary<int, Delegate>();
            // methods[0] = new Func<List<InputEntity>>(ReadAll);
            // methods[1] = new Func<InputEntity>(ReadOne);
            // methods[2] = new Action(DisplayCurrent);
            // methods[3] = new Action(DisplayAll);

            //input
            List <InputEntity> list = reader.ReadMultipleInputs();

            // //calculate
            List <OutputEntity> outputList = calculator.Calculate(list);

            //save to DB
            foreach (var op in outputList)
            {
                repository.AddRecord(op);
            }

            //output
            System.Console.WriteLine("\n============Successfully Added to Database=============\n");
            int exitChoice = 1;

            do
            {
                System.Console.WriteLine("\n1. Display Current Session Records \n2. Display All Records \n(Any Other). Exit : ");

                if (Int32.TryParse(Console.ReadLine(), out exitChoice))
                {
                    switch (exitChoice)
                    {
                    case 1:
                        DisplayCurrentResults(outputList);
                        break;

                    case 2:
                        DisplayAllDatabaseRecords(repository);
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    break;
                }
            }while(exitChoice == 1 || exitChoice == 2);
        }