示例#1
0
        /// <summary>
        /// Gets the history of a storage site environemental factor history.
        /// </summary>
        /// <param name="id">ID of the storage site.</param>
        /// <param name="factor">The factor to get data for.</param>
        /// <param name="startTime">The history start time.</param>
        /// <param name="endTime">The history end time.</param>
        /// <returns>Returns the history.</returns>
        private IActionResult GetStorageSiteEnvironementalFactorHistory(Guid id, EnvironmentalFactor factor, DateTime startTime, DateTime endTime, int?maxPoints)
        {
            // Validate times
            if (startTime == null || startTime == default)
            {
                return(HandleBadRequest("No start time provided."));
            }
            startTime = startTime.ToUniversalTime();
            if (endTime == null || endTime == default)
            {
                endTime = DateTime.UtcNow;
            }
            if (startTime > DateTime.UtcNow || startTime > endTime)
            {
                return(HandleBadRequest("Invalid start time provided."));
            }

            // Get history
            try
            {
                IEnumerable <DataPoint> history = EnvironmentService.GetHistory(id, factor, startTime, endTime, maxPoints);
                return(Ok(history));
            }
            catch (StorageSiteNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }
示例#2
0
        /// <summary>
        /// Gets the min and max values of a storage site environemental factor.
        /// </summary>
        /// <param name="id">ID of the storage site.</param>
        /// <param name="factor">The factor to get data for.</param>
        /// <param name="startTime">The period start time.</param>
        /// <param name="endTime">The period end time.</param>
        /// <returns>Returns the maxima.</returns>
        private IActionResult GetStorageSiteEnvironementalFactorExtrema(Guid id, EnvironmentalFactor factor, DateTime startTime, DateTime endTime)
        {
            // Validate times
            if (startTime == null || startTime == default)
            {
                return(HandleBadRequest("No start time provided."));
            }
            startTime = startTime.ToUniversalTime();
            if (endTime == null || endTime == default)
            {
                endTime = DateTime.UtcNow;
            }
            if (startTime > DateTime.UtcNow || startTime > endTime)
            {
                return(HandleBadRequest("Invalid start time provided."));
            }

            // Get extrema
            try
            {
                Extrema extrema = EnvironmentService.GetExtrema(id, factor, startTime, endTime);
                return(Ok(extrema));
            }
            catch (StorageSiteNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }
示例#3
0
        /// <summary>
        /// Gets the history of a specific storage site and environmental factor.
        /// </summary>
        /// <param name="siteId">The ID of the storage site to get the history for.</param>
        /// <param name="factor">The factor to get the history for.</param>
        /// <param name="startTime">Start time of the period to get value for.</param>
        /// <param name="endTime">End time of the period to get value for.</param>
        /// <param name="maxPoints">Optional upper bound of points for data density reduction.</param>
        /// <returns>Returns the matching values.</returns>
        public IEnumerable <DataPoint> GetHistory(Guid siteId, EnvironmentalFactor factor, DateTime startTime, DateTime endTime, int?maxPoints)
        {
            StorageSite             site    = LocationsService.GetStorageSite(siteId);
            IEnumerable <DataPoint> history = EnvironmentalDataRepository.GetHistory(site, factor, startTime, endTime);

            if (maxPoints.HasValue && history.Count() > maxPoints)
            {
                history = DataDensityReducer.ReduceDensity(history, maxPoints.Value);
            }
            return(history);
        }
示例#4
0
        /// <summary>
        /// Gets the database colum name for an environmental factor..
        /// </summary>
        /// <param name="factor">The factor to get the column name for.</param>
        /// <returns>Returns the column name</returns>
        /// <exception cref="NotImplementedException"/>
        private string GetColumnName(EnvironmentalFactor factor)
        {
            switch (factor)
            {
            case EnvironmentalFactor.Temperature:
                return("temperature");

            case EnvironmentalFactor.Humidity:
                return("humidity");

            default: throw new NotImplementedException();
            }
        }
示例#5
0
        /// <summary>
        /// Gets the latest value for a specific storage site and environmental factor.
        /// </summary>
        /// <param name="site">The storage site to get the latest value for.</param>
        /// <param name="factor">The factor to get the latest value for.</param>
        /// <returns>
        /// Returns the latest value, or null.
        /// </returns>
        public DataPoint GetLatestValue(StorageSite site, EnvironmentalFactor factor)
        {
            string column = GetColumnName(factor);

            DataPoint value = null;

            using (IDbConnection connection = GetNewConnection())
            {
                value = connection.QuerySingleOrDefault <DataPoint>($"SELECT timestamp,{column} AS value FROM environment WHERE site=@Id AND timestamp>now()-interval '1day' ORDER BY timestamp DESC LIMIT 1", new
                {
                    site.Id
                });
            }
            return(value);
        }
示例#6
0
 /// <summary>
 /// Gets the last value for a storage site environemental factor.
 /// </summary>
 /// <param name="id">ID of the storage site.</param>
 /// <param name="factor">The factor to get data for.</param>
 /// <returns>Returns the last recorded value.</returns>
 private IActionResult GetStorageSiteEnvironementalFactor(Guid id, EnvironmentalFactor factor)
 {
     try
     {
         DataPoint lastValue = EnvironmentService.GetLatestValue(id, factor);
         return(Ok(lastValue));
     }
     catch (StorageSiteNotFoundException exception)
     {
         return(HandleResourceNotFoundException(exception));
     }
     catch (Exception exception)
     {
         return(HandleUnexpectedException(exception));
     }
 }
示例#7
0
        /// <summary>
        /// Gets the min and max values for a specific storage site and environmental factor.
        /// </summary>
        /// <param name="site">The storage site to get the history for.</param>
        /// <param name="factor">The factor to get the history for.</param>
        /// <param name="startTime">Start time of the period to get value for.</param>
        /// <param name="endTime">End time of the period to get value for.</param>
        /// <returns>
        /// Returns the extrema.
        /// </returns>
        public Extrema GetExtrema(StorageSite site, EnvironmentalFactor factor, DateTime startTime, DateTime endTime)
        {
            string column = GetColumnName(factor);

            Extrema extrema = null;

            using (IDbConnection connection = GetNewConnection())
            {
                extrema = connection.QuerySingleOrDefault <Extrema>($"SELECT min({column}) AS min_value, max({column}) AS max_value FROM environment WHERE site=@Id AND timestamp>@StartTime AND timestamp<@EndTime", new
                {
                    site.Id,
                    startTime,
                    endTime
                });
            }
            return(extrema);
        }
示例#8
0
        /// <summary>
        /// Gets the history of a specific storage site and environmental factor.
        /// </summary>
        /// <param name="site">The storage site to get the history for.</param>
        /// <param name="factor">The factor to get the history for.</param>
        /// <param name="startTime">Start time of the period to get value for.</param>
        /// <param name="endTime">End time of the period to get value for.</param>
        /// <returns>
        /// Returns the matching values.
        /// </returns>
        public IEnumerable <DataPoint> GetHistory(StorageSite site, EnvironmentalFactor factor, DateTime startTime, DateTime endTime)
        {
            string column = GetColumnName(factor);

            IEnumerable <DataPoint> history = null;

            using (IDbConnection connection = GetNewConnection())
            {
                history = connection.Query <DataPoint>($"SELECT timestamp,{column} AS value FROM environment WHERE site=@Id AND timestamp>@StartTime AND timestamp<@EndTime ORDER BY timestamp ASC", new
                {
                    site.Id,
                    startTime,
                    endTime
                });
            }
            return(history);
        }
        public Extrema GetExtrema(StorageSite site, EnvironmentalFactor factor, DateTime startTime, DateTime endTime)
        {
            Extrema extrema = null;

            switch (factor)
            {
            case EnvironmentalFactor.Temperature:
                if (Temperature.TryGetValue(site.Id, out List <DataPoint> temperatures))
                {
                    if (temperatures.Count > 0)
                    {
                        extrema = new Extrema()
                        {
                            MaxValue = temperatures.Where(t => t.Timestamp >= startTime && t.Timestamp <= endTime).Max(t => t.Value),
                            MinValue = temperatures.Where(t => t.Timestamp >= startTime && t.Timestamp <= endTime).Min(t => t.Value)
                        };
                    }
                }
                break;

            case EnvironmentalFactor.Humidity:
                if (Humidity.TryGetValue(site.Id, out List <DataPoint> humidities))
                {
                    if (humidities.Count > 0)
                    {
                        extrema = new Extrema()
                        {
                            MaxValue = humidities.Where(t => t.Timestamp >= startTime && t.Timestamp <= endTime).Max(t => t.Value),
                            MinValue = humidities.Where(t => t.Timestamp >= startTime && t.Timestamp <= endTime).Min(t => t.Value)
                        };
                    }
                }
                break;

            default: throw new NotImplementedException();
            }
            return(extrema);
        }
        public IEnumerable <DataPoint> GetHistory(StorageSite site, EnvironmentalFactor factor, DateTime startTime, DateTime endTime)
        {
            IEnumerable <DataPoint> history = new List <DataPoint>();

            switch (factor)
            {
            case EnvironmentalFactor.Temperature:
                if (Temperature.TryGetValue(site.Id, out List <DataPoint> temperatures))
                {
                    history = temperatures.Where(t => t.Timestamp >= startTime && t.Timestamp <= endTime);
                }
                break;

            case EnvironmentalFactor.Humidity:
                if (Humidity.TryGetValue(site.Id, out List <DataPoint> humidities))
                {
                    history = humidities.Where(t => t.Timestamp >= startTime && t.Timestamp <= endTime);
                }
                break;

            default: throw new NotImplementedException();
            }
            return(history);
        }
        public DataPoint GetLatestValue(StorageSite site, EnvironmentalFactor factor)
        {
            DataPoint dataPoint = null;

            switch (factor)
            {
            case EnvironmentalFactor.Temperature:
                if (Temperature.TryGetValue(site.Id, out List <DataPoint> temperatures))
                {
                    dataPoint = temperatures.LastOrDefault();
                }
                break;

            case EnvironmentalFactor.Humidity:
                if (Humidity.TryGetValue(site.Id, out List <DataPoint> humidities))
                {
                    dataPoint = humidities.LastOrDefault();
                }
                break;

            default: throw new NotImplementedException();
            }
            return(dataPoint);
        }
示例#12
0
        /// <summary>
        /// Gets the min and max values for a specific storage site and environmental factor.
        /// </summary>
        /// <param name="siteId">The ID of the storage site to get the history for.</param>
        /// <param name="factor">The factor to get the history for.</param>
        /// <param name="startTime">Start time of the period to get value for.</param>
        /// <param name="endTime">End time of the period to get value for.</param>
        /// <returns>Returns the extrema.</returns>
        public Extrema GetExtrema(Guid siteId, EnvironmentalFactor factor, DateTime startTime, DateTime endTime)
        {
            StorageSite site = LocationsService.GetStorageSite(siteId);

            return(EnvironmentalDataRepository.GetExtrema(site, factor, startTime, endTime));
        }
示例#13
0
        /// <summary>
        /// Gets the latest value for a specific storage site and environmental factor.
        /// </summary>
        /// <param name="siteId">The ID of the storage site to get the latest value for.</param>
        /// <param name="factor">The factor to get the latest value for.</param>
        /// <returns>Returns the latest value, or null.</returns>
        public DataPoint GetLatestValue(Guid siteId, EnvironmentalFactor factor)
        {
            StorageSite site = LocationsService.GetStorageSite(siteId);

            return(EnvironmentalDataRepository.GetLatestValue(site, factor));
        }