settings.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. define([
  2. 'lodash',
  3. 'crypto',
  4. ],
  5. function (_, crypto) {
  6. "use strict";
  7. return function Settings (options) {
  8. /**
  9. * To add a setting, you MUST define a default. Also,
  10. * THESE ARE ONLY DEFAULTS.
  11. * They are overridden by config.js in the root directory
  12. * @type {Object}
  13. */
  14. var defaults = {
  15. datasources : {},
  16. panels : ['graph', 'text'],
  17. plugins : {},
  18. default_route : '/dashboard/file/default.json',
  19. playlist_timespan : "1m",
  20. unsaved_changes_warning : true,
  21. search : { max_results: 16 },
  22. admin : {}
  23. };
  24. // This initializes a new hash on purpose, to avoid adding parameters to
  25. // config.js without providing sane defaults
  26. var settings = {};
  27. _.each(defaults, function(value, key) {
  28. settings[key] = typeof options[key] !== 'undefined' ? options[key] : defaults[key];
  29. });
  30. var parseBasicAuth = function(datasource) {
  31. var passwordEnd = datasource.url.indexOf('@');
  32. if (passwordEnd > 0) {
  33. var userStart = datasource.url.indexOf('//') + 2;
  34. var userAndPassword = datasource.url.substring(userStart, passwordEnd);
  35. var bytes = crypto.charenc.Binary.stringToBytes(userAndPassword);
  36. datasource.basicAuth = crypto.util.bytesToBase64(bytes);
  37. var urlHead = datasource.url.substring(0, userStart);
  38. datasource.url = urlHead + datasource.url.substring(passwordEnd + 1);
  39. }
  40. return datasource;
  41. };
  42. var parseMultipleHosts = function(datasource) {
  43. datasource.urls = _.map(datasource.url.split(","), function (url) { return url.trim(); });
  44. return datasource;
  45. };
  46. // backward compatible with old config
  47. if (options.graphiteUrl) {
  48. settings.datasources.graphite = {
  49. type: 'graphite',
  50. url: options.graphiteUrl,
  51. default: true
  52. };
  53. }
  54. if (options.elasticsearch) {
  55. settings.datasources.elasticsearch = {
  56. type: 'elasticsearch',
  57. url: options.elasticsearch,
  58. index: options.grafana_index,
  59. grafanaDB: true
  60. };
  61. }
  62. _.each(settings.datasources, function(datasource, key) {
  63. datasource.name = key;
  64. if (datasource.url) { parseBasicAuth(datasource); }
  65. if (datasource.type === 'influxdb') { parseMultipleHosts(datasource); }
  66. });
  67. if (settings.plugins.panels) {
  68. settings.panels = _.union(settings.panels, settings.plugins.panels);
  69. }
  70. if (!settings.plugins.dependencies) {
  71. settings.plugins.dependencies = [];
  72. }
  73. return settings;
  74. };
  75. });