meta.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { QueryResultMetaNotice } from '@grafana/data';
  2. import { MetricTankSeriesMeta } from './types';
  3. // https://github.com/grafana/metrictank/blob/master/scripts/config/storage-schemas.conf#L15-L46
  4. export interface RetentionInfo {
  5. interval: string;
  6. retention?: string;
  7. chunkspan?: string;
  8. numchunks?: number;
  9. ready?: boolean | number; // whether, or as of what data timestamp, the archive is ready for querying.
  10. }
  11. function toInteger(val?: string): number | undefined {
  12. if (val) {
  13. return parseInt(val, 10);
  14. }
  15. return undefined;
  16. }
  17. function toBooleanOrTimestamp(val?: string): number | boolean | undefined {
  18. if (val) {
  19. if (val === 'true') {
  20. return true;
  21. }
  22. if (val === 'false') {
  23. return false;
  24. }
  25. return parseInt(val, 10);
  26. }
  27. return undefined;
  28. }
  29. export function getRollupNotice(metaList: MetricTankSeriesMeta[]): QueryResultMetaNotice | null {
  30. for (const meta of metaList) {
  31. const archiveIndex = meta['archive-read'];
  32. if (archiveIndex > 0) {
  33. const schema = parseSchemaRetentions(meta['schema-retentions']);
  34. const intervalString = schema[archiveIndex].interval;
  35. const func = (meta['consolidator-normfetch'] ?? '').replace('Consolidator', '');
  36. return {
  37. text: `Data is rolled up, aggregated over ${intervalString} using ${func} function`,
  38. severity: 'info',
  39. inspect: 'meta',
  40. };
  41. }
  42. }
  43. return null;
  44. }
  45. export function getRuntimeConsolidationNotice(metaList: MetricTankSeriesMeta[]): QueryResultMetaNotice | null {
  46. for (const meta of metaList) {
  47. const runtimeNr = meta['aggnum-rc'];
  48. if (runtimeNr > 0) {
  49. const func = (meta['consolidator-rc'] ?? '').replace('Consolidator', '');
  50. return {
  51. text: `Data is runtime consolidated, ${runtimeNr} datapoints combined using ${func} function`,
  52. severity: 'info',
  53. inspect: 'meta',
  54. };
  55. }
  56. }
  57. return null;
  58. }
  59. export function parseSchemaRetentions(spec: string): RetentionInfo[] {
  60. if (!spec) {
  61. return [];
  62. }
  63. return spec.split(',').map((str) => {
  64. const vals = str.split(':');
  65. return {
  66. interval: vals[0],
  67. retention: vals[1],
  68. chunkspan: vals[2],
  69. numchunks: toInteger(vals[3]),
  70. ready: toBooleanOrTimestamp(vals[4]),
  71. };
  72. });
  73. }