query_ctrl.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import appEvents from 'app/core/app_events';
  2. import { QueryCtrl } from 'app/plugins/sdk';
  3. function makeDefaultQuery(database) {
  4. return `from(db: "${database}")
  5. |> range($range)
  6. |> limit(n:1000)
  7. `;
  8. }
  9. export class InfluxIfqlQueryCtrl extends QueryCtrl {
  10. static templateUrl = 'partials/query.editor.html';
  11. dataPreview: string;
  12. resultRecordCount: string;
  13. resultTableCount: string;
  14. resultFormats: any[];
  15. /** @ngInject **/
  16. constructor($scope, $injector) {
  17. super($scope, $injector);
  18. this.resultRecordCount = '';
  19. this.resultTableCount = '';
  20. if (this.target.query === undefined) {
  21. this.target.query = makeDefaultQuery(this.datasource.database);
  22. }
  23. this.resultFormats = [{ text: 'Time series', value: 'time_series' }, { text: 'Table', value: 'table' }];
  24. appEvents.on('ds-request-response', this.onResponseReceived, $scope);
  25. this.panelCtrl.events.on('refresh', this.onRefresh, $scope);
  26. this.panelCtrl.events.on('data-received', this.onDataReceived, $scope);
  27. }
  28. onDataReceived = dataList => {
  29. this.resultRecordCount = dataList.reduce((count, model) => {
  30. const records = model.type === 'table' ? model.rows.length : model.datapoints.length;
  31. return count + records;
  32. }, 0);
  33. this.resultTableCount = dataList.length;
  34. };
  35. onResponseReceived = response => {
  36. this.dataPreview = response.data;
  37. };
  38. onRefresh = () => {
  39. this.dataPreview = '';
  40. this.resultRecordCount = '';
  41. this.resultTableCount = '';
  42. };
  43. getCollapsedText() {
  44. return this.target.query;
  45. }
  46. }