datasource.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. export class GenericDatasource {
  2. constructor(instanceSettings, $q, backendSrv) {
  3. this.type = instanceSettings.type;
  4. this.url = instanceSettings.url;
  5. this.name = instanceSettings.name;
  6. this.q = $q;
  7. this.backendSrv = backendSrv;
  8. }
  9. // Called once per panel (graph)
  10. query(options) {
  11. var query = this.buildQueryParameters(options);
  12. if (query.targets.length <= 0) {
  13. return this.q.when([]);
  14. }
  15. return this.backendSrv.datasourceRequest({
  16. url: this.url + '/query',
  17. data: query,
  18. method: 'POST',
  19. headers: { 'Content-Type': 'application/json' }
  20. });
  21. }
  22. // Required
  23. // Used for testing datasource in datasource configuration pange
  24. testDatasource() {
  25. return this.backendSrv.datasourceRequest({
  26. url: this.url + '/',
  27. method: 'GET'
  28. }).then(response => {
  29. if (response.status === 200) {
  30. return { status: "success", message: "Data source is working", title: "Success" };
  31. }
  32. });
  33. }
  34. // Optional
  35. // Required for templating
  36. metricFindQuery(options) {
  37. return this.backendSrv.datasourceRequest({
  38. url: this.url + '/search',
  39. data: options,
  40. method: 'POST',
  41. headers: { 'Content-Type': 'application/json' }
  42. }).then(this.mapToTextValue);
  43. }
  44. mapToTextValue(result) {
  45. return _.map(result.data, (d, i) => {
  46. return { text: d, value: i};
  47. });
  48. }
  49. buildQueryParameters(options) {
  50. //remove placeholder targets
  51. options.targets = _.filter(options.targets, target => {
  52. return target.target !== 'select metric';
  53. });
  54. return options;
  55. }
  56. }