public PrognosingService(PrognosingParams @params, ISeriesStorage seriesStorage)
 {
     Params             = @params;
     rForecast          = new RForecast(@params);
     this.seriesStorage = seriesStorage;
 }
예제 #2
0
        static void Main(string[] args)
        {
            string rPackagesPath = "rPackages";

            if (!Directory.Exists(rPackagesPath))
            {
                Directory.CreateDirectory(rPackagesPath);
            }
            var settings = new Prognosing.Models.PrognosingParams()
            {
                PrognoseDepth  = 1,
                MinTraceWindow = 20,
                MaxTraceWindow = 25
            };
            var steps = 5000;

            string inputFolder = args.Any() ? args[0] : "Input";

            string vm1TracePath = Path.Combine(inputFolder, "Traces", "1.csv");

            var trace = ReadTrace(new FileInfo(vm1TracePath)).Take(steps).ToArray();

            var traceSerieses = Enumerable.Range(0, trace.Count() - settings.MinTraceWindow - 1)
                                .Select(i => trace.Skip(i).Take(settings.MaxTraceWindow))
                                .SelectMany(series =>
                                            new VmResourceTrace[]
            {
                new VmResourceTrace(1, Resource.Cpu, series.Select(r => r.CPU)),
                new VmResourceTrace(1, Resource.Network, series.Select(r => r.Network)),
                new VmResourceTrace(1, Resource.Memory, series.Select(r => r.Memmory)),
                new VmResourceTrace(1, Resource.Iops, series.Select(r => r.IOPS))
            });

            var reportService = new ReportService("vmPrognose.xlsx", settings.PrognoseDepth);

            reportService.InitVmPrognoseSheets(1, "ARIMA", "SES", "HOLT", "DHOLT", "CROST");

            var vm = new VM()
            {
                Id = 1
            };
            var previousForecast = new Dictionary <Resource, Dictionary <string, IEnumerable <ForecastResult> > >()
            {
                { Resource.Cpu, null },
                { Resource.Network, null },
                { Resource.Memory, null },
                { Resource.Iops, null }
            };
            var stat = new Dictionary <Resource, Dictionary <string, float[]> >()
            {
                { Resource.Cpu, new Dictionary <string, float[]>() },
                { Resource.Network, new Dictionary <string, float[]>() },
                { Resource.Memory, new Dictionary <string, float[]>() },
                { Resource.Iops, new Dictionary <string, float[]>() }
            };

            try
            {
                RGlobalEnvironment.InitREngineWithForecasing(rPackagesPath);
                RForecast rForecast = new RForecast(settings);

                for (int i = 0; i < steps; i++)
                {
                    Console.WriteLine($"step {i}");
                    vm.Resources = trace[i];


                    if (i >= settings.MinTraceWindow - 1)
                    {
                        var series  = trace.Skip(Math.Max(i - settings.MaxTraceWindow + 1, 0)).Take(settings.MaxTraceWindow).ToArray();
                        var results = rForecast.RunAlgorythms(
                            new VmResourceTrace[]
                        {
                            new VmResourceTrace(1, Resource.Cpu, series.Select(r => r.CPU)),
                            new VmResourceTrace(1, Resource.Network, series.Select(r => r.Network)),
                            new VmResourceTrace(1, Resource.Memory, series.Select(r => r.Memmory)),
                            new VmResourceTrace(1, Resource.Iops, series.Select(r => r.IOPS))
                        }).ToDictionary(f => f.Resource, f => f.Forecast);

                        stat[Resource.Cpu] = results[Resource.Cpu].ToDictionary(p => p.Key,
                                                                                p => p.Value.First(
                                                                                    f => f.WindowOffset == GetBest(p.Key, previousForecast[Resource.Cpu], trace[i].CPU)).Result);
                        stat[Resource.Network] = results[Resource.Network].ToDictionary(p => p.Key,
                                                                                        p => p.Value.First(
                                                                                            f => f.WindowOffset == GetBest(p.Key, previousForecast[Resource.Network], trace[i].Network)).Result);
                        stat[Resource.Memory] = results[Resource.Memory].ToDictionary(p => p.Key,
                                                                                      p => p.Value.First(
                                                                                          f => f.WindowOffset == GetBest(p.Key, previousForecast[Resource.Memory], trace[i].Memmory)).Result);
                        stat[Resource.Iops] = results[Resource.Iops].ToDictionary(p => p.Key,
                                                                                  p => p.Value.First(
                                                                                      f => f.WindowOffset == GetBest(p.Key, previousForecast[Resource.Iops], trace[i].IOPS)).Result);

                        previousForecast = results;
                    }

                    reportService.WriteVmPrognoseStatistics(i + 1, vm, stat);
                }


                reportService.DrawVmForecastCharts();
                reportService.Save();
            }
            finally
            {
                RGlobalEnvironment.R.Dispose();
                reportService.Dispose();
            }
        }