Exemple #1
0
        public IHttpActionResult SetDefaultParameters(JObject parametersModel)
        {
            var parametersObj = parametersModel.GetValue("parametersModel").ToObject <PlotParameters>();

            parametersObj.FromCycle         = null;
            parametersObj.ToCycle           = null;
            parametersObj.EveryNthCycle     = null;
            parametersObj.CustomCycleFilter = string.Empty;
            parametersObj.LegendShowen      = null;
            parametersObj.DisableCharge     = null;
            parametersObj.DisableDischarge  = null;
            parametersObj.Threshold         = null;
            parametersObj.MinY           = null;
            parametersObj.Simplification = 1;
            parametersObj.MaxY           = null;
            parametersObj.CurrentUoM     = CurrentUoM.mA;
            parametersObj.CapacityUoM    = CapacityUoM.mAh;
            parametersObj.TimeUoM        = TimeUoM.Seconds;
            parametersObj.PowerUoM       = PowerUoM.W;
            parametersObj.EnergyUoM      = EnergyUoM.Wh;
            parametersObj.ResistanceUoM  = ResistanceUoM.Ohm;
            parametersObj.NormalizeBy    = NormalizeBy.None;
            parametersObj.IsInitialized  = true;
            parametersObj.AxisRange      = new AxisRange();

            parametersObj = _chartSettingProvider.MergeWithGlobal(User.Identity.GetUserId(), parametersObj);
            _chartSettingProvider.SetSettings(User.Identity.GetUserId(), parametersObj);
            _chartSettingProvider.ClearSettings($"{User.Identity.GetUserId()}_Template");

            return(Ok(parametersObj));
        }
Exemple #2
0
        public IHttpActionResult SaveChartTemplate(JObject templateModel)
        {
            var templateObj = templateModel.GetValue("templateModel").ToObject <PlotTemplateRequestModel>();
            var result      = _selectProjectListQuery.Execute(User.Identity.GetUserId(), templateObj.projectIds);

            if (result.Status != ProjectStatusDto.Ready)
            {
                return(Content(HttpStatusCode.NotFound, new { result.Status }));
            }

            var templateUserId = _db.Users.First(u => u.UserName == "*****@*****.**");
            var userId         = HttpContext.Current.User.Identity.GetUserId();

            templateObj.newTemplate.UserId  = userId;
            templateObj.newTemplate.CanEdit = userId != templateUserId.Id;

            templateObj.newTemplate.PlotParameters.PointSize = result.Projects.HasProjectsWithOneCycle() ? PointSizeOnOneCyclePlots : templateObj.newTemplate.PlotParameters.PointSize;
            templateObj.newTemplate.PlotParameters.MaxCycles = result.Projects.IsTotalProjectsCyclesGreaterThanMaximum(MaxSeriesPerPlot) ? MaxSeriesPerPlot : templateObj.newTemplate.PlotParameters.MaxCycles;

            templateObj.newTemplate.PlotParameters.CustomCycleFilter = HttpContext.Current.Server.UrlDecode(templateObj.newTemplate.PlotParameters.CustomCycleFilter);
            _chartSettingProvider.ClearSettings($"{User.Identity.GetUserId()}_Template");

            var parameters = templateObj.newTemplate.PlotParameters;

            if (string.IsNullOrEmpty(templateObj.newTemplate.Id))
            {
                var itemNew = _db.PlotTemplates.Add(new DataLayer.PlotTemplate
                {
                    UserId    = User.Identity.GetUserId(),
                    Name      = templateObj.newTemplate.Name,
                    Content   = JsonConvert.SerializeObject(templateObj.newTemplate),
                    CreatedAt = DateTime.UtcNow,
                    UpdatedAt = DateTime.UtcNow
                });
                _db.SaveChanges();
                templateObj.newTemplate.Id = itemNew.Id.ToString();
                itemNew.Content            = JsonConvert.SerializeObject(templateObj.newTemplate);
                itemNew.UpdatedAt          = DateTime.UtcNow;
            }
            else
            {
                int.TryParse(templateObj.newTemplate.Id, out var templateId);
                var templateToUpdate = GetAllUserPlotTemplates(templateUserId.Id).FirstOrDefault(pt => pt.Id == templateId);
                if (templateToUpdate != null)
                {
                    templateToUpdate.Content   = JsonConvert.SerializeObject(templateObj.newTemplate);
                    templateToUpdate.UpdatedAt = DateTime.UtcNow;
                }
                else
                {
                    Log.Error($"Template {templateId} not found");
                    return(NotFound());
                }
            }

            _db.SaveChanges();

            var timer         = Stopwatch.StartNew();
            var chartTemplate = _plotTemplateService.GetChartTemplate(new ChartOwner(User.Identity.GetUserId(), User.Identity.Name), int.Parse(templateObj.newTemplate.Id), result.Projects);
            var dto           = chartTemplate.Chart.ToChartDto($"Template - {templateObj.newTemplate.Name}");

            Log.Info($"ChartController.SaveChartTemplate: Plot({templateObj.newTemplate.Name}, {parameters}, {templateObj.projectIds}): plot {timer.ElapsedMilliseconds} ms");

            return(Ok(
                       new
            {
                selectedTemplate = templateObj.newTemplate,
                chart = dto
            }));
        }