< 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_22190969454
Line coverage
89%
Covered lines: 77
Uncovered lines: 9
Coverable lines: 86
Total lines: 176
Line coverage: 89.5%
Branch coverage
61%
Covered branches: 21
Total branches: 34
Branch coverage: 61.7%
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(...)80%101083.87%
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.Helpers.Extensions;
 2using pva.SuperV.Engine.Exceptions;
 3using pva.SuperV.Engine.HistoryStorage;
 4
 5namespace pva.SuperV.Engine.Processing
 6{
 7    /// <summary>
 8    /// Field value historization processing.
 9    /// </summary>
 10    /// <typeparam name="T">Type of field on which historization processing is applied.</typeparam>
 11    public class HistorizationProcessing<T> : FieldValueProcessing<T>, IHistorizationProcessing
 12    {
 13        /// <summary>
 14        /// Associated history repository.
 15        /// </summary>
 407416        public HistoryRepository? HistoryRepository { get; set; }
 17
 18        /// <summary>
 19        /// Field providing the timestamp of time serie.
 20        /// </summary>
 15621        public FieldDefinition<DateTime>? TimestampFieldDefinition { get; set; }
 22
 23        /// <summary>
 24        /// List of fields whose value is to be historized.
 25        /// </summary>
 1724226        public List<IFieldDefinition> FieldsToHistorize { get; } = [];
 27
 28        /// <summary>
 29        /// The class time serie ID returned from history storage.
 30        /// </summary>
 342431        public string? ClassTimeSerieId { get; set; }
 32
 33        /// <summary>
 34        /// Initializes a new instance of the <see cref="HistorizationProcessing{T}"/> class. Used for deserialization.
 35        /// </summary>
 1436        public HistorizationProcessing()
 1437        {
 1438        }
 39
 40        /// <summary>
 41        /// Initializes a new instance.
 42        /// </summary>
 43        /// <param name="name">Name of instance.</param>
 44        /// <param name="project">Project.</param>
 45        /// <param name="clazz">Class.</param>
 46        /// <param name="trigerringFieldName">Field trigerring the processing.</param>
 47        /// <param name="historyRepositoryName">Name of history repository (<see cref="HistoryRepository"/>).</param>
 48        /// <param name="timestampFieldName">Name of the timestamp field. If null, the timestamp of tirgerring field zil
 49        /// <param name="fieldsToHistorize">List of fields to historize.</param>
 50        public HistorizationProcessing(string name, Project project, Class clazz, string trigerringFieldName,
 51            string historyRepositoryName, string? timestampFieldName, List<string> fieldsToHistorize)
 221452            : base(name)
 221453        {
 221454            CtorArguments.Add(trigerringFieldName);
 221455            CtorArguments.Add(historyRepositoryName);
 221456            CtorArguments.Add(timestampFieldName ?? "");
 221457            fieldsToHistorize.ForEach(fieldtoHistorize =>
 444358                CtorArguments.Add(fieldtoHistorize));
 221459            ValidateParameters(project, clazz, trigerringFieldName, historyRepositoryName, timestampFieldName, fieldsToH
 221360        }
 61
 62        /// <summary>
 63        /// Validate the processing parameters.
 64        /// </summary>
 65        /// <param name="project">Project.</param>
 66        /// <param name="clazz">Class.</param>
 67        /// <param name="trigerringFieldName">Field trigerring the processing.</param>
 68        /// <param name="historyRepositoryName">Name of history repository (<see cref="HistoryRepository"/>).</param>
 69        /// <param name="timestampFieldName">Name of the timestamp field. If null, the timestamp of tirgerring field zil
 70        /// <param name="fieldsToHistorize">List of fields to historize.</param>
 71        /// <exception cref="UnknownEntityException"></exception>
 72        private void ValidateParameters(Project project, Class clazz, string trigerringFieldName, string historyReposito
 222873        {
 222874            TrigerringFieldDefinition = GetFieldDefinition<T>(clazz, trigerringFieldName);
 222875            if (!string.IsNullOrEmpty(timestampFieldName))
 076            {
 077                TimestampFieldDefinition = GetFieldDefinition<DateTime>(clazz, timestampFieldName);
 078            }
 222879            if (project.HistoryRepositories.TryGetValue(historyRepositoryName, out var repository))
 222880            {
 222881                HistoryRepository = repository;
 222882            }
 83            else
 084            {
 085                throw new UnknownEntityException("History repository", historyRepositoryName);
 86            }
 222887            List<IHistorizationProcessing> otherHistorizationProcessings = [];
 222888            clazz.FieldDefinitions.Values.ForEach(f =>
 1589989                otherHistorizationProcessings.AddRange(
 1245090                    [.. f.ValuePostChangeProcessings.OfType<IHistorizationProcessing>().Where(p => p.Name != this.Name)]
 1589991                    )
 222892                );
 222893            fieldsToHistorize.ForEach(fieldToHistorize =>
 224394            {
 2705195                if (otherHistorizationProcessings.Any(p => p.FieldsToHistorize.Any(f => f.Name.Equals(fieldToHistorize))
 196                {
 197                    throw new FieldUsedInOtherProcessingException(fieldToHistorize, "historization");
 222898                }
 224299                IFieldDefinition? fieldDefinition = GetFieldDefinition(clazz, fieldToHistorize);
 2242100                if (fieldDefinition is not null)
 2242101                {
 2242102                    FieldsToHistorize.Add(fieldDefinition);
 2242103                }
 4470104            });
 2227105        }
 106
 107        /// <summary>
 108        /// Build the processing after deserialization.
 109        /// </summary>
 110        /// <param name="project">Project.</param>
 111        /// <param name="clazz">Class.</param>
 112        public override void BuildAfterDeserialization(Project project, Class clazz)
 14113        {
 14114            string trigerringFieldName = GetCtorArgument<string>(0)!;
 14115            string historyRepositoryName = GetCtorArgument<string>(1)!;
 14116            string? timestampFieldName = GetCtorArgument<string?>(2);
 14117            List<string> fieldsToHistorize = [];
 56118            for (int index = 3; index < CtorArguments.Count - 1; index++)
 14119            {
 14120                fieldsToHistorize.Add(GetCtorArgument<string>(index)!);
 14121            }
 14122            ClassTimeSerieId = GetCtorArgument<string?>(CtorArguments.Count - 1);
 14123            ValidateParameters(project, clazz, trigerringFieldName, historyRepositoryName, timestampFieldName, fieldsToH
 14124        }
 125
 126        public bool IsUsingRepository(string historyRepositoryName)
 1127            => HistoryRepository?.Name.Equals(historyRepositoryName) == true;
 128
 129        public override bool IsFieldUsed(string fieldName)
 1130            => (TimestampFieldDefinition?.Name.Equals(fieldName) == true)
 3131                || FieldsToHistorize.Any(field => field.Name.Equals(fieldName));
 132
 133        /// <summary>
 134        /// Upserts the time series in history storage.
 135        /// </summary>
 136        /// <param name="projectName">Name of project.</param>
 137        /// <param name="className">Name of class.</param>
 138        public void UpsertInHistoryStorage(string projectName, string className)
 1635139        {
 1635140            ClassTimeSerieId = HistoryRepository?.UpsertClassTimeSerie(projectName, className, this);
 1634141            CtorArguments.Add(ClassTimeSerieId!);
 1634142        }
 143
 144        /// <summary>
 145        /// Processes the trigerring field value change.
 146        /// </summary>
 147        /// <param name="instance">Instance whose field has changed.</param>
 148        /// <param name="changedField">Changed field.</param>
 149        /// <param name="valueChanged">Indicates if the value changed or not.</param>
 150        /// <param name="previousValue">Previous value 9i.e. before change).</param>
 151        /// <param name="currentValue">New value.</param>
 152        public override void ProcessValue(IInstance instance, Field<T> changedField, bool valueChanged, T previousValue,
 97153        {
 154            DateTime? historyTs;
 97155            if (TimestampFieldDefinition != null)
 0156            {
 0157                Field<DateTime>? timestamp = GetInstanceField<DateTime>(instance, TimestampFieldDefinition?.Name);
 0158                historyTs = timestamp?.Value;
 0159            }
 160            else
 97161            {
 97162                historyTs = changedField.Timestamp.GetValueOrDefault();
 97163            }
 97164            List<IField> fieldsToHistorize = [];
 97165            FieldsToHistorize.ForEach(fieldToHistorize =>
 153166            {
 153167                IField? field = GetInstanceField(instance, fieldToHistorize.Name);
 153168                if (field != null)
 153169                {
 153170                    fieldsToHistorize.Add(field);
 153171                }
 250172            });
 97173            HistoryRepository?.HistorizeValues(this.Name, ClassTimeSerieId!, instance, historyTs ?? DateTime.Now, change
 97174        }
 175    }
 176}