datasource.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Types
  2. import {
  3. DataQueryOptions,
  4. SeriesData,
  5. DataQueryResponse,
  6. DataSourceApi,
  7. DataSourceInstanceSettings,
  8. } from '@grafana/ui/src/types';
  9. import { InputQuery } from './types';
  10. export class InputDatasource implements DataSourceApi<InputQuery> {
  11. data: SeriesData[];
  12. // Filled in by grafana plugin system
  13. name?: string;
  14. // Filled in by grafana plugin system
  15. id?: number;
  16. /** @ngInject */
  17. constructor(instanceSettings: DataSourceInstanceSettings) {
  18. if (instanceSettings.jsonData) {
  19. this.data = instanceSettings.jsonData.data;
  20. }
  21. if (!this.data) {
  22. this.data = [];
  23. }
  24. }
  25. getDescription(data: SeriesData[]): string {
  26. if (!data) {
  27. return '';
  28. }
  29. if (data.length > 1) {
  30. const count = data.reduce((acc, series) => {
  31. return acc + series.rows.length;
  32. }, 0);
  33. return `${data.length} Series, ${count} Rows`;
  34. }
  35. const series = data[0];
  36. return `${series.fields.length} Fields, ${series.rows.length} Rows`;
  37. }
  38. /**
  39. * Convert a query to a simple text string
  40. */
  41. getQueryDisplayText(query: InputQuery): string {
  42. if (query.data) {
  43. return 'Panel Data: ' + this.getDescription(query.data);
  44. }
  45. return `Shared Data From: ${this.name} (${this.getDescription(this.data)})`;
  46. }
  47. metricFindQuery(query: string, options?: any) {
  48. return new Promise((resolve, reject) => {
  49. const names = [];
  50. for (const series of this.data) {
  51. for (const field of series.fields) {
  52. // TODO, match query/options?
  53. names.push({
  54. text: field.name,
  55. });
  56. }
  57. }
  58. resolve(names);
  59. });
  60. }
  61. query(options: DataQueryOptions<InputQuery>): Promise<DataQueryResponse> {
  62. const results: SeriesData[] = [];
  63. for (const query of options.targets) {
  64. if (query.hide) {
  65. continue;
  66. }
  67. const data = query.data ? query.data : this.data;
  68. for (const series of data) {
  69. results.push({
  70. refId: query.refId,
  71. ...series,
  72. });
  73. }
  74. }
  75. return Promise.resolve({ data: results });
  76. }
  77. testDatasource() {
  78. return new Promise((resolve, reject) => {
  79. let rowCount = 0;
  80. let info = `${this.data.length} Series:`;
  81. for (const series of this.data) {
  82. info += ` [${series.fields.length} Fields, ${series.rows.length} Rows]`;
  83. rowCount += series.rows.length;
  84. }
  85. if (rowCount > 0) {
  86. resolve({
  87. status: 'success',
  88. message: info,
  89. });
  90. }
  91. reject({
  92. status: 'error',
  93. message: 'No Data Entered',
  94. });
  95. });
  96. }
  97. }
  98. export default InputDatasource;