datasource.example.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'kbn',
  5. 'moment'
  6. ],
  7. function (angular, _, kbn) {
  8. 'use strict';
  9. var module = angular.module('grafana.services');
  10. module.factory('CustomDatasource', function($q) {
  11. // the datasource object passed to constructor
  12. // is the same defined in config.js
  13. function CustomDatasource(datasource) {
  14. this.name = datasource.name;
  15. this.supportMetrics = true;
  16. this.url = datasource.url;
  17. }
  18. CustomDatasource.prototype.query = function(filterSrv, options) {
  19. // get from & to in seconds
  20. var from = kbn.parseDate(options.range.from).getTime() / 1000;
  21. var to = kbn.parseDate(options.range.to).getTime() / 1000;
  22. var series = [];
  23. var stepInSeconds = (to - from) / options.maxDataPoints;
  24. for (var i = 0; i < 3; i++) {
  25. var walker = Math.random() * 100;
  26. var time = from;
  27. var timeSeries = {
  28. target: "Series " + i,
  29. datapoints: []
  30. };
  31. for (var j = 0; j < options.maxDataPoints; j++) {
  32. timeSeries.datapoints[j] = [walker, time];
  33. walker += Math.random() - 0.5;
  34. time += stepInSeconds;
  35. }
  36. series.push(timeSeries);
  37. }
  38. return $q.when({data: series });
  39. };
  40. return CustomDatasource;
  41. });
  42. });