From f432e153ca07314cd8bcb0db47f77b06d10842ff Mon Sep 17 00:00:00 2001 From: Kyle Brandt Date: Wed, 12 Aug 2020 12:30:45 -0400 Subject: [PATCH] Dataframe/Alerting: to tsdb.TimeSeriesSlice - accept "empty" time series (#26903) this is in order to trigger no data when used with alerting. fixes #26897. This is perhaps better solved in the SDK but that needs more thought and is tracked in grafana/grafana-plugin-sdk-go#214 --- pkg/tsdb/frame_util.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/tsdb/frame_util.go b/pkg/tsdb/frame_util.go index d3b0d0c6535..066d16b9ffb 100644 --- a/pkg/tsdb/frame_util.go +++ b/pkg/tsdb/frame_util.go @@ -44,6 +44,14 @@ func convertTSDBTimePoint(point TimePoint) (t *time.Time, f *float64) { func FrameToSeriesSlice(frame *data.Frame) (TimeSeriesSlice, error) { tsSchema := frame.TimeSeriesSchema() if tsSchema.Type == data.TimeSeriesTypeNot { + // If no fields, or only a time field, create an empty TimeSeriesSlice with a single + // time series in order to trigger "no data" in alerting. + if len(frame.Fields) == 0 || (len(frame.Fields) == 1 && frame.Fields[0].Type().Time()) { + return TimeSeriesSlice{{ + Name: frame.Name, + Points: make(TimeSeriesPoints, 0), + }}, nil + } return nil, fmt.Errorf("input frame is not recognized as a time series") } // If Long, make wide