Skip to content

Commit

Permalink
fix(insights): Move from GET to POST queries (#19859)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra authored Feb 12, 2024
1 parent a0fcba4 commit de278a2
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 15 deletions.
8 changes: 4 additions & 4 deletions cypress/productAnalytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export const savedInsights = {
}

export function interceptInsightLoad(insightType: string): string {
cy.intercept('GET', /api\/projects\/\d+\/insights\/trend\/\?.*/).as('loadNewTrendsInsight')
cy.intercept('POST', /api\/projects\/\d+\/insights\/funnel\/?/).as('loadNewFunnelInsight')
cy.intercept('GET', /api\/projects\/\d+\/insights\/retention\/\?.*/).as('loadNewRetentionInsight')
cy.intercept('POST', /api\/projects\/\d+\/insights\/path\/?/).as('loadNewPathsInsight')
cy.intercept('POST', /api\/projects\/\d+\/insights\/trend\//).as('loadNewTrendsInsight')
cy.intercept('POST', /api\/projects\/\d+\/insights\/funnel\//).as('loadNewFunnelInsight')
cy.intercept('POST', /api\/projects\/\d+\/insights\/retention\//).as('loadNewRetentionInsight')
cy.intercept('POST', /api\/projects\/\d+\/insights\/path\//).as('loadNewPathsInsight')
cy.intercept('POST', /api\/projects\/\d+\/query\//).as('loadNewQueryInsight')

let networkInterceptAlias: string = ''
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 29 additions & 9 deletions frontend/src/queries/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,25 @@ export function legacyInsightQueryURL({ filters, currentTeamId, refresh }: Legac
}
}

export function legacyInsightQueryData({
filters,
currentTeamId,
refresh,
}: LegacyInsightQueryParams): [string, Record<string, any>] {
const baseUrl = `api/projects/${currentTeamId}/insights`

if (isTrendsFilter(filters) || isStickinessFilter(filters) || isLifecycleFilter(filters)) {
return [`${baseUrl}/trend/`, { ...filterTrendsClientSideParams(filters), refresh }]
} else if (isRetentionFilter(filters)) {
return [`${baseUrl}/retention/`, { ...filters, refresh }]
} else if (isFunnelsFilter(filters)) {
return [`${baseUrl}/funnel/`, { ...filters, refresh }]
} else if (isPathsFilter(filters)) {
return [`${baseUrl}/path/`, { ...filters, refresh }]
}
throw new Error(`Unsupported insight type: ${filters.insight}`)
}

export function legacyInsightQueryExportContext({
filters,
currentTeamId,
Expand Down Expand Up @@ -442,16 +461,17 @@ export async function legacyInsightQuery({
methodOptions,
refresh,
}: LegacyInsightQueryParams): Promise<[Response, string]> {
const apiUrl = legacyInsightQueryURL({ filters, currentTeamId, refresh })
const [apiUrl, data] = legacyInsightQueryData({ filters, currentTeamId, refresh })
let fetchResponse: Response
if (isTrendsFilter(filters) || isStickinessFilter(filters) || isLifecycleFilter(filters)) {
fetchResponse = await api.getResponse(apiUrl, methodOptions)
} else if (isRetentionFilter(filters)) {
fetchResponse = await api.getResponse(apiUrl, methodOptions)
} else if (isFunnelsFilter(filters)) {
fetchResponse = await api.createResponse(apiUrl, filters, methodOptions)
} else if (isPathsFilter(filters)) {
fetchResponse = await api.createResponse(apiUrl, filters, methodOptions)
if (
isTrendsFilter(filters) ||
isStickinessFilter(filters) ||
isLifecycleFilter(filters) ||
isRetentionFilter(filters) ||
isFunnelsFilter(filters) ||
isPathsFilter(filters)
) {
fetchResponse = await api.createResponse(apiUrl, data, methodOptions)
} else {
throw new Error(`Unsupported insight type: ${filters.insight}`)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export const ValidationError: StoryFn = () => {
ctx.status(200),
ctx.json({ count: 1, results: [{ ...insight, result: null }] }),
],
},
post: {
'/api/projects/:team_id/insights/:id': (_, __, ctx) => [
ctx.delay(100),
ctx.status(400),
Expand All @@ -92,6 +94,8 @@ export const EstimatedQueryExecutionTimeTooLong: StoryFn = () => {
ctx.status(200),
ctx.json({ count: 1, results: [{ ...insight, result: null }] }),
],
},
post: {
'/api/projects/:team_id/insights/trend/': (_, __, ctx) => [
ctx.delay(100),
ctx.status(512),
Expand Down Expand Up @@ -120,6 +124,8 @@ export const LongLoading: StoryFn = () => {
ctx.status(200),
ctx.json({ count: 1, results: [{ ...insight, result: null }] }),
],
},
post: {
'/api/projects/:team_id/insights/trend/': (_, __, ctx) => [
ctx.delay(86400000),
ctx.status(200),
Expand Down
4 changes: 2 additions & 2 deletions posthog/api/insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ def calculate_funnel(self, request: request.Request) -> Dict[str, Any]:
# - start_entity: (dict) specifies id and type of the entity to focus retention on
# - **shared filter types
# ******************************************
@action(methods=["GET"], detail=False)
@action(methods=["GET", "POST"], detail=False)
def retention(self, request: request.Request, *args: Any, **kwargs: Any) -> Response:
timings = HogQLTimings()
try:
Expand All @@ -965,7 +965,7 @@ def retention(self, request: request.Request, *args: Any, **kwargs: Any) -> Resp
def calculate_retention(self, request: request.Request) -> Dict[str, Any]:
team = self.team
data = {}
if not request.GET.get("date_from"):
if not request.GET.get("date_from") and not request.data.get("date_from"):
data.update({"date_from": "-11d"})
filter = RetentionFilter(data=data, request=request, team=self.team)
base_uri = request.build_absolute_uri("/")
Expand Down

0 comments on commit de278a2

Please sign in to comment.