helpers.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { forEach, sortBy } from 'lodash';
  2. import { SelectableValue } from '@grafana/data';
  3. import { FuncDefs, FuncInstance, ParamDef } from '../gfunc';
  4. import { GraphiteSegment } from '../types';
  5. import { EditableParam } from './FunctionParamEditor';
  6. export function mapStringsToSelectables<T extends string>(values: T[]): Array<SelectableValue<T>> {
  7. return values.map((value) => ({
  8. value,
  9. label: value,
  10. }));
  11. }
  12. export function mapSegmentsToSelectables(segments: GraphiteSegment[]): Array<SelectableValue<GraphiteSegment>> {
  13. return segments.map((segment) => ({
  14. label: segment.value,
  15. value: segment,
  16. }));
  17. }
  18. export function mapFuncDefsToSelectables(funcDefs: FuncDefs): Array<SelectableValue<string>> {
  19. const categories: any = {};
  20. forEach(funcDefs, (funcDef) => {
  21. if (!funcDef.category) {
  22. return;
  23. }
  24. if (!categories[funcDef.category]) {
  25. categories[funcDef.category] = { label: funcDef.category, value: funcDef.category, options: [] };
  26. }
  27. categories[funcDef.category].options.push({
  28. label: funcDef.name,
  29. value: funcDef.name,
  30. });
  31. });
  32. return sortBy(categories, 'label');
  33. }
  34. function createEditableParam(paramDef: ParamDef, additional: boolean, value?: string | number): EditableParam {
  35. return {
  36. name: paramDef.name,
  37. value: value?.toString() || '',
  38. optional: !!paramDef.optional || additional, // only first param is required when multiple are allowed
  39. multiple: !!paramDef.multiple,
  40. options:
  41. paramDef.options?.map((option: string | number) => ({
  42. value: option.toString(),
  43. label: option.toString(),
  44. })) ?? [],
  45. };
  46. }
  47. /**
  48. * Create a list of params that can be edited in the function editor.
  49. */
  50. export function mapFuncInstanceToParams(func: FuncInstance): EditableParam[] {
  51. // list of required parameters (from func.def)
  52. const params: EditableParam[] = func.def.params.map((paramDef: ParamDef, index: number) =>
  53. createEditableParam(paramDef, false, func.params[index])
  54. );
  55. // list of additional (multiple or optional) params entered by the user
  56. while (params.length < func.params.length) {
  57. const paramDef = func.def.params[func.def.params.length - 1];
  58. const value = func.params[params.length];
  59. params.push(createEditableParam(paramDef, true, value));
  60. }
  61. // extra "fake" param to allow adding more multiple values at the end
  62. if (params.length && params[params.length - 1].value && params[params.length - 1]?.multiple) {
  63. const paramDef = func.def.params[func.def.params.length - 1];
  64. params.push(createEditableParam(paramDef, true, ''));
  65. }
  66. return params;
  67. }