Skip to content

Commit 25d4305

Browse files
committed
Support multiple return targets per query.
1 parent c9d2284 commit 25d4305

File tree

7 files changed

+626
-293
lines changed

7 files changed

+626
-293
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Allows MongoDB to be used as a data source for Grafana by providing a proxy to c
2525

2626
Create a new data source of type MongoDB as shown below. The MongoDB details are :
2727

28-
* **MongoDB URL** - `mongodb://rpiread:rpiread@rpi-sensorreadings-shard-00-00-fgscn.mongodb.net:27017,rpi-sensorreadings-shard-00-01-fgscn.mongodb.net:27017,rpi-sensorreadings-shard-00-02-fgscn.mongodb.net:27017/test?ssl=true&replicaSet=RPI-SensorReadings-shard-0&authSource=admin`
28+
* **MongoDB URL** - `mongodb://rpiread:rpiread@rpi-sensor-data-shard-00-00-ifxxs.mongodb.net:27017,rpi-sensor-data-shard-00-01-ifxxs.mongodb.net:27017,rpi-sensor-data-shard-00-02-ifxxs.mongodb.net:27017/test?ssl=true&replicaSet=rpi-sensor-data-shard-0&authSource=admin`
2929
* **MongoDB Database** - `rpi`
3030

3131
<img src="src/img/sample_datasource.png" alt="Sample Data Source" style="width: 500px;"/>
@@ -84,6 +84,21 @@ Note that ```_id``` field of the bucketAuto output contains the start and end of
8484

8585
The dashboard in `examples\RPI MongoDB Bucket - Atlas.json` shows this.
8686

87+
#### Example 3 - Using a Tabel Panel
88+
89+
Table panels are now supported with queries of the form
90+
91+
```javascript
92+
db.sensor_value.aggregate(
93+
[
94+
{ "$match" : { "ts" : { "$gte" : "$from", "$lt" : "$to" }}},
95+
{ "$group": { "_id": { "sensor_name" : "$sensor_name", "sensor_type" : "$sensor_type" }, "cnt" : { "$sum" : 1 }, "ts" : { "$max" : "$ts" } } },
96+
{ "$project": { "name" : { "$concat" : ["$_id.sensor_name",":","$_id.sensor_type" ]}, "value" : "$cnt", "ts" : 1, "_id" : 0} }
97+
])
98+
```
99+
100+
The dashboard in `examples\Sensor Values Count - Atlas.json` shows this.
101+
87102
## Running the proxy as a service on a Mac
88103

89104
* Install [forever-mac](https://www.npmjs.com/package/forever-mac)

dist/README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Allows MongoDB to be used as a data source for Grafana by providing a proxy to c
2525

2626
Create a new data source of type MongoDB as shown below. The MongoDB details are :
2727

28-
* **MongoDB URL** - `mongodb://rpiread:rpiread@rpi-sensorreadings-shard-00-00-fgscn.mongodb.net:27017,rpi-sensorreadings-shard-00-01-fgscn.mongodb.net:27017,rpi-sensorreadings-shard-00-02-fgscn.mongodb.net:27017/test?ssl=true&replicaSet=RPI-SensorReadings-shard-0&authSource=admin`
28+
* **MongoDB URL** - `mongodb://rpiread:rpiread@rpi-sensor-data-shard-00-00-ifxxs.mongodb.net:27017,rpi-sensor-data-shard-00-01-ifxxs.mongodb.net:27017,rpi-sensor-data-shard-00-02-ifxxs.mongodb.net:27017/test?ssl=true&replicaSet=rpi-sensor-data-shard-0&authSource=admin`
2929
* **MongoDB Database** - `rpi`
3030

3131
<img src="src/img/sample_datasource.png" alt="Sample Data Source" style="width: 500px;"/>
@@ -84,6 +84,21 @@ Note that ```_id``` field of the bucketAuto output contains the start and end of
8484

8585
The dashboard in `examples\RPI MongoDB Bucket - Atlas.json` shows this.
8686

87+
#### Example 3 - Using a Tabel Panel
88+
89+
Table panels are now supported with queries of the form
90+
91+
```javascript
92+
db.sensor_value.aggregate(
93+
[
94+
{ "$match" : { "ts" : { "$gte" : "$from", "$lt" : "$to" }}},
95+
{ "$group": { "_id": { "sensor_name" : "$sensor_name", "sensor_type" : "$sensor_type" }, "cnt" : { "$sum" : 1 }, "ts" : { "$max" : "$ts" } } },
96+
{ "$project": { "name" : { "$concat" : ["$_id.sensor_name",":","$_id.sensor_type" ]}, "value" : "$cnt", "ts" : 1, "_id" : 0} }
97+
])
98+
```
99+
100+
The dashboard in `examples\Sensor Values Count - Atlas.json` shows this.
101+
87102
## Running the proxy as a service on a Mac
88103

89104
* Install [forever-mac](https://www.npmjs.com/package/forever-mac)

dist/server/mongodb-proxy.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ function queryFinished(requestId, queryId, results, res, next)
9090
output = []
9191
for ( var i = 0; i < queryStatus.length; i++)
9292
{
93-
if ( queryStatus[i].results.datapoints.length > 0)
93+
var queryResults = queryStatus[i].results
94+
var keys = Object.keys(queryResults)
95+
for (var i = i; i < keys.length; i++)
9496
{
95-
output.push(queryStatus[i].results)
97+
var tg = keys[i]
98+
console.log(tg)
99+
output.push(queryResults[tg])
96100
}
97101
}
98102
res.json(output);
@@ -285,18 +289,28 @@ function runAggregateQuery( requestId, queryId, body, queryArgs, res, next )
285289
{
286290
try
287291
{
288-
datapoints = []
292+
var results = {}
289293
for ( var i = 0; i < docs.length; i++)
290294
{
291295
var doc = docs[i]
292-
tg = doc.name
293-
datapoints.push([doc['value'], doc['ts'].getTime()])
296+
var tg = doc.name
297+
var dp = null
298+
if (tg in results)
299+
{
300+
dp = results[tg]
301+
}
302+
else
303+
{
304+
dp = { 'target' : tg, 'datapoints' : [] }
305+
results[tg] = dp
306+
}
307+
308+
results[tg].datapoints.push([doc['value'], doc['ts'].getTime()])
294309
}
295310

296311
client.close();
297312
var elapsedTimeMs = stopwatch.stop()
298-
var results = { 'target' : tg, 'datapoints' : datapoints }
299-
logTiming(body, elapsedTimeMs, datapoints)
313+
logTiming(body, elapsedTimeMs)
300314
// Mark query as finished - will send back results when all queries finished
301315
queryFinished(requestId, queryId, results, res, next)
302316
}
@@ -376,14 +390,14 @@ function logQuery(query, type)
376390
}
377391
}
378392

379-
function logTiming(body, elapsedTimeMs, datapoints)
393+
function logTiming(body, elapsedTimeMs)
380394
{
381395
if (serverConfig.logTimings)
382396
{
383397
var range = new Date(body.range.to) - new Date(body.range.from)
384398
var diff = moment.duration(range)
385399

386-
console.log("Request: " + intervalCount(diff, body.interval, body.intervalMs) + " - Returned " + datapoints.length + " data points in " + elapsedTimeMs.toFixed(2) + "ms")
400+
console.log("Request: " + intervalCount(diff, body.interval, body.intervalMs) + " - Returned in " + elapsedTimeMs.toFixed(2) + "ms")
387401
}
388402
}
389403

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
{
2+
"__inputs": [
3+
{
4+
"name": "DS_RPI_- ATLAS",
5+
"label": "RPI - Atlas",
6+
"description": "",
7+
"type": "datasource",
8+
"pluginId": "grafana-mongodb-datasource",
9+
"pluginName": "MongoDB"
10+
}
11+
],
12+
"__requires": [
13+
{
14+
"type": "grafana",
15+
"id": "grafana",
16+
"name": "Grafana",
17+
"version": "5.1.3"
18+
},
19+
{
20+
"type": "datasource",
21+
"id": "grafana-mongodb-datasource",
22+
"name": "MongoDB",
23+
"version": "1.3.5"
24+
},
25+
{
26+
"type": "panel",
27+
"id": "table",
28+
"name": "Table",
29+
"version": "5.0.0"
30+
}
31+
],
32+
"annotations": {
33+
"list": [
34+
{
35+
"builtIn": 1,
36+
"datasource": "-- Grafana --",
37+
"enable": true,
38+
"hide": true,
39+
"iconColor": "rgba(0, 211, 255, 1)",
40+
"name": "Annotations & Alerts",
41+
"type": "dashboard"
42+
}
43+
]
44+
},
45+
"editable": true,
46+
"gnetId": null,
47+
"graphTooltip": 0,
48+
"id": null,
49+
"links": [],
50+
"panels": [
51+
{
52+
"columns": [],
53+
"datasource": "${DS_RPI_- ATLAS}",
54+
"fontSize": "100%",
55+
"gridPos": {
56+
"h": 13,
57+
"w": 18,
58+
"x": 0,
59+
"y": 0
60+
},
61+
"id": 2,
62+
"links": [],
63+
"pageSize": null,
64+
"scroll": true,
65+
"showHeader": true,
66+
"sort": {
67+
"col": 0,
68+
"desc": true
69+
},
70+
"styles": [
71+
{
72+
"alias": "",
73+
"colorMode": null,
74+
"colors": [
75+
"rgba(245, 54, 54, 0.9)",
76+
"rgba(237, 129, 40, 0.89)",
77+
"rgba(50, 172, 45, 0.97)"
78+
],
79+
"dateFormat": "YYYY-MM-DD HH:mm:ss",
80+
"decimals": 0,
81+
"mappingType": 1,
82+
"pattern": "Value",
83+
"thresholds": [],
84+
"type": "number",
85+
"unit": "none"
86+
},
87+
{
88+
"alias": "",
89+
"colorMode": null,
90+
"colors": [
91+
"rgba(245, 54, 54, 0.9)",
92+
"rgba(237, 129, 40, 0.89)",
93+
"rgba(50, 172, 45, 0.97)"
94+
],
95+
"dateFormat": "YYYY-MM-DD HH:mm:ss",
96+
"decimals": 2,
97+
"mappingType": 1,
98+
"pattern": "Time",
99+
"thresholds": [],
100+
"type": "date",
101+
"unit": "short"
102+
},
103+
{
104+
"alias": "",
105+
"colorMode": null,
106+
"colors": [
107+
"rgba(245, 54, 54, 0.9)",
108+
"rgba(237, 129, 40, 0.89)",
109+
"rgba(50, 172, 45, 0.97)"
110+
],
111+
"decimals": 2,
112+
"pattern": "/.*/",
113+
"thresholds": [],
114+
"type": "number",
115+
"unit": "short"
116+
}
117+
],
118+
"targets": [
119+
{
120+
"groupByAliases": [],
121+
"groupByColumns": [],
122+
"metricAggs": [
123+
{
124+
"column": "value",
125+
"type": "avg"
126+
}
127+
],
128+
"rawQuery": true,
129+
"refId": "A",
130+
"resultFormat": "time_series",
131+
"target": "db.sensor_value.aggregate(\n[\n { \"$match\" : { \"ts\" : { \"$gte\" : \"$from\", \"$lt\" : \"$to\" }}},\n { \"$group\": { \"_id\": { \"sensor_name\" : \"$sensor_name\", \"sensor_type\" : \"$sensor_type\" }, \"cnt\" : { \"$sum\" : 1 }, \"ts\" : { \"$max\" : \"$ts\" } } }, \n { \"$project\": { \"name\" : { \"$concat\" : [\"$_id.sensor_name\",\":\",\"$_id.sensor_type\" ]}, \"value\" : \"$cnt\", \"ts\" : 1, \"_id\" : 0} } \n])",
132+
"timeInterval": "auto_gf",
133+
"type": "timeserie",
134+
"whereClauses": []
135+
}
136+
],
137+
"title": "Counts",
138+
"transform": "timeseries_to_rows",
139+
"type": "table"
140+
}
141+
],
142+
"refresh": "5s",
143+
"schemaVersion": 16,
144+
"style": "dark",
145+
"tags": [],
146+
"templating": {
147+
"list": []
148+
},
149+
"time": {
150+
"from": "now/d",
151+
"to": "now"
152+
},
153+
"timepicker": {
154+
"refresh_intervals": [
155+
"5s",
156+
"10s",
157+
"30s",
158+
"1m",
159+
"5m",
160+
"15m",
161+
"30m",
162+
"1h",
163+
"2h",
164+
"1d"
165+
],
166+
"time_options": [
167+
"5m",
168+
"15m",
169+
"1h",
170+
"6h",
171+
"12h",
172+
"24h",
173+
"2d",
174+
"7d",
175+
"30d"
176+
]
177+
},
178+
"timezone": "",
179+
"title": "Sensor Value Counts",
180+
"uid": "9A9EQ3Omk",
181+
"version": 4
182+
}

0 commit comments

Comments
 (0)