meln5674 / grafana-mongodb-community-plugin

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

Date filter not supported ? #7

Closed vcrc30 closed 1 year ago

vcrc30 commented 1 year ago

Hello and thank you for the work on this project. My env:

I'm having an issue with the following query:

[ { "$match": { "supplier.supplierName": "supplier1", "tradeState.createTime": { "$gte": { "$date": "2022-01-01T00:00:00Z" }, "$lte": { "$date": "2022-02-01T00:00:00Z" } } } }, { "$group": { "_id": "$tradeState.payState", "count": { "$sum": 1 } } } ]

It is working when I'm querying MongoDB Atlas from the API and it returns the following:

{ "documents": [ { "count": 644 }, { "count": 7373 }, { "count": 20 } ] }

From the plugin with the following settings I have the error :

error decoding key 0: invalid JSON input; expected {

image

Removing time filter part and the error is gone. I'm far from being used to work with MongoDB so sorry if I missed a point. Is there any compatibility issue with MongoDB Atlas ? Does the plugin support the nested date comparison filter ?

Thank you.

meln5674 commented 1 year ago

I haven't tried with Atlas yet, but that should be valid. I can reproduce it locally against an OSS mongo, and will get back to you when I figure out what's wrong.

meln5674 commented 1 year ago

Okay, found the issue. Turns out I was mistakenly restricting pipelines to their "canonical" representation, which only permits the millisecond date representation. I've pushed a new release candidate here, please let me know if this resolves your issue.

vcrc30 commented 1 year ago

Hello,

I confirm the new RC is working as expected ! Thank you very much !

vcrc30 commented 1 year ago

Hello,

I encounter kind of the same issue with the dashboard variables. For instance:

[{"$match": {"tradeState.createTime": {"$exists": "true", "$gte": {"$date": "${__from:date}"}, "$lte": {"$date": "${__to:date}"}}}}, {"$match": {"supplier.currency": {"$exists": "true", "$ne": "null"}}}, {"$group": {"_id": "$supplier.currency", "count": {"$sum": 1}}}, {"$project": {"count": 0}}]

Preview values is not refreshing until I remove the first $match. From the grafana logs:

logger=plugin.meln5674-mongodb-community t=2022-11-22T10:54:11.807396379Z level=info msg="QueryData called" request="map[Headers:<nil> PluginContext:map[AppInstanceSettings:<nil> DataSourceInstanceSettings:map[BasicAuthEnabled:false BasicAuthUser: Database: DecryptedSecureJSONData:map[password:xxx username:xxx] ID:2 JSONData:map[url:xxx] Name:mongodb-community Type:meln5674-mongodb-community UID:mkavvMDVk URL: Updated:2022-11-21T14:29:58Z User:] OrgID:1 PluginID:meln5674-mongodb-community User:map[Email:admin@localhost Login:admin Name: Role:Admin]] Queries:[map[Interval:1e+09 JSON:map[aggregation:[{\"$match\": {\"tradeState.createTime\": {\"$exists\": \"true\", \"$gte\": {\"$date\": \"${__from:date}\"}, \"$lte\": {\"$date\": \"${__to:date}\"}}}}, {\"$match\": {\"supplier.currency\": {\"$exists\": \"true\", \"$ne\": \"null\"}}}, {\"$group\": {\"_id\": \"$supplier.currency\", \"count\": {\"$sum\": 1}}}, {\"$project\": {\"count\": 0}}] autoTimeBound:false autoTimeSort:false collection:trade databasexxx datasource:map[type:meln5674-mongodb-community uid:mkavvMDVk] labelFields:[] queryType:Table refId:variable-query timestampField: timestampFormat: valueFieldTypes:[string] valueFields:[_id]] MaxDataPoints:100 QueryType:Table RefID:variable-query TimeRange:map[From:1970-01-01T00:00:00Z To:1970-01-01T00:00:00Z]]]]"
logger=plugin.meln5674-mongodb-community t=2022-11-22T10:54:11.80758938Z level=info msg="query called" context="map[AppInstanceSettings:<nil> DataSourceInstanceSettings:map[BasicAuthEnabled:false BasicAuthUser: Database: DecryptedSecureJSONData:map[password:xxx username:xxx] ID:2 JSONData:map[url:xxx] Name:mongodb-community Type:meln5674-mongodb-community UID:mkavvMDVk URL: Updated:2022-11-21T14:29:58Z User:] OrgID:1 PluginID:meln5674-mongodb-community User:map[Email:admin@localhost Login:admin Name: Role:Admin]]" query="map[Interval:1e+09 JSON:map[aggregation:[{\"$match\": {\"tradeState.createTime\": {\"$exists\": \"true\", \"$gte\": {\"$date\": \"${__from:date}\"}, \"$lte\": {\"$date\": \"${__to:date}\"}}}}, {\"$match\": {\"supplier.currency\": {\"$exists\": \"true\", \"$ne\": \"null\"}}}, {\"$group\": {\"_id\": \"$supplier.currency\", \"count\": {\"$sum\": 1}}}, {\"$project\": {\"count\": 0}}] autoTimeBound:false autoTimeSort:false collection:trade database:xxx datasource:map[type:meln5674-mongodb-community uid:mkavvMDVk] labelFields:[] queryType:Table refId:variable-query timestampField: timestampFormat: valueFieldTypes:[string] valueFields:[_id]] MaxDataPoints:100 QueryType:Table RefID:variable-query TimeRange:map[From:1970-01-01T00:00:00Z To:1970-01-01T00:00:00Z]]"
logger=context userId=1 orgId=1 uname=admin t=2022-11-22T10:54:11.807837982Z level=info msg="Request Completed" method=POST path=/api/ds/query status=400 remote_addr=192.168.65.3 time_ms=10 duration=10.651467ms size=192 referer="http://localhost:3000/d/r14BdeNVz/new-dashboard?editIndex=0&editview=templating&from=now-6h&orgId=1&to=now&var-cur=EUR" handler=/api/ds/query

Thank you.

meln5674 commented 1 year ago

Try this . Variables weren't being replaced in the variable queries, and if there are further errors, it should now report them in the corner.

meln5674 commented 1 year ago

I haven't heard back in a while, this appears to be fixed in my integrations tests, and this fix is included in the latest release. If you are still encountering this issue, please re-open or create a new issue.