private static async Task RecordDetailedHistoricUsage(Config options, MetricsCollector influxCollector) { VerifyLookbackSet(options); var client = new FlickWebClient(options.Username, options.Password); for (var lookback = options.LookBackDays; lookback >= 0; lookback--) { var date = DateTime.Now.AddDays(-lookback); try { Console.WriteLine($"Fetching detailed power usage for {date.Date}"); var powerUsage = await client.FetchDetailedUsageForDay(date).ConfigureAwait(false); foreach (var powerAndPriceInterval in powerUsage) { var fields = new Dictionary <string, object> { ["price"] = powerAndPriceInterval.Price, ["units"] = powerAndPriceInterval.Units, ["total_cost"] = powerAndPriceInterval.Units * powerAndPriceInterval.Price }; influxCollector.Write("DetailedPowerUsage", fields, timestamp: powerAndPriceInterval.Start.ToUniversalTime()); } } catch (Exception exception) { Console.WriteLine($"Skipping {date} due to an exception {exception}"); } } }
private static async Task RecordSimpleHistoricUsage(Config options, MetricsCollector influxCollector) { VerifyLookbackSet(options); var webClient = new FlickWebClient(options.Username, options.Password); var powerUsage = await webClient.GetPowerUsage(DateTime.Now.Subtract(TimeSpan.FromDays(options.LookBackDays)), DateTime.Now).ConfigureAwait(false); foreach (var usageBucket in powerUsage) { var fields = new Dictionary <string, object> { ["usage"] = usageBucket.Value }; influxCollector.Write("PowerUsage", fields, timestamp: usageBucket.StartedAt.ToUniversalTime()); } Console.WriteLine($"Finished recording {powerUsage.Count} power usage buckets"); }