< Summary - pva.SuperV

Information
Class: pva.SuperV.Engine.HistoryRetrieval.HistoryRow
Assembly: pva.SuperV.Engine
File(s): /home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/HistoryRetrieval/HistoryRow.cs
Tag: dotnet-ubuntu_22190969454
Line coverage
84%
Covered lines: 136
Uncovered lines: 25
Coverable lines: 161
Total lines: 226
Line coverage: 84.4%
Branch coverage
50%
Covered branches: 53
Total branches: 104
Branch coverage: 50.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Ts()100%11100%
get_Quality()100%11100%
get_Values()100%11100%
.ctor(...)87.5%323297.14%
ConvertToUshort(...)33.33%6680%
ConvertToUlong(...)33.33%6680%
ConvertToUint(...)33.33%6680%
ConvertToStringt(...)50%4488.88%
ConvertToShort(...)33.33%6680%
ConvertToTimeSpan(...)50%6680%
ConvertToLong(...)33.33%6680%
ConvertToInt(...)33.33%6680%
ConvertToFloat(...)20%141066.66%
ConvertToDouble(...)25%9872.72%
ConvertToDatetime(...)50%4488.88%
ConvertToBool(...)50%4488.88%
GetValue(...)100%11100%

File(s)

/home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/HistoryRetrieval/HistoryRow.cs

#LineLine coverage
 1using pva.SuperV.Common;
 2using pva.SuperV.Engine.Exceptions;
 3using TDengine.Driver;
 4
 5namespace pva.SuperV.Engine.HistoryRetrieval
 6{
 7    /// <summary>
 8    /// Row of history for a specific timestamp.
 9    /// </summary>
 10    public class HistoryRow
 11    {
 12        /// <summary>
 13        /// Timestamp of history row.
 14        /// </summary>
 11415        public DateTime Ts { get; }
 16
 17        /// <summary>
 18        /// Quality of history row.
 19        /// </summary>
 11020        public QualityLevel Quality { get; }
 21
 22        /// <summary>
 23        /// List of values as objects.
 24        /// </summary>
 29925        public List<object?> Values { get; } = [];
 26
 27        /// <summary>
 28        /// Builds a row from a TDengine row.
 29        /// </summary>
 30        /// <param name="row">TDengine row</param>
 31        /// <param name="fields">List of field definitions.</param>
 32        /// <param name="keepFieldType">Whether to convert values to field type or keep as is.</param>
 6033        public HistoryRow(IRows row, List<IFieldDefinition> fields, bool keepFieldType)
 6034        {
 6035            Ts = ((DateTime)row.GetValue(row.FieldCount - 2)).ToUniversalTime();
 6036            string qualityValueString = (string)row.GetValue(row.FieldCount - 1);
 6037            Quality = qualityValueString != null
 6038                ? Enum.Parse<QualityLevel>(qualityValueString)
 6039                : QualityLevel.Uncertain;
 34640            for (int i = 0; i < fields.Count; i++)
 11341            {
 11342                if (keepFieldType)
 7843                {
 7844                    IFieldDefinition field = fields[i];
 7845                    Values.Add(field switch
 7846                    {
 647                        FieldDefinition<bool> => ConvertToBool(field.Name, row.GetValue(i)),
 248                        FieldDefinition<DateTime> => ConvertToDatetime(field.Name, row.GetValue(i)),
 649                        FieldDefinition<double> => ConvertToDouble(field.Name, row.GetValue(i)),
 650                        FieldDefinition<float> => ConvertToFloat(field.Name, row.GetValue(i)),
 1651                        FieldDefinition<int> => ConvertToInt(field.Name, row.GetValue(i)),
 652                        FieldDefinition<long> => ConvertToLong(field.Name, row.GetValue(i)),
 653                        FieldDefinition<TimeSpan> => ConvertToTimeSpan(field.Name, row.GetValue(i)),
 654                        FieldDefinition<short> => ConvertToShort(field.Name, row.GetValue(i)),
 655                        FieldDefinition<string> => ConvertToStringt(field.Name, row.GetValue(i)),
 656                        FieldDefinition<uint> => ConvertToUint(field.Name, row.GetValue(i)),
 657                        FieldDefinition<ulong> => ConvertToUlong(field.Name, row.GetValue(i)),
 658                        FieldDefinition<ushort> => ConvertToUshort(field.Name, row.GetValue(i)),
 059                        _ => throw new UnhandledMappingException(nameof(HistoryRow), field?.Type.ToString()),
 7860
 7861                    });
 7862                }
 63                else
 3564                {
 3565                    Values.Add(row.GetValue(i));
 3566                }
 11367            }
 6068        }
 69
 70        private static ushort? ConvertToUshort(string fieldName, object fieldValue)
 671        {
 72
 673            return fieldValue == null
 674                ? null
 675                : fieldValue switch
 676                {
 677                    ushort directValue => directValue,
 078                    double doubleValue => (ushort)Math.Truncate(doubleValue),
 079                    _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType())
 680                };
 681        }
 82
 83        private static ulong? ConvertToUlong(string fieldName, object fieldValue)
 684        {
 685            return fieldValue == null
 686                ? null
 687                : fieldValue switch
 688                {
 689                    ulong directValue => directValue,
 090                    double doubleValue => (ulong)Math.Truncate(doubleValue),
 091                    _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType())
 692                };
 693        }
 94
 95        private static uint? ConvertToUint(string fieldName, object fieldValue)
 696        {
 697            return fieldValue == null
 698                ? null
 699                : fieldValue switch
 6100                {
 6101                    uint directValue => directValue,
 0102                    double doubleValue => (uint)Math.Truncate(doubleValue),
 0103                    _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType())
 6104                };
 6105        }
 106
 107        private static string? ConvertToStringt(string fieldName, object fieldValue)
 6108        {
 6109            return fieldValue == null
 6110                ? null
 6111                : fieldValue switch
 6112                {
 6113                    string directValue => directValue,
 0114                    _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType())
 6115                };
 6116        }
 117
 118        private static short? ConvertToShort(string fieldName, object fieldValue)
 6119        {
 6120            return fieldValue == null
 6121                ? null
 6122                : fieldValue switch
 6123                {
 6124                    short directValue => directValue,
 0125                    double doubleValue => (short)Math.Truncate(doubleValue),
 0126                    _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType())
 6127                };
 6128        }
 129
 130        private static TimeSpan? ConvertToTimeSpan(string fieldName, object fieldValue)
 6131        {
 6132            return fieldValue == null
 6133                ? null
 6134                : fieldValue switch
 6135                {
 0136                    TimeSpan directValue => directValue,
 6137                    long longValue => TimeSpan.FromTicks(longValue),
 0138                    _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType())
 6139                };
 6140        }
 141
 142        private static long? ConvertToLong(string fieldName, object fieldValue)
 6143        {
 6144            return fieldValue == null
 6145                ? null
 6146                : fieldValue switch
 6147                {
 6148                    long directValue => directValue,
 0149                    double doubleValue => (long)Math.Truncate(doubleValue),
 0150                    _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType())
 6151                };
 6152        }
 153
 154        private static int? ConvertToInt(string fieldName, object fieldValue)
 16155        {
 16156            return fieldValue == null
 16157                ? null
 16158                : fieldValue switch
 16159                {
 16160                    int directValue => directValue,
 0161                    double doubleValue => (int)Math.Truncate(doubleValue),
 0162                    _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType())
 16163                };
 16164        }
 165
 166        private static float? ConvertToFloat(string fieldName, object fieldValue)
 6167        {
 6168            return fieldValue == null
 6169                ? null
 6170                : fieldValue switch
 6171                {
 6172                    float directValue => directValue,
 0173                    double doubleValue => (float)doubleValue,
 0174                    long directValue => (float)directValue,
 0175                    int directValue => (float)directValue,
 0176                    _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType())
 6177                };
 6178        }
 179
 180        private static double? ConvertToDouble(string fieldName, object fieldValue)
 6181        {
 6182            return fieldValue == null
 6183                ? null
 6184                : fieldValue switch
 6185                {
 6186                    double directValue => directValue,
 0187                    int directValue => (double)directValue,
 0188                    long directValue => (double)directValue,
 0189                    _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType())
 6190                };
 6191        }
 192
 193        private static DateTime? ConvertToDatetime(string fieldName, object fieldValue)
 2194        {
 2195            return fieldValue == null
 2196                ? null
 2197                : fieldValue switch
 2198                {
 2199                    DateTime directValue => directValue,
 0200                    _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType())
 2201                };
 2202        }
 203
 204        private static bool? ConvertToBool(string fieldName, object fieldValue)
 6205        {
 6206            return fieldValue == null
 6207                ? null
 6208                : fieldValue switch
 6209                {
 6210                    bool directValue => directValue,
 0211                    _ => throw new UnhandledFieldTypeException(fieldName, fieldValue.GetType())
 6212                };
 6213        }
 214
 215        /// <summary>
 216        /// Gets field value at a specific index as the specified type.
 217        /// </summary>
 218        /// <typeparam name="T">Type of field</typeparam>
 219        /// <param name="colIndex">Index of value.</param>
 220        /// <returns>Value of field</returns>
 221        public T? GetValue<T>(int colIndex)
 8222        {
 8223            return (T?)Values[colIndex];
 8224        }
 225    }
 226}