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