settings.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. window_title_prefix : 'Grafana - ',
  17. panels : {
  18. 'graph': { path: 'panels/graph' },
  19. 'text': { path: 'panels/text' }
  20. },
  21. plugins : {},
  22. default_route : '/dashboard/file/default.json',
  23. playlist_timespan : "1m",
  24. unsaved_changes_warning : true,
  25. search : { max_results: 100 },
  26. admin : {}
  27. };
  28. // This initializes a new hash on purpose, to avoid adding parameters to
  29. // config.js without providing sane defaults
  30. var settings = {};
  31. _.each(defaults, function(value, key) {
  32. settings[key] = typeof options[key] !== 'undefined' ? options[key] : defaults[key];
  33. });
  34. var parseBasicAuth = function(datasource) {
  35. var passwordEnd = datasource.url.indexOf('@');
  36. if (passwordEnd > 0) {
  37. var userStart = datasource.url.indexOf('//') + 2;
  38. var userAndPassword = datasource.url.substring(userStart, passwordEnd);
  39. var bytes = crypto.charenc.Binary.stringToBytes(userAndPassword);
  40. datasource.basicAuth = crypto.util.bytesToBase64(bytes);
  41. var urlHead = datasource.url.substring(0, userStart);
  42. datasource.url = urlHead + datasource.url.substring(passwordEnd + 1);
  43. }
  44. return datasource;
  45. };
  46. var parseMultipleHosts = function(datasource) {
  47. datasource.urls = _.map(datasource.url.split(","), function (url) { return url.trim(); });
  48. return datasource;
  49. };
  50. // backward compatible with old config
  51. if (options.graphiteUrl) {
  52. settings.datasources.graphite = {
  53. type: 'graphite',
  54. url: options.graphiteUrl,
  55. default: true
  56. };
  57. }
  58. if (options.elasticsearch) {
  59. settings.datasources.elasticsearch = {
  60. type: 'elasticsearch',
  61. url: options.elasticsearch,
  62. index: options.grafana_index,
  63. grafanaDB: true
  64. };
  65. }
  66. _.each(settings.datasources, function(datasource, key) {
  67. datasource.name = key;
  68. if (datasource.url) { parseBasicAuth(datasource); }
  69. if (datasource.type === 'influxdb') { parseMultipleHosts(datasource); }
  70. });
  71. if (settings.plugins.panels) {
  72. _.extend(settings.panels, settings.plugins.panels);
  73. }
  74. if (!settings.plugins.dependencies) {
  75. settings.plugins.dependencies = [];
  76. }
  77. return settings;
  78. };
  79. });