mirror of
https://github.com/SigNoz/signoz.git
synced 2026-08-04 12:10:43 +01:00
Compare commits
2 Commits
main
...
fix/dashbo
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
908f30161f | ||
|
|
b50ccd225b |
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/SigNoz/signoz/pkg/errors"
|
||||
"github.com/SigNoz/signoz/pkg/types"
|
||||
"github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
|
||||
"github.com/SigNoz/signoz/pkg/types/telemetrytypes"
|
||||
"github.com/SigNoz/signoz/pkg/valuer"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
@@ -176,69 +177,75 @@ func NewGettableDashboardFromDashboard(dashboard *Dashboard) (*GettableDashboard
|
||||
}, nil
|
||||
}
|
||||
|
||||
const (
|
||||
statKeyDashboardCount = "dashboard.count"
|
||||
statKeyPanelCount = "dashboard.panels.count"
|
||||
statKeyPanelTracesCount = "dashboard.panels.traces.count"
|
||||
statKeyPanelMetricsCount = "dashboard.panels.metrics.count"
|
||||
statKeyPanelLogsCount = "dashboard.panels.logs.count"
|
||||
)
|
||||
|
||||
// panelSignalStatKeys maps a builder query's signal to the stat it contributes
|
||||
// to. Signal-less queries (promql, clickhouse sql, formulas) count towards the
|
||||
// panel total only.
|
||||
var panelSignalStatKeys = map[telemetrytypes.Signal]string{
|
||||
telemetrytypes.SignalTraces: statKeyPanelTracesCount,
|
||||
telemetrytypes.SignalMetrics: statKeyPanelMetricsCount,
|
||||
telemetrytypes.SignalLogs: statKeyPanelLogsCount,
|
||||
}
|
||||
|
||||
func NewStatsFromStorableDashboards(dashboards []*StorableDashboard) map[string]any {
|
||||
stats := make(map[string]any)
|
||||
stats["dashboard.panels.count"] = int64(0)
|
||||
stats["dashboard.panels.traces.count"] = int64(0)
|
||||
stats["dashboard.panels.metrics.count"] = int64(0)
|
||||
stats["dashboard.panels.logs.count"] = int64(0)
|
||||
stats := map[string]any{
|
||||
statKeyPanelCount: int64(0),
|
||||
statKeyPanelTracesCount: int64(0),
|
||||
statKeyPanelMetricsCount: int64(0),
|
||||
statKeyPanelLogsCount: int64(0),
|
||||
}
|
||||
for _, dashboard := range dashboards {
|
||||
addStatsFromStorableDashboard(dashboard, stats)
|
||||
}
|
||||
|
||||
stats["dashboard.count"] = int64(len(dashboards))
|
||||
stats[statKeyDashboardCount] = int64(len(dashboards))
|
||||
return stats
|
||||
}
|
||||
|
||||
// addStatsFromStorableDashboard counts the panels and per-signal queries of a v2
|
||||
// dashboard. Rows that do not decode as v2 contribute to dashboard.count only.
|
||||
func addStatsFromStorableDashboard(dashboard *StorableDashboard, stats map[string]any) {
|
||||
if dashboard.Data == nil {
|
||||
if dashboard == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if dashboard.Data["widgets"] == nil {
|
||||
dashboardV2, err := dashboard.ToDashboardV2(nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
widgets, ok := dashboard.Data["widgets"]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
for _, panel := range dashboardV2.Spec.Panels {
|
||||
if panel == nil {
|
||||
continue
|
||||
}
|
||||
incrementStat(stats, statKeyPanelCount)
|
||||
|
||||
data, ok := widgets.([]interface{})
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
for _, widget := range data {
|
||||
sData, ok := widget.(map[string]interface{})
|
||||
if ok && sData["query"] != nil {
|
||||
stats["dashboard.panels.count"] = stats["dashboard.panels.count"].(int64) + 1
|
||||
query, ok := sData["query"].(map[string]interface{})
|
||||
if ok && query["queryType"] == "builder" && query["builder"] != nil {
|
||||
builderData, ok := query["builder"].(map[string]interface{})
|
||||
if ok && builderData["queryData"] != nil {
|
||||
builderQueryData, ok := builderData["queryData"].([]interface{})
|
||||
if ok {
|
||||
for _, queryData := range builderQueryData {
|
||||
data, ok := queryData.(map[string]interface{})
|
||||
if ok {
|
||||
switch data["dataSource"] {
|
||||
case "traces":
|
||||
stats["dashboard.panels.traces.count"] = stats["dashboard.panels.traces.count"].(int64) + 1
|
||||
case "metrics":
|
||||
stats["dashboard.panels.metrics.count"] = stats["dashboard.panels.metrics.count"].(int64) + 1
|
||||
case "logs":
|
||||
stats["dashboard.panels.logs.count"] = stats["dashboard.panels.logs.count"].(int64) + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, query := range panel.Spec.Queries {
|
||||
composite, err := query.Spec.Plugin.buildV5CompositeQueryFromPlugin()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, envelope := range composite.Queries {
|
||||
if key, ok := panelSignalStatKeys[envelope.GetSignal()]; ok {
|
||||
incrementStat(stats, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func incrementStat(stats map[string]any, key string) {
|
||||
count, _ := stats[key].(int64)
|
||||
stats[key] = count + 1
|
||||
}
|
||||
|
||||
func (storableDashboardData *StorableDashboardData) GetWidgetIds() []string {
|
||||
data := *storableDashboardData
|
||||
widgetIds := []string{}
|
||||
|
||||
178
pkg/types/dashboardtypes/dashboard_stats_test.go
Normal file
178
pkg/types/dashboardtypes/dashboard_stats_test.go
Normal file
@@ -0,0 +1,178 @@
|
||||
package dashboardtypes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/SigNoz/signoz/pkg/types"
|
||||
"github.com/SigNoz/signoz/pkg/valuer"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// newStatsStorableV2 builds a stored v2 row from a panels JSON fragment, going
|
||||
// through the untyped data blob the way a row read off the DB does.
|
||||
func newStatsStorableV2(t *testing.T, panelsJSON string) *StorableDashboard {
|
||||
t.Helper()
|
||||
|
||||
raw := `{
|
||||
"metadata": {"schemaVersion": "` + SchemaVersion + `"},
|
||||
"spec": {
|
||||
"display": {"name": "Stats Dashboard"},
|
||||
"variables": [],
|
||||
"panels": {` + panelsJSON + `},
|
||||
"layouts": [],
|
||||
"links": []
|
||||
}
|
||||
}`
|
||||
|
||||
var data StorableDashboardData
|
||||
require.NoError(t, json.Unmarshal([]byte(raw), &data))
|
||||
|
||||
return &StorableDashboard{
|
||||
Identifiable: types.Identifiable{ID: valuer.GenerateUUID()},
|
||||
OrgID: valuer.GenerateUUID(),
|
||||
Source: SourceUser,
|
||||
Name: "stats-dashboard",
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func statsPanel(queriesJSON string) string {
|
||||
return `{
|
||||
"kind": "Panel",
|
||||
"spec": {
|
||||
"links": [],
|
||||
"plugin": {"kind": "signoz/TimeSeriesPanel", "spec": {}},
|
||||
"queries": [` + queriesJSON + `]
|
||||
}
|
||||
}`
|
||||
}
|
||||
|
||||
// A panel holds a single query, so its name never matters to the assertions.
|
||||
func statsBuilderQuery(signal string) string {
|
||||
return `{
|
||||
"kind": "time_series",
|
||||
"spec": {"plugin": {"kind": "signoz/BuilderQuery", "spec": ` + statsBuilderQuerySpec("A", signal) + `}}
|
||||
}`
|
||||
}
|
||||
|
||||
func statsBuilderQuerySpec(name, signal string) string {
|
||||
aggregations := `[{"expression": "count()"}]`
|
||||
if signal == "metrics" {
|
||||
aggregations = `[{"metricName": "m", "timeAggregation": "rate", "spaceAggregation": "sum"}]`
|
||||
}
|
||||
return `{"name": "` + name + `", "signal": "` + signal + `", "aggregations": ` + aggregations + `}`
|
||||
}
|
||||
|
||||
func TestNewStatsFromStorableDashboardsCountsV2Panels(t *testing.T) {
|
||||
dashboard := newStatsStorableV2(t, `
|
||||
"p1": `+statsPanel(statsBuilderQuery("logs"))+`,
|
||||
"p2": `+statsPanel(statsBuilderQuery("metrics"))+`,
|
||||
"p3": `+statsPanel(statsBuilderQuery("traces"))+`
|
||||
`)
|
||||
|
||||
stats := NewStatsFromStorableDashboards([]*StorableDashboard{dashboard})
|
||||
|
||||
assert.Equal(t, int64(1), stats[statKeyDashboardCount])
|
||||
assert.Equal(t, int64(3), stats[statKeyPanelCount])
|
||||
assert.Equal(t, int64(1), stats[statKeyPanelLogsCount])
|
||||
assert.Equal(t, int64(1), stats[statKeyPanelMetricsCount])
|
||||
assert.Equal(t, int64(1), stats[statKeyPanelTracesCount])
|
||||
}
|
||||
|
||||
// A panel carries exactly one query envelope, so multi-signal panels arrive as a
|
||||
// composite: the panel counts once and every builder sub-query counts its signal.
|
||||
func TestNewStatsFromStorableDashboardsCountsCompositeSubQueries(t *testing.T) {
|
||||
composite := `{
|
||||
"kind": "time_series",
|
||||
"spec": {"plugin": {"kind": "signoz/CompositeQuery", "spec": {"queries": [
|
||||
{"type": "builder_query", "spec": ` + statsBuilderQuerySpec("A", "traces") + `},
|
||||
{"type": "builder_query", "spec": ` + statsBuilderQuerySpec("B", "logs") + `}
|
||||
]}}}
|
||||
}`
|
||||
dashboard := newStatsStorableV2(t, `"p1": `+statsPanel(composite))
|
||||
|
||||
stats := NewStatsFromStorableDashboards([]*StorableDashboard{dashboard})
|
||||
|
||||
assert.Equal(t, int64(1), stats[statKeyPanelCount])
|
||||
assert.Equal(t, int64(1), stats[statKeyPanelTracesCount])
|
||||
assert.Equal(t, int64(1), stats[statKeyPanelLogsCount])
|
||||
}
|
||||
|
||||
// promql and clickhouse queries carry no signal, so they land in the panel total
|
||||
// and nowhere else.
|
||||
func TestNewStatsFromStorableDashboardsIgnoresSignallessQueries(t *testing.T) {
|
||||
promql := `{
|
||||
"kind": "time_series",
|
||||
"spec": {"plugin": {"kind": "signoz/PromQLQuery", "spec": {"name": "A", "query": "up"}}}
|
||||
}`
|
||||
dashboard := newStatsStorableV2(t, `"p1": `+statsPanel(promql))
|
||||
|
||||
stats := NewStatsFromStorableDashboards([]*StorableDashboard{dashboard})
|
||||
|
||||
assert.Equal(t, int64(1), stats[statKeyPanelCount])
|
||||
assert.Equal(t, int64(0), stats[statKeyPanelTracesCount])
|
||||
assert.Equal(t, int64(0), stats[statKeyPanelMetricsCount])
|
||||
assert.Equal(t, int64(0), stats[statKeyPanelLogsCount])
|
||||
}
|
||||
|
||||
func TestNewStatsFromStorableDashboardsAggregatesAcrossDashboards(t *testing.T) {
|
||||
first := newStatsStorableV2(t, `"p1": `+statsPanel(statsBuilderQuery("logs")))
|
||||
second := newStatsStorableV2(t, `
|
||||
"p1": `+statsPanel(statsBuilderQuery("logs"))+`,
|
||||
"p2": `+statsPanel(statsBuilderQuery("traces"))+`
|
||||
`)
|
||||
|
||||
stats := NewStatsFromStorableDashboards([]*StorableDashboard{first, second})
|
||||
|
||||
assert.Equal(t, int64(2), stats[statKeyDashboardCount])
|
||||
assert.Equal(t, int64(3), stats[statKeyPanelCount])
|
||||
assert.Equal(t, int64(2), stats[statKeyPanelLogsCount])
|
||||
assert.Equal(t, int64(1), stats[statKeyPanelTracesCount])
|
||||
}
|
||||
|
||||
// v1 rows are counted as dashboards but contribute no panel stats — the counters
|
||||
// read the v2 spec only.
|
||||
func TestNewStatsFromStorableDashboardsSkipsNonV2Rows(t *testing.T) {
|
||||
v1 := &StorableDashboard{
|
||||
Identifiable: types.Identifiable{ID: valuer.GenerateUUID()},
|
||||
OrgID: valuer.GenerateUUID(),
|
||||
Source: SourceUser,
|
||||
Name: "legacy-dashboard",
|
||||
Data: StorableDashboardData{
|
||||
"title": "Legacy Title",
|
||||
"version": "v5",
|
||||
"widgets": []any{
|
||||
map[string]any{"query": map[string]any{
|
||||
"queryType": "builder",
|
||||
"builder": map[string]any{
|
||||
"queryData": []any{map[string]any{"dataSource": "logs"}},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
}
|
||||
empty := &StorableDashboard{
|
||||
Identifiable: types.Identifiable{ID: valuer.GenerateUUID()},
|
||||
OrgID: valuer.GenerateUUID(),
|
||||
Source: SourceUser,
|
||||
Name: "bare",
|
||||
}
|
||||
|
||||
stats := NewStatsFromStorableDashboards([]*StorableDashboard{v1, empty})
|
||||
|
||||
assert.Equal(t, int64(2), stats[statKeyDashboardCount])
|
||||
assert.Equal(t, int64(0), stats[statKeyPanelCount])
|
||||
assert.Equal(t, int64(0), stats[statKeyPanelLogsCount])
|
||||
}
|
||||
|
||||
func TestNewStatsFromStorableDashboardsWithNoDashboards(t *testing.T) {
|
||||
stats := NewStatsFromStorableDashboards(nil)
|
||||
|
||||
assert.Equal(t, int64(0), stats[statKeyDashboardCount])
|
||||
assert.Equal(t, int64(0), stats[statKeyPanelCount])
|
||||
assert.Equal(t, int64(0), stats[statKeyPanelTracesCount])
|
||||
assert.Equal(t, int64(0), stats[statKeyPanelMetricsCount])
|
||||
assert.Equal(t, int64(0), stats[statKeyPanelLogsCount])
|
||||
}
|
||||
Reference in New Issue
Block a user