Skip to content

Commit a4e0f2b

Browse files
committed
Re-add docker-compose provisioning files; update datasources tests
1 parent 2963168 commit a4e0f2b

File tree

5 files changed

+187
-37
lines changed

5 files changed

+187
-37
lines changed

tests/dashboards/demo.json

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"annotations": {
3+
"list": [
4+
{
5+
"builtIn": 1,
6+
"datasource": {
7+
"type": "grafana",
8+
"uid": "-- Grafana --"
9+
},
10+
"enable": true,
11+
"hide": true,
12+
"iconColor": "rgba(0, 211, 255, 1)",
13+
"name": "Annotations & Alerts",
14+
"type": "dashboard"
15+
}
16+
]
17+
},
18+
"editable": true,
19+
"fiscalYearStartMonth": 0,
20+
"graphTooltip": 0,
21+
"id": 1,
22+
"isStarred": true,
23+
"links": [],
24+
"panels": [
25+
{
26+
"datasource": {
27+
"default": true,
28+
"type": "prometheus",
29+
"uid": "robustperception"
30+
},
31+
"fieldConfig": {
32+
"defaults": {
33+
"color": {
34+
"mode": "palette-classic"
35+
},
36+
"custom": {
37+
"axisBorderShow": false,
38+
"axisCenteredZero": false,
39+
"axisColorMode": "text",
40+
"axisLabel": "",
41+
"axisPlacement": "auto",
42+
"barAlignment": 0,
43+
"barWidthFactor": 0.6,
44+
"drawStyle": "line",
45+
"fillOpacity": 0,
46+
"gradientMode": "none",
47+
"hideFrom": {
48+
"legend": false,
49+
"tooltip": false,
50+
"viz": false
51+
},
52+
"insertNulls": false,
53+
"lineInterpolation": "linear",
54+
"lineWidth": 1,
55+
"pointSize": 5,
56+
"scaleDistribution": {
57+
"type": "linear"
58+
},
59+
"showPoints": "auto",
60+
"spanNulls": false,
61+
"stacking": {
62+
"group": "A",
63+
"mode": "none"
64+
},
65+
"thresholdsStyle": {
66+
"mode": "off"
67+
}
68+
},
69+
"mappings": [],
70+
"thresholds": {
71+
"mode": "absolute",
72+
"steps": [
73+
{
74+
"color": "green",
75+
"value": null
76+
},
77+
{
78+
"color": "red",
79+
"value": 80
80+
}
81+
]
82+
}
83+
},
84+
"overrides": []
85+
},
86+
"gridPos": {
87+
"h": 8,
88+
"w": 12,
89+
"x": 0,
90+
"y": 0
91+
},
92+
"id": 1,
93+
"options": {
94+
"legend": {
95+
"calcs": [],
96+
"displayMode": "list",
97+
"placement": "bottom",
98+
"showLegend": true
99+
},
100+
"tooltip": {
101+
"mode": "single",
102+
"sort": "none"
103+
}
104+
},
105+
"targets": [
106+
{
107+
"datasource": {
108+
"type": "prometheus",
109+
"uid": "robustperception"
110+
},
111+
"editorMode": "code",
112+
"expr": "node_load1",
113+
"instant": false,
114+
"legendFormat": "__auto",
115+
"range": true,
116+
"refId": "A"
117+
}
118+
],
119+
"title": "Node Load",
120+
"type": "timeseries"
121+
}
122+
],
123+
"schemaVersion": 39,
124+
"tags": [
125+
"demo"
126+
],
127+
"templating": {
128+
"list": []
129+
},
130+
"time": {
131+
"from": "now-6h",
132+
"to": "now"
133+
},
134+
"timepicker": {},
135+
"timezone": "browser",
136+
"title": "Demo",
137+
"uid": "fe9gm6guyzi0wd",
138+
"version": 2,
139+
"weekStart": ""
140+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: 1
2+
3+
providers:
4+
- name: 'docker-compose'
5+
orgId: 1
6+
folder: ''
7+
folderUid: ''
8+
type: file
9+
disableDeletion: true
10+
updateIntervalSeconds: 60
11+
allowUiUpdates: false
12+
options:
13+
# <string, required> path to dashboard files on disk. Required when using the 'file' type
14+
path: /var/lib/grafana/dashboards
15+
# <bool> use folder names from filesystem to create folders in Grafana
16+
foldersFromFilesStructure: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: 1
2+
3+
datasources:
4+
- name: Prometheus
5+
id: 1
6+
uid: prometheus
7+
type: prometheus
8+
access: proxy
9+
url: http://prometheus:9090
10+
isDefault: true

