{"version":3,"file":"suggestions.cjs","sources":["../../../src/types/suggestions.ts"],"sourcesContent":["import { defaultsDeep } from 'lodash';\n\nimport { DataTransformerConfig } from '@grafana/schema';\n\nimport { getPanelDataSummary, PanelDataSummary } from '../panel/suggestions/getPanelDataSummary';\n\nimport { DataFrame } from './dataFrame';\nimport { FieldConfigSource } from './fieldOverrides';\n\n/**\n * @internal\n * generates a hash for a suggestion based for use by the UI.\n */\nexport function getSuggestionHash(suggestion: Omit<PanelPluginVisualizationSuggestion, 'hash'>): string {\n  return deterministicObjectHash(suggestion);\n}\n\nfunction strHash(str: string): string {\n  let hash = 0;\n  for (let i = 0; i < str.length; i++) {\n    const char = str.charCodeAt(i);\n    hash = (hash << 5) - hash + char;\n    hash |= 0; // Convert to 32bit integer\n  }\n  return hash.toString(36);\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction deterministicObjectHash<T extends Record<string, any>>(obj: T): string {\n  let result = '';\n  for (const key of Object.keys(obj).sort()) {\n    const value = obj[key];\n    if (value === undefined) {\n      continue;\n    }\n    result += key + ':';\n    if (typeof value === 'object' && value !== null) {\n      result += deterministicObjectHash(value);\n    } else if (Array.isArray(value)) {\n      result += value\n        .map((v) => (typeof value === 'object' && value !== null ? deterministicObjectHash(v) : String(v)))\n        .join(',');\n    } else {\n      result += String(value);\n    }\n    result += ';';\n  }\n  return strHash(result);\n}\n\n/**\n * @alpha\n * A suggestion for a visualization given some data. This represents the shape of the panel (including options and field config)\n * that will be used to show a small preview in the Grafana UI when suggesting visualizations in the Panel Editor.\n */\nexport interface VisualizationSuggestion<TOptions extends unknown = {}, TFieldConfig extends {} = {}> {\n  /** Name of suggestion */\n  name?: string;\n  /** Description */\n  description?: string;\n  /** Panel plugin options */\n  options?: Partial<TOptions>;\n  /** Panel plugin field options */\n  fieldConfig?: FieldConfigSource<Partial<TFieldConfig>>;\n  /** Data transformations */\n  transformations?: DataTransformerConfig[];\n  /** A value between 0-100 how suitable suggestion is */\n  score?: VisualizationSuggestionScore;\n  /** Options for how to render suggestion card */\n  cardOptions?: {\n    /**\n     * Given that the suggestion is being rendered as a small preview, you may want to modify certain options\n     * specifically for the smaller preview version of the visualization. In this method, you should directly\n     * mutate the suggestion object which is passed in as the first argument.\n     */\n    previewModifier?: (suggestion: VisualizationSuggestion<TOptions, TFieldConfig>) => void;\n    /** @deprecated this will no longer be supported in the new Suggestions UI. */\n    icon?: string;\n    /** @deprecated this will no longer be supported in the new Suggestions UI. */\n    imgSrc?: string;\n  };\n}\n\n/**\n * @internal\n * the internal interface that the PanelPlugin transforms the supplied suggestions into.\n */\nexport interface PanelPluginVisualizationSuggestion<TOptions extends unknown = {}, TFieldConfig extends {} = {}>\n  extends VisualizationSuggestion<TOptions, TFieldConfig> {\n  /** Name of suggestion */\n  name: string;\n  /** Panel plugin id */\n  pluginId: string;\n  /** unique hash assigned by Grafana for use by the UI. */\n  hash: string;\n}\n\n/**\n * @alpha\n */\nexport enum VisualizationSuggestionScore {\n  /** We are pretty sure this is the best possible option */\n  Best = 100,\n  /** Should be a really good option */\n  Good = 70,\n  /** Can be visualized but there are likely better options. If no score is set this score is assumed */\n  OK = 50,\n}\n\n/**\n * @alpha\n * TODO: this name is temporary; it will become just \"VisualizationSuggestionsSupplier\" when the other interface is deleted.\n *\n * executed while rendering suggestions each time the DataFrame changes, this method\n * determines which suggestions can be shown for this PanelPlugin given the PanelDataSummary.\n *\n * - returns an array of VisualizationSuggestions\n * - boolean return equates to \"show a single suggestion card for this panel plugin with the default options\" (true = show, false or void = hide)\n */\nexport type VisualizationSuggestionsSupplier<TOptions extends unknown, TFieldConfig extends {} = {}> = (\n  panelDataSummary: PanelDataSummary\n) => Array<VisualizationSuggestion<TOptions, TFieldConfig>> | void;\n\n/**\n * DEPRECATED - the below exports need to remain in the code base to help make the transition for the Polystat plugin, which implements\n * suggestions using the old API. These should be removed for Grafana 13.\n */\n/**\n * @deprecated use VisualizationSuggestionsSupplier\n */\nexport interface VisualizationSuggestionsSupplierDeprecated {\n  getSuggestionsForData: (builder: VisualizationSuggestionsBuilder) => void;\n}\n\n/**\n * @deprecated use VisualizationSuggestionsSupplier\n */\nexport class VisualizationSuggestionsBuilder {\n  public dataSummary: PanelDataSummary;\n  public list: PanelPluginVisualizationSuggestion[] = [];\n\n  constructor(dataFrames: DataFrame[]) {\n    this.dataSummary = getPanelDataSummary(dataFrames);\n  }\n\n  getList(): PanelPluginVisualizationSuggestion[] {\n    return this.list;\n  }\n\n  getListAppender(suggestionDefaults: Omit<PanelPluginVisualizationSuggestion, 'hash'>) {\n    const withDefaults = (suggestion: VisualizationSuggestion): PanelPluginVisualizationSuggestion => {\n      const s = defaultsDeep({}, suggestion, suggestionDefaults);\n      return {\n        ...s,\n        hash: getSuggestionHash(s),\n      };\n    };\n    return {\n      append: (suggestion: VisualizationSuggestion) => {\n        this.list.push(withDefaults(suggestion));\n      },\n    };\n  }\n}\n"],"names":["VisualizationSuggestionScore","getPanelDataSummary","defaultsDeep"],"mappings":";;;;;;;;AAaO,SAAS,kBAAkB,UAAA,EAAsE;AACtG,EAAA,OAAO,wBAAwB,UAAU,CAAA;AAC3C;AAEA,SAAS,QAAQ,GAAA,EAAqB;AACpC,EAAA,IAAI,IAAA,GAAO,CAAA;AACX,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,GAAA,CAAI,QAAQ,CAAA,EAAA,EAAK;AACnC,IAAA,MAAM,IAAA,GAAO,GAAA,CAAI,UAAA,CAAW,CAAC,CAAA;AAC7B,IAAA,IAAA,GAAA,CAAQ,IAAA,IAAQ,KAAK,IAAA,GAAO,IAAA;AAC5B,IAAA,IAAA,IAAQ,CAAA;AAAA,EACV;AACA,EAAA,OAAO,IAAA,CAAK,SAAS,EAAE,CAAA;AACzB;AAGA,SAAS,wBAAuD,GAAA,EAAgB;AAC9E,EAAA,IAAI,MAAA,GAAS,EAAA;AACb,EAAA,KAAA,MAAW,OAAO,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,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