Exemple #1
0
        private static void TestSensorDataStringValues(SensorDataEntity expected, SensorData actual, SensorType sensorType)
        {
            switch (sensorType)
            {
            case SensorType.BooleanSensor:
                BoolSensorData boolData = JsonSerializer.Deserialize <BoolSensorData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetSimpleSensorsString(expected.TimeCollected, boolData.Comment, boolData.BoolValue),
                             actual.StringValue);
                Assert.Equal(boolData.BoolValue.ToString(), actual.ShortStringValue);
                break;

            case SensorType.IntSensor:
                IntSensorData intData = JsonSerializer.Deserialize <IntSensorData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetSimpleSensorsString(expected.TimeCollected, intData.Comment, intData.IntValue),
                             actual.StringValue);
                Assert.Equal(intData.IntValue.ToString(), actual.ShortStringValue);
                break;

            case SensorType.DoubleSensor:
                DoubleSensorData doubleData = JsonSerializer.Deserialize <DoubleSensorData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetSimpleSensorsString(expected.TimeCollected, doubleData.Comment, doubleData.DoubleValue),
                             actual.StringValue);
                Assert.Equal(doubleData.DoubleValue.ToString(), actual.ShortStringValue);
                break;

            case SensorType.StringSensor:
                StringSensorData stringData = JsonSerializer.Deserialize <StringSensorData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetSimpleSensorsString(expected.TimeCollected, stringData.Comment, stringData.StringValue),
                             actual.StringValue);
                Assert.Equal(stringData.StringValue, actual.ShortStringValue);
                break;

            case SensorType.IntegerBarSensor:
                IntBarSensorData intBarData = JsonSerializer.Deserialize <IntBarSensorData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetBarSensorsString(expected.TimeCollected, intBarData.Comment, intBarData.Min, intBarData.Mean, intBarData.Max, intBarData.Count, intBarData.LastValue),
                             actual.StringValue);
                Assert.Equal(SensorDataStringValuesFactory.GetBarSensorsShortString(intBarData.Min, intBarData.Mean, intBarData.Max, intBarData.Count, intBarData.LastValue),
                             actual.ShortStringValue);
                break;

            case SensorType.DoubleBarSensor:
                DoubleBarSensorData doubleBarData = JsonSerializer.Deserialize <DoubleBarSensorData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetBarSensorsString(expected.TimeCollected, doubleBarData.Comment, doubleBarData.Min, doubleBarData.Mean, doubleBarData.Max, doubleBarData.Count, doubleBarData.LastValue),
                             actual.StringValue);
                Assert.Equal(SensorDataStringValuesFactory.GetBarSensorsShortString(doubleBarData.Min, doubleBarData.Mean, doubleBarData.Max, doubleBarData.Count, doubleBarData.LastValue),
                             actual.ShortStringValue);
                break;

            case SensorType.FileSensorBytes:
                FileSensorBytesData fileSensorBytesData = JsonSerializer.Deserialize <FileSensorBytesData>(expected.TypedData);
                Assert.Equal(SensorDataStringValuesFactory.GetFileSensorsString(expected.TimeCollected, fileSensorBytesData.Comment, fileSensorBytesData.FileName, fileSensorBytesData.Extension, fileSensorBytesData.FileContent.Length),
                             actual.StringValue);
                Assert.Equal(SensorDataStringValuesFactory.GetFileSensorsShortString(fileSensorBytesData.FileName, fileSensorBytesData.Extension, fileSensorBytesData.FileContent.Length),
                             actual.ShortStringValue);
                break;
            }
        }
        private SensorHistoryData Convert(IntBarSensorData typedData)
        {
            SensorHistoryData result = new SensorHistoryData();

            result.TypedData  = JsonSerializer.Serialize(typedData);
            result.Time       = typedData.EndTime;
            result.SensorType = SensorType.IntegerBarSensor;
            return(result);
        }
        /// <summary>
        /// Set fields, for which collecting lists of values is required
        /// </summary>
        /// <param name="currentItem"></param>
        private void AddDataFromLists(IntBarSensorData currentItem)
        {
            currentItem.Mean        = CountMean(_MeanList);
            currentItem.Percentiles = new List <PercentileValueInt>();
            //Just add values that "seem to be fine" if there is no more data
            if (_percentilesList.Count < 3)
            {
                currentItem.Percentiles.Add(new PercentileValueInt()
                {
                    Percentile = 0.5, Value = currentItem.Mean
                });
                currentItem.Percentiles.Add(new PercentileValueInt()
                {
                    Percentile = 0.25, Value = currentItem.Min
                });
                currentItem.Percentiles.Add(new PercentileValueInt()
                {
                    Percentile = 0.75, Value = currentItem.Max
                });
                return;
            }

            _percentilesList.Sort();
            //Special case where Q1 and Q3 calculations may fail
            if (_percentilesList.Count == 3)
            {
                currentItem.Percentiles.Add(new PercentileValueInt()
                {
                    Percentile = 0.5, Value = _percentilesList[1]
                });
                currentItem.Percentiles.Add(new PercentileValueInt()
                {
                    Percentile = 0.25, Value = _percentilesList[0]
                });
                currentItem.Percentiles.Add(new PercentileValueInt()
                {
                    Percentile = 0.75, Value = _percentilesList[2]
                });
                return;
            }

            //Calculate all percentiles normally
            currentItem.Percentiles.Add(new PercentileValueInt()
            {
                Percentile = 0.5, Value = CountMedian()
            });
            currentItem.Percentiles.Add(new PercentileValueInt()
            {
                Percentile = 0.25, Value = CountQ1()
            });
            currentItem.Percentiles.Add(new PercentileValueInt()
            {
                Percentile = 0.75, Value = CountQ3()
            });
        }
        /// <summary>
        /// This method applies possible changes to the current data item for fields, for which
        /// collecting datas is not required
        /// </summary>
        /// <param name="data">Currently processed data item</param>
        /// <param name="currentItem">Current summary item</param>
        private void ProcessItem(IntBarSensorData data, IntBarSensorData currentItem)
        {
            currentItem.Count += data.Count;
            if (data.Max > currentItem.Max)
            {
                currentItem.Max = data.Max;
            }

            if (data.Min < currentItem.Min)
            {
                currentItem.Min = data.Min;
            }
        }
 /// <summary>
 /// Add all percentiles to united percentile list for later calculations of Q1, median and Q3
 /// </summary>
 /// <param name="data"></param>
 private void AddDataToList(IntBarSensorData data)
 {
     try
     {
         _MeanList.Add(new KeyValuePair <int, int>(data.Mean, data.Count));
         if (data.Percentiles != null && data.Percentiles.Any())
         {
             _percentilesList.AddRange(data.Percentiles.Select(p => p.Value));
         }
     }
     catch (Exception e)
     {
     }
 }
        protected override List <SensorHistoryData> ProcessHistoryInternal(List <SensorHistoryData> uncompressedData,
                                                                           TimeSpan compressionInterval)
        {
            if (uncompressedData == null || !uncompressedData.Any())
            {
                return(new List <SensorHistoryData>());
            }

            if (uncompressedData.Count == 1)
            {
                return(uncompressedData);
            }

            List <IntBarSensorData>  typedDatas  = GetTypeDatas(uncompressedData);
            List <SensorHistoryData> result      = new List <SensorHistoryData>();
            IntBarSensorData         currentItem = new IntBarSensorData()
            {
                Count = 0, Max = int.MinValue, Min = int.MaxValue
            };
            DateTime startDate                = typedDatas[0].StartTime;
            int      processingCount          = 0;
            bool     needToAddCurrentAsSingle = false;
            bool     addingCurrent            = false;

            for (int i = 0; i < typedDatas.Count; ++i)
            {
                //We must add current processed object if its period bigger than interval,
                //or if current object is longer than than interval, or if we are processing the last object
                if (typedDatas[i].EndTime - typedDatas[i].StartTime > compressionInterval ||
                    (processingCount > 0 && startDate + compressionInterval < typedDatas[i].EndTime &&
                     i == typedDatas.Count - 1))
                {
                    needToAddCurrentAsSingle = true;
                }

                //Just process current bar as usual if it is not the last & in the interval
                if (typedDatas[i].EndTime < startDate + compressionInterval && i == typedDatas.Count - 1)
                {
                    AddDataToList(typedDatas[i]);
                    ProcessItem(typedDatas[i], currentItem);
                    addingCurrent = true;
                }

                //Finish bar if necessary. We finish previous bar if we are adding current as single
                //or if we are processing the last bar or if next bar is not in the interval
                if (i > 0 && (startDate + compressionInterval < typedDatas[i].EndTime || needToAddCurrentAsSingle ||
                              i == typedDatas.Count - 1))
                {
                    if (processingCount > 0)
                    {
                        AddDataFromLists(currentItem);
                        ClearLists();
                        currentItem.StartTime = startDate;
                        currentItem.EndTime   = addingCurrent ? typedDatas[i].EndTime : typedDatas[i - 1].EndTime;
                        result.Add(Convert(currentItem));
                        currentItem = new IntBarSensorData()
                        {
                            Count = 0, Max = int.MinValue, Min = int.MaxValue
                        };
                        processingCount = 0;
                    }
                }

                //We add current bar to list if needed, and proceed to the next one
                if (needToAddCurrentAsSingle)
                {
                    result.Add(Convert(typedDatas[i]));
                    needToAddCurrentAsSingle = false;
                    if (i != typedDatas.Count - 1)
                    {
                        startDate = typedDatas[i + 1].StartTime;
                        continue;
                    }
                }

                //if (i == typedDatas.Count - 1)
                //{
                //    result.Add(Convert(typedDatas[i], typedDatas[i].EndTime));
                //}

                //Start new bar, might need this right after finished previous
                //We start new bar if we finished previous and there are more objects in the list
                //We continue after starting because we have already processed it
                if (processingCount == 0 && i != typedDatas.Count - 1)
                {
                    startDate = typedDatas[i].StartTime;
                    AddDataToList(typedDatas[i]);
                    ProcessItem(typedDatas[i], currentItem);
                    ++processingCount;
                    continue;
                }

                //If we did not finish previous bar and did not add current, just add currently processed bar
                // and continue
                AddDataToList(typedDatas[i]);
                ProcessItem(typedDatas[i], currentItem);
                ++processingCount;
            }

            return(result);
        }