From 0eeee2c6f9bfd0d59975ad0f4c8d0b7fb3f41a30 Mon Sep 17 00:00:00 2001 From: ZuperZee Date: Fri, 6 Nov 2020 11:35:18 +0100 Subject: [PATCH] feat: add getMetricValueByName function Retrieves last metric value based off of the name provided --- .vscode/settings.json | 5 +- CHANGELOG.md | 6 +- README.md | 23 +-- jest.config.js | 2 +- package-lock.json | 267 ++++++++++++++++++++++++++++++++- package.json | 16 +- src/__mocks__/create-series.ts | 76 ++++++++++ src/index.test.ts | 46 ++++++ src/index.ts | 42 ++++-- src/something.ts | 10 -- test/index.test.ts | 14 -- tsconfig.json | 3 +- 12 files changed, 434 insertions(+), 76 deletions(-) create mode 100644 src/__mocks__/create-series.ts create mode 100644 src/index.test.ts delete mode 100644 src/something.ts delete mode 100644 test/index.test.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 9c70b97..ca8ff1a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,8 @@ "editor.formatOnSave": true, "files.exclude": { "**/node_modules/*": true - } + }, + "cSpell.words": [ + "grafana" + ] } diff --git a/CHANGELOG.md b/CHANGELOG.md index 26f96c8..1938fe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,7 @@ -# Changelog for component-template +# Changelog for grafana-metric ## v0.0.0 (YYYY/MM/DD) -### Breaking changes - ### Features / enhancements -### Bug fixes +- Add getMetricValueByName which retrieves last metric value based off of the name provided. diff --git a/README.md b/README.md index da4d30b..05ea5cb 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,15 @@ -# Component name +# HTMLGraphics metric -A short description of the component - -E.g. This component adds two numbers together. -It also has a variable and function that returns "something". +Retrieves metric value from Grafana's [data interface](https://grafana.com/docs/grafana/latest/packages_api/data/paneldata/). ## Usage -Description/example on how to use the component. - -### add - -```ts -import { add } from "@gapit/component-template"; - -add(2, 3); // Returns 5 -``` +### getMetricValueByName -### SOMETHING_VARIABLE +Retrieves last metric value based off of the name provided. ```ts -import { SOMETHING_VARIABLE } from "@gapit/component-template"; +import { getMetricValueByName } from "@gapit/grafana-metric"; -console.log(SOMETHING_VARIABLE); // Returns "something +getMetricValueByName("metric-name"); // Returns metric value ``` diff --git a/jest.config.js b/jest.config.js index c45ee67..7c9fd66 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,5 @@ module.exports = { - roots: ["/src", "/test"], + roots: ["/src"], testMatch: ["**/__tests__/**/*.+(ts|js)", "**/?(*.)+(spec|test).+(ts|js)"], transform: { "^.+\\.(ts)$": "ts-jest", diff --git a/package-lock.json b/package-lock.json index 8c982e7..8f42eb1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -407,6 +407,11 @@ "integrity": "sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==", "dev": true }, + "@braintree/sanitize-url": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/@braintree/sanitize-url/-/sanitize-url-4.0.0.tgz", + "integrity": "sha512-bOoFoTxuEUuri/v1q0OXN0HIrZ2EiZlRSKdveU8vS5xf2+g0TmpXhmxkTc1s+XWR5xZNoVU4uvf/Mher98tfLw==" + }, "@cnakazawa/watch": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/@cnakazawa/watch/-/watch-1.0.4.tgz", @@ -443,6 +448,25 @@ } } }, + "@grafana/data": { + "version": "7.2.2", + "resolved": "https://registry.npmjs.org/@grafana/data/-/data-7.2.2.tgz", + "integrity": "sha512-uJBZOLF3foeVeZIpjgA7w8qhQFR5EQwi/JMECO/uv4lSES7PXPk5I76oug5YxZYN1V+kD/ExSEL31OVQDiT8eg==", + "requires": { + "@braintree/sanitize-url": "4.0.0", + "apache-arrow": "0.16.0", + "lodash": "4.17.19", + "rxjs": "6.6.2", + "xss": "1.0.6" + }, + "dependencies": { + "lodash": { + "version": "4.17.19", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz", + "integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==" + } + } + }, "@istanbuljs/load-nyc-config": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz", @@ -1081,6 +1105,11 @@ "integrity": "sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==", "dev": true }, + "@types/flatbuffers": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/@types/flatbuffers/-/flatbuffers-1.10.0.tgz", + "integrity": "sha512-7btbphLrKvo5yl/5CC2OCxUSMx1wV1wvGT1qDXkSt7yi00/YW7E8k6qzXqJHsp+WU0eoG7r6MTQQXI9lIvd0qA==" + }, "@types/graceful-fs": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.3.tgz", @@ -1170,6 +1199,11 @@ "integrity": "sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw==", "dev": true }, + "@types/text-encoding-utf-8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@types/text-encoding-utf-8/-/text-encoding-utf-8-1.0.1.tgz", + "integrity": "sha512-GpIEYaS+yNfYqpowLLziiY42pyaL+lThd/wMh6tTubaKuG4IRkXqqyxK7Nddn3BvpUg2+go3Gv/jbXvAFMRjiQ==" + }, "@types/yargs": { "version": "15.0.8", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.8.tgz", @@ -1362,6 +1396,30 @@ "picomatch": "^2.0.4" } }, + "apache-arrow": { + "version": "0.16.0", + "resolved": "https://registry.npmjs.org/apache-arrow/-/apache-arrow-0.16.0.tgz", + "integrity": "sha512-hiabMZb2XgHiNK6f7C/2x/fyGS85PoCdkeMhJDyZipaJy1il1BJMDDEa3VLvM6mdxsG61pY1zev6zWetOj8Eog==", + "requires": { + "@types/flatbuffers": "^1.9.1", + "@types/node": "^12.0.4", + "@types/text-encoding-utf-8": "^1.0.1", + "command-line-args": "5.0.2", + "command-line-usage": "5.0.5", + "flatbuffers": "1.11.0", + "json-bignum": "^0.0.3", + "pad-left": "^2.1.0", + "text-encoding-utf-8": "^1.0.2", + "tslib": "^1.9.3" + }, + "dependencies": { + "@types/node": { + "version": "12.19.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.19.2.tgz", + "integrity": "sha512-SRH6QM0IMOBBFmDiJ75vlhcbUEYEquvSuhsVW9ijG20JvdFTfOrB1p6ddZxz5y/JNnbf+9HoHhjhOVSX2hsJyA==" + } + } + }, "argparse": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", @@ -1371,6 +1429,15 @@ "sprintf-js": "~1.0.2" } }, + "argv-tools": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/argv-tools/-/argv-tools-0.1.2.tgz", + "integrity": "sha512-wxqoymY0BEu9NblZVQiOTOAiJUjPhaa/kbNMjC2h6bnrmUSgnxKgWJo3lzXvi3bHJRwXyqK/dHzMlZVRT89Cxg==", + "requires": { + "array-back": "^2.0.0", + "find-replace": "^2.0.1" + } + }, "arr-diff": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/arr-diff/-/arr-diff-4.0.0.tgz", @@ -1389,6 +1456,14 @@ "integrity": "sha1-45sJrqne+Gao8gbiiK9jkZuuOcQ=", "dev": true }, + "array-back": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/array-back/-/array-back-2.0.0.tgz", + "integrity": "sha512-eJv4pLLufP3g5kcZry0j6WXpIbzYw9GUB4mVJZno9wfwiBxbizTnHCw3VJb07cBihbFX48Y7oSrW9y+gt4glyw==", + "requires": { + "typical": "^2.6.1" + } + }, "array-includes": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/array-includes/-/array-includes-3.1.1.tgz", @@ -1873,6 +1948,80 @@ "delayed-stream": "~1.0.0" } }, + "command-line-args": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/command-line-args/-/command-line-args-5.0.2.tgz", + "integrity": "sha512-/qPcbL8zpqg53x4rAaqMFlRV4opN3pbla7I7k9x8kyOBMQoGT6WltjN6sXZuxOXw6DgdK7Ad+ijYS5gjcr7vlA==", + "requires": { + "argv-tools": "^0.1.1", + "array-back": "^2.0.0", + "find-replace": "^2.0.1", + "lodash.camelcase": "^4.3.0", + "typical": "^2.6.1" + } + }, + "command-line-usage": { + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/command-line-usage/-/command-line-usage-5.0.5.tgz", + "integrity": "sha512-d8NrGylA5oCXSbGoKz05FkehDAzSmIm4K03S5VDh4d5lZAtTWfc3D1RuETtuQCn8129nYfJfDdF7P/lwcz1BlA==", + "requires": { + "array-back": "^2.0.0", + "chalk": "^2.4.1", + "table-layout": "^0.4.3", + "typical": "^2.6.1" + }, + "dependencies": { + "ansi-styles": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", + "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "requires": { + "color-convert": "^1.9.0" + } + }, + "chalk": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "requires": { + "ansi-styles": "^3.2.1", + "escape-string-regexp": "^1.0.5", + "supports-color": "^5.3.0" + } + }, + "color-convert": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", + "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "requires": { + "color-name": "1.1.3" + } + }, + "color-name": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" + }, + "has-flag": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" + }, + "supports-color": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", + "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "requires": { + "has-flag": "^3.0.0" + } + } + } + }, + "commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==" + }, "commondir": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", @@ -1929,6 +2078,11 @@ "which": "^2.0.1" } }, + "cssfilter": { + "version": "0.0.10", + "resolved": "https://registry.npmjs.org/cssfilter/-/cssfilter-0.0.10.tgz", + "integrity": "sha1-xtJnJjKi5cg+AT5oZKQs6N79IK4=" + }, "cssom": { "version": "0.4.4", "resolved": "https://registry.npmjs.org/cssom/-/cssom-0.4.4.tgz", @@ -1999,6 +2153,11 @@ "integrity": "sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU=", "dev": true }, + "deep-extend": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", + "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==" + }, "deep-is": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz", @@ -2196,8 +2355,7 @@ "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", - "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", - "dev": true + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" }, "escodegen": { "version": "1.14.3", @@ -3030,6 +3188,15 @@ "pkg-dir": "^4.1.0" } }, + "find-replace": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/find-replace/-/find-replace-2.0.1.tgz", + "integrity": "sha512-LzDo3Fpa30FLIBsh6DCDnMN1KW2g4QKkqKmejlImgWY67dDFPX/x9Kh/op/GK522DchQXEvDi/wD48HKW49XOQ==", + "requires": { + "array-back": "^2.0.0", + "test-value": "^3.0.0" + } + }, "find-up": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", @@ -3062,6 +3229,11 @@ } } }, + "flatbuffers": { + "version": "1.11.0", + "resolved": "https://registry.npmjs.org/flatbuffers/-/flatbuffers-1.11.0.tgz", + "integrity": "sha512-0PqFKtXI4MjxomI7jO4g5XfLPm/15g2R+5WGCHBGYGh0ihQiypnHlJ6bMmkkrAe0GzZ4d7PDAfCONKIPUxNF+A==" + }, "flatted": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/flatted/-/flatted-2.0.2.tgz", @@ -5103,6 +5275,11 @@ "integrity": "sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==", "dev": true }, + "json-bignum": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/json-bignum/-/json-bignum-0.0.3.tgz", + "integrity": "sha1-QRY7UENsdz2CQk28IO1w23YEuNc=" + }, "json-parse-even-better-errors": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz", @@ -5241,12 +5418,22 @@ "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==", "dev": true }, + "lodash.camelcase": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", + "integrity": "sha1-soqmKIorn8ZRA1x3EfZathkDMaY=" + }, "lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", "integrity": "sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=", "dev": true }, + "lodash.padend": { + "version": "4.6.1", + "resolved": "https://registry.npmjs.org/lodash.padend/-/lodash.padend-4.6.1.tgz", + "integrity": "sha1-U8y6BH0G4VjTEfRdpiX05J5vFm4=" + }, "lodash.sortby": { "version": "4.7.0", "resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz", @@ -5691,6 +5878,14 @@ "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==", "dev": true }, + "pad-left": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/pad-left/-/pad-left-2.1.0.tgz", + "integrity": "sha1-FuajstRKjhOMsIOMx8tAOk/J6ZQ=", + "requires": { + "repeat-string": "^1.5.4" + } + }, "parent-module": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz", @@ -5901,6 +6096,11 @@ "type-fest": "^0.8.1" } }, + "reduce-flatten": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/reduce-flatten/-/reduce-flatten-1.0.1.tgz", + "integrity": "sha1-JYx479FT3fk8tWEjf2EYTzaW4yc=" + }, "regex-not": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/regex-not/-/regex-not-1.0.2.tgz", @@ -5932,8 +6132,7 @@ "repeat-string": { "version": "1.6.1", "resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz", - "integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc=", - "dev": true + "integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc=" }, "request": { "version": "2.88.2", @@ -6126,6 +6325,14 @@ "integrity": "sha512-DEqnSRTDw/Tc3FXf49zedI638Z9onwUotBMiUFKmrO2sdFKIbXamXGQ3Axd4qgphxKB4kw/qP1w5kTxnfU1B9Q==", "dev": true }, + "rxjs": { + "version": "6.6.2", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.2.tgz", + "integrity": "sha512-BHdBMVoWC2sL26w//BCu3YzKT4s2jip/WhwsGEDmeKYBhKDZeYezVUnHatYB7L85v5xs0BAQmg6BEYJEKxBabg==", + "requires": { + "tslib": "^1.9.0" + } + }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", @@ -6814,6 +7021,18 @@ "string-width": "^3.0.0" } }, + "table-layout": { + "version": "0.4.5", + "resolved": "https://registry.npmjs.org/table-layout/-/table-layout-0.4.5.tgz", + "integrity": "sha512-zTvf0mcggrGeTe/2jJ6ECkJHAQPIYEwDoqsiqBjI24mvRmQbInK5jq33fyypaCBxX08hMkfmdOqj6haT33EqWw==", + "requires": { + "array-back": "^2.0.0", + "deep-extend": "~0.6.0", + "lodash.padend": "^4.6.1", + "typical": "^2.6.1", + "wordwrapjs": "^3.0.0" + } + }, "terminal-link": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/terminal-link/-/terminal-link-2.1.1.tgz", @@ -6835,6 +7054,20 @@ "minimatch": "^3.0.4" } }, + "test-value": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/test-value/-/test-value-3.0.0.tgz", + "integrity": "sha512-sVACdAWcZkSU9x7AOmJo5TqE+GyNJknHaHsMrR6ZnhjVlVN9Yx6FjHrsKZ3BjIpPCT68zYesPWkakrNupwfOTQ==", + "requires": { + "array-back": "^2.0.0", + "typical": "^2.6.1" + } + }, + "text-encoding-utf-8": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/text-encoding-utf-8/-/text-encoding-utf-8-1.0.2.tgz", + "integrity": "sha512-8bw4MY9WjdsD2aMtO0OzOCY3pXGYNx2d2FfHRVUKkiCPDWjKuOlhLVASS+pD7VkLTVjW268LYJHwsnPFlBpbAg==" + }, "text-table": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz", @@ -6985,8 +7218,7 @@ "tslib": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", - "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", - "dev": true + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" }, "tsutils": { "version": "3.17.1", @@ -7048,6 +7280,11 @@ "integrity": "sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==", "dev": true }, + "typical": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/typical/-/typical-2.6.1.tgz", + "integrity": "sha1-XAgOXWYcu+OCWdLnCjxyU+hziB0=" + }, "union-value": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.1.tgz", @@ -7260,6 +7497,15 @@ "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==", "dev": true }, + "wordwrapjs": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/wordwrapjs/-/wordwrapjs-3.0.0.tgz", + "integrity": "sha512-mO8XtqyPvykVCsrwj5MlOVWvSnCdT+C+QVbm6blradR7JExAhbkZ7hZ9A+9NUtwzSqrlUo9a67ws0EiILrvRpw==", + "requires": { + "reduce-flatten": "^1.0.1", + "typical": "^2.6.1" + } + }, "wrap-ansi": { "version": "6.2.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz", @@ -7341,6 +7587,15 @@ "integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==", "dev": true }, + "xss": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/xss/-/xss-1.0.6.tgz", + "integrity": "sha512-6Q9TPBeNyoTRxgZFk5Ggaepk/4vUOYdOsIUYvLehcsIZTFjaavbVnsuAkLA5lIFuug5hw8zxcB9tm01gsjph2A==", + "requires": { + "commander": "^2.9.0", + "cssfilter": "0.0.10" + } + }, "y18n": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", diff --git a/package.json b/package.json index 65fd130..ae24b27 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "@gapit/component-template", - "description": "A template for making components", + "name": "@gapit/grafana-metric", + "description": "Retrieves metric value", "version": "0.0.0", "license": "MIT", "main": "dist/index.js", @@ -22,7 +22,9 @@ "test": "jest --coverage", "watch": "rollup -cw" }, - "dependencies": {}, + "dependencies": { + "@grafana/data": "^7.2.2" + }, "devDependencies": { "@rollup/plugin-commonjs": "^15.1.0", "@rollup/plugin-node-resolve": "^9.0.0", @@ -48,13 +50,13 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/gapitio/component-template.git" + "url": "git+https://github.com/gapitio/grafana-metric.git" }, - "author": "Gapit", + "author": "https://github.com/gapitio", "bugs": { - "url": "https://github.com/gapitio/component-template/issues" + "url": "https://github.com/gapitio/grafana-metric/issues" }, - "homepage": "https://github.com/gapitio/component-template#readme", + "homepage": "https://github.com/gapitio/grafana-metric#readme", "publishConfig": { "access": "public" } diff --git a/src/__mocks__/create-series.ts b/src/__mocks__/create-series.ts new file mode 100644 index 0000000..4ba5b7b --- /dev/null +++ b/src/__mocks__/create-series.ts @@ -0,0 +1,76 @@ +import { DataFrame, FieldType } from "@grafana/data"; + +const TIME_FIELD = { + name: "Time", + type: FieldType.time, + config: {}, + values: { + length: 0, + get: (index: number) => { + return [][index]; + }, + toArray: () => [], + }, + state: { + displayName: null, + scopedVars: {}, + }, +}; + +function valueField(value: number) { + return { + name: "Value", + type: FieldType.number, + config: {}, + values: { + length: 0, + get: (index: number) => { + return [][index]; + }, + toArray: () => [], + }, + state: { + displayName: null, + scopedVars: {}, + calcs: { + allIsNull: false, + allIsZero: false, + count: 1, + delta: value, + diff: value, + first: value, + firstNotNull: value, + last: value, + lastNotNull: value, + logmin: value, + max: value, + mean: value, + min: 0, + nonNullCount: 1, + previousDeltaUp: true, + range: value, + step: value, + sum: value, + }, + }, + }; +} + +function createMinimalSeries(name: string, refId = "A"): DataFrame { + return { + name: name, + fields: [], + length: 1, + refId: refId, + meta: undefined, + }; +} + +function createSeries(name: string, value: number, refId = "A"): DataFrame { + const series = createMinimalSeries(name, refId); + series.fields.push(TIME_FIELD, valueField(value)); + + return series; +} + +export { createMinimalSeries, createSeries }; diff --git a/src/index.test.ts b/src/index.test.ts new file mode 100644 index 0000000..32b209e --- /dev/null +++ b/src/index.test.ts @@ -0,0 +1,46 @@ +import { LoadingState, PanelData, dateTime } from "@grafana/data"; + +import { createMinimalSeries, createSeries } from "./__mocks__/create-series"; + +import { getMetricValueByName } from "./index"; + +declare global { + interface Window { + data: PanelData; + } +} + +window.data = { + state: LoadingState.Done, + series: [ + createSeries("test", 1000), + createSeries("test", 2), + createSeries("something", 2000), + createSeries("something", 2000), + createMinimalSeries("minimal"), + ], + timeRange: { + from: dateTime(0), + to: dateTime(0), + raw: { + from: dateTime(0), + to: dateTime(0), + }, + }, +}; + +describe("getMetricValueByName", () => { + it("retrieves correct value", () => { + expect(getMetricValueByName("test", {})).toBe(1000); + }); + + it("returns noDataValue when no value is found", () => { + expect(getMetricValueByName("nonExistentName")).toBe(null); + expect( + getMetricValueByName("nonExistentName", { noDataValue: "something" }) + ).toBe("something"); + expect(getMetricValueByName("minimal", { noDataValue: "something" })).toBe( + "something" + ); + }); +}); diff --git a/src/index.ts b/src/index.ts index 76df2ca..05aeb92 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,20 +1,32 @@ -import { SOMETHING_VARIABLE, somethingFunction } from "./something"; +import { PanelData } from "@grafana/data"; + +declare const data: PanelData; /** - * Adds x and y together. - * - * @example + * Gets a metric value by name/alias * - * ```ts - * add(2, 3); // Returns 5 - * ``` - * - * @param {number} x - Number to be added together. - * @param {number} y - Number to be added together. - * @return {number} Returns the sum of x and y. + * @param {string} metricName + * @param {object} metricOptions */ -function add(x: number, y: number): number { - return x + y; -} +const getMetricValueByName = ( + metricName: string, + { + noDataValue = null, + }: { + noDataValue?: unknown; + } = {} +): unknown => { + const filteredSeries = data.series.filter( + (series) => series.name == metricName + ); + if (filteredSeries.length > 0) { + const valueField = filteredSeries[0].fields[1]; + + if (valueField && valueField.state && valueField.state.calcs) { + return valueField.state.calcs.last; + } + } + return noDataValue; +}; -export { SOMETHING_VARIABLE, add, somethingFunction }; +export { getMetricValueByName }; diff --git a/src/something.ts b/src/something.ts deleted file mode 100644 index 5b83c19..0000000 --- a/src/something.ts +++ /dev/null @@ -1,10 +0,0 @@ -/** - * @returns something - */ -function somethingFunction(): string { - return "something"; -} - -const SOMETHING_VARIABLE = "something"; - -export { SOMETHING_VARIABLE, somethingFunction }; diff --git a/test/index.test.ts b/test/index.test.ts deleted file mode 100644 index d32b99a..0000000 --- a/test/index.test.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { SOMETHING_VARIABLE, add, somethingFunction } from "../src/index"; - -describe("add", () => { - it("adds correctly", () => { - expect(add(2, 5)).toBe(7); - }); -}); - -describe("something", () => { - it("is something", () => { - expect(somethingFunction()).toBe("something"); - expect(SOMETHING_VARIABLE).toBe("something"); - }); -}); diff --git a/tsconfig.json b/tsconfig.json index 41fa683..8d8cc37 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,5 +9,6 @@ "strict": true, "target": "ESNext" }, - "include": ["./src/**/*"] + "include": ["./src/**/*.ts"], + "exclude": ["./src/**/*.test.ts", "./src/**/__mocks__/*"] }