private static async Task WriteDataToInfluxDb() { try { AIO_Values values = await FetchValuesFromAIO(); //first the current data point bool success = await influxClient.CreateDatabaseAsync(influxDbName); //create db if not exist if (success) { await CreateRetentionPolicy(); // craete retention policy for minute data points if not exists var point = CreateInfluxSeries(values); //create the influx point var result = await influxClient.PostPointAsync(influxDbName, point); //save the data point Console.Write("Wrote point to influx db"); } //Now the longterm data success = await influxClient.CreateDatabaseAsync(influxDbNameLongterm); //create longeterm db if not exist if (success) { await CreateRetentionPolicyLongterm(); MonthlyValues monthlyValues = await GetCurrentMonthAggregation(); //check if we already have aggregations for the last month and year await InsertMonthlyValues(monthlyValues); } } catch (Exception ex) { Console.Write(ex.Message); } }
private static async Task <MonthlyValues> GetCurrentMonthAggregation() { if (DateTime.Now.Hour == 01 && DateTime.Now.Minute == 00) //we make this check only at 01:00 at night once a day { //get first and last day of last month string firstDayOfLastMonth = DateTime.Now.PreviousMonth().FirstDayOfMonth().BeginningOfDay().ToString("s") + "Z"; string lastDayOfLastMonth = DateTime.Now.PreviousMonth().LastDayOfMonth().EndOfDay().ToString("s") + "Z"; string monthIndex = DateTime.Now.PreviousMonth().FirstDayOfMonth().ToString("MM"); string yearIndex = DateTime.Now.PreviousMonth().FirstDayOfMonth().ToString("yyyy"); List <IInfluxSeries> resultsLastMonth = null; try { resultsLastMonth = await influxClient.QueryMultiSeriesAsync(influxDbNameLongterm, $"SELECT * from {measurement_PVmonthly} WHERE YearIndex = '{yearIndex}' AND MonthIndex ='{monthIndex}' limit 1"); } catch (InfluxDBException ex) { Console.Write(ex.Message); } //If e have not craeted the result for last month, we write it now. if (resultsLastMonth == null || !resultsLastMonth.Any()) { //get aggregated values for the last month List <IInfluxSeries> aggregatedLastMonth = null; MonthlyValues monthlyValues = null; try { aggregatedLastMonth = await influxClient.QueryMultiSeriesAsync(influxDbName, $"SELECT INTEGRAL(Pv,1h)/1000 as Pv_month,INTEGRAL(Load,1h)/1000 as Load_month, INTEGRAL(Inv,1h)/1000 as Inv_month, INTEGRAL(Demand,1h)/1000 as Demand_month, INTEGRAL(FeedIn,1h)/1000 as FeedIn_month FROM {measurement_points} WHERE time > '{firstDayOfLastMonth}' AND time < '{lastDayOfLastMonth}'"); monthlyValues = new MonthlyValues(); monthlyValues.PvMonth = float.Parse(aggregatedLastMonth.FirstOrDefault()?.Entries[0].Pv_month); monthlyValues.LoadMonth = float.Parse(aggregatedLastMonth.FirstOrDefault()?.Entries[0].Load_month); monthlyValues.InvMonth = float.Parse(aggregatedLastMonth.FirstOrDefault()?.Entries[0].Inv_month); monthlyValues.DemandMonth = float.Parse(aggregatedLastMonth.FirstOrDefault()?.Entries[0].Demand_month); monthlyValues.FeedInMonth = float.Parse(aggregatedLastMonth.FirstOrDefault()?.Entries[0].FeedIn_month); monthlyValues.MonthIndex = monthIndex; monthlyValues.YearIndex = yearIndex; } catch (InfluxDBException ex) { Console.Write(ex.Message); } return(monthlyValues); } } return(null); }
private static async Task InsertMonthlyValues(MonthlyValues monthlyValues) { if (monthlyValues != null) { var influxMonthlyValues = new InfluxDatapoint <InfluxValueField>(); influxMonthlyValues.UtcTimestamp = DateTime.Now.PreviousMonth().LastDayOfMonth().EndOfDay().ToUniversalTime(); influxMonthlyValues.Fields.Add("PvMonth", new InfluxValueField(monthlyValues.PvMonth)); influxMonthlyValues.Fields.Add("LoadMonth", new InfluxValueField(monthlyValues.LoadMonth)); influxMonthlyValues.Fields.Add("InvMonth", new InfluxValueField(monthlyValues.InvMonth)); influxMonthlyValues.Fields.Add("DemandMonth", new InfluxValueField(monthlyValues.DemandMonth)); influxMonthlyValues.Fields.Add("FeedInMonth", new InfluxValueField(monthlyValues.FeedInMonth)); influxMonthlyValues.Fields.Add("MonthIndex", new InfluxValueField(monthlyValues.MonthIndex)); influxMonthlyValues.Fields.Add("YearIndex", new InfluxValueField(monthlyValues.YearIndex)); influxMonthlyValues.MeasurementName = measurement_PVmonthly; influxMonthlyValues.Precision = TimePrecision.Minutes; influxMonthlyValues.Retention = new InfluxRetentionPolicy() { Name = ret_policy_longterm }; var result = await influxClient.PostPointAsync(influxDbNameLongterm, influxMonthlyValues); //save the monthly value } }