|
| 1 | +/* |
| 2 | + * Copyright 2024 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | +const utils = module.exports |
| 8 | + |
| 9 | +/** |
| 10 | + * @typedef {object} Page |
| 11 | + * @property {string} name |
| 12 | + * @property {description} description |
| 13 | + * @property {Widget[]} widgets |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * @typedef {object} Dashboard |
| 18 | + * @property {string} name |
| 19 | + * @property {Page[]} pages |
| 20 | + */ |
| 21 | + |
| 22 | +/** |
| 23 | + * @typedef {object} Widget |
| 24 | + * @property {string} title |
| 25 | + * @property {number} column |
| 26 | + * @property {number} row |
| 27 | + * @property {number} width |
| 28 | + * @property {number} height |
| 29 | + * @property {string} query |
| 30 | + */ |
| 31 | + |
| 32 | +/** |
| 33 | + * Makes object structure of a New Relic dashboard |
| 34 | + * |
| 35 | + * @param {object} params to function |
| 36 | + * @param {string} params.name name of dashboard |
| 37 | + * @param {Page[]} params.pages page contents |
| 38 | + * @returns {Dashboard} dashboard object |
| 39 | + */ |
| 40 | +utils.makeDashboard = function makeDashboard({ name, pages }) { |
| 41 | + return { |
| 42 | + name, |
| 43 | + description: null, |
| 44 | + permissions: 'PUBLIC_READ_WRITE', |
| 45 | + pages |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Makes a page in a New Relic dashboard |
| 51 | + * @param {object} params to function |
| 52 | + * @param {string} params.name name of page |
| 53 | + * @param {string} params.description description of page |
| 54 | + * @param {Widget[]} params.widgets widgets in page |
| 55 | + * @returns {Page} page object |
| 56 | + */ |
| 57 | +utils.makePage = function makePage({ name, description, widgets }) { |
| 58 | + return { |
| 59 | + name, |
| 60 | + description, |
| 61 | + widgets |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Makes a widget in a New Relic page |
| 67 | + * @param {object} params to function |
| 68 | + * @param {string} params.title of widget |
| 69 | + * @param {number} params.column column number |
| 70 | + * @param {number} params.row row number |
| 71 | + * @param {number} [params.width] width of widget |
| 72 | + * @param {number} [params.height] height of widget |
| 73 | + * @param {string} params.query nrql query |
| 74 | + * @returns {Widget} widget object |
| 75 | + */ |
| 76 | +utils.makeWidget = function makeWidget({ title, column, row, width = 4, height = 3, query }) { |
| 77 | + return { |
| 78 | + title, |
| 79 | + layout: { |
| 80 | + column, |
| 81 | + row, |
| 82 | + width, |
| 83 | + height |
| 84 | + }, |
| 85 | + linkedEntityGuids: null, |
| 86 | + visualization: { |
| 87 | + id: 'viz.bar' |
| 88 | + }, |
| 89 | + rawConfiguration: { |
| 90 | + facet: { |
| 91 | + showOtherSeries: false |
| 92 | + }, |
| 93 | + nrqlQueries: [ |
| 94 | + { |
| 95 | + accountIds: [1], |
| 96 | + query |
| 97 | + } |
| 98 | + ], |
| 99 | + platformOptions: { |
| 100 | + ignoreTimeRange: false |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Constructs NRQL for library usage |
| 108 | + * |
| 109 | + * @param {object} params to function |
| 110 | + * @param {string} params.lib name of library |
| 111 | + * @param {string} params.nodeVersion minimum node version |
| 112 | + * @returns {string} NRQL query |
| 113 | + */ |
| 114 | +utils.libraryUsageQuery = function libraryUsageQuery({ lib, nodeVersion }) { |
| 115 | + return `FROM NodeMetadataSummary SELECT uniqueCount(entity.guid) where \`${lib}.version\` IS NOT NULL and node.version.major >= '${nodeVersion}' facet \`${lib}.version\`, agentVersion, node.version.major limit max` |
| 116 | +} |
0 commit comments