< Summary - pva.SuperV

Information
Class: pva.SuperV.Engine.Processing.HistorizationProcessing<T>
Assembly: pva.SuperV.Engine
File(s): /home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/Processing/HistorizationProcessing.cs
Tag: dotnet-ubuntu_18869653307
Line coverage
88%
Covered lines: 67
Uncovered lines: 9
Coverable lines: 76
Total lines: 165
Line coverage: 88.1%
Branch coverage
56%
Covered branches: 17
Total branches: 30
Branch coverage: 56.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_HistoryRepository()100%11100%
get_TimestampFieldDefinition()100%11100%
get_FieldsToHistorize()100%11100%
get_ClassTimeSerieId()100%11100%
.ctor()100%11100%
.ctor(...)100%22100%
ValidateParameters(...)66.66%6676.19%
BuildAfterDeserialization(...)100%22100%
IsUsingRepository(...)50%22100%
IsFieldUsed(...)50%44100%
UpsertInHistoryStorage(...)50%22100%
ProcessValue(...)41.66%131280%

File(s)

/home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/Processing/HistorizationProcessing.cs

#LineLine coverage
 1using pva.SuperV.Engine.Exceptions;
 2using pva.SuperV.Engine.HistoryStorage;
 3
 4namespace pva.SuperV.Engine.Processing
 5{
 6    /// <summary>
 7    /// Field value historization processing.
 8    /// </summary>
 9    /// <typeparam name="T"></typeparam>
 10    public class HistorizationProcessing<T> : FieldValueProcessing<T>, IHistorizationProcessing
 11    {
 12        /// <summary>
 13        /// Associated history repository.
 14        /// </summary>
 57515        public HistoryRepository? HistoryRepository { get; set; }
 16
 17        /// <summary>
 18        /// Field providing the timestamp of time serie.
 19        /// </summary>
 3920        public FieldDefinition<DateTime>? TimestampFieldDefinition { get; set; }
 21
 22        /// <summary>
 23        /// List of fields whose value is to be historized.
 24        /// </summary>
 227325        public List<IFieldDefinition> FieldsToHistorize { get; } = [];
 26
 27        /// <summary>
 28        /// The class time serie ID returned from history storage.
 29        /// </summary>
 48930        public string? ClassTimeSerieId { get; set; }
 31
 32        /// <summary>
 33        /// Initializes a new instance of the <see cref="HistorizationProcessing{T}"/> class. Used for deserialization.
 34        /// </summary>
 235        public HistorizationProcessing()
 236        {
 237        }
 38
 39        /// <summary>
 40        /// Initializes a new instance.
 41        /// </summary>
 42        /// <param name="name">Name of instance.</param>
 43        /// <param name="project">Project.</param>
 44        /// <param name="clazz">Class.</param>
 45        /// <param name="trigerringFieldName">Field trigerring the processing.</param>
 46        /// <param name="historyRepositoryName">Name of history repository (<see cref="HistoryRepository"/>).</param>
 47        /// <param name="timestampFieldName">Name of the timestamp field. If null, the timestamp of tirgerring field zil
 48        /// <param name="fieldsToHistorize">List of fields to historize.</param>
 49        public HistorizationProcessing(string name, Project project, Class clazz, string trigerringFieldName,
 50            string historyRepositoryName, string? timestampFieldName, List<string> fieldsToHistorize)
 29651            : base(name)
 29652        {
 29653            CtorArguments.Add(trigerringFieldName);
 29654            CtorArguments.Add(historyRepositoryName);
 29655            CtorArguments.Add(timestampFieldName ?? "");
 29656            fieldsToHistorize.ForEach(fieldtoHistorize =>
 219157                CtorArguments.Add(fieldtoHistorize));
 29658            ValidateParameters(project, clazz, trigerringFieldName, historyRepositoryName, timestampFieldName, fieldsToH
 29659        }
 60
 61        /// <summary>
 62        /// Validate the processing parameters.
 63        /// </summary>
 64        /// <param name="project">Project.</param>
 65        /// <param name="clazz">Class.</param>
 66        /// <param name="trigerringFieldName">Field trigerring the processing.</param>
 67        /// <param name="historyRepositoryName">Name of history repository (<see cref="HistoryRepository"/>).</param>
 68        /// <param name="timestampFieldName">Name of the timestamp field. If null, the timestamp of tirgerring field zil
 69        /// <param name="fieldsToHistorize">List of fields to historize.</param>
 70        /// <exception cref="UnknownEntityException"></exception>
 71        private void ValidateParameters(Project project, Class clazz, string trigerringFieldName, string historyReposito
 29872        {
 29873            TrigerringFieldDefinition = GetFieldDefinition<T>(clazz, trigerringFieldName);
 29874            if (!string.IsNullOrEmpty(timestampFieldName))
 075            {
 076                TimestampFieldDefinition = GetFieldDefinition<DateTime>(clazz, timestampFieldName);
 077            }
 29878            if (project.HistoryRepositories.TryGetValue(historyRepositoryName, out var repository))
 29879            {
 29880                HistoryRepository = repository;
 29881            }
 82            else
 083            {
 084                throw new UnknownEntityException("History repository", historyRepositoryName);
 85            }
 29886            fieldsToHistorize.ForEach(fieldToHistorize =>
 190887            {
 190888                IFieldDefinition? fieldDefinition = GetFieldDefinition(clazz, fieldToHistorize);
 190889                if (fieldDefinition is not null)
 190890                {
 190891                    FieldsToHistorize.Add(fieldDefinition);
 190892                }
 220693            });
 29894        }
 95
 96        /// <summary>
 97        /// Build the processing after deserialization.
 98        /// </summary>
 99        /// <param name="project">Project.</param>
 100        /// <param name="clazz">Class.</param>
 101        public override void BuildAfterDeserialization(Project project, Class clazz)
 2102        {
 2103            string trigerringFieldName = GetCtorArgument<string>(0)!;
 2104            string historyRepositoryName = GetCtorArgument<string>(1)!;
 2105            string? timestampFieldName = GetCtorArgument<string?>(2);
 2106            List<string> fieldsToHistorize = [];
 30107            for (int index = 3; index < CtorArguments.Count - 1; index++)
 13108            {
 13109                fieldsToHistorize.Add(GetCtorArgument<string>(index)!);
 13110            }
 2111            ClassTimeSerieId = GetCtorArgument<string?>(CtorArguments.Count - 1);
 2112            ValidateParameters(project, clazz, trigerringFieldName, historyRepositoryName, timestampFieldName, fieldsToH
 2113        }
 114
 115        public bool IsUsingRepository(string historyRepositoryName)
 1116            => HistoryRepository is not null && HistoryRepository.Name.Equals(historyRepositoryName);
 117
 118        public override bool IsFieldUsed(string fieldName)
 1119            => (TimestampFieldDefinition is not null && TimestampFieldDefinition.Name.Equals(fieldName))
 3120                || FieldsToHistorize.Any(field => field.Name.Equals(fieldName));
 121
 122        /// <summary>
 123        /// Upserts the time series in history storage.
 124        /// </summary>
 125        /// <param name="projectName">Name of project.</param>
 126        /// <param name="className">Name of class.</param>
 127        public void UpsertInHistoryStorage(string projectName, string className)
 220128        {
 220129            ClassTimeSerieId = HistoryRepository?.UpsertClassTimeSerie(projectName, className, this);
 219130            CtorArguments.Add(ClassTimeSerieId!);
 219131        }
 132
 133        /// <summary>
 134        /// Processes the trigerring field value change.
 135        /// </summary>
 136        /// <param name="instance">Instance whose field has changed.</param>
 137        /// <param name="changedField">Changed field.</param>
 138        /// <param name="valueChanged">Indicates if the value changed or not.</param>
 139        /// <param name="previousValue">Previous value 9i.e. before change).</param>
 140        /// <param name="currentValue">New value.</param>
 141        public override void ProcessValue(IInstance instance, Field<T> changedField, bool valueChanged, T previousValue,
 26142        {
 143            DateTime? historyTs;
 26144            if (TimestampFieldDefinition != null)
 0145            {
 0146                Field<DateTime>? timestamp = GetInstanceField<DateTime>(instance, TimestampFieldDefinition?.Name);
 0147                historyTs = timestamp?.Value;
 0148            }
 149            else
 26150            {
 26151                historyTs = changedField.Timestamp.GetValueOrDefault();
 26152            }
 26153            List<IField> fieldsToHistorize = [];
 26154            FieldsToHistorize.ForEach(fieldToHistorize =>
 118155            {
 118156                IField? field = GetInstanceField(instance, fieldToHistorize.Name);
 118157                if (field != null)
 118158                {
 118159                    fieldsToHistorize.Add(field);
 118160                }
 144161            });
 26162            HistoryRepository?.HistorizeValues(ClassTimeSerieId!, instance, historyTs ?? DateTime.Now, changedField.Qual
 26163        }
 164    }
 165}