/// <summary> /// /// </summary> /// <returns></returns> public static string GetGroupBy(Time.Range r) { var from = DateTimeMathParser.ToDateTime(r.From, false); var to = DateTimeMathParser.ToDateTime(r.To, true); return((from.HasValue && to.HasValue) ? GetGroupBy(from.Value, to.Value) : "1m"); }
/// <summary> /// /// </summary> /// <returns></returns> public string Build(string root, Time.Range r) { var list = this .Where(x => x.Params?.Length > 0) .ToList(); var groupByTime = list.FirstOrDefault(x => x.Type == GroupBy.Kind.Time); var groupByFill = list.FirstOrDefault(x => x.Type == GroupBy.Kind.Fill); var groupByTag = list .Where(x => x.Type == GroupBy.Kind.Tag) .ToList(); if (null != groupByTime) { var influxGroupBy = InfluxDateTimeHelper.GetGroupBy(r); //query = query.Replace( VmFilter.TIME_FILTER_KEY, influxFilter ); //var gb = ( this.range ) ? this.getOptimalAutoGroupBy() : groupByTime.params[ 0 ]; var gb = groupByTime.Params?[0]; root = $"{root} GROUP BY time({gb})"; var groupByAutoIndex = root.IndexOf(GROUP_BY_AUTO_KEY); if (-1 != groupByAutoIndex) { root = root.Replace(GROUP_BY_AUTO_KEY, influxGroupBy); // TODO } } if (groupByTag.Count > 0) { root = (null == groupByTime) ? $"{root} GROUP BY" : $"{root},"; var index = 0; groupByTag .ForEach(e => { var i = index > 0 ? ", " : " "; root = $"{root}{i} \"{e.Params [ 0 ]}\""; ++index; }); } if (null != groupByFill) { root = $"{root} FILL({groupByFill.Params [ 0 ] })"; } return(root); }
/// <summary> /// /// </summary> /// <returns></returns> private string RenderMeasurement(Time.Range r) { var meas = (string.IsNullOrEmpty(Measurement)) ? "measurement" : Measurement; var rp = (!string.IsNullOrEmpty(Policy) && Policy != "default") ? $"\"{Policy}\"." : ""; var root = $"{rp}\"{meas}\""; var cond = Tags?.Build() ?? string.Empty; var timeFilter = InfluxDateTimeHelper.GetFilter(r); if (!string.IsNullOrEmpty(cond)) { root = $"{root} WHERE ({cond}) and {timeFilter}"; } else { // TODO root = $"{root} WHERE {timeFilter}"; } if (null != GroupBy) { root = GroupBy?.Build(root, r); } if (Order != OrderByTime.Asc) { root = $"{root} ORDER BY time DESC"; } if (Limit > 0) { root = $"{root} LIMIT {Limit}"; } if (SLimit > 0) { root = $"{root} SLIMIT {SLimit}"; } return(root); }
/// <summary> /// /// </summary> /// <returns></returns> public static string GetFilter(Time.Range range) { if (null == range) { return(TIME_FILTER); } var from = GetTime(range?.From, false); var to = GetTime(range?.To, true); var fromIsAbsolute = from.EndsWith("ms"); if (to == "now()" && !fromIsAbsolute) { return($"time >= {from}"); } return($"time >= {from} and time <= {to}"); }
/// <summary> /// /// </summary> /// <returns></returns> public string Compile(Time.Range r = null) { var res = string.Empty; if (UseRawQuery && !string.IsNullOrEmpty(RawQuery)) { res = RawQuery; } else { var sb = new StringBuilder(); sb.Append($"SELECT {Fields?.Build()} FROM {RenderMeasurement( r )}"); res = sb.ToString(); } //InfluxDateTimeHelper.GetFilter() return(res); }