Name

sn_app_insights.UIMetadataBuilder

Description

No description available

Script

var UIMetadataBuilder = Class.create();
UIMetadataBuilder.prototype = {
  initialize: function() {
  },

  getElementPayload: function() {
  	var payload = {};
  	var elDict = {};
  	var tabGR = this._orderGR("sn_app_insights_ui_tab");
  	payload["tabs"] = this._addTabs(tabGR);
  	payload["elements"] = this._addElements(payload, elDict, tabGR, "sn_app_insights_metric_graph");
  	return payload;
  },

  _orderGR: function(grName) {
  	var gr = new GlideRecord(grName);
  	gr.orderBy('order');
  	gr.addActiveQuery();
  	gr.query();
  	return gr;
  },

  _addTabs: function(tabGR) {
  	var tabArr = [];
  	while (tabGR.next()) {
  		var tabName = tabGR.getValue('name');
  		var tabLabel = tabGR.getValue('label');
  		tabArr.push({
  			id: tabName,
  			label: tabLabel
  		});
  	}
  	return tabArr;
  },

  _addElements: function(payload, elDict, tabGR, grName) {
  	var metricGR = this._orderGR(grName);
  	var metricToTabMap = {};
  	while (metricGR.next()) {
  		var metricDict = {};
  		var metricName = metricGR.metric.element.toString();
  		var tabName = this._getTabInfo(tabGR, metricGR);
  		metricToTabMap[metricName] = tabName; 
  		metricDict[metricName] = this._createGraph(metricGR);
  		if (!elDict[tabName]) {
  			elDict[tabName] = {};
  		}
  		elDict[tabName][metricName] = metricDict[metricName];
  	}
  	this._addCombined(elDict, tabGR);
  	this._addTables(elDict, tabGR);
  	payload['metricToTabMap'] = metricToTabMap;
  	return elDict;
  },

  _getTabInfo: function(tabGr, uiGR) {
  	var tabID = uiGR.getValue("tab");
  	tabGr.get(tabID);
  	return tabGr.getValue('name');
  },

  _createGraph: function(metricGR) {
  	return {
  		title: metricGR.getValue('title'),
  		popupContent: metricGR.getValue('hint'),
  		metricName: metricGR.metric.element.toString(),
  		metricsTable: metricGR.getValue('metrics_table'),
  		tableName: metricGR.metric.name.toString(),
  		yAxisLabel: metricGR.getValue('y_axis_label'),
  		showTable: Boolean(metricGR.show_table),
  		aggregate: metricGR.getValue('aggregate'),
  		plotType: metricGR.getValue('plot_type'),
  		elementType: 'graph'
  	};
  },

  _addCombined: function(elDict, tabGR){
  	var combinedGR = this._orderGR("sn_app_insights_combined_graph");
  	var id;
  	while (combinedGR.next()) {
  		var metricDict = {};
  		var graphs = [];
  		id = combinedGR.getValue('sys_id');
  		var ids = combinedGR.getValue('metric_graphs').split(",");

  		var metricGr = new GlideRecord('sn_app_insights_metric_graph');
  		metricGr.addQuery('sys_id', 'IN', ids);
  		metricGr.query();
  		while (metricGr.next()){
  			graphs.push(this._createCombined(metricGr));
  			metricDict[id] = {
  				label: combinedGR.getValue('title'),
  				popupContent: combinedGR.getValue('hint'),
  				graphs: graphs,
  				elementType: 'combined'
  			};
  		}

  		var tabName = this._getTabInfo(tabGR, combinedGR);
  		if (!elDict[tabName]) {
  			elDict[tabName] = {};
  		}
  		elDict[tabName][id] = metricDict[id];
  	}
  },

  _createCombined: function(metricGR) {
  	return {
  		metricName: metricGR.metric.element.toString(),
  		tableName: metricGR.metric.name.toString(),
  		aggregate: metricGR.getValue('aggregate'),
  	};
  },

  _addTables: function(elDict, tabGR){
  	var tableGR = this._orderGR("sn_app_insights_table");
  	while (tableGR.next()){
  		var tableDict = {};
  		var id = tableGR.getValue('sys_id');
  		tableDict[id] = this._createTable(tableGR, tabGR);
  		tableGR.getValue('tab');
  		var tabName = this._getTabInfo(tabGR, tableGR);
  		if (!elDict[tabName]) {
  			elDict[tabName] = {};
  		}
  		elDict[tabName][id] = tableDict[id];
  	}
  },

  _createTable: function(tableGR, tabGR) {
  	var tableDict = {
  		title: tableGR.getValue('title'),
  		tab: this._getTabInfo(tabGR, tableGR),
  		popupContent: tableGR.getValue('hint'),
  		popupContentDrillable: tableGR.getValue('drilldown_graph_hint'),
  		remoteTable: tableGR.getValue('table'),
  		query: tableGR.getValue('query'),
  		yAxisLabel: tableGR.getValue('y_axis_label'),
  		graphTitle: tableGR.getValue('drilldown_graph_title'),
  		table: tableGR.drilldown_metric.name.toString(),
  		metricName: tableGR.drilldown_metric.element.toString(),
  		plotType: tableGR.getValue('plot_type'),
  		elementType: 'table'
  	};
  	return tableDict;
  },

  type: 'UIMetadataBuilder'
};

Sys ID

dd0da11a532130107ea5ddeeff7b1294

Offical Documentation

Official Docs: