public Resource GetById(Guid guid) { using (var session = _sessionFactory.OpenSession()) { var resource = session.QueryOver <Database.Resource>().Where(x => x.Guid == guid) .JoinQueryOver(x => x.Sensors) .SingleOrDefault(); var complexMetrics = session.QueryOver <ComplexMetric>() .JoinQueryOver(x => x.Sensor) .JoinQueryOver(x => x.Resource) .Where(x => x.Guid == guid).List() .Select(x => x.Guid); if (resource == null) { throw new ArgumentException("resource with given guid does not exist"); } var mappedResource = _mapper.Map <Resource>(resource); mappedResource.Measurements = mappedResource.Measurements .Concat(complexMetrics.Select(x => _pathBuilder.CreateForSensor(x))).ToArray(); return(mappedResource); } }
public bool TryGet(ValuesQueryParameters parameters, out ValuesResponse values) { using (var session = _sessionFactory.OpenSession()) { var metric = session.QueryOver <ComplexMetric>() .Where(x => x.Guid == parameters.Id) .SingleOrDefault(); if (metric == null) { values = null; return(false); } var simpleValues = _simpleValuesRetriever.GetSimpleMetric( new ValuesQueryParameters { Id = metric.Sensor.Guid, From = parameters.From - TimeSpan.FromMilliseconds(metric.WindowSize), To = parameters.To }, session); var complexValues = CountComplexValues(metric, simpleValues, parameters); values = new ValuesResponse { Measurements = _pathBuilder.CreateForSensor(parameters.Id), Values = complexValues }; return(true); } }
public IMapper Create() { var config = new MapperConfiguration(cfg => { cfg.CreateMap <Resource, Api.Resources.Query.Resource>() .ForMember(x => x.Measurements, opt => opt.MapFrom( src => src.Sensors.SelectMany( sensor => sensor.ComplexMetrics.Select(complex => _pathBuilder .CreateForSensor(complex.Guid))) .Concat(src.Sensors.Select(sensor => _pathBuilder.CreateForSensor(sensor.Guid))) .ToArray() )) .ForMember(x => x.Id, opt => opt.MapFrom(x => x.Guid)); cfg.CreateMap <Sensor, Api.Measurements.Query.Sensor>() .ForMember(x => x.Host, opt => opt.MapFrom(x => _pathBuilder.CreateForResource(x.Resource.Guid))) .ForMember(x => x.Values, opt => opt.MapFrom(x => _pathBuilder.CreateForValues(x.Guid))); cfg.CreateMap <Database.Measurement, SensorValue>(); cfg.CreateMap <Metadata, UpdateResource>() .ForMember(x => x.Sensors, opt => opt.MapFrom(x => x.MeasuresArray)) .ForMember(x => x.Guid, opt => opt.MapFrom(x => x.ResourceId)); cfg.CreateMap <ComplexMetric, Api.Measurements.Query.Sensor>() .ForMember(x => x.Host, opt => opt.MapFrom(src => _pathBuilder.CreateForResource(src.Sensor.Resource.Guid))) .ForMember(x => x.Metric, opt => opt.MapFrom(src => src.Sensor.Metric)) .ForMember(x => x.Unit, opt => opt.MapFrom(src => src.Sensor.Unit)) .ForMember(x => x.Complex, opt => opt.MapFrom(src => true)) .ForMember(x => x.MaxValue, opt => opt.MapFrom(src => src.Sensor.MaxValue)) .ForMember(x => x.Values, opt => opt.MapFrom(src => _pathBuilder.CreateForValues(src.Guid))); }); var mapper = config.CreateMapper(); return(mapper); }
public ValuesResponse GetSimpleMetric(ValuesQueryParameters parameters, ISession session) { var query = session.QueryOver <Measurement>() .Where(x => x.Timestamp > parameters.From).And(x => x.Timestamp < parameters.To) .JoinQueryOver(x => x.Sensor) .Where(x => x.Guid == parameters.Id); var measurementsRowCount = query.RowCount(); if (measurementsRowCount > MaxMeasurementsLimit) { throw new ArgumentException("Too big date/time range."); } var measurements = query.List(); return(new ValuesResponse { Measurements = _pathBuilder.CreateForSensor(parameters.Id), Values = measurements.Select(x => _mapper.Map <SensorValue>(x)).ToArray() }); }