You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

project-analyses.ts 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
  21. import {
  22. CreateEventResponse,
  23. ProjectActivityStatuses,
  24. changeEvent,
  25. createEvent,
  26. deleteAnalysis,
  27. deleteEvent,
  28. getAllTimeProjectActivity,
  29. } from '../api/projectActivity';
  30. import {
  31. useComponent,
  32. useTopLevelComponentKey,
  33. } from '../app/components/componentContext/withComponentContext';
  34. import { getBranchLikeQuery } from '../helpers/branch-like';
  35. import { parseDate } from '../helpers/dates';
  36. import { serializeStringArray } from '../helpers/query';
  37. import { BranchParameters } from '../types/branch-like';
  38. import { ParsedAnalysis } from '../types/project-activity';
  39. import { useBranchesQuery } from './branch';
  40. const ACTIVITY_PAGE_SIZE = 500;
  41. function useProjectActivityQueryKey() {
  42. const { component } = useComponent();
  43. const componentKey = useTopLevelComponentKey();
  44. const { data: { branchLike } = {} } = useBranchesQuery(component);
  45. const branchParams = getBranchLikeQuery(branchLike);
  46. return ['activity', 'list', componentKey, branchParams] as [
  47. string,
  48. string,
  49. string | undefined,
  50. BranchParameters,
  51. ];
  52. }
  53. export function useAllProjectAnalysesQuery(enabled = true) {
  54. const queryKey = useProjectActivityQueryKey();
  55. return useQuery({
  56. queryKey,
  57. queryFn: ({ queryKey: [_0, _1, project, branchParams] }) =>
  58. getAllTimeProjectActivity({
  59. ...branchParams,
  60. project,
  61. statuses: serializeStringArray([
  62. ProjectActivityStatuses.STATUS_PROCESSED,
  63. ProjectActivityStatuses.STATUS_LIVE_MEASURE_COMPUTE,
  64. ]),
  65. p: 1,
  66. ps: ACTIVITY_PAGE_SIZE,
  67. }).then(({ analyses }) =>
  68. analyses.map((analysis) => ({
  69. ...analysis,
  70. date: parseDate(analysis.date),
  71. })),
  72. ),
  73. enabled,
  74. });
  75. }
  76. export function useDeleteAnalysisMutation(successCb?: () => void) {
  77. const queryClient = useQueryClient();
  78. const queryKey = useProjectActivityQueryKey();
  79. return useMutation({
  80. mutationFn: (analysis: string) => deleteAnalysis(analysis),
  81. onSuccess: (_, analysis) => {
  82. queryClient.setQueryData(queryKey, (oldData: ParsedAnalysis[]) =>
  83. oldData.filter((a) => a.key !== analysis),
  84. );
  85. queryClient.invalidateQueries({ queryKey: ['measures', 'history', queryKey[2]] });
  86. successCb?.();
  87. },
  88. });
  89. }
  90. export function useCreateEventMutation(successCb?: () => void) {
  91. const queryClient = useQueryClient();
  92. const queryKey = useProjectActivityQueryKey();
  93. return useMutation({
  94. mutationFn: (data: Parameters<typeof createEvent>[0]) => createEvent(data),
  95. onSuccess: (event) => {
  96. queryClient.setQueryData(queryKey, (oldData: ParsedAnalysis[]) => {
  97. return oldData.map((analysis) => {
  98. if (analysis.key !== event.analysis) {
  99. return analysis;
  100. }
  101. return { ...analysis, events: [...analysis.events, event] };
  102. });
  103. });
  104. successCb?.();
  105. },
  106. });
  107. }
  108. export function useChangeEventMutation(successCb?: () => void) {
  109. const queryClient = useQueryClient();
  110. const queryKey = useProjectActivityQueryKey();
  111. return useMutation({
  112. mutationFn: (data: Parameters<typeof changeEvent>[0]) => changeEvent(data),
  113. onSuccess: (event) => {
  114. queryClient.setQueryData(queryKey, updateQueryDataOnChangeEvent(event));
  115. successCb?.();
  116. },
  117. });
  118. }
  119. const updateQueryDataOnChangeEvent =
  120. (event: CreateEventResponse) => (oldData: ParsedAnalysis[]) => {
  121. return oldData.map((a) => {
  122. if (a.key !== event.analysis) {
  123. return a;
  124. }
  125. return {
  126. ...a,
  127. events: a.events.map((e) => (e.key === event.key ? event : e)),
  128. };
  129. });
  130. };
  131. export function useDeleteEventMutation(successCb?: () => void) {
  132. const queryClient = useQueryClient();
  133. const queryKey = useProjectActivityQueryKey();
  134. return useMutation({
  135. mutationFn: ({ event }: { analysis: string; event: string }) => deleteEvent(event),
  136. onSuccess: (_, variables) => {
  137. queryClient.setQueryData(queryKey, updateQueryDataOnDeleteEvent(variables));
  138. successCb?.();
  139. },
  140. });
  141. }
  142. const updateQueryDataOnDeleteEvent =
  143. ({ analysis, event }: { analysis: string; event: string }) =>
  144. (oldData: ParsedAnalysis[]) => {
  145. return oldData.map((a) => {
  146. if (a.key !== analysis) {
  147. return a;
  148. }
  149. return {
  150. ...a,
  151. events: a.events.filter((ev) => ev.key !== event),
  152. };
  153. });
  154. };