-
Notifications
You must be signed in to change notification settings - Fork 22
refactor(analytics): replace useGetAnalytics with individual queries for decision outcomes, rule hits, and screening hits #1203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
packages/app-builder/src/queries/analytics/factory/create-analytics-query.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { useAgnosticNavigation } from '@app-builder/contexts/AgnosticNavigationContext'; | ||
| import { type AnalyticsFiltersQuery } from '@app-builder/models/analytics'; | ||
| import { getRoute } from '@app-builder/utils/routes'; | ||
| import { keepPreviousData, useQuery } from '@tanstack/react-query'; | ||
|
|
||
| export function createAnalyticsQuery<TData>(queryName: string) { | ||
| return ({ scenarioId, queryString }: { scenarioId: string; queryString: string }) => { | ||
| const navigate = useAgnosticNavigation(); | ||
| const endpoint = getRoute('/ressources/analytics/:scenarioId/query/:queryName', { | ||
| scenarioId, | ||
| queryName, | ||
| }); | ||
| console.log('endpoint', endpoint); | ||
| const qs = queryString ? atob(queryString) : null; | ||
| const parsed: AnalyticsFiltersQuery = JSON.parse(qs || '{}'); | ||
|
|
||
| const { range, compareRange, scenarioVersion, trigger } = parsed; | ||
|
|
||
| return useQuery({ | ||
| queryKey: ['analytics', 'query', scenarioId, queryName, queryString], | ||
| enabled: Boolean(qs && range), | ||
| queryFn: async () => { | ||
| const response = await fetch(endpoint, { | ||
| method: 'POST', | ||
| body: JSON.stringify({ | ||
| scenarioVersion, | ||
| range, | ||
| compareRange, | ||
| trigger, | ||
| }), | ||
| }); | ||
|
|
||
| const responseData = (await response.json()) as { redirectTo: string } | { data: TData }; | ||
|
|
||
| if ('redirectTo' in responseData) { | ||
| navigate(responseData.redirectTo); | ||
| return; | ||
| } | ||
|
|
||
| return responseData.data; | ||
| }, | ||
| placeholderData: keepPreviousData, | ||
| }); | ||
| }; | ||
| } |
57 changes: 0 additions & 57 deletions
57
packages/app-builder/src/queries/analytics/get-analytics.ts
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
packages/app-builder/src/queries/analytics/get-decisions-outcomes-per-day.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { DecisionOutcomesPerPeriod } from '@app-builder/models/analytics'; | ||
| import { createAnalyticsQuery } from './factory/create-analytics-query'; | ||
|
|
||
| export const useGetDecisionsOutcomesPerDay = | ||
| createAnalyticsQuery<DecisionOutcomesPerPeriod>('decision-outcomes-per-day'); |
4 changes: 4 additions & 0 deletions
4
packages/app-builder/src/queries/analytics/get-rule-hit-table.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { RuleHitTableResponse } from '@app-builder/models/analytics/rule-hit'; | ||
| import { createAnalyticsQuery } from './factory/create-analytics-query'; | ||
|
|
||
| export const useGetRuleHitTable = createAnalyticsQuery<RuleHitTableResponse[]>('rule-hit-table'); |
4 changes: 4 additions & 0 deletions
4
packages/app-builder/src/queries/analytics/get-rule-vs-decision-outcome.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { RuleVsDecisionOutcome } from '@app-builder/models/analytics'; | ||
| import { createAnalyticsQuery } from './factory/create-analytics-query'; | ||
|
|
||
| export const useGetRuleVsDecisionOutcome = createAnalyticsQuery<RuleVsDecisionOutcome[]>('rule-vs-decision-outcome'); |
4 changes: 4 additions & 0 deletions
4
packages/app-builder/src/queries/analytics/get-screening-hits-table.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { ScreeningHitTableResponse } from '@app-builder/models/analytics'; | ||
| import { createAnalyticsQuery } from './factory/create-analytics-query'; | ||
|
|
||
| export const useGetScreeningHitsTable = createAnalyticsQuery<ScreeningHitTableResponse[]>('screening-hits-table'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export * from './get-available-filters'; | ||
| export * from './get-decisions-outcomes-per-day'; | ||
| export * from './get-rule-hit-table'; | ||
| export * from './get-rule-vs-decision-outcome'; | ||
| export * from './get-screening-hits-table'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/app-builder/src/routes/ressources+/analytics+/$scenarioId+/query.$queryName.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { createServerFn } from '@app-builder/core/requests'; | ||
| import { authMiddleware } from '@app-builder/middlewares/auth-middleware'; | ||
| import { handleRedirectMiddleware } from '@app-builder/middlewares/handle-redirect-middleware'; | ||
| import { analyticsQuery } from '@app-builder/models/analytics'; | ||
| import invariant from 'tiny-invariant'; | ||
| import { z } from 'zod'; | ||
|
|
||
| const urlParamsSchema = z.object({ | ||
| scenarioId: z.uuidv4(), | ||
| queryName: z.string(), | ||
| }); | ||
|
|
||
| export const action = createServerFn( | ||
| [handleRedirectMiddleware, authMiddleware], | ||
| async function createAnalyticsQueryAction({ params, request, context }) { | ||
| try { | ||
| const urlParams = urlParamsSchema.parse(params); | ||
| invariant(urlParams.queryName, 'queryName is required'); | ||
| console.log('urlParams', urlParams); | ||
|
|
||
| const body = await request.json(); | ||
|
|
||
| const queryParams = analyticsQuery.parse({ | ||
| ...body, | ||
| scenarioId: urlParams.scenarioId, | ||
| }); | ||
|
|
||
| switch (urlParams.queryName) { | ||
| case 'decision-outcomes-per-day': { | ||
| const data = await context.authInfo.analytics.getDecisionOutcomesPerDay({ | ||
| ...queryParams, | ||
| scenarioId: urlParams.scenarioId, | ||
| }); | ||
| return { | ||
| success: true, | ||
| data, | ||
| }; | ||
| } | ||
|
|
||
| case 'rule-hit-table': { | ||
| const data = await context.authInfo.analytics.getRuleHitTable({ | ||
| ...queryParams, | ||
| scenarioId: urlParams.scenarioId, | ||
| }); | ||
| return { | ||
| success: true, | ||
| data, | ||
| }; | ||
| } | ||
|
|
||
| case 'rule-vs-decision-outcome': { | ||
| const data = await context.authInfo.analytics.getRuleVsDecisionOutcome({ | ||
| ...queryParams, | ||
| scenarioId: urlParams.scenarioId, | ||
| }); | ||
| return { | ||
| success: true, | ||
| data, | ||
| }; | ||
| } | ||
|
|
||
| case 'screening-hits-table': { | ||
| const data = await context.authInfo.analytics.getScreeningHitsTable({ | ||
| ...queryParams, | ||
| scenarioId: urlParams.scenarioId, | ||
| }); | ||
| return { | ||
| success: true, | ||
| data, | ||
| }; | ||
| } | ||
|
|
||
| default: { | ||
| return { | ||
| success: false, | ||
| errors: 'Invalid query name', | ||
| }; | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.error('error in analytics query', error); | ||
| return { success: false, errors: 'Internal server error' }; | ||
| } | ||
| }, | ||
| ); |
68 changes: 0 additions & 68 deletions
68
packages/app-builder/src/routes/ressources+/analytics+/$scenarioId+/query.ts
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The destructuring is not consistent with other uses of useQuery and also makes you alias field. It would be better to keep variables like
decisionsOutcomesPerDayQuery