/// <summary> /// Posts an InfluxDataPoint to given measurement /// </summary> /// <param name="dbName">InfluxDB database name</param> /// <param name="point">Influx data point to be written</param> /// <returns>True:Success, False:Failure</returns> ///<exception cref="UnauthorizedAccessException">When Influx needs authentication, and no user name password is supplied or auth fails</exception> ///<exception cref="HttpRequestException">all other HTTP exceptions</exception> public async Task <bool> PostPointAsync(string dbName, IInfluxDatapoint point) { ByteArrayContent requestContent = new ByteArrayContent(Encoding.UTF8.GetBytes(point.ConvertToInfluxLineProtocol())); var endPoint = new Dictionary <string, string>() { { "db", dbName }, { "precision", precisionLiterals[(int)point.Precision] } }; if (!String.IsNullOrWhiteSpace(point.Retention?.Name)) { endPoint.Add("rp", point.Retention?.Name); } HttpResponseMessage response = await PostAsync(endPoint, requestContent); if (response.StatusCode == HttpStatusCode.BadRequest) { throw InfluxDBException.ProcessInfluxDBError(await response.Content.ReadAsStringAsync()); } else if (response.StatusCode == HttpStatusCode.NoContent) { point.Saved = true; return(true); } else { point.Saved = false; return(false); } }
/// <summary> /// Posts an InfluxDataPoint to given measurement /// </summary> /// <param name="dbName">InfluxDB database name</param> /// <param name="point">Influx data point to be written</param> /// <returns>True:Success, False:Failure</returns> ///<exception cref="UnauthorizedAccessException">When Influx needs authentication, and no user name password is supplied or auth fails</exception> ///<exception cref="HttpRequestException">all other HTTP exceptions</exception> public async Task <bool> PostPointAsync(string dbName, IInfluxDatapoint point) { var influxAddress = new Uri(String.Format("{0}/write?", InfluxUrl)); var builder = new UriBuilder(influxAddress); builder.Query = await new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("db", dbName), new KeyValuePair <string, string>("precision", precisionLiterals[(int)point.Precision]) }).ReadAsStringAsync(); ByteArrayContent requestContent = new ByteArrayContent(Encoding.UTF8.GetBytes(point.ConvertToInfluxLineProtocol())); HttpResponseMessage response = await PostAsync(builder, requestContent); if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadGateway || (response.StatusCode == HttpStatusCode.InternalServerError && response.ReasonPhrase == "INKApi Error")) //502 Connection refused { throw new UnauthorizedAccessException("InfluxDB needs authentication. Check uname, pwd parameters"); } //if(response.StatusCode==HttpStatusCode.NotFound) else if (response.StatusCode == HttpStatusCode.NoContent) { point.Saved = true; return(true); } else { point.Saved = false; return(false); } }
private static async Task ExecuteHourlyRevenue() { List <IInfluxDatapoint> influxDatapoints = new List <IInfluxDatapoint>(); using (var session = dataAccessor.SessionFactory.OpenSession()) using (var trans = session.BeginTransaction()) { PeiuPlatformMethodsExecutor methodsExecutor = new PeiuPlatformMethodsExecutor(session); var results = await session.CreateCriteria <HourlyAccmofMeasurement>().ListAsync <HourlyAccmofMeasurement>(); foreach (HourlyAccmofMeasurement row in results) { HourlyActualRevenue hourlyRevenue = new HourlyActualRevenue(); hourlyRevenue.Createdt = row.Createdt; hourlyRevenue.Hour = row.Hour; hourlyRevenue.Rcc = row.Rcc; hourlyRevenue.Siteid = row.Siteid; double pvPower = row.Sumofpvgeneration; double dhg = row.Sumofdischarge; int? rec = methodsExecutor.GetRec(row.Createdt); float?smp = methodsExecutor.GetSmpPrice(row.Rcc == 16 ? 1 : 0, row.Createdt); //hourlyRevenue.Rec = rec.HasValue ? rec.Value : 0; //hourlyRevenue.Smp = smp.HasValue ? smp.Value : 0; if (rec.HasValue && smp.HasValue) { double sumOfChg = row.Sumofcharge * -1; double exp1 = ((pvPower - sumOfChg) * 1 * rec.Value / 1000) + (dhg * 5 * rec.Value / 1000); double exp2 = (((pvPower - sumOfChg) * 1) + (dhg * 5)) * (rec.Value / 1000); hourlyRevenue.Revenue = exp1 + exp2; } else { hourlyRevenue.Revenue = 0; } await session.SaveOrUpdateAsync(hourlyRevenue); IInfluxDatapoint datapoint = ConvertinfluxDatapoint(hourlyRevenue); influxDatapoints.Add(datapoint); } await trans.CommitAsync(); } logger.Info("Complete record to mysql"); await influxDBClient.PostPointsAsync("statistics", influxDatapoints, influxDatapoints.Count); logger.Info("Completed record to influxdb"); }
public InfluxDBException(string reason, string message, IInfluxDatapoint point) : this (reason, message) { FailedPoint = point; }
private static async Task Run(DateTime?dt) { DateTime date = DateTime.Now.Date.AddHours(DateTime.Now.Hour - 1); if (dt.HasValue) { date = dt.Value; } logger.Info("RUN Datetime: " + date); long tps = ToUnixTimeSeconds(date); long endTps = ToUnixTimeSeconds(date.AddHours(1)); logger.Info("Reading A4 data from influxdb..."); List <IInfluxSeries> result = await Select(tps, endTps, "sum_chg_1h", "sum_dhg_1h", "sum_pv_1h"); IInfluxSeries pvSeries = result.FirstOrDefault(x => x.SeriesName == "sum_pv_1h"); IInfluxSeries chgSeries = result.FirstOrDefault(x => x.SeriesName == "sum_chg_1h"); IInfluxSeries dhgSeries = result.FirstOrDefault(x => x.SeriesName == "sum_dhg_1h"); List <IInfluxDatapoint> influxDatapoints = new List <IInfluxDatapoint>(); using (var session = dataAccessor.SessionFactory.OpenSession()) using (var trans = session.BeginTransaction()) { PeiuPlatformMethodsExecutor methodsExecutor = new PeiuPlatformMethodsExecutor(session); foreach (dynamic row in pvSeries.Entries) { string siteId = row.SiteId; string rccString = row.Rcc; DateTime time = row.Time; TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Asia/Seoul"); DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(time, timeZoneInfo); string inJeju = row.InJeju; string sumOfPvPower = row.SumOfPvPower; dynamic chg_row = chgSeries.Entries.FirstOrDefault(x => x.Time == time && x.SiteId == siteId); dynamic dhg_row = dhgSeries.Entries.FirstOrDefault(x => x.Time == time && x.SiteId == siteId); PeiuPlatform.Models.Mysql.HourlyAccmofMeasurement newLine = new Models.Mysql.HourlyAccmofMeasurement(); HourlyActualRevenue hourlyRevenue = new HourlyActualRevenue(); newLine.Createdt = hourlyRevenue.Createdt = localTime.Date; newLine.Hour = hourlyRevenue.Hour = localTime.Hour; newLine.Inisland = hourlyRevenue.Inisland = inJeju.Equals("1") ? true : false; int rcc = int.Parse(rccString); newLine.Rcc = hourlyRevenue.Rcc = rcc; newLine.Siteid = hourlyRevenue.Siteid = int.Parse(siteId); double pvPower = double.Parse(sumOfPvPower); double dhg = dhg_row == null ? 0d : double.Parse(dhg_row.SumOfDhg); newLine.Sumofpvgeneration = pvPower; newLine.Sumofcharge = chg_row == null ? 0d : double.Parse(chg_row.SumOfChg); newLine.Sumofdischarge = dhg; int? rec = methodsExecutor.GetRec(localTime.Date); float?smp = methodsExecutor.GetSmpPrice(int.Parse(inJeju), localTime.Date); //hourlyRevenue.Rec = rec.HasValue ? rec.Value : 0; //hourlyRevenue.Smp = smp.HasValue ? smp.Value : 0; if (rec.HasValue && smp.HasValue) { double sumOfChg = newLine.Sumofcharge * -1; double exp1 = ((pvPower - sumOfChg) * 1 * rec.Value / 1000) + (dhg * 5 * rec.Value / 1000); double exp2 = (((pvPower - sumOfChg) * 1) + (dhg * 5)) * (rec.Value / 1000); hourlyRevenue.Revenue = exp1 + exp2; } else { hourlyRevenue.Revenue = 0; } IInfluxDatapoint datapoint = ConvertinfluxDatapoint(hourlyRevenue); influxDatapoints.Add(datapoint); await session.SaveOrUpdateAsync(newLine); await session.SaveOrUpdateAsync(hourlyRevenue); } await trans.CommitAsync(); } logger.Info("Completed record to mysql"); await influxDBClient.PostPointsAsync("statistics", influxDatapoints, influxDatapoints.Count); logger.Info("Completed record to influxdb"); logger.Info("Complete"); }
public InfluxDBException(string reason, string message, IInfluxDatapoint point) : this(reason, message) { FailedPoint = point; }
/// <summary> /// Posts an InfluxDataPoint to given measurement /// </summary> /// <param name="dbName">InfluxDB database name</param> /// <param name="point">Influx data point to be written</param> /// <returns>True:Success, False:Failure</returns> ///<exception cref="UnauthorizedAccessException">When Influx needs authentication, and no user name password is supplied or auth fails</exception> ///<exception cref="HttpRequestException">all other HTTP exceptions</exception> public async Task<bool> PostPointAsync(string dbName, IInfluxDatapoint point) { var influxAddress = new Uri (String.Format ("{0}/write?", InfluxUrl)); var builder = new UriBuilder (influxAddress); builder.Query = await new FormUrlEncodedContent (new[] { new KeyValuePair<string, string>("db", dbName) , new KeyValuePair<string, string>("precision", precisionLiterals[(int) point.Precision]) }).ReadAsStringAsync (); ByteArrayContent requestContent = new ByteArrayContent (Encoding.UTF8.GetBytes (point.ConvertToInfluxLineProtocol ())); HttpResponseMessage response = await PostAsync (builder, requestContent); if ( response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadGateway || ( response.StatusCode == HttpStatusCode.InternalServerError && response.ReasonPhrase == "INKApi Error" ) ) //502 Connection refused throw new UnauthorizedAccessException ("InfluxDB needs authentication. Check uname, pwd parameters"); //if(response.StatusCode==HttpStatusCode.NotFound) else if ( response.StatusCode == HttpStatusCode.BadRequest ) { var content = await response.Content.ReadAsStringAsync (); //regex assumes error text from https://github.com/influxdata/influxdb/blob/master/models/points.go ParsePointsWithPrecision //fmt.Sprintf("'%s': %v", string(block[start:len(block)]) List<string> parts; bool partialWrite; if ( content.Contains ("partial write") ) { if ( content.Contains ("\\n") ) parts = Regex.Matches (content.Substring (content.IndexOf ("partial write:\\n") + 16), @"([\P{Cc}].*?) '([\P{Cc}].*?)':([\P{Cc}].*?)\\n").ToList (); else parts = Regex.Matches (content.Substring (content.IndexOf ("partial write:\\n") + 16), @"([\P{Cc}].*?) '([\P{Cc}].*?)':([\P{Cc}].*?)").ToList (); partialWrite = true; } else { parts = Regex.Matches (content, @"{\""error"":""([9\P{Cc}]+) '([\P{Cc}]+)':([a-zA-Z0-9 ]+)").ToList (); partialWrite = false; } throw new InfluxDBException (partialWrite ? "Partial Write" : "Failed to Write", String.Format ("{0}: {1} due to {2}", partialWrite ? "Partial Write" : "Failed to Write", parts[0], parts[2]), point); return false; } else if ( response.StatusCode == HttpStatusCode.NoContent ) { point.Saved = true; return true; } else { point.Saved = false; return false; } }
/// <summary> /// Posts an InfluxDataPoint to given measurement /// </summary> /// <param name="dbName">InfluxDB database name</param> /// <param name="point">Influx data point to be written</param> /// <returns>True:Success, False:Failure</returns> ///<exception cref="UnauthorizedAccessException">When Influx needs authentication, and no user name password is supplied or auth fails</exception> ///<exception cref="HttpRequestException">all other HTTP exceptions</exception> public async Task<bool> PostPointAsync (string dbName, IInfluxDatapoint point) { ByteArrayContent requestContent = new ByteArrayContent (Encoding.UTF8.GetBytes (point.ConvertToInfluxLineProtocol ())); var endPoint = new Dictionary<string, string> () { { "db", dbName }, { "precision", precisionLiterals[(int)point.Precision] }}; if (!String.IsNullOrWhiteSpace (point.Retention?.Name)) endPoint.Add ("rp", point.Retention?.Name); HttpResponseMessage response = await PostAsync (endPoint, requestContent); if (response.StatusCode == HttpStatusCode.BadRequest) { throw InfluxDBException.ProcessInfluxDBError (await response.Content.ReadAsStringAsync ()); } else if (response.StatusCode == HttpStatusCode.NoContent) { point.Saved = true; return true; } else { point.Saved = false; return false; } }