public object Handle(CreateComplexMeasurement command)
        {
            using (var session = _sessionFactory.OpenSession())
            {
                var baseMeasurement = session.QueryOver <Database.Sensor>()
                                      .Where(x => x.Guid == command.BaseMeasurement).SingleOrDefault();

                if (baseMeasurement == null)
                {
                    throw new ArgumentException($"No sensor with id {command.BaseMeasurement} found");
                }

                using (var transaction = session.BeginTransaction())
                {
                    var newComplex = new ComplexMetric()
                    {
                        Guid        = Guid.NewGuid(),
                        Frequency   = command.Frequency,
                        Sensor      = baseMeasurement,
                        TimeStart   = DateTime.Now,
                        WindowSize  = command.Windowsize,
                        Description = command.Description
                    };
                    session.Save(newComplex);
                    transaction.Commit();
                    return(new object());
                }
            }
        }
예제 #2
0
        private SensorValue[] CountComplexValues(ComplexMetric metric, ValuesResponse simpleValues, ValuesQueryParameters parameters)
        {
            var values     = new List <SensorValue>();
            var windowSize = TimeSpan.FromMilliseconds(metric.WindowSize);

            // TODO this can be optimized if needed
            for (var valueDate = parameters.From;
                 valueDate < parameters.To;
                 valueDate += TimeSpan.FromMilliseconds(metric.Frequency))
            {
                var aggregateValues =
                    simpleValues.Values.Where(x => x.Timestamp < valueDate && x.Timestamp >= valueDate - windowSize);
                if (!aggregateValues.Any())
                {
                    continue;
                }

                var newValue = new SensorValue()
                {
                    Value     = aggregateValues.Select(x => x.Value).Average(),
                    Timestamp = valueDate
                };
                values.Add(newValue);
            }
            return(values.ToArray());
        }