public StatisticItemView(StatisticsItem item, StatisticsScenarioInfo info) { string getUnit() { if (info.ValueTypeName == FloatValueTypeName) { return(((FloatValueType)Scenarios[info.ID].ValueType).Unit); } return(string.Empty); } string getValue() { if (info.ValueTypeName == ToggleValueTypeName) { if (Value == ToggleValueType.ValueON) { return("Вкл."); } else { return("Выкл."); } } return(item.Value + getUnit()); } ScenarioName = info.Name; UserName = item.SourceName; SourceType = item.SourceType; DateTime = item.DateTime; Value = getValue(); }
public StatisticsItem[] GetItems(StatisticsScenarioInfo info, DateTime since, DateTime to, ScenarioActionSource source) { var scenario = ScenariosRepository.Scenarios.FirstOrDefault(x => x.Id == info.ID && ActionsDomain.Utils.GetValueTypeClassName(x.ValueType.GetType()) == info.ValueTypeName); if (scenario?.SecuritySettings.IsAvailableForUser(source.User, source.Source, source.Action) ?? false) { if (scenario is RemoteScenario remoteScenario) { try { if (!scenario.GetIsAvailable()) { throw new ScenarioExecutionException(ScenarioExecutionError.NotAvailable); } var remoteScenarioInfo = new StatisticsScenarioInfo() { Name = remoteScenario.Name, ID = remoteScenario.RemoteScenarioId, ValueTypeName = ActionsDomain.Utils.GetValueTypeClassName(remoteScenario.ValueType.GetType()), Since = DateTime.Now, To = DateTime.Now, }; var server = ClientFactory.GetServer(remoteScenario.Credentials); var statistics = server.GetStatistics(since, to, new Encrypted <StatisticsScenarioInfo>(remoteScenarioInfo, remoteScenario.Credentials.SecretKey)) .Decrypt(remoteScenario.Credentials.SecretKey) .ToArray(); foreach (var item in statistics) { //crutch item.Target.ID = remoteScenario.Id; item.Target.Name = remoteScenario.Name; } return(statistics); } catch (Exception e) { throw e; } } else { return(_dataManager.GetDataItems(info.ID, info.ValueTypeName, since, to) .Select(x => { var item = new StatisticsItem(); item.Source = new StatisticsItemSource(); item.Source.SourceType = x.SourceType; item.Source.Name = x.SourceName; item.Source.ID = x.SourceId; item.DateTime = new DateTime(x.Year, x.Month, x.Day, x.Hour, x.Minute, x.Second); item.Value = x.Value; item.Target = info; return item; }).ToArray()); } } throw new ScenarioExecutionException(ScenarioExecutionError.AccessDenied); }
public void RefreshItems(ScenarioStatistic[] scenarioStatistics, DateTime since, DateTime to) { var scenarioStatistic = scenarioStatistics.FirstOrDefault(); _info = scenarioStatistic?.ScenarioInfo; var items = scenarioStatistic?.Statistic ?? new StatisticsItem[0]; PieItemView prev = null; var views = items.Select(x => { if (prev != null) { //calculating weights prev.Weight = (x.DateTime - prev.DateTime).Seconds; } var weight = 0; if (x == items.Last()) { weight = (DateTime.Now - x.DateTime).Seconds; } return(prev = new PieItemView() { DayOfWeek = GetDayOfWeek(x.DateTime.DayOfWeek), UserName = x.SourceName, Value = GetValue(x), DateTime = x.DateTime, Weight = weight }); }).ToArray(); var viewsByVal = views.Select(x => x.Value).Distinct().Select(x => new StatisticsCategoryView() { Category = x, Weight = views.Where(z => z.Value == x).Sum(z => z.Weight) }).ToArray(); var viewsByUser = views.Select(x => x.UserName).Distinct().Select(x => new StatisticsCategoryView() { Category = x, Weight = views.Count(z => z.UserName == x) }).ToArray(); var viewsByWeek = views.Select(x => x.DayOfWeek).Distinct().Select(x => new StatisticsCategoryView() { Category = x, Weight = views.Count(z => z.DayOfWeek == x) }).ToArray(); chartByVal.SetItems(viewsByVal); chartByUser.SetItems(viewsByUser); chartByWeek.SetItems(viewsByWeek); lblEmpty.Visibility = items.Any() ? Visibility.Collapsed : Visibility.Visible; lblCaption.Content = _info?.Name; }
public void Refresh(StatisticsScenarioInfo info, StatisticsItem item, DateTime dateTime) { if (item == null) { Visibility = Visibility.Collapsed; } else { Visibility = Visibility.Visible; tbScenName.Text = info.Name; if (info.ValueTypeName == Lazurite.ActionsDomain.Utils.GetValueTypeClassName(typeof(ToggleValueType))) { tbScenVal.Text = item.Value == ToggleValueType.ValueON ? "Вкл." : "Выкл."; } else { tbScenVal.Text = item.Value; } var unit = string.Empty; if (_scen == null && !_notExist) { _scen = ScenariosRepository.Scenarios.FirstOrDefault(x => x.Id == info.ID); if (_scen == null) { _notExist = true; } } if (_scen != null && _scen.ValueType is FloatValueType floatValueType) { tbScenVal.Text += floatValueType.Unit; } tbScenVal.Visibility = string.IsNullOrEmpty(tbScenVal.Text) ? Visibility.Collapsed : Visibility.Visible; tbUser.Text = item.SourceName; tbUser.Visibility = string.IsNullOrEmpty(tbUser.Text) || tbUser.Text == UsersRepository.SystemUser.Name ? Visibility.Collapsed : Visibility.Visible; tbDateTimeSetted.Text = "Выставлено: " + item.DateTime.ToString(); } }
public StatisticsScenarioInfo GetInfo(string scenarioId, string scenarioName, string scenarioValueType) { var info = new StatisticsScenarioInfo { ID = scenarioId, Name = scenarioName, ValueTypeName = scenarioValueType }; var path = Path.Combine(_rootPath, scenarioId + scenarioValueType); if (!Directory.Exists(path) || !Directory.GetFiles(path).Any()) { info.IsEmpty = true; } else { var dates = Directory.GetFiles(path) .Select(x => DateTime.Parse(Path.GetFileName(x))); info.Since = dates.Min(); info.To = dates.Max(); } return(info); }
public async Task <ScenarioStatistic> GetItems(StatisticsScenarioInfo info, DateTime since, DateTime to, ScenarioActionSource source) { var scenario = ScenariosRepository.Scenarios.FirstOrDefault(x => x.Id == info.ID); if (scenario?.SecuritySettings.IsAvailableForUser(source.User, source.Source, source.Action) ?? false) { if (scenario is RemoteScenario remoteScenario) { try { if (!scenario.GetIsAvailable()) { throw new ScenarioExecutionException(ScenarioExecutionError.NotAvailable); } var remoteScenarioInfo = new StatisticsScenarioInfo() { Name = remoteScenario.Name, ID = remoteScenario.RemoteScenarioId, Since = DateTime.Now, To = DateTime.Now, }; var client = ServiceClientFactory.Current.GetClient(remoteScenario.Credentials); var remoteScenarioStatistics = await client.GetStatistics(since, to, remoteScenarioInfo); var scenarioStatistics = new ScenarioStatistic(); scenarioStatistics.ScenarioInfo = info; scenarioStatistics.Statistic = remoteScenarioStatistics.Statistic ?? new StatisticsItem[0]; // Crutch return(scenarioStatistics); } catch (Exception e) { throw e; } } else { var valueTypeName = ActionsDomain.Utils.GetValueTypeClassName(scenario.ValueType.GetType()); var statistics = _dataManager .GetDataItems(info.ID, valueTypeName, since, to) .Select(x => { var item = new StatisticsItem(); item.SourceType = x.SourceType; item.SourceName = x.SourceName; item.DateTime = new DateTime(x.Year, x.Month, x.Day, x.Hour, x.Minute, x.Second); item.Value = x.Value; return(item); }) .OrderBy(x => x.DateTime) .ToArray(); var scenarioStatistics = new ScenarioStatistic(); scenarioStatistics.ScenarioInfo = info; scenarioStatistics.Statistic = statistics; return(scenarioStatistics); } } throw new ScenarioExecutionException(ScenarioExecutionError.AccessDenied); }
public async Task <ScenarioStatistic> GetStatistics(DateTime since, DateTime to, StatisticsScenarioInfo info) { return(await Client.CallMethod <ScenarioStatistic>("GetStatistics", new object[] { since, to, info })); }
public ScenarioStatistic GetStatistics(DateTime since, DateTime to, StatisticsScenarioInfo info) { var user = GetCurrentUser(); return(TaskUtils.Wait(StatisticsManager.GetItems(info, since, to, new ScenarioActionSource(user, ScenarioStartupSource.Network, ScenarioAction.ViewValue)))); }