useSystemSelectorOptions.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 {SpanMetricsField} from 'sentry/views/insights/types';
  6. /**
  7. * The supported relational database system values are based on what is
  8. * set in the Sentry Python SDK. The only currently supported NoSQL DBMS is MongoDB.
  9. *
  10. * https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/sqlalchemy.py#L125
  11. */
  12. enum SupportedDatabaseSystems {
  13. // SQL
  14. SQLITE = 'sqlite',
  15. POSTGRESQL = 'postgresql',
  16. MARIADB = 'mariadb',
  17. MYSQL = 'mysql',
  18. ORACLE = 'oracle',
  19. // NoSQL
  20. MONGODB = 'mongodb',
  21. }
  22. const DATABASE_SYSTEM_TO_LABEL: Record<SupportedDatabaseSystems, string> = {
  23. [SupportedDatabaseSystems.SQLITE]: 'SQLite',
  24. [SupportedDatabaseSystems.POSTGRESQL]: 'PostgreSQL',
  25. [SupportedDatabaseSystems.MARIADB]: 'MariaDB',
  26. [SupportedDatabaseSystems.MYSQL]: 'MySQL',
  27. [SupportedDatabaseSystems.ORACLE]: 'Oracle',
  28. [SupportedDatabaseSystems.MONGODB]: 'MongoDB',
  29. };
  30. export function useSystemSelectorOptions() {
  31. const [selectedSystem, setSelectedSystem] = useLocalStorageState<string>(
  32. 'insights-db-system-selector',
  33. ''
  34. );
  35. const {data, isLoading, isError} = useSpanMetrics(
  36. {
  37. search: MutableSearch.fromQueryObject({'span.op': 'db'}),
  38. fields: [SpanMetricsField.SPAN_SYSTEM, 'count()'],
  39. sorts: [{field: 'count()', kind: 'desc'}],
  40. },
  41. 'api.starfish.database-system-selector'
  42. );
  43. const options: SelectOption<string>[] = [];
  44. data.forEach(entry => {
  45. const system = entry['span.system'];
  46. if (system) {
  47. const label: string =
  48. system in DATABASE_SYSTEM_TO_LABEL ? DATABASE_SYSTEM_TO_LABEL[system] : system;
  49. options.push({value: system, label, textValue: label});
  50. }
  51. });
  52. // Edge case: Invalid DB system was retrieved from localStorage
  53. if (!options.find(option => selectedSystem === option.value) && options.length > 0) {
  54. setSelectedSystem(options[0].value);
  55. }
  56. // Edge case: No current system is saved in localStorage
  57. if (!selectedSystem && options.length > 0) {
  58. setSelectedSystem(options[0].value);
  59. }
  60. return {selectedSystem, setSelectedSystem, options, isLoading, isError};
  61. }