parseLokiLabelMappings.ts 911 B

12345678910111213141516171819202122232425262728293031
  1. import { GraphiteLokiMapping } from '../types';
  2. /**
  3. * Converts a simple string used in LokiLogsMappings component (e.g. "servers.(name).*")
  4. * to data model saved in data source configuration.
  5. */
  6. export function fromString(text: string): GraphiteLokiMapping {
  7. return {
  8. matchers: text.split('.').map((metricNode) => {
  9. if (metricNode.startsWith('(') && metricNode.endsWith(')')) {
  10. return {
  11. value: '*',
  12. labelName: metricNode.slice(1, -1),
  13. };
  14. } else {
  15. return { value: metricNode };
  16. }
  17. }),
  18. };
  19. }
  20. /**
  21. * Coverts configuration stored in data source configuration into a string displayed in LokiLogsMappings component.
  22. */
  23. export function toString(mapping: GraphiteLokiMapping): string {
  24. return mapping.matchers
  25. .map((matcher) => {
  26. return matcher.labelName ? `(${matcher.labelName})` : `${matcher.value}`;
  27. })
  28. .join('.');
  29. }