-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Modularise and document analytics endpoints (#2288)
* feat: Modularise analytics endpoints * docs: Improve response description
- Loading branch information
1 parent
330006e
commit 238b548
Showing
6 changed files
with
235 additions
and
61 deletions.
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,29 @@ | ||
openapi: 3.1.0 | ||
info: | ||
title: Plan✕ API | ||
version: 0.1.0 | ||
tags: | ||
- name: analytics | ||
components: | ||
responses: | ||
AnalyticsResponse: | ||
description: Successful response with no content. Not awaited from server as endpoint is called via the Beacon API | ||
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": | ||
$ref: "#/components/responses/AnalyticsResponse" | ||
/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": | ||
$ref: "#/components/responses/AnalyticsResponse" |
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