meln5674 / grafana-mongodb-community-plugin

Open Source Grafana Plugin for querying MongoDB
GNU Affero General Public License v3.0
137 stars 18 forks source link

Bad document: interface conversion: interface {} is nil #4

Closed seasoncool closed 2 years ago

seasoncool commented 2 years ago

Hi @meln5674 , please tell me what type should be input here ?

image

meln5674 commented 2 years ago

I would recommend placing your mouse over the (i) icons next to each of the fields, they document what each field does. If these are confusing or don't explain well enough, let me know and I'll try and come up with better ones.

The "value fields" is a set of fields that are expected in each of your documents, separated by commas. So if you want to plot/show the "foo", "bar", and "baz" fields in your documents, then the value fields would be set to "foo,bar,baz" (no quotes). The "value field types" are the types of those fields, again separated by commas, in the same order. Grafana requires all data in a column to be the same type, and the plugin is still quite primitive, so you have to tell it what the type of each column is in order for it to be able to parse it. You can see the list of types here: https://pkg.go.dev/github.com/grafana/grafana-plugin-sdk-go/data#FieldType, e.g. int8 for an 8-byte signed integer, uint8 for an 8-bit unsigned integer, float64 for a 64-bit float, and adding a in front makes a type "nullable" (optional), so int64 means "An 64-bit signed integer that may or may not be present in all of the documents". At some point, I'll be looking into a way to have the plugin figure this out for you, but I haven't worked out a good way yet.

seasoncool commented 2 years ago

Hi @meln5674 , I've tried as below values which still unable to get response, could you give me an example please? Thank you.

Database: my_db
Collection: device
QueryType: Table
Value Fields: nama
Value Field Types: string
Aggregation: [{"$match" : {"device.uuid":"13afb405-5d64-42b2-8e6c-6e49620b3a53"}},{ "$project" : {"_id" : 0,"device.name":1}}]`

image

meln5674 commented 2 years ago

Sure. Based on your query above, you're nearly there. If you want the "device.name" field to show up as "nama" in the grafana table, you'd just change it to

[{"$match" : {"device.uuid":"13afb405-5d64-42b2-8e6c-6e49620b3a53"}},{ "$project" : {"_id" : 0,"device.name":"nama"}}]`

The names in "Value Fields" need to be present in the documents returned by the query, so you can either populate Value Fields from what you know will be in the result documents, or you can project the result documents to whatever you've set Value Fields to. Unfortunately, nested fields are not currently supported, but you can get around that by doing a projection like above. If you also want the device UUID to show up, you'd do something like

Database: my_db
Collection: device
QueryType: Table
Value Fields: nama,uuid
Value Field Types: string,string
Aggregation: [{"$match" : {"device.uuid":"13afb405-5d64-42b2-8e6c-6e49620b3a53"}},{ "$project" : {"_id" : 0,"device.name":"nama","device.uuid":"uuid"}}]

You can also check out the examples used in the automated tests.

https://github.com/meln5674/grafana-mongodb-community-plugin/blob/v0.1.1%2Brc1/integration-test/datasets/weather.js and https://github.com/meln5674/grafana-mongodb-community-plugin/tree/v0.1.1%2Brc1/integration-test/queries/weather are directly based on the official docs at https://www.mongodb.com/docs/manual/core/timeseries/timeseries-procedures/.

https://github.com/meln5674/grafana-mongodb-community-plugin/blob/v0.1.1%2Brc1/integration-test/datasets/tweets.sh downloads a dataset from https://github.com/ozlerhakan/mongodb-json-files/blob/master/datasets/tweets.zip, and it has its corresponding example queries at https://github.com/meln5674/grafana-mongodb-community-plugin/tree/v0.1.1%2Brc1/integration-test/queries/tweets

seasoncool commented 2 years ago

Hi @meln5674 , I noticed that i typed the wrong word : "nama" should be "name", sorry .

I'v tested below cases and still no one get luck, please help me to finish anyone of below case, thanks in advance.

case 0 : single field

image

case 1 : two fields to show name & uuid only

image

case 2 : two fields with "name" and "uuid"

image

case 3 : two fields with parameters for "name" and "uuid"

image

case 4 : import "weather" database

image

I guess that problem might be data type however i tested each types according to grafana officially :(

meln5674 commented 2 years ago

My apologies, I seem to have given you bad information the first time. The final pipeline stage should look like this. { "$project": { "name": "$device.name", "uuid": "$device.uuid" } } I had the order of new/old field names backwards.

Also, for case 4, that file itself isn't the aggregation, but each field in the top-level object correseponds to one of the fields in the query interface, e.g. "valueFields" in the query json file corresponds to the "Value Fields" section in the UI.

seasoncool commented 2 years ago

Hi @meln5674 , I'v got the result according to your last comment, thanks so much ...

image