< Summary - pva.SuperV

Information
Class: pva.SuperV.Api.Services.History.HistoryValuesService
Assembly: pva.SuperV.Api
File(s): /home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Api/Services/History/HistoryValuesService.cs
Tag: dotnet-ubuntu_22190969454
Line coverage
95%
Covered lines: 90
Uncovered lines: 4
Coverable lines: 94
Total lines: 136
Line coverage: 95.7%
Branch coverage
50%
Covered branches: 6
Total branches: 12
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetInstanceRawHistoryValuesAsync()50%2290%
GetInstanceRawHistoryValuesAsync()100%11100%
GetInstanceHistoryValuesAsync()50%2292.85%
GetInstanceRawHistoryStatisticsAsync()50%2292.85%
GetInstanceHistoryStatisticsAsync()50%2293.33%
BuildStatisticsHeader(...)50%44100%

File(s)

/home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Api/Services/History/HistoryValuesService.cs

#LineLine coverage
 1using pva.SuperV.Api.Exceptions;
 2using pva.SuperV.Api.Mappers;
 3using pva.SuperV.Engine;
 4using pva.SuperV.Engine.HistoryRetrieval;
 5using pva.SuperV.Engine.HistoryStorage;
 6using pva.SuperV.Model.HistoryRetrieval;
 7using pva.SuperV.Model.Services;
 8
 9namespace pva.SuperV.Api.Services.History
 10{
 11    public class HistoryValuesService : BaseService, IHistoryValuesService
 12    {
 13        private readonly ILogger logger;
 14
 515        public HistoryValuesService(ILoggerFactory loggerFactory)
 516        {
 517            this.logger = loggerFactory.CreateLogger(this.GetType());
 518        }
 19
 20        public async Task<HistoryRawResultModel> GetInstanceRawHistoryValuesAsync(string projectId, string instanceName,
 1421        {
 1422            Project project = GetProjectEntity(projectId);
 1423            if (project is RunnableProject runnableProject)
 1424            {
 1425                Instance instance = runnableProject.GetInstance(instanceName);
 1426                InstanceTimeSerieParameters instanceTimeSerieParameters = RunnableProject.GetHistoryParametersForFields(
 1427                    instance, request.HistoryFields);
 1428                return await GetInstanceRawHistoryValuesAsync(runnableProject, instanceName, request, instanceTimeSerieP
 29            }
 030            return await Task.FromException<HistoryRawResultModel>(new NonRunnableProjectException(projectId));
 1431        }
 32
 33        private async Task<HistoryRawResultModel> GetInstanceRawHistoryValuesAsync(RunnableProject runnableProject, stri
 1434        {
 1435            logger.LogDebug("Getting raw history values for instance {InstanceName} of project {ProjectId} between {Star
 1436                instanceName, runnableProject.GetId(), request.StartTime, request.EndTime, String.Join(",", request.Hist
 1437            HistoryTimeRange query = new(request.StartTime, request.EndTime);
 1438            List<HistoryRow> rows = runnableProject.GetHistoryValues(instanceName, query, instanceTimeSerieParameters);
 39
 1440            int fieldIndex = 0;
 3941            List<HistoryFieldModel> header = [.. instanceTimeSerieParameters.Fields.Select(fieldDefinition => new Histor
 1442            return await Task.FromResult(new HistoryRawResultModel(header, HistoryRowMapper.ToRawDto(rows)));
 1443        }
 44
 45        public async Task<HistoryResultModel> GetInstanceHistoryValuesAsync(string projectId, string instanceName, Histo
 1446        {
 1447            logger.LogDebug("Getting history values for instance {InstanceName} of project {ProjectId}between {StartTime
 1448                instanceName, projectId, request.StartTime, request.EndTime, String.Join(",", request.HistoryFields));
 1449            Project project = GetProjectEntity(projectId);
 1450            if (project is RunnableProject runnableProject)
 1451            {
 1452                Instance instance = runnableProject.GetInstance(instanceName);
 1453                InstanceTimeSerieParameters instanceTimeSerieParameters = RunnableProject.GetHistoryParametersForFields(
 54
 1455                HistoryTimeRange query = new(request.StartTime, request.EndTime);
 1456                List<HistoryRow> rows = runnableProject.GetHistoryValues(instanceName, query, instanceTimeSerieParameter
 57
 1458                int fieldIndex = 0;
 3959                List<HistoryFieldModel> header = [.. instanceTimeSerieParameters.Fields.Select(fieldDefinition => new Hi
 1460                return await Task.FromResult(new HistoryResultModel(header, HistoryRowMapper.ToDto(rows, instanceTimeSer
 61            }
 062            return await Task.FromException<HistoryResultModel>(new NonRunnableProjectException(projectId));
 1463        }
 64
 65        public async Task<HistoryStatisticsRawResultModel> GetInstanceRawHistoryStatisticsAsync(string projectId, string
 1266        {
 1267            logger.LogDebug("Getting raw history statistics for instance {InstanceName} of project {ProjectId} between {
 1268                instanceName, projectId, request.StartTime, request.EndTime, request.InterpolationInterval, String.Join(
 1269            Project project = GetProjectEntity(projectId);
 1270            if (project is RunnableProject runnableProject)
 1271            {
 1272                Instance instance = runnableProject.GetInstance(instanceName);
 2673                InstanceTimeSerieParameters instanceTimeSerieParameters = RunnableProject.GetHistoryParametersForFields(
 1274                HistoryStatisticTimeRange query = new(request.StartTime, request.EndTime, request.InterpolationInterval,
 1275                int fieldIndex = 0;
 1276                List<HistoryStatisticField> statisticFields = [.. instanceTimeSerieParameters.Fields.Select(fieldDefinit
 1477                {
 1478                    int savedFieldIndex = fieldIndex;
 1479                    fieldIndex++;
 1480                    return new HistoryStatisticField(fieldDefinition, request.HistoryFields[savedFieldIndex].StatisticFu
 2681                })];
 82
 1283                List<HistoryStatisticRow> rows = runnableProject.GetHistoryStatistics(instanceName, query, statisticFiel
 84
 1285                return await Task.FromResult(new HistoryStatisticsRawResultModel(BuildStatisticsHeader(request, instance
 86
 87            }
 088            return await Task.FromException<HistoryStatisticsRawResultModel>(new NonRunnableProjectException(projectId))
 1289        }
 90
 91        public async Task<HistoryStatisticsResultModel> GetInstanceHistoryStatisticsAsync(string projectId, string insta
 1292        {
 1293            logger.LogDebug("Getting history statistics for instance {InstanceName} of project {ProjectId} between {Star
 1294                instanceName, projectId, request.StartTime, request.EndTime, request.InterpolationInterval, String.Join(
 1295            Project project = GetProjectEntity(projectId);
 1296            if (project is RunnableProject runnableProject)
 1297            {
 1298                Instance instance = runnableProject.GetInstance(instanceName);
 2999                InstanceTimeSerieParameters instanceTimeSerieParameters = RunnableProject.GetHistoryParametersForFields(
 12100                HistoryStatisticTimeRange query = new(request.StartTime, request.EndTime, request.InterpolationInterval,
 12101                int fieldIndex = 0;
 12102                List<HistoryStatisticField> statisticFields = [.. instanceTimeSerieParameters.Fields.Select(fieldDefinit
 17103                {
 17104                    int savedFieldIndex = fieldIndex;
 17105                    fieldIndex++;
 17106                    return new HistoryStatisticField(fieldDefinition, request.HistoryFields[savedFieldIndex].StatisticFu
 29107                })];
 108
 12109                List<HistoryStatisticRow> rows = runnableProject.GetHistoryStatistics(instanceName, query, statisticFiel
 110
 12111                return await Task.FromResult(new HistoryStatisticsResultModel(BuildStatisticsHeader(request, instanceTim
 12112                    HistoryRowMapper.ToDto(rows, instanceTimeSerieParameters.Fields)));
 113
 114            }
 0115            return await Task.FromException<HistoryStatisticsResultModel>(new NonRunnableProjectException(projectId));
 12116        }
 117
 118        private static List<HistoryStatisticResultFieldModel> BuildStatisticsHeader(HistoryStatisticsRequestModel reques
 24119        {
 24120            HistoryStatisticRow? firstRow = rows.FirstOrDefault();
 24121            ArgumentNullException.ThrowIfNull(firstRow);
 24122            int fieldIndex = 0;
 24123            return [.. fields.Select(fieldDefinition =>
 31124                {
 31125                    object? valueAsObject = firstRow.Values[fieldIndex];
 31126                    HistoryStatisticResultFieldModel historyStatisticResultFieldModel = new(fieldDefinition.Name,
 31127                        firstRow is not null && valueAsObject is not null
 31128                        ? valueAsObject.GetType().ToString()
 31129                        : fieldDefinition.Type.ToString(), fieldIndex, request.HistoryFields[fieldIndex].StatisticFuncti
 31130                    fieldIndex++;
 31131                    return historyStatisticResultFieldModel;
 55132                })];
 24133        }
 134
 135    }
 136}