useLogsQuery.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import type EventView from 'sentry/utils/discover/eventView';
  2. import {useQuery} from 'sentry/utils/queryClient';
  3. import useOrganization from 'sentry/utils/useOrganization';
  4. import useProjects from 'sentry/utils/useProjects';
  5. import {
  6. useLogsFields,
  7. useLogsSearch,
  8. useLogsSortBys,
  9. } from 'sentry/views/explore/contexts/logs/logsPageParams';
  10. import {useOurlogs} from 'sentry/views/insights/common/queries/useDiscover';
  11. export interface OurLogsTableResult {
  12. eventView: EventView;
  13. result: ReturnType<typeof useOurlogs>;
  14. }
  15. export function useExploreLogsTable(options: Parameters<typeof useOurlogs>[0]) {
  16. const search = useLogsSearch();
  17. const fields = useLogsFields();
  18. const sortBys = useLogsSortBys();
  19. const {data, isError, isPending, pageLinks} = useOurlogs(
  20. {
  21. ...options,
  22. sorts: sortBys,
  23. fields,
  24. search,
  25. },
  26. 'api.logs-tab.view'
  27. );
  28. return {data, isError, isPending, pageLinks};
  29. }
  30. export interface AttributeAnyValue {
  31. type: 'str' | 'int' | 'float' | 'bool';
  32. value: string | number | null;
  33. }
  34. type LogDetailsAttributes = Record<string, AttributeAnyValue>;
  35. export interface OurLogsTableRowDetails {
  36. attributes: LogDetailsAttributes;
  37. itemId: string;
  38. timestamp: string;
  39. meta?: {
  40. requestId: string;
  41. };
  42. }
  43. export function useExploreLogsTableRow(_props: {
  44. log_id: string | number;
  45. project_id: string;
  46. enabled?: boolean;
  47. }) {
  48. const organization = useOrganization();
  49. const {projects} = useProjects();
  50. const _project = projects.find(p => p.id === _props.project_id);
  51. const {data, isError, isPending} = useQuery<OurLogsTableRowDetails>({
  52. queryKey: ['logs-table-row', _props.log_id, _props.project_id],
  53. queryFn: async () => {
  54. if (!_project) {
  55. throw new Error('Project not found');
  56. }
  57. const res = await fetch(
  58. `/api/0/projects/${organization.slug}/${_project?.slug}/trace-items/${_props.log_id}/?dataset=ourlogs`
  59. );
  60. if (!res.ok) {
  61. throw new Error('Failed to fetch log details');
  62. }
  63. return await res.json();
  64. },
  65. });
  66. return {
  67. data,
  68. isError,
  69. isPending,
  70. };
  71. }