2022-05-16 14:08:42 -05:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetQueriesFromDashboard(dashboard *simplejson.Json) map[int64][]*simplejson.Json {
|
|
|
|
result := make(map[int64][]*simplejson.Json)
|
|
|
|
|
|
|
|
for _, panelObj := range dashboard.Get("panels").MustArray() {
|
|
|
|
panel := simplejson.NewFromAny(panelObj)
|
|
|
|
|
|
|
|
var panelQueries []*simplejson.Json
|
|
|
|
|
|
|
|
for _, queryObj := range panel.Get("targets").MustArray() {
|
|
|
|
query := simplejson.NewFromAny(queryObj)
|
|
|
|
|
|
|
|
if _, ok := query.CheckGet("datasource"); !ok {
|
|
|
|
query.Set("datasource", panel.Get("datasource"))
|
|
|
|
}
|
|
|
|
|
|
|
|
panelQueries = append(panelQueries, query)
|
|
|
|
}
|
|
|
|
|
|
|
|
result[panel.Get("id").MustInt64()] = panelQueries
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2022-06-13 18:23:56 -05:00
|
|
|
|
|
|
|
func GroupQueriesByDataSource(queries []*simplejson.Json) (result [][]*simplejson.Json) {
|
|
|
|
byDataSource := make(map[string][]*simplejson.Json)
|
|
|
|
|
|
|
|
for _, query := range queries {
|
|
|
|
dataSourceUid, err := query.GetPath("datasource", "uid").String()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
byDataSource[dataSourceUid] = append(byDataSource[dataSourceUid], query)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, queries := range byDataSource {
|
|
|
|
result = append(result, queries)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|