コード例 #1
0
        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);
        }