useSystemSelectorOptions.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import type {SelectOption} from 'sentry/components/compactSelect';
  2. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  3. import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
  4. import {useSpanMetrics} from 'sentry/views/insights/common/queries/useDiscover';
  5. import {
  6. DATABASE_SYSTEM_TO_LABEL,
  7. SupportedDatabaseSystem,
  8. } from 'sentry/views/insights/database/utils/constants';
  9. import {SpanMetricsField} from 'sentry/views/insights/types';
  10. export function useSystemSelectorOptions() {
  11. const [selectedSystem, setSelectedSystem] = useLocalStorageState<string>(
  12. 'insights-db-system-selector',
  13. ''
  14. );
  15. const {data, isPending, isError} = useSpanMetrics(
  16. {
  17. search: MutableSearch.fromQueryObject({'span.op': 'db'}),
  18. fields: [SpanMetricsField.SPAN_SYSTEM, 'count()'],
  19. sorts: [{field: 'count()', kind: 'desc'}],
  20. },
  21. 'api.starfish.database-system-selector'
  22. );
  23. const options: SelectOption<string>[] = [];
  24. data.forEach(entry => {
  25. const system = entry['span.system'];
  26. if (system) {
  27. const textValue =
  28. system in DATABASE_SYSTEM_TO_LABEL ? DATABASE_SYSTEM_TO_LABEL[system] : system;
  29. const supportedSystemSet: Set<string> = new Set(
  30. Object.values(SupportedDatabaseSystem)
  31. );
  32. if (supportedSystemSet.has(system)) {
  33. options.push({value: system, label: textValue, textValue});
  34. }
  35. }
  36. });
  37. // Edge case: Invalid DB system was retrieved from localStorage
  38. if (!options.find(option => selectedSystem === option.value) && options.length > 0) {
  39. setSelectedSystem(options[0].value);
  40. }
  41. // Edge case: No current system is saved in localStorage
  42. if (!selectedSystem && options.length > 0) {
  43. setSelectedSystem(options[0].value);
  44. }
  45. return {selectedSystem, setSelectedSystem, options, isLoading: isPending, isError};
  46. }