-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: Modularise and document analytics endpoints #2288
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains 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,26 @@ | ||
import { z } from "zod"; | ||
import { trackAnalyticsLogExit } from "./service"; | ||
import { ValidatedRequestHandler } from "../../shared/middleware/validate"; | ||
|
||
export const logAnalyticsSchema = z.object({ | ||
query: z.object({ | ||
analyticsLogId: z.string(), | ||
}), | ||
}); | ||
|
||
export type LogAnalytics = ValidatedRequestHandler< | ||
typeof logAnalyticsSchema, | ||
Record<string, never> | ||
>; | ||
|
||
export const logUserExitController: LogAnalytics = async (req, res) => { | ||
const { analyticsLogId } = req.query; | ||
trackAnalyticsLogExit({ id: Number(analyticsLogId), isUserExit: true }); | ||
res.status(204).send(); | ||
}; | ||
|
||
export const logUserResumeController: LogAnalytics = async (req, res) => { | ||
const { analyticsLogId } = req.query; | ||
trackAnalyticsLogExit({ id: Number(analyticsLogId), isUserExit: false }); | ||
res.status(204).send(); | ||
}; |
This file contains 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,25 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: Plan✕ API | ||
version: 0.1.0 | ||
tags: | ||
- name: analytics | ||
paths: | ||
/analytics/log-user-exit: | ||
post: | ||
summary: Log user exit | ||
description: Capture an analytic event which represents a user exiting a service | ||
tags: | ||
- analytics | ||
responses: | ||
"204": | ||
description: Successful response - no awaited response from server | ||
/analytics/log-user-resume: | ||
post: | ||
summary: Log user resume | ||
description: Capture an analytic event which represents a user resuming a service | ||
tags: | ||
- analytics | ||
responses: | ||
"204": | ||
description: Successful response - no awaited response from server | ||
This file contains 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,99 @@ | ||
import supertest from "supertest"; | ||
import app from "../../server"; | ||
import { queryMock } from "../../tests/graphqlQueryMock"; | ||
|
||
describe("Logging analytics", () => { | ||
beforeEach(() => { | ||
queryMock.mockQuery({ | ||
name: "SetAnalyticsEndedDate", | ||
matchOnVariables: false, | ||
data: { | ||
update_analytics_by_pk: { | ||
id: 12345, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it("validates that analyticsLogId is present in the query", async () => { | ||
await supertest(app) | ||
.post("/analytics/log-user-exit") | ||
.query({}) | ||
.expect(400) | ||
.then((res) => { | ||
expect(res.body).toHaveProperty("issues"); | ||
expect(res.body).toHaveProperty("name", "ZodError"); | ||
}); | ||
}); | ||
|
||
it("logs a user exit", async () => { | ||
queryMock.mockQuery({ | ||
name: "UpdateAnalyticsLogUserExit", | ||
variables: { | ||
id: 123, | ||
user_exit: true, | ||
}, | ||
data: { | ||
update_analytics_logs_by_pk: { | ||
analytics_id: 12345, | ||
}, | ||
}, | ||
}); | ||
|
||
await supertest(app) | ||
.post("/analytics/log-user-exit") | ||
.query({ analyticsLogId: "123" }) | ||
.expect(204) | ||
.then((res) => { | ||
expect(res.body).toEqual({}); | ||
}); | ||
}); | ||
|
||
it("logs a user resume", async () => { | ||
queryMock.mockQuery({ | ||
name: "UpdateAnalyticsLogUserExit", | ||
variables: { | ||
id: 456, | ||
user_exit: false, | ||
}, | ||
data: { | ||
update_analytics_logs_by_pk: { | ||
analytics_id: 12345, | ||
}, | ||
}, | ||
}); | ||
|
||
await supertest(app) | ||
.post("/analytics/log-user-resume") | ||
.query({ analyticsLogId: "456" }) | ||
.expect(204) | ||
.then((res) => { | ||
expect(res.body).toEqual({}); | ||
}); | ||
}); | ||
|
||
it("handles errors whilst writing analytics records", async () => { | ||
queryMock.mockQuery({ | ||
name: "UpdateAnalyticsLogUserExit", | ||
matchOnVariables: false, | ||
data: { | ||
update_analytics_logs_by_pk: { | ||
analytics_id: 12345, | ||
}, | ||
}, | ||
graphqlErrors: [ | ||
{ | ||
message: "Something went wrong", | ||
}, | ||
], | ||
}); | ||
|
||
await supertest(app) | ||
.post("/analytics/log-user-resume") | ||
.query({ analyticsLogId: "456" }) | ||
.expect(204) | ||
.then((res) => { | ||
expect(res.body).toEqual({}); | ||
}); | ||
}); | ||
}); |
This file contains 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,22 @@ | ||
import { validate } from "./../../shared/middleware/validate"; | ||
import { Router } from "express"; | ||
import { | ||
logAnalyticsSchema, | ||
logUserExitController, | ||
logUserResumeController, | ||
} from "./controller"; | ||
|
||
const router = Router(); | ||
|
||
router.post( | ||
"/log-user-exit", | ||
validate(logAnalyticsSchema), | ||
logUserExitController, | ||
); | ||
router.post( | ||
"/log-user-resume", | ||
validate(logAnalyticsSchema), | ||
logUserResumeController, | ||
); | ||
|
||
export default router; |
This file contains 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,57 @@ | ||
import { gql } from "graphql-request"; | ||
import { adminGraphQLClient as adminClient } from "../../hasura"; | ||
|
||
export const trackAnalyticsLogExit = async ({ | ||
id, | ||
isUserExit, | ||
}: { | ||
id: number; | ||
isUserExit: boolean; | ||
}) => { | ||
try { | ||
const result = await adminClient.request( | ||
gql` | ||
mutation UpdateAnalyticsLogUserExit($id: bigint!, $user_exit: Boolean) { | ||
update_analytics_logs_by_pk( | ||
pk_columns: { id: $id } | ||
_set: { user_exit: $user_exit } | ||
) { | ||
id | ||
user_exit | ||
analytics_id | ||
} | ||
} | ||
`, | ||
{ | ||
id, | ||
user_exit: isUserExit, | ||
}, | ||
); | ||
|
||
const analyticsId = result.update_analytics_logs_by_pk.analytics_id; | ||
await adminClient.request( | ||
gql` | ||
mutation SetAnalyticsEndedDate($id: bigint!, $ended_at: timestamptz) { | ||
update_analytics_by_pk( | ||
pk_columns: { id: $id } | ||
_set: { ended_at: $ended_at } | ||
) { | ||
id | ||
} | ||
} | ||
`, | ||
{ | ||
id: analyticsId, | ||
ended_at: isUserExit ? new Date().toISOString() : null, | ||
}, | ||
); | ||
} catch (e) { | ||
// We need to catch this exception here otherwise the exception would become an unhandled rejection which brings down the whole node.js process | ||
console.error( | ||
"There's been an error while recording metrics for analytics but because this thread is non-blocking we didn't reject the request", | ||
(e as Error).stack, | ||
); | ||
} | ||
|
||
return; | ||
}; |
This file contains 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
Oops, something went wrong.
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.
nit: can either the post description or response description be tweaked to include "via the Beacon API" or similar so it's more clear why our "success" here is no awaited response?
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.
Very good catch - change made! Will merge when CI is happy 👌