SHARE
TWEET

Untitled

a guest Oct 21st, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* global _ */
  2.  
  3. /*
  4.  * Complex scripted dashboard
  5.  * This script generates a dashboard object that Grafana can load. It also takes a number of user
  6.  * supplied URL parameters (in the ARGS variable)
  7.  *
  8.  * Return a dashboard object, or a function
  9.  *
  10.  * For async scripts, return a function, this function must take a single callback function as argument,
  11.  * call this callback function with the dashboard object (look at scripted_async.js for an example)
  12.  */
  13.  
  14. 'use strict'
  15.  
  16. // accessible variables in this scope
  17. var window, document, ARGS, $, jQuery, moment, kbn
  18.  
  19. // Setup some variables
  20. var dashboard
  21.  
  22. // All url parameters are available via the ARGS object
  23. var ARGS
  24.  
  25. // Initialize a skeleton with nothing but a rows array and service object
  26. dashboard = {
  27.     rows: []
  28. }
  29.  
  30. // Initialize args data and elasticseach data
  31.  
  32. var rows = 1
  33. var cell_id = '41S01SMCON8701_S01SMCON87'
  34. var counters = []
  35. var theme = 'dark'
  36.  
  37. if (!_.isUndefined(ARGS.theme)) {
  38.     theme = ARGS.theme
  39.   }
  40.  
  41.  
  42. if (!_.isUndefined(ARGS.rows)) {
  43.     rows = parseInt(ARGS.rows, 10)
  44. }
  45.  
  46. if (!_.isUndefined(ARGS.cell_id)) {
  47.     cell_id = ARGS.cell_id
  48. }
  49.  
  50. if (!_.isUndefined(ARGS.counter)) {
  51.     if (Array.isArray(ARGS.counter)) {
  52.         counters = ARGS.counter
  53.     } else {
  54.         counters.push(ARGS.counter)
  55.     }
  56. }
  57.  
  58. // mode options
  59. var fill = 1
  60. var lineWidth = 1
  61. var staircase = false
  62. var pointRadius = 2
  63.  
  64. if (!_.isUndefined(ARGS.fill)) {
  65.     fill = parseInt(ARGS.fill)
  66. }
  67.  
  68. if (!_.isUndefined(ARGS.lineWidth)) {
  69.     lineWidth = parseInt(ARGS.lineWidth)
  70. }
  71.  
  72. if (!_.isUndefined(ARGS.staircase)) {
  73.     if (ARGS.staircase == 'false') {
  74.         staircase = false
  75.     } else if (ARGS.staircase == 'true') {
  76.         staircase = true
  77.     }
  78. }
  79.  
  80. if (!_.isUndefined(ARGS.pointRadius)) {
  81.     pointRadius = parseFloat(ARGS.pointRadius)
  82. }
  83.  
  84. // stacking and null value
  85. var stack = false
  86. var nullValue = 'null'
  87.  
  88. if (!_.isUndefined(ARGS.stack)) {
  89.     if (ARGS.stack == 'false') {
  90.         stack = false
  91.         console.log(ARGS.stack)
  92.     } else if (ARGS.stack == 'true') {
  93.         stack = true
  94.     }
  95. }
  96.  
  97. if (!_.isUndefined(ARGS.nullValue)) {
  98.     if ((ARGS.nullValue == 'null') ||
  99.         (ARGS.nullValue == 'null as zero') ||
  100.         (ARGS.nullValue == 'connected')) {
  101.             nullValue = ARGS.nullValue
  102.     }
  103. }
  104.  
  105. // draw modes
  106. var bars = false
  107. var lines = true
  108. var points = true
  109.  
  110. if (!_.isUndefined(ARGS.bars)) {
  111.     if (ARGS.bars == 'false') {
  112.         bars = false
  113.     } else if (ARGS.bars == 'true') {
  114.         bars = true
  115.     }
  116. }
  117.  
  118. if (!_.isUndefined(ARGS.lines)) {
  119.     if (ARGS.lines == 'false') {
  120.         lines = false
  121.     } else if (ARGS.lines == 'true') {
  122.         lines = true
  123.     }
  124. }
  125.  
  126. if (!_.isUndefined(ARGS.points)) {
  127.     if (ARGS.points == 'false') {
  128.         points = false
  129.     } else if (ARGS.points == 'true') {
  130.         points = true
  131.     }
  132. }
  133.  
  134. // yaxis
  135. var showYAxis = true
  136. var scale = 1;
  137. var yMin = null
  138. var yMax = null
  139.  
  140.  
  141. if (!_.isUndefined(ARGS.showYAxis)) {
  142.     if (ARGS.showYAxis == 'false') {
  143.         showYAxis = false
  144.     } else if (ARGS.showYAxis == 'true') {
  145.         showYAxis = true
  146.     }
  147. }
  148.  
  149. console.log("showYAxis", showYAxis);
  150.  
  151. if (!_.isUndefined(ARGS.scale)) {
  152.     if (ARGS.scale == 'linear') {
  153.         scale = 1
  154.     } else if (ARGS.scale == 'log (base 2)') {
  155.         scale = 2
  156.     } else if (ARGS.scale == 'log (base 10)') {
  157.         scale = 10
  158.     } else if (ARGS.scale == 'log (base 32)') {
  159.         scale = 32
  160.     } else if (ARGS.scale == 'log (base 1024)') {
  161.         scale = 1024
  162.     }
  163. }
  164.  
  165.  
  166. if (!_.isUndefined(ARGS.yMin)) {
  167.     if (ARGS.yMin == "null"){
  168.         yMin = null
  169.     } else {
  170.         yMin = parseFloat(ARGS.yMin)
  171.     }
  172. }
  173.  
  174. if (!_.isUndefined(ARGS.yMax)) {
  175.     if (ARGS.yMax == "null"){
  176.         yMax = null
  177.     } else {
  178.         yMax = parseFloat(ARGS.yMax)
  179.     }
  180. }
  181.  
  182. // legend
  183. var legend = {
  184.     alignAsTable: false,
  185.     avg: false,
  186.     rightSide: false,
  187.     current: false,
  188.     max: false,
  189.     min: false,
  190.     show: true,
  191.     total: false,
  192.     values: false
  193. }
  194.  
  195. if (!_.isUndefined(ARGS.asTable)) {
  196.     if (ARGS.asTable == 'false') {
  197.         legend.alignAsTable = false
  198.     } else if (ARGS.asTable == 'true') {
  199.         legend.alignAsTable = true
  200.     }
  201. }
  202.  
  203. if (!_.isUndefined(ARGS.toTheRight)) {
  204.     if (ARGS.toTheRight == 'false') {
  205.         legend.rightSide = false
  206.     } else if (ARGS.toTheRight == 'true') {
  207.         legend.rightSide = true
  208.     }
  209. }
  210.  
  211.  
  212. if (!_.isUndefined(ARGS.legendAvg)) {
  213.     if (ARGS.legendAvg == 'false') {
  214.         legend.avg = false
  215.         legend.values = true
  216.     } else if (ARGS.legendAvg == 'true') {
  217.         legend.avg = true
  218.         legend.values = true
  219.     }
  220. }
  221.  
  222. if (!_.isUndefined(ARGS.legendCurrent)) {
  223.     if (ARGS.legendCurrent == 'false') {
  224.         legend.current = false
  225.         legend.values = true
  226.     } else if (ARGS.legendCurrent == 'true') {
  227.         legend.current = true
  228.         legend.values = true
  229.     }
  230. }
  231.  
  232. if (!_.isUndefined(ARGS.legendMax)) {
  233.     if (ARGS.legendMax == 'false') {
  234.         legend.max = false
  235.         legend.values = true
  236.     } else if (ARGS.legendMax == 'true') {
  237.         legend.max = true
  238.         legend.values = true
  239.     }
  240. }
  241.  
  242. if (!_.isUndefined(ARGS.legendMin)) {
  243.     if (ARGS.legendMin == 'false') {
  244.         legend.min = false
  245.         legend.values = true
  246.     } else if (ARGS.legendMin == 'true') {
  247.         legend.min = true
  248.         legend.values = true
  249.     }
  250. }
  251.  
  252. if (!_.isUndefined(ARGS.showLegend)) {
  253.     if (ARGS.showLegend == 'false') {
  254.         legend.show = false
  255.         legend.values = true
  256.     } else if (ARGS.showLegend == 'true') {
  257.         legend.show = true
  258.         legend.values = true
  259.     }
  260. }
  261.  
  262. if (!_.isUndefined(ARGS.legendTotal)) {
  263.     if (ARGS.legendTotal == 'false') {
  264.         legend.total = false
  265.         legend.values = true
  266.     } else if (ARGS.legendTotal == 'true') {
  267.         legend.total = true
  268.         legend.values = true
  269.     }
  270. }
  271.  
  272. var decimals = 2;
  273.  
  274. if (!_.isUndefined(ARGS.decimals)) {
  275.     decimals = parseInt(ARGS.decimals)
  276. }
  277.  
  278. var interval = "15m"
  279.  
  280. if (!_.isUndefined(ARGS.interval)) {
  281.     interval = ARGS.interval
  282. }
  283.  
  284. console.log(interval);
  285.  
  286. // Set a title
  287. dashboard.title = 'Data Analysis'
  288. dashboard.timezone = 'utc'
  289.  
  290. dashboard.editable = true
  291. dashboard.gnetId = null
  292. dashboard.graphTooltip = 0
  293. dashboard.id = 1
  294. dashboard.iteration = 1554385724770
  295.  
  296. dashboard.refresh = '5s'
  297. dashboard.style = 'dark'
  298. dashboard.tags = []
  299. dashboard.timepicker = {
  300.     refresh_intervals: [
  301.         '5s',
  302.         '10s',
  303.         '30s',
  304.         '1m',
  305.         '5m',
  306.         '15m',
  307.         '30m',
  308.         '1h',
  309.         '2h',
  310.         '1d'
  311.     ],
  312.     time_options: ['5m', '15m', '1h', '6h', '12h', '24h', '2d', '7d', '30d']
  313. }
  314. dashboard.version = 1
  315.  
  316. dashboard.time = {
  317.     from: 'now/y',
  318.     to: 'now'
  319. }
  320.  
  321. var color_list = ['#7eb26d', '#cca300', '#bf1b00']
  322. var primary_counter_color = {}
  323. var primary_counter = {}
  324. var primary_counter_target = []
  325. console.log('COUNTERS: ', counters)
  326. for (var i = 0; i < counters.length; i++) {
  327.     console.log('THE COUNTER: ', counters[i])
  328.     primary_counter_color['' + counters[i]] = color_list[i]
  329.     console.log(primary_counter_color)
  330.  
  331.     primary_counter = {
  332.         bucketAggs: [
  333.             {
  334.                 field: 'time',
  335.                 id: '2',
  336.                 settings: {
  337.                     interval: interval,
  338.                     min_doc_count: 0,
  339.                     trimEdges: 0
  340.                 },
  341.                 type: 'date_histogram'
  342.             }
  343.         ],
  344.         metrics: [
  345.             {
  346.                 field: counters[i],
  347.                 id: '1',
  348.                 meta: {},
  349.                 settings: {},
  350.                 type: 'avg'
  351.             }
  352.         ],
  353.         query: 'cell_id:' + cell_id,
  354.         refId: 'A',
  355.         timeField: 'time'
  356.     }
  357.     primary_counter_target.push(primary_counter)
  358. }
  359.  
  360. var primary_panel = {
  361.     height: '340px',
  362.     aliasColors: primary_counter_color,
  363.     bars: bars,
  364.     dashLength: 10,
  365.     dashes: false,
  366.     datasource: 'Elasticsearch',
  367.     fill: fill,
  368.     span: 12,
  369.     gridPos: {
  370.         h: 12,
  371.         w: 24,
  372.         x: 0,
  373.         y: 4
  374.     },
  375.     id: 1,
  376.     legend: legend,
  377.     lines: lines,
  378.     linewidth: lineWidth,
  379.     links: [],
  380.     nullPointMode: nullValue,
  381.     percentage: false,
  382.     pointradius: pointRadius,
  383.     points: points,
  384.     renderer: 'flot',
  385.     seriesOverrides: [
  386.         {
  387.             alias: 'Average degradated',
  388.             yaxis: 1
  389.         }
  390.     ],
  391.     spaceLength: 10,
  392.     stack: stack,
  393.     staircase: staircase,
  394.     steppedLine: staircase,
  395.     targets: primary_counter_target,
  396.     timeFrom: null,
  397.     timeRegions: [],
  398.     timeShift: null,
  399.     title: 'Counters',
  400.     tooltip: {
  401.         shared: true,
  402.         sort: 0,
  403.         value_type: 'individual'
  404.     },
  405.     type: 'graph',
  406.     xaxis: {
  407.         buckets: null,
  408.         mode: 'time',
  409.         name: null,
  410.         show: true,
  411.         values: []
  412.     },
  413.     transparent: true,
  414.     yaxes: [
  415.         {
  416.             format: 'short',
  417.             label: null,
  418.             logBase: scale,
  419.             max: yMax,
  420.             min: yMin,
  421.             show: showYAxis,
  422.             decimals: decimals
  423.         },
  424.         {
  425.             format: 'short',
  426.             label: null,
  427.             logBase: scale,
  428.             max: yMax,
  429.             min: yMin,
  430.             show: showYAxis,
  431.             decimals: decimals
  432.         }
  433.     ],
  434.     yaxis: {
  435.         align: false,
  436.         alignLevel: null
  437.     }
  438. }
  439.  
  440. var panel_list = []
  441.  
  442. panel_list.push(primary_panel)
  443.  
  444. for (var i = 0; i < rows; i++) {
  445.     dashboard.rows.push({
  446.         panels: panel_list,
  447.         schemaVersion: 16
  448.     })
  449. }
  450.  
  451. var origin
  452.  
  453. function sendTime (event) {
  454.     var message
  455.     origin = event.origin
  456.     event.source.postMessage(
  457.         JSON.stringify({ from: dashboard.time.from, to: dashboard.time.to }),
  458.         event.origin
  459.     )
  460. }
  461.  
  462. if (window.addEventListener) {
  463.     // For standards-compliant web browsers
  464.     window.addEventListener('message', sendTime, false)
  465. } else {
  466.     window.attachEvent('onmessage', sendTime)
  467. }
  468.  
  469. if (theme == 'light') {
  470.     document.getElementsByClassName('scroll-canvas')[0].style.backgroundColor =
  471.       '#f2f2f2'
  472.     document.getElementsByClassName('scroll-canvas')[0].style.border =
  473.       '1px solid #dadada'
  474.   } else {
  475.     document.getElementsByClassName('scroll-canvas')[0].style.backgroundColor =
  476.       '#242424'
  477.     document.getElementsByClassName('scroll-canvas')[0].style.border =
  478.       '1px solid rgb(75, 75, 75)'
  479.   }
  480.  
  481. return dashboard
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top