| | | 1 | | using pva.SuperV.Api.Exceptions; |
| | | 2 | | using pva.SuperV.Api.Mappers; |
| | | 3 | | using pva.SuperV.Engine; |
| | | 4 | | using pva.SuperV.Engine.HistoryRetrieval; |
| | | 5 | | using pva.SuperV.Engine.HistoryStorage; |
| | | 6 | | using pva.SuperV.Model.HistoryRetrieval; |
| | | 7 | | using pva.SuperV.Model.Services; |
| | | 8 | | |
| | | 9 | | namespace pva.SuperV.Api.Services.History |
| | | 10 | | { |
| | | 11 | | public class HistoryValuesService : BaseService, IHistoryValuesService |
| | | 12 | | { |
| | | 13 | | private readonly ILogger logger; |
| | | 14 | | |
| | 5 | 15 | | public HistoryValuesService(ILoggerFactory loggerFactory) |
| | 5 | 16 | | { |
| | 5 | 17 | | this.logger = loggerFactory.CreateLogger(this.GetType()); |
| | 5 | 18 | | } |
| | | 19 | | |
| | | 20 | | public async Task<HistoryRawResultModel> GetInstanceRawHistoryValuesAsync(string projectId, string instanceName, |
| | 14 | 21 | | { |
| | 14 | 22 | | Project project = GetProjectEntity(projectId); |
| | 14 | 23 | | if (project is RunnableProject runnableProject) |
| | 14 | 24 | | { |
| | 14 | 25 | | Instance instance = runnableProject.GetInstance(instanceName); |
| | 14 | 26 | | InstanceTimeSerieParameters instanceTimeSerieParameters = RunnableProject.GetHistoryParametersForFields( |
| | 14 | 27 | | instance, request.HistoryFields); |
| | 14 | 28 | | return await GetInstanceRawHistoryValuesAsync(runnableProject, instanceName, request, instanceTimeSerieP |
| | | 29 | | } |
| | 0 | 30 | | return await Task.FromException<HistoryRawResultModel>(new NonRunnableProjectException(projectId)); |
| | 14 | 31 | | } |
| | | 32 | | |
| | | 33 | | private async Task<HistoryRawResultModel> GetInstanceRawHistoryValuesAsync(RunnableProject runnableProject, stri |
| | 14 | 34 | | { |
| | 14 | 35 | | logger.LogDebug("Getting raw history values for instance {InstanceName} of project {ProjectId} between {Star |
| | 14 | 36 | | instanceName, runnableProject.GetId(), request.StartTime, request.EndTime, String.Join(",", request.Hist |
| | 14 | 37 | | HistoryTimeRange query = new(request.StartTime, request.EndTime); |
| | 14 | 38 | | List<HistoryRow> rows = runnableProject.GetHistoryValues(instanceName, query, instanceTimeSerieParameters); |
| | | 39 | | |
| | 14 | 40 | | int fieldIndex = 0; |
| | 39 | 41 | | List<HistoryFieldModel> header = [.. instanceTimeSerieParameters.Fields.Select(fieldDefinition => new Histor |
| | 14 | 42 | | return await Task.FromResult(new HistoryRawResultModel(header, HistoryRowMapper.ToRawDto(rows))); |
| | 14 | 43 | | } |
| | | 44 | | |
| | | 45 | | public async Task<HistoryResultModel> GetInstanceHistoryValuesAsync(string projectId, string instanceName, Histo |
| | 14 | 46 | | { |
| | 14 | 47 | | logger.LogDebug("Getting history values for instance {InstanceName} of project {ProjectId}between {StartTime |
| | 14 | 48 | | instanceName, projectId, request.StartTime, request.EndTime, String.Join(",", request.HistoryFields)); |
| | 14 | 49 | | Project project = GetProjectEntity(projectId); |
| | 14 | 50 | | if (project is RunnableProject runnableProject) |
| | 14 | 51 | | { |
| | 14 | 52 | | Instance instance = runnableProject.GetInstance(instanceName); |
| | 14 | 53 | | InstanceTimeSerieParameters instanceTimeSerieParameters = RunnableProject.GetHistoryParametersForFields( |
| | | 54 | | |
| | 14 | 55 | | HistoryTimeRange query = new(request.StartTime, request.EndTime); |
| | 14 | 56 | | List<HistoryRow> rows = runnableProject.GetHistoryValues(instanceName, query, instanceTimeSerieParameter |
| | | 57 | | |
| | 14 | 58 | | int fieldIndex = 0; |
| | 39 | 59 | | List<HistoryFieldModel> header = [.. instanceTimeSerieParameters.Fields.Select(fieldDefinition => new Hi |
| | 14 | 60 | | return await Task.FromResult(new HistoryResultModel(header, HistoryRowMapper.ToDto(rows, instanceTimeSer |
| | | 61 | | } |
| | 0 | 62 | | return await Task.FromException<HistoryResultModel>(new NonRunnableProjectException(projectId)); |
| | 14 | 63 | | } |
| | | 64 | | |
| | | 65 | | public async Task<HistoryStatisticsRawResultModel> GetInstanceRawHistoryStatisticsAsync(string projectId, string |
| | 12 | 66 | | { |
| | 12 | 67 | | logger.LogDebug("Getting raw history statistics for instance {InstanceName} of project {ProjectId} between { |
| | 12 | 68 | | instanceName, projectId, request.StartTime, request.EndTime, request.InterpolationInterval, String.Join( |
| | 12 | 69 | | Project project = GetProjectEntity(projectId); |
| | 12 | 70 | | if (project is RunnableProject runnableProject) |
| | 12 | 71 | | { |
| | 12 | 72 | | Instance instance = runnableProject.GetInstance(instanceName); |
| | 26 | 73 | | InstanceTimeSerieParameters instanceTimeSerieParameters = RunnableProject.GetHistoryParametersForFields( |
| | 12 | 74 | | HistoryStatisticTimeRange query = new(request.StartTime, request.EndTime, request.InterpolationInterval, |
| | 12 | 75 | | int fieldIndex = 0; |
| | 12 | 76 | | List<HistoryStatisticField> statisticFields = [.. instanceTimeSerieParameters.Fields.Select(fieldDefinit |
| | 14 | 77 | | { |
| | 14 | 78 | | int savedFieldIndex = fieldIndex; |
| | 14 | 79 | | fieldIndex++; |
| | 14 | 80 | | return new HistoryStatisticField(fieldDefinition, request.HistoryFields[savedFieldIndex].StatisticFu |
| | 26 | 81 | | })]; |
| | | 82 | | |
| | 12 | 83 | | List<HistoryStatisticRow> rows = runnableProject.GetHistoryStatistics(instanceName, query, statisticFiel |
| | | 84 | | |
| | 12 | 85 | | return await Task.FromResult(new HistoryStatisticsRawResultModel(BuildStatisticsHeader(request, instance |
| | | 86 | | |
| | | 87 | | } |
| | 0 | 88 | | return await Task.FromException<HistoryStatisticsRawResultModel>(new NonRunnableProjectException(projectId)) |
| | 12 | 89 | | } |
| | | 90 | | |
| | | 91 | | public async Task<HistoryStatisticsResultModel> GetInstanceHistoryStatisticsAsync(string projectId, string insta |
| | 12 | 92 | | { |
| | 12 | 93 | | logger.LogDebug("Getting history statistics for instance {InstanceName} of project {ProjectId} between {Star |
| | 12 | 94 | | instanceName, projectId, request.StartTime, request.EndTime, request.InterpolationInterval, String.Join( |
| | 12 | 95 | | Project project = GetProjectEntity(projectId); |
| | 12 | 96 | | if (project is RunnableProject runnableProject) |
| | 12 | 97 | | { |
| | 12 | 98 | | Instance instance = runnableProject.GetInstance(instanceName); |
| | 29 | 99 | | InstanceTimeSerieParameters instanceTimeSerieParameters = RunnableProject.GetHistoryParametersForFields( |
| | 12 | 100 | | HistoryStatisticTimeRange query = new(request.StartTime, request.EndTime, request.InterpolationInterval, |
| | 12 | 101 | | int fieldIndex = 0; |
| | 12 | 102 | | List<HistoryStatisticField> statisticFields = [.. instanceTimeSerieParameters.Fields.Select(fieldDefinit |
| | 17 | 103 | | { |
| | 17 | 104 | | int savedFieldIndex = fieldIndex; |
| | 17 | 105 | | fieldIndex++; |
| | 17 | 106 | | return new HistoryStatisticField(fieldDefinition, request.HistoryFields[savedFieldIndex].StatisticFu |
| | 29 | 107 | | })]; |
| | | 108 | | |
| | 12 | 109 | | List<HistoryStatisticRow> rows = runnableProject.GetHistoryStatistics(instanceName, query, statisticFiel |
| | | 110 | | |
| | 12 | 111 | | return await Task.FromResult(new HistoryStatisticsResultModel(BuildStatisticsHeader(request, instanceTim |
| | 12 | 112 | | HistoryRowMapper.ToDto(rows, instanceTimeSerieParameters.Fields))); |
| | | 113 | | |
| | | 114 | | } |
| | 0 | 115 | | return await Task.FromException<HistoryStatisticsResultModel>(new NonRunnableProjectException(projectId)); |
| | 12 | 116 | | } |
| | | 117 | | |
| | | 118 | | private static List<HistoryStatisticResultFieldModel> BuildStatisticsHeader(HistoryStatisticsRequestModel reques |
| | 24 | 119 | | { |
| | 24 | 120 | | HistoryStatisticRow? firstRow = rows.FirstOrDefault(); |
| | 24 | 121 | | ArgumentNullException.ThrowIfNull(firstRow); |
| | 24 | 122 | | int fieldIndex = 0; |
| | 24 | 123 | | return [.. fields.Select(fieldDefinition => |
| | 31 | 124 | | { |
| | 31 | 125 | | object? valueAsObject = firstRow.Values[fieldIndex]; |
| | 31 | 126 | | HistoryStatisticResultFieldModel historyStatisticResultFieldModel = new(fieldDefinition.Name, |
| | 31 | 127 | | firstRow is not null && valueAsObject is not null |
| | 31 | 128 | | ? valueAsObject.GetType().ToString() |
| | 31 | 129 | | : fieldDefinition.Type.ToString(), fieldIndex, request.HistoryFields[fieldIndex].StatisticFuncti |
| | 31 | 130 | | fieldIndex++; |
| | 31 | 131 | | return historyStatisticResultFieldModel; |
| | 55 | 132 | | })]; |
| | 24 | 133 | | } |
| | | 134 | | |
| | | 135 | | } |
| | | 136 | | } |