Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* global _ */
- /*
- * Complex scripted dashboard
- * This script generates a dashboard object that Grafana can load. It also takes a number of user
- * supplied URL parameters (in the ARGS variable)
- *
- * Return a dashboard object, or a function
- *
- * For async scripts, return a function, this function must take a single callback function as argument,
- * call this callback function with the dashboard object (look at scripted_async.js for an example)
- */
- 'use strict'
- // accessible variables in this scope
- var window, document, ARGS, $, jQuery, moment, kbn
- // Setup some variables
- var dashboard
- // All url parameters are available via the ARGS object
- var ARGS
- // Initialize a skeleton with nothing but a rows array and service object
- dashboard = {
- rows: []
- }
- // Initialize args data and elasticseach data
- var rows = 1
- var cell_id = '41S01SMCON8701_S01SMCON87'
- var counters = []
- var theme = 'dark'
- if (!_.isUndefined(ARGS.theme)) {
- theme = ARGS.theme
- }
- if (!_.isUndefined(ARGS.rows)) {
- rows = parseInt(ARGS.rows, 10)
- }
- if (!_.isUndefined(ARGS.cell_id)) {
- cell_id = ARGS.cell_id
- }
- if (!_.isUndefined(ARGS.counter)) {
- if (Array.isArray(ARGS.counter)) {
- counters = ARGS.counter
- } else {
- counters.push(ARGS.counter)
- }
- }
- // mode options
- var fill = 1
- var lineWidth = 1
- var staircase = false
- var pointRadius = 2
- if (!_.isUndefined(ARGS.fill)) {
- fill = parseInt(ARGS.fill)
- }
- if (!_.isUndefined(ARGS.lineWidth)) {
- lineWidth = parseInt(ARGS.lineWidth)
- }
- if (!_.isUndefined(ARGS.staircase)) {
- if (ARGS.staircase == 'false') {
- staircase = false
- } else if (ARGS.staircase == 'true') {
- staircase = true
- }
- }
- if (!_.isUndefined(ARGS.pointRadius)) {
- pointRadius = parseFloat(ARGS.pointRadius)
- }
- // stacking and null value
- var stack = false
- var nullValue = 'null'
- if (!_.isUndefined(ARGS.stack)) {
- if (ARGS.stack == 'false') {
- stack = false
- console.log(ARGS.stack)
- } else if (ARGS.stack == 'true') {
- stack = true
- }
- }
- if (!_.isUndefined(ARGS.nullValue)) {
- if ((ARGS.nullValue == 'null') ||
- (ARGS.nullValue == 'null as zero') ||
- (ARGS.nullValue == 'connected')) {
- nullValue = ARGS.nullValue
- }
- }
- // draw modes
- var bars = false
- var lines = true
- var points = true
- if (!_.isUndefined(ARGS.bars)) {
- if (ARGS.bars == 'false') {
- bars = false
- } else if (ARGS.bars == 'true') {
- bars = true
- }
- }
- if (!_.isUndefined(ARGS.lines)) {
- if (ARGS.lines == 'false') {
- lines = false
- } else if (ARGS.lines == 'true') {
- lines = true
- }
- }
- if (!_.isUndefined(ARGS.points)) {
- if (ARGS.points == 'false') {
- points = false
- } else if (ARGS.points == 'true') {
- points = true
- }
- }
- // yaxis
- var showYAxis = true
- var scale = 1;
- var yMin = null
- var yMax = null
- if (!_.isUndefined(ARGS.showYAxis)) {
- if (ARGS.showYAxis == 'false') {
- showYAxis = false
- } else if (ARGS.showYAxis == 'true') {
- showYAxis = true
- }
- }
- console.log("showYAxis", showYAxis);
- if (!_.isUndefined(ARGS.scale)) {
- if (ARGS.scale == 'linear') {
- scale = 1
- } else if (ARGS.scale == 'log (base 2)') {
- scale = 2
- } else if (ARGS.scale == 'log (base 10)') {
- scale = 10
- } else if (ARGS.scale == 'log (base 32)') {
- scale = 32
- } else if (ARGS.scale == 'log (base 1024)') {
- scale = 1024
- }
- }
- if (!_.isUndefined(ARGS.yMin)) {
- if (ARGS.yMin == "null"){
- yMin = null
- } else {
- yMin = parseFloat(ARGS.yMin)
- }
- }
- if (!_.isUndefined(ARGS.yMax)) {
- if (ARGS.yMax == "null"){
- yMax = null
- } else {
- yMax = parseFloat(ARGS.yMax)
- }
- }
- // legend
- var legend = {
- alignAsTable: false,
- avg: false,
- rightSide: false,
- current: false,
- max: false,
- min: false,
- show: true,
- total: false,
- values: false
- }
- if (!_.isUndefined(ARGS.asTable)) {
- if (ARGS.asTable == 'false') {
- legend.alignAsTable = false
- } else if (ARGS.asTable == 'true') {
- legend.alignAsTable = true
- }
- }
- if (!_.isUndefined(ARGS.toTheRight)) {
- if (ARGS.toTheRight == 'false') {
- legend.rightSide = false
- } else if (ARGS.toTheRight == 'true') {
- legend.rightSide = true
- }
- }
- if (!_.isUndefined(ARGS.legendAvg)) {
- if (ARGS.legendAvg == 'false') {
- legend.avg = false
- legend.values = true
- } else if (ARGS.legendAvg == 'true') {
- legend.avg = true
- legend.values = true
- }
- }
- if (!_.isUndefined(ARGS.legendCurrent)) {
- if (ARGS.legendCurrent == 'false') {
- legend.current = false
- legend.values = true
- } else if (ARGS.legendCurrent == 'true') {
- legend.current = true
- legend.values = true
- }
- }
- if (!_.isUndefined(ARGS.legendMax)) {
- if (ARGS.legendMax == 'false') {
- legend.max = false
- legend.values = true
- } else if (ARGS.legendMax == 'true') {
- legend.max = true
- legend.values = true
- }
- }
- if (!_.isUndefined(ARGS.legendMin)) {
- if (ARGS.legendMin == 'false') {
- legend.min = false
- legend.values = true
- } else if (ARGS.legendMin == 'true') {
- legend.min = true
- legend.values = true
- }
- }
- if (!_.isUndefined(ARGS.showLegend)) {
- if (ARGS.showLegend == 'false') {
- legend.show = false
- legend.values = true
- } else if (ARGS.showLegend == 'true') {
- legend.show = true
- legend.values = true
- }
- }
- if (!_.isUndefined(ARGS.legendTotal)) {
- if (ARGS.legendTotal == 'false') {
- legend.total = false
- legend.values = true
- } else if (ARGS.legendTotal == 'true') {
- legend.total = true
- legend.values = true
- }
- }
- var decimals = 2;
- if (!_.isUndefined(ARGS.decimals)) {
- decimals = parseInt(ARGS.decimals)
- }
- var interval = "15m"
- if (!_.isUndefined(ARGS.interval)) {
- interval = ARGS.interval
- }
- console.log(interval);
- // Set a title
- dashboard.title = 'Data Analysis'
- dashboard.timezone = 'utc'
- dashboard.editable = true
- dashboard.gnetId = null
- dashboard.graphTooltip = 0
- dashboard.id = 1
- dashboard.iteration = 1554385724770
- dashboard.refresh = '5s'
- dashboard.style = 'dark'
- dashboard.tags = []
- dashboard.timepicker = {
- refresh_intervals: [
- '5s',
- '10s',
- '30s',
- '1m',
- '5m',
- '15m',
- '30m',
- '1h',
- '2h',
- '1d'
- ],
- time_options: ['5m', '15m', '1h', '6h', '12h', '24h', '2d', '7d', '30d']
- }
- dashboard.version = 1
- dashboard.time = {
- from: 'now/y',
- to: 'now'
- }
- var color_list = ['#7eb26d', '#cca300', '#bf1b00']
- var primary_counter_color = {}
- var primary_counter = {}
- var primary_counter_target = []
- console.log('COUNTERS: ', counters)
- for (var i = 0; i < counters.length; i++) {
- console.log('THE COUNTER: ', counters[i])
- primary_counter_color['' + counters[i]] = color_list[i]
- console.log(primary_counter_color)
- primary_counter = {
- bucketAggs: [
- {
- field: 'time',
- id: '2',
- settings: {
- interval: interval,
- min_doc_count: 0,
- trimEdges: 0
- },
- type: 'date_histogram'
- }
- ],
- metrics: [
- {
- field: counters[i],
- id: '1',
- meta: {},
- settings: {},
- type: 'avg'
- }
- ],
- query: 'cell_id:' + cell_id,
- refId: 'A',
- timeField: 'time'
- }
- primary_counter_target.push(primary_counter)
- }
- var primary_panel = {
- height: '340px',
- aliasColors: primary_counter_color,
- bars: bars,
- dashLength: 10,
- dashes: false,
- datasource: 'Elasticsearch',
- fill: fill,
- span: 12,
- gridPos: {
- h: 12,
- w: 24,
- x: 0,
- y: 4
- },
- id: 1,
- legend: legend,
- lines: lines,
- linewidth: lineWidth,
- links: [],
- nullPointMode: nullValue,
- percentage: false,
- pointradius: pointRadius,
- points: points,
- renderer: 'flot',
- seriesOverrides: [
- {
- alias: 'Average degradated',
- yaxis: 1
- }
- ],
- spaceLength: 10,
- stack: stack,
- staircase: staircase,
- steppedLine: staircase,
- targets: primary_counter_target,
- timeFrom: null,
- timeRegions: [],
- timeShift: null,
- title: 'Counters',
- tooltip: {
- shared: true,
- sort: 0,
- value_type: 'individual'
- },
- type: 'graph',
- xaxis: {
- buckets: null,
- mode: 'time',
- name: null,
- show: true,
- values: []
- },
- transparent: true,
- yaxes: [
- {
- format: 'short',
- label: null,
- logBase: scale,
- max: yMax,
- min: yMin,
- show: showYAxis,
- decimals: decimals
- },
- {
- format: 'short',
- label: null,
- logBase: scale,
- max: yMax,
- min: yMin,
- show: showYAxis,
- decimals: decimals
- }
- ],
- yaxis: {
- align: false,
- alignLevel: null
- }
- }
- var panel_list = []
- panel_list.push(primary_panel)
- for (var i = 0; i < rows; i++) {
- dashboard.rows.push({
- panels: panel_list,
- schemaVersion: 16
- })
- }
- var origin
- function sendTime (event) {
- var message
- origin = event.origin
- event.source.postMessage(
- JSON.stringify({ from: dashboard.time.from, to: dashboard.time.to }),
- event.origin
- )
- }
- if (window.addEventListener) {
- // For standards-compliant web browsers
- window.addEventListener('message', sendTime, false)
- } else {
- window.attachEvent('onmessage', sendTime)
- }
- if (theme == 'light') {
- document.getElementsByClassName('scroll-canvas')[0].style.backgroundColor =
- '#f2f2f2'
- document.getElementsByClassName('scroll-canvas')[0].style.border =
- '1px solid #dadada'
- } else {
- document.getElementsByClassName('scroll-canvas')[0].style.backgroundColor =
- '#242424'
- document.getElementsByClassName('scroll-canvas')[0].style.border =
- '1px solid rgb(75, 75, 75)'
- }
- return dashboard
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement