UNPKG

@grafana/ui

Version:
144 lines (141 loc) 5.47 kB
import { useMemo } from 'react'; import { FieldType, getFieldDisplayName, FieldNamePickerBaseNameMode } from '@grafana/data'; import { t } from '@grafana/i18n'; import { getFieldTypeIcon } from '../../types/icon.mjs'; "use strict"; function frameHasName(name, names) { if (!name) { return false; } return names.display.has(name) || names.raw.has(name); } function getFrameFieldsDisplayNames(data, filter, existingNames, parentData = data, scope = "series") { const names = existingNames != null ? existingNames : { display: /* @__PURE__ */ new Set(), raw: /* @__PURE__ */ new Set(), fields: /* @__PURE__ */ new Map(), scopes: /* @__PURE__ */ new Map() }; for (const frame of data) { for (const field of frame.fields) { if (filter && !filter(field)) { continue; } if (field.type === FieldType.nestedFrames) { field.values.forEach( (nestedData) => getFrameFieldsDisplayNames(nestedData, filter, names, parentData, "nested") ); continue; } const disp = getFieldDisplayName(field, frame, parentData); names.display.add(disp); names.fields.set(disp, field); names.scopes.set(disp, scope); if (field.name && disp !== field.name) { names.raw.add(field.name); names.fields.set(field.name, field); names.scopes.set(field.name, scope); } } } return names; } function getGroupLabelForScope(scope) { switch (scope) { case "nested": return t("grafana-ui.matchers.groups.nested", "Nested"); case "annotation": return t("grafana-ui.matchers.groups.annotation", "Annotations"); case "exemplar": return t("grafana-ui.matchers.groups.exemplar", "Exemplars"); case "series": default: return t("grafana-ui.matchers.groups.series", "Dataframe"); } } function getGroupDescriptionForScope(scope) { switch (scope) { case "nested": return t("grafana-ui.matchers.groups.nested.description", "Fields from nested dataframes."); case "annotation": return t("grafana-ui.matchers.groups.annotation.description", "Annotations series for this panel."); case "series": return t("grafana-ui.matchers.groups.series.description", "Fields from the dataframes in this panel."); case "exemplar": return t("grafana-ui.matchers.groups.exemplar.description", "Trace exemplars for this panel."); default: return void 0; } } function useFieldDisplayNames(data, filter, scope) { return useMemo(() => getFrameFieldsDisplayNames(data, filter, void 0, void 0, scope), [data, filter, scope]); } function useMatcherSelectOptions(displayNames, currentName, { firstItem, fieldType, baseNameMode, scope } = {}) { return useMemo(() => { var _a; let found = false; const uniqueScopes = new Set(displayNames.scopes.values()); const shouldShowScopes = !scope && uniqueScopes.size > 1; const isFound = (name) => name === currentName && (scope ? displayNames.scopes.get(name) === scope : true); const getGroup = (name) => shouldShowScopes ? getGroupLabelForScope(displayNames.scopes.get(name)) : void 0; const optionFactory = (getLabel, getExtraOptions) => (name) => ({ value: name, label: getLabel(name), group: getGroup(name), ...getExtraOptions == null ? void 0 : getExtraOptions(name) }); const displayNameBuilder = optionFactory( (name) => name, (name) => { const field = displayNames.fields.get(name); if (!field) { return; } return { icon: getFieldTypeIcon(field) }; } ); const baseFieldNameBuilder = optionFactory( (name) => t("grafana-ui.matchers.labels.base-field-name", "{{name}} (base field name)", { name }) ); let options = []; if (firstItem) { options.push(firstItem); } const sets = []; if (baseNameMode === FieldNamePickerBaseNameMode.OnlyBaseNames) { sets.push({ set: displayNames.raw, builder: baseFieldNameBuilder }); } else { sets.push({ set: displayNames.display, builder: displayNameBuilder }); } if (baseNameMode !== FieldNamePickerBaseNameMode.ExcludeBaseNames) { sets.push({ set: displayNames.raw, builder: baseFieldNameBuilder }); } for (const { set, builder } of sets) { const shouldCheckFieldType = fieldType && set === displayNames.display; for (const name of set) { if (!found && isFound(name)) { found = true; } if (scope && displayNames.scopes.get(name) !== scope) { continue; } if (shouldCheckFieldType && fieldType !== ((_a = displayNames.fields.get(name)) == null ? void 0 : _a.type)) { continue; } options.push(builder(name)); } } if (currentName && !found) { const notFoundOption = optionFactory( (name) => t("grafana-ui.matchers.labels.not-found", "{{name}} (not found)", { name }) ); options.push(notFoundOption(currentName)); } return options; }, [baseNameMode, currentName, displayNames, fieldType, firstItem, scope]); } function getUniqueMatcherScopes(data) { return /* @__PURE__ */ new Set([...getFrameFieldsDisplayNames(data).scopes.values()]); } export { frameHasName, getFrameFieldsDisplayNames, getGroupDescriptionForScope, getGroupLabelForScope, getUniqueMatcherScopes, useFieldDisplayNames, useMatcherSelectOptions }; //# sourceMappingURL=utils.mjs.map