utils.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { valid } from 'semver';
  2. import { DataSourceSettings } from '@grafana/data';
  3. import { ElasticsearchOptions } from '../types';
  4. import { coerceESVersion } from '../utils';
  5. import { defaultMaxConcurrentShardRequests } from './ElasticDetails';
  6. export const coerceOptions = (
  7. options: DataSourceSettings<ElasticsearchOptions, {}>
  8. ): DataSourceSettings<ElasticsearchOptions, {}> => {
  9. const esVersion = coerceESVersion(options.jsonData.esVersion);
  10. return {
  11. ...options,
  12. jsonData: {
  13. ...options.jsonData,
  14. timeField: options.jsonData.timeField || '@timestamp',
  15. esVersion,
  16. maxConcurrentShardRequests:
  17. options.jsonData.maxConcurrentShardRequests || defaultMaxConcurrentShardRequests(esVersion),
  18. logMessageField: options.jsonData.logMessageField || '',
  19. logLevelField: options.jsonData.logLevelField || '',
  20. includeFrozen: options.jsonData.includeFrozen ?? false,
  21. },
  22. };
  23. };
  24. export const isValidOptions = (options: DataSourceSettings<ElasticsearchOptions, {}>): boolean => {
  25. return (
  26. // esVersion should be a valid semver string
  27. !!valid(options.jsonData.esVersion) &&
  28. // timeField should not be empty or nullish
  29. !!options.jsonData.timeField &&
  30. // maxConcurrentShardRequests should be a number AND greater than 0
  31. !!options.jsonData.maxConcurrentShardRequests &&
  32. // message & level fields should be defined
  33. options.jsonData.logMessageField !== undefined &&
  34. options.jsonData.logLevelField !== undefined
  35. );
  36. };
  37. type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T;
  38. export const isTruthy = <T>(value: T): value is Truthy<T> => Boolean(value);