Beispiel #1
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);
        }
Beispiel #2
0
        /// <summary>
        /// Sets up the service with all necessare components. Gets all known storage sites and starts data readers for each of them.
        /// </summary>
        /// <param name="loggerFactory">A factory to create loggers from.</param>
        /// <param name="configuration">The app configuration.</param>
        /// <param name="locationsService">Provides storage sites.</param>
        /// <param name="environmentalDataRepository">Repository of environmental data.</param>
        /// <param name="dataDensityReducer">Data density reducing strategy.</param>
        public EnvironmentService(ILoggerFactory loggerFactory,
                                  IConfiguration configuration,
                                  LocationsService locationsService,
                                  IEnvironmentalDataRepository environmentalDataRepository,
                                  IDataDensityReducer dataDensityReducer)
        {
            LoggerFactory               = loggerFactory;
            Logger                      = LoggerFactory.CreateLogger <EnvironmentService>();
            LocationsService            = locationsService;
            EnvironmentalDataRepository = environmentalDataRepository;
            SnapshotQueue               = new ConcurrentQueue <EnvironmentSnapshot>();
            DataDensityReducer          = dataDensityReducer;

            // Get Kafka endpoint
            KafkaEndpoint = configuration.GetValue("Kafka", "");
            if (string.IsNullOrEmpty(KafkaEndpoint))
            {
                throw new Exception("Invalid Kafka endpoint provided!");
            }
            else
            {
                Logger.LogInformation($"Configured Kafka endpoint `{KafkaEndpoint}`.");
            }

            // Start reading from snapshot queue
            QueueConsumer = new TaskFactory().StartNew(() =>
            {
                while (true)
                {
                    if (SnapshotQueue.TryDequeue(out EnvironmentSnapshot snapshot))
                    {
                        EnvironmentalDataRepository.RecordEnvironmentalSnapshot(snapshot);
                    }
                    else
                    {
                        Task.Delay(1000);
                    }
                }
            });

            // Set up data readers for all existing storage sites
            DataReaders = new Dictionary <Guid, EnvironmentalDataReader>();
            foreach (StorageSite site in LocationsService.GetAllStorageSites())
            {
                CreateAndRegisterDataReader(site);
            }

            // Subscribe to the creation of new storage sites
            LocationsService.Subscribe(this);

            // TODO: check for failed readers (Kafka not available?) and restart them
        }
Beispiel #3
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));
        }
Beispiel #4
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));
        }