Example #1
0
        private static async Task MergeGiosDataWithFileStorage(PMDataSource giosDataSource, string storageFile, ILogger log)
        {
            var storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("StorageConnectionString"));
            var fileClient     = storageAccount.CreateCloudFileClient();

            var share = fileClient.GetShareReference(Environment.GetEnvironmentVariable("AzureShare"));

            try
            {
                if (await share.ExistsAsync())
                {
                    var giosDir     = share.GetRootDirectoryReference().GetDirectoryReference(Environment.GetEnvironmentVariable("AzureDir"));
                    var fileRef     = giosDir.GetFileReference(storageFile);
                    var fileDataSrc = JsonConvert.DeserializeObject <PMDataSource>(await fileRef.DownloadTextAsync());
                    if (fileDataSrc != null && fileDataSrc.Values.Any())
                    {
                        foreach (var fdv in fileDataSrc.Values)
                        {
                            if (!giosDataSource.Values.Any(x => x.Date.Equals(fdv.Date)))
                            {
                                giosDataSource.Values.Add(fdv);
                            }
                        }
                        log.LogInformation($"{giosDataSource.Values.Count} entries after merge - saving to {fileRef.Name} file...");
                    }

                    await fileRef.UploadTextAsync(JsonConvert.SerializeObject(giosDataSource));
                }
            }
            catch (Exception ex)
            {
                log.LogError($"Error occurred: {ex.Message}");
            }
        }
Example #2
0
        private static PMDataOutput GetCurrentDataOutput(PMDataSource source)
        {
            var    lastEntry = source.Values.First();
            double v         = Double.NaN;

            Double.TryParse(lastEntry?.Value, out v);

            var avg = Double.NaN;

            if (source.Values.All(x => !string.IsNullOrEmpty(x.Value)))
            {
                avg = source.Values
                      .Take(24)
                      .Select(x => Double.Parse(x?.Value))
                      .Average();
            }


            return(new PMDataOutput
            {
                Name = source.Key,
                LastUpdateDt = lastEntry?.Date.ToUniversalTime(),
                LastUpdateValue = v,
                AvgValue = avg
            });
        }