{"version":3,"file":"utils.cjs","sources":["../../../../src/grafana/contactPoints/utils.ts"],"sourcesContent":["import { countBy, isEmpty } from 'lodash';\n\nimport { type Receiver } from '@grafana/api-clients/rtkq/notifications.alerting/v0alpha1';\n\nimport { type ContactPoint, type ContactPointMetadataAnnotations } from '../api/notifications/v0alpha1/types';\n\n// Annotation key that indicates whether a contact point can be used in routes and rules\nconst CAN_USE_ANNOTATION = 'grafana.com/canUse';\n\n/**\n * Minimal structural type that any k8s alerting entity satisfies.\n * Accepts ContactPoint, Receiver, and the app-internal EntityToCheck type alike.\n */\ntype WithContactPointAnnotations = {\n  metadata?: { annotations?: ContactPointMetadataAnnotations };\n};\n\n/**\n * Checks if a contact point can be used in routes and rules.\n * Contact points that are imported from external sources (e.g., Prometheus Alertmanager)\n * have the `grafana.com/canUse` annotation set to `false` and cannot be used.\n *\n * @param contactPoint - The ContactPoint object to check\n * @returns `true` if the contact point can be used, `false` otherwise\n */\nexport function isUsableContactPoint(contactPoint: ContactPoint | Receiver): boolean {\n  const canUse = contactPoint.metadata?.annotations?.[CAN_USE_ANNOTATION];\n  return canUse === 'true';\n}\n\n/**\n * Returns the number of notification policy routes that reference this contact point,\n * as reported by the server-set `grafana.com/inUse/routes` annotation.\n * Returns 0 when the annotation is absent.\n */\nexport function getContactPointInUseRoutes(contactPoint: WithContactPointAnnotations): number {\n  const value = contactPoint.metadata?.annotations?.['grafana.com/inUse/routes'];\n  return Number(value) || 0;\n}\n\n/**\n * Returns the number of alert rules that reference this contact point via simplified routing,\n * as reported by the server-set `grafana.com/inUse/rules` annotation.\n * Returns 0 when the annotation is absent.\n */\nexport function getContactPointInUseRules(contactPoint: WithContactPointAnnotations): number {\n  const value = contactPoint.metadata?.annotations?.['grafana.com/inUse/rules'];\n  return Number(value) || 0;\n}\n\n/**\n * Returns how many routes and rules currently reference this contact point,\n * as reported by the server-set in-use annotations.\n *\n * @returns `{ routes, rules }` — each count is 0 when the annotation is absent.\n */\nexport function getContactPointInUse(contactPoint: WithContactPointAnnotations): { routes: number; rules: number } {\n  return {\n    routes: getContactPointInUseRoutes(contactPoint),\n    rules: getContactPointInUseRules(contactPoint),\n  };\n}\n\n/**\n * Generates a human-readable description of a ContactPoint by summarizing its integrations.\n * If the ContactPoint has no integrations, it returns an empty placeholder text.\n *\n * For integrations, it counts the occurrences of each type and formats them as a comma-separated list.\n * Multiple integrations of the same type are indicated with a count in parentheses.\n *\n * @param contactPoint - The ContactPoint object to describe\n * @returns A string description of the ContactPoint's integrations\n */\nexport function getContactPointDescription(contactPoint: ContactPoint | Receiver): string {\n  if (isEmpty(contactPoint.spec.integrations)) {\n    return '<empty contact point>';\n  }\n\n  // Count the occurrences of each integration type\n  const integrationCounts = countBy(contactPoint.spec.integrations, (integration) => integration.type);\n\n  const description = Object.entries(integrationCounts)\n    .map(([type, count]) => {\n      // either \"email\" or \"email (2)\" but not \"email (1)\"\n      return count > 1 ? `${type} (${count})` : type;\n    })\n    .join(', ');\n\n  return description;\n}\n"],"names":["isEmpty","countBy"],"mappings":";;;;;;;AAOA,MAAM,kBAAA,GAAqB,oBAAA;AAkBpB,SAAS,qBAAqB,YAAA,EAAgD;AAzBrF,EAAA,IAAA,EAAA,EAAA,EAAA;AA0BE,EAAA,MAAM,MAAA,GAAA,CAAS,EAAA,GAAA,CAAA,EAAA,GAAA,YAAA,CAAa,QAAA,KAAb,IAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAuB,gBAAvB,IAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAqC,kBAAA,CAAA;AACpD,EAAA,OAAO,MAAA,KAAW,MAAA;AACpB;AAOO,SAAS,2BAA2B,YAAA,EAAmD;AAnC9F,EAAA,IAAA,EAAA,EAAA,EAAA;AAoCE,EAAA,MAAM,KAAA,GAAA,CAAQ,EAAA,GAAA,CAAA,EAAA,GAAA,YAAA,CAAa,QAAA,KAAb,IAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAuB,gBAAvB,IAAA,GAAA,KAAA,CA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