Postgres/MySQL/MSSQL: Fix region annotations not displayed correctly (#38936) (#38953)

Fix region annotations not displayed correctly when returning timeend column
as epoch timestamp and by that making sure that the returned data frame field
named timeend is treated as time type.

Fixes #38533

(cherry picked from commit fbdaf56a84)
This commit is contained in:
Marcus Efraimsson authored and GitHub committed 2021-09-08 09:51:27 +03:00
1 parent ccef062388
commit 4f215e06f1
4 files changed
+109 -8

No files matched your search

+28 -5
View File
@@ -20,6 +20,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/tsdb/interval"
"github.com/grafana/grafana/pkg/util/errutil"
"xorm.io/core"
"xorm.io/xorm"
)
@@ -270,11 +271,9 @@ func (e *dataPlugin) executeQuery(query plugins.DataSubQuery, wg *sync.WaitGroup
return
}
if qm.timeIndex != -1 {
if err := convertSQLTimeColumnToEpochMS(frame, qm.timeIndex); err != nil {
errAppendDebug("db convert time column failed", err, interpolatedQuery)
return
}
if err := convertSQLTimeColumnsToEpochMS(frame, qm); err != nil {
errAppendDebug("converting time columns failed", err, interpolatedQuery)
return
}
if qm.Format == dataQueryFormatSeries {
@@ -375,6 +374,7 @@ func (e *dataPlugin) newProcessCfg(query plugins.DataSubQuery, queryContext plug
columnNames: columnNames,
rows: rows,
timeIndex: -1,
timeEndIndex: -1,
metricIndex: -1,
metricPrefix: false,
queryContext: queryContext,
@@ -418,6 +418,12 @@ func (e *dataPlugin) newProcessCfg(query plugins.DataSubQuery, queryContext plug
break
}
}
if qm.Format == dataQueryFormatTable && col == "timeend" {
qm.timeEndIndex = i
continue
}
switch col {
case "metric":
qm.metricIndex = i
@@ -456,6 +462,7 @@ type dataQueryModel struct {
columnNames []string
columnTypes []*sql.ColumnType
timeIndex int
timeEndIndex int
metricIndex int
rows *core.Rows
metricPrefix bool
@@ -785,6 +792,22 @@ func convertNullableFloat32ToEpochMS(origin *data.Field, newField *data.Field) {
}
}
func convertSQLTimeColumnsToEpochMS(frame *data.Frame, qm *dataQueryModel) error {
if qm.timeIndex != -1 {
if err := convertSQLTimeColumnToEpochMS(frame, qm.timeIndex); err != nil {
return errutil.Wrap("failed to convert time column", err)
}
}
if qm.timeEndIndex != -1 {
if err := convertSQLTimeColumnToEpochMS(frame, qm.timeEndIndex); err != nil {
return errutil.Wrap("failed to convert timeend column", err)
}
}
return nil
}
// convertSQLTimeColumnToEpochMS converts column named time to unix timestamp in milliseconds
// to make native datetime types and epoch dates work in annotation and table queries.
func convertSQLTimeColumnToEpochMS(frame *data.Frame, timeIndex int) error {