From 7e534629ff56d651095d13fa2ae153273e3e4909 Mon Sep 17 00:00:00 2001 From: Vladyslav Matsiiako Date: Fri, 6 Jan 2023 17:32:19 -0800 Subject: [PATCH] Started adding telemetry to v2 routes --- .../src/controllers/v2/secretController.ts | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/backend/src/controllers/v2/secretController.ts b/backend/src/controllers/v2/secretController.ts index 3bb9f1720..f291dd1e7 100644 --- a/backend/src/controllers/v2/secretController.ts +++ b/backend/src/controllers/v2/secretController.ts @@ -9,6 +9,7 @@ import { AnyBulkWriteOperation } from 'mongodb'; import { SECRET_PERSONAL, SECRET_SHARED } from "../../variables"; import { validateMembership } from "../../helpers/membership"; import { ADMIN, MEMBER } from '../../variables'; +import { postHogClient } from '../../services'; export const createSingleSecret = async (req: Request, res: Response) => { const secretToCreate: CreateSecretRequestBody = req.body.secret; @@ -38,6 +39,20 @@ export const createSingleSecret = async (req: Request, res: Response) => { throw RouteValidationError({ message: error.message, stack: error.stack }) } + if (postHogClient) { + postHogClient.capture({ + event: 'secrets added', + distinctId: req.user.email, + properties: { + numberOfSecrets: 1, + environment: environmentName, + workspaceId, + // #TODO: why does this route have no channel? + // channel: channel ? channel : 'cli' + } + }); + } + res.status(200).send() } @@ -78,6 +93,20 @@ export const batchCreateSecrets = async (req: Request, res: Response) => { throw InternalServerError({ message: "Unable to process your batch create request. Please try again", stack: bulkCreateError.stack }) } + if (postHogClient) { + postHogClient.capture({ + event: 'secrets added', + distinctId: req.user.email, + properties: { + numberOfSecrets: (secretsToCreate ?? []).length, + environment: environmentName, + workspaceId, + // #TODO: why does this route have no channel? + // channel: channel ? channel : 'cli' + } + }); + } + res.status(200).send() } @@ -93,10 +122,12 @@ export const batchDeleteSecrets = async (req: Request, res: Response) => { const secretsUserCanDeleteSet: Set = new Set(secretIdsUserCanDelete.map(objectId => objectId._id.toString())); const deleteOperationsToPerform: AnyBulkWriteOperation[] = [] + let numSecretsDeleted = 0; secretIdsToDelete.forEach(secretIdToDelete => { if (secretsUserCanDeleteSet.has(secretIdToDelete)) { const deleteOperation = { deleteOne: { filter: { _id: new Types.ObjectId(secretIdToDelete) } } } deleteOperationsToPerform.push(deleteOperation) + numSecretsDeleted++; } else { throw RouteValidationError({ message: "You cannot delete secrets that you do not have access to" }) } @@ -110,6 +141,20 @@ export const batchDeleteSecrets = async (req: Request, res: Response) => { throw InternalServerError() } + if (postHogClient) { + postHogClient.capture({ + event: 'secrets deleted', + distinctId: req.user.email, + properties: { + numberOfSecrets: numSecretsDeleted, + environment: environmentName, + workspaceId, + // #TODO: why does this route have no channel? + // channel: channel ? channel : 'cli' + } + }); + } + res.status(200).send() } @@ -134,6 +179,21 @@ export const deleteSingleSecret = async (req: Request, res: Response) => { await Secret.findByIdAndDelete(secretId) + if (postHogClient) { + postHogClient.capture({ + event: 'secrets deleted', + distinctId: req.user.email, + properties: { + numberOfSecrets: 1, + // #TODO: how do we get env name ans project id? + // environment: environmentName, + // workspaceId, + // #TODO: why does this route have no channel? + // channel: channel ? channel : 'cli' + } + }); + } + res.status(200).send() } else { throw BadRequestError() @@ -184,9 +244,24 @@ export const batchModifySecrets = async (req: Request, res: Response) => { throw InternalServerError() } + if (postHogClient) { + postHogClient.capture({ + event: 'secrets modified', + distinctId: req.user.email, + properties: { + numberOfSecrets: (secretsModificationsRequested ?? []).length, + environment: environmentName, + workspaceId, + // #TODO: why does this route have no channel? + // channel: channel ? channel : 'cli' + } + }); + } + return res.status(200).send() } +// #TODO: I assume this should be '...Secret'? export const modifySingleSecrets = async (req: Request, res: Response) => { const { workspaceId, environmentName } = req.params const secretModificationsRequested: ModifySecretRequestBody = req.body.secret; @@ -216,6 +291,20 @@ export const modifySingleSecrets = async (req: Request, res: Response) => { throw RouteValidationError({ message: "Unable to apply modifications, please try again", stack: error.stack }) } + if (postHogClient) { + postHogClient.capture({ + event: 'secrets modified', + distinctId: req.user.email, + properties: { + numberOfSecrets: 1, + environment: environmentName, + workspaceId, + // #TODO: why does this route have no channel? + // channel: channel ? channel : 'cli' + } + }); + } + return res.status(200).send(singleModificationUpdate) } @@ -245,6 +334,20 @@ export const fetchAllSecrets = async (req: Request, res: Response) => { throw RouteValidationError({ message: "Unable to get secrets, please try again", stack: retriveAllSecretsError.stack }) } + if (postHogClient) { + postHogClient.capture({ + event: 'secrets pulled', + distinctId: req.user.email, + properties: { + numberOfSecrets: (allSecrets ?? []).length, + environment, + workspaceId, + // #TODO: why does this route have no channel? + // channel: channel ? channel : 'cli' + } + }); + } + return res.json(allSecrets) } @@ -270,6 +373,21 @@ export const fetchSingleSecret = async (req: Request, res: Response) => { res.json(singleSecretRetrieved) + if (postHogClient) { + postHogClient.capture({ + event: 'secrets pulled', + distinctId: req.user.email, + properties: { + numberOfSecrets: 1, + // #TODO: how do we get environment and workspace here? Do we need that? When is this route used? + // environment, + // workspaceId, + // #TODO: why does this route have no channel? + // channel: channel ? channel : 'cli' + } + }); + } + } else { throw BadRequestError() }