private Task <ServiceMetricsResponse> GetServiceMetricsAsync(
            DateTime startDate,
            DateTime endDate,
            string startLocation,
            string endLocation,
            CancellationToken cancellationToken)
        {
            Days days = startDate.DayOfWeek switch
            {
                DayOfWeek.Saturday => Days.Saturday,
                DayOfWeek.Sunday => Days.Sunday,
                _ => Days.Weekday
            };

            var requestCotent = new ServiceMetricsRequest
            {
                FromLocation = startLocation,
                ToLocation   = endLocation,
                FromTime     = $"{GetTwoDigitValue(startDate.Hour)}{GetTwoDigitValue(startDate.Minute)}",
                ToTime       = $"{GetTwoDigitValue(endDate.Hour)}{GetTwoDigitValue(endDate.Minute)}",
                FromDate     = startDate.ToString("yyyy-MM-dd"),
                ToDate       = endDate.ToString("yyyy-MM-dd"),
                Days         = days
            };

            return(PostContent <ServiceMetricsRequest, ServiceMetricsResponse>(
                       requestCotent,
                       serviceUris.CurrentValue.ServiceMetrics,
                       cancellationToken));
        }
Example #2
0
        private static async Task RebuildDatabaseAsync(Config config)
        {
            Console.WriteLine("RebuildDatabase:");

            var database = new TrainingDatabase();

            var client = new HspClient(config.HspClientBaseUrl, config.NationalRailDataPortalUsername, config.NationalRailDataPortalPassword);

            var startDate = new DateTime(2018, 01, 01);
            var toDate    = new DateTime(2018, 02, 01);

            var request = new ServiceMetricsRequest
            {
                from_loc  = "WDB",
                to_loc    = "IPS",
                from_time = "0830",
                to_time   = "0840",
                days      = Days.WEEKDAY
            };

            Console.WriteLine($"  Requesting data...");

            while (startDate < toDate)
            {
                request.from_date = startDate.ToHspDate();
                var nextDay = startDate.AddDays(1);
                request.to_date = nextDay.ToHspDate();

                var metricsResponse = await client.ServiceMetricsAsync(request);

                foreach (var service in metricsResponse.Services)
                {
                    foreach (var metric in service.Metrics)
                    {
                        var entry = new TrainingDatabaseEntry(metric, startDate);
                        Console.WriteLine($"    Adding entry: {entry}");
                        database.AddEntry(entry);
                    }
                }

                startDate = nextDay;
            }

            Console.WriteLine($"  Saving database to '{config.DatabasePath}'...");

            if (File.Exists(config.DatabasePath))
            {
                File.Delete(config.DatabasePath);
            }

            await database.SaveAsync(config.DatabasePath);

            Console.WriteLine("  Database rebuilt.");
        }
        public async Task BuildsServiceMetricsRequestSuccessfully()
        {
            const string startLocation = "TND";
            const string endLocation   = "OFM";
            var          startDate     = new DateTime(2021, 03, 31, 5, 3, 0);
            var          endDate       = new DateTime(2021, 03, 31, 5, 20, 0);

            var expectedRequest = new ServiceMetricsRequest
            {
                FromLocation = startLocation,
                ToLocation   = endLocation,
                FromTime     = "0503",
                ToTime       = "0520",
                FromDate     = "2021-03-31",
                ToDate       = "2021-03-31",
                Days         = Days.Weekday
            };

            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When(serviceMetricsUrl.ToString())
            .WithContent(JsonConvert.SerializeObject(expectedRequest))
            .Respond("application/json", "{}");

            var mockclient = mockHttp.ToHttpClient();

            var clientFactory = Mock.Of <IHttpClientFactory>(i => i.CreateClient(darwinClientName) == mockclient);
            var options       = Mock.Of <IOptionsMonitor <HistoricServiceUris> >(i => i.CurrentValue == new HistoricServiceUris
            {
                ServiceDetails = serviceDetailsUrl,
                ServiceMetrics = serviceMetricsUrl
            });

            var repository = new HistoricServiceRepository(clientFactory, options);

            await repository.GetTrainTimes(
                startDate,
                endDate,
                startLocation,
                endLocation,
                CancellationToken.None);
        }
Example #4
0
 public async Task <ServiceMetricsResponse> ServiceMetricsAsync(ServiceMetricsRequest request)
 {
     return(await RequestAsync <ServiceMetricsResponse>(request));
 }
Example #5
0
 public static ServiceMetricsResponse GetServiceMetrics(ServiceMetricsRequest serviceMetricsRequest)
 {
     return(GetData <ServiceMetricsRequest, ServiceMetricsResponse>(ConfigurationManager.AppSettings["ServiceMetricsUrl"], serviceMetricsRequest));
 }