fr-ser / grafana-sqlite-datasource

Grafana Plugin to enable SQLite as a Datasource
Apache License 2.0
124 stars 17 forks source link

Variable query with SQLite datasource: error updating options: e.data[0] is undefined #54

Closed nicomeunier closed 2 years ago

nicomeunier commented 3 years ago

Describe the bug When creating a new variable as query with SQLite backend, I get an error.

To Reproduce Steps to reproduce the behavior:

  1. Go to variables
  2. Click on new variable
  3. Select query variable on SQLite datasource
  4. Write a simple query like "select id from device" (that should return 4 rows)
  5. Click outside of the query field in order to make the query request, an error appear (like in the screenshot)

Screenshots If applicable, add screenshots to help explain your problem. image

Versions (please complete the following information):

Additional context Add any other context about the problem here. Please also attach the logs of Grafana!

Here is the query of select id from device: POST http://localhost:3000/api/ds/query with body:

{
    "queries": [
        {
            "datasourceId": 2,
            "queryText": "select id from device",
            "rawQueryText": "select id from device",
            "timeColumns": []
        }
    ]
}

and result

{
    "results": {
        "A": {
            "frames": [
                {
                    "schema": {
                        "name": "response",
                        "refId": "A",
                        "meta": {
                            "executedQueryString": "select id from device"
                        },
                        "fields": [
                            {
                                "name": "id",
                                "type": "string",
                                "typeInfo": {
                                    "frame": "string",
                                    "nullable": true
                                }
                            }
                        ]
                    },
                    "data": {
                        "values": [
                            [
                                "DESKTOP-87EKQJF_fsp-fnf-solution_dummy",
                                "DESKTOP-87EKQJF_fsp-ftv_dummy",
                                "FFS000-00000-CS000-0000",
                                "FFS220-0129-CS220-1124-EX2"
                            ]
                        ]
                    }
                }
            ]
        }
    }
}

Here is the error stack: from DataSource.ts (see the red line):

 import { DataSourceInstanceSettings, DataFrame, ScopedVars } from '@grafana/data';
 import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime';
 import { MyDataSourceOptions, SQLiteQuery } from './types';

 export class DataSource extends DataSourceWithBackend<SQLiteQuery, MyDataSourceOptions> {
   templateSrv;

   constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
     super(instanceSettings);

     this.templateSrv = getTemplateSrv();
   }

   applyTemplateVariables(query: SQLiteQuery, scopedVars: ScopedVars): SQLiteQuery {
     query.queryText = this.templateSrv.replace(query.rawQueryText, scopedVars);
     return query;
   }

   async metricFindQuery(query: string, options?: any) {
     if (!query) {
       return [];
     }
     const response = await this.query({
       targets: [
         {
           rawQueryText: query,
           queryText: query,
           timeColumns: [],
         },
       ],
     } as any).toPromise();

     if (response.error) {
       throw new Error(response.error.message);
     }

-    const data = response.data[0] as DataFrame;

     if (data.fields.length !== 1) {
       throw new Error(
         `Received more than one (${data.fields.length}) fields: ${data.fields.map(x => x.name).join(',')}`
       );
     }

     return data.fields[0].values.toArray().map(text => ({ text }));
   }
 }
fr-ser commented 2 years ago

There has been a bug with Grafana v8, which was fixed in v2.0.2 of the plugin (see link). Can you install a newer plugin version? Here is how to do it: link

See also: https://github.com/fr-ser/grafana-sqlite-datasource/issues/46