tools/datasources.go

+4-14
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package tools
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

8-
"github.com/mark3labs/mcp-go/mcp"
97
"github.com/mark3labs/mcp-go/server"
108

119
"github.com/grafana/grafana-openapi-client-go/client/datasources"
@@ -33,17 +31,13 @@ type GetDatasourceByUIDParams struct {
3331
UID string `json:"uid" jsonschema:"required,description=The uid of the datasource"`
3432
}
3533

36-
func getDatasourceByUID(ctx context.Context, args GetDatasourceByUIDParams) (*mcp.CallToolResult, error) {
34+
func getDatasourceByUID(ctx context.Context, args GetDatasourceByUIDParams) (*datasources.GetDataSourceByUIDOK, error) {
3735
c := mcpgrafana.GrafanaClientFromContext(ctx)
3836
datasource, err := c.Datasources.GetDataSourceByUID(args.UID)
3937
if err != nil {
4038
return nil, fmt.Errorf("get datasource by uid %s: %w", args.UID, err)
4139
}
42-
b, err := json.Marshal(datasource.Payload)
43-
if err != nil {
44-
return nil, fmt.Errorf("marshal datasource: %w", err)
45-
}
46-
return mcp.NewToolResultText(string(b)), nil
40+
return datasource, nil
4741
}
4842

4943
var GetDatasourceByUID = mcpgrafana.MustTool(
@@ -56,17 +50,13 @@ type GetDatasourceByNameParams struct {
5650
Name string `json:"name" jsonschema:"required,description=The name of the datasource"`
5751
}
5852

59-
func getDatasourceByName(ctx context.Context, args GetDatasourceByNameParams) (*mcp.CallToolResult, error) {
53+
func getDatasourceByName(ctx context.Context, args GetDatasourceByNameParams) (*datasources.GetDataSourceByNameOK, error) {
6054
c := mcpgrafana.GrafanaClientFromContext(ctx)
6155
datasource, err := c.Datasources.GetDataSourceByName(args.Name)
6256
if err != nil {
6357
return nil, fmt.Errorf("get datasource by name %s: %w", args.Name, err)
6458
}
65-
b, err := json.Marshal(datasource.Payload)
66-
if err != nil {
67-
return nil, fmt.Errorf("marshal datasource: %w", err)
68-
}
69-
return mcp.NewToolResultText(string(b)), nil
59+
return datasource, nil
7060
}
7161

7262
var GetDatasourceByName = mcpgrafana.MustTool(

tools/datasources_test.go

+17-23
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,24 @@ func TestDatasourcesTools(t *testing.T) {
4141
ctx := newTestContext()
4242
result, err := listDatasources(ctx, ListDatasourcesParams{})
4343
require.NoError(t, err)
44-
assert.Len(t, result.Payload, 0)
45-
// tc := result.Content[0].(mcp.TextContent)
46-
// assert.Equal(t, tc.Text, "[]")
44+
assert.Len(t, result.Payload, 1)
4745
})
4846

49-
// t.Run("get datasource by uid", func(t *testing.T) {
50-
// ctx := context.Background()
51-
// result, err := getDatasourceByUID(ctx, GetDatasourceByUIDParams{
52-
// UID: "uid-1",
53-
// })
54-
// if err != nil {
55-
// t.Fatal(err)
56-
// }
57-
// t.Log(result)
58-
// })
47+
t.Run("get datasource by uid", func(t *testing.T) {
48+
ctx := newTestContext()
49+
result, err := getDatasourceByUID(ctx, GetDatasourceByUIDParams{
50+
UID: "prometheus",
51+
})
52+
require.NoError(t, err)
53+
assert.Equal(t, "Prometheus", result.Payload.Name)
54+
})
5955

60-
// t.Run("get datasource by name", func(t *testing.T) {
61-
// ctx := context.Background()
62-
// result, err := getDatasourceByName(ctx, GetDatasourceByNameParams{
63-
// Name: "name-1",
64-
// })
65-
// if err != nil {
66-
// t.Fatal(err)
67-
// }
68-
// t.Log(result)
69-
// })
56+
t.Run("get datasource by name", func(t *testing.T) {
57+
ctx := newTestContext()
58+
result, err := getDatasourceByName(ctx, GetDatasourceByNameParams{
59+
Name: "Prometheus",
60+
})
61+
require.NoError(t, err)
62+
assert.Equal(t, "Prometheus", result.Payload.Name)
63+
})
7064
}

0 commit comments

Comments
 (0)