Skip to content

Commit

Permalink
(feature) add script for generating alert
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilShahi committed Sep 13, 2022
1 parent 2a69c52 commit 48af715
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dev-collector": "nodemon -r tsconfig-paths/register src/collector.ts",
"start-collector": "TS_NODE_BASEURL=./dist/ node -r tsconfig-paths/register dist/collector.js",
"sync-endpoints": "ts-node -r tsconfig-paths/register src/scripts/generate-endpoints.ts",
"generate-alerts": "ts-node -r tsconfig-paths/register src/scripts/generate-alerts.ts",
"format": "prettier --write './src/**/*.{ts,tsx}'"
},
"keywords": [],
Expand Down Expand Up @@ -55,7 +56,8 @@
"typeorm": "^0.3.7",
"uuid": "^8.3.2",
"validator": "^13.7.0",
"whatwg-mimetype": "^3.0.0"
"whatwg-mimetype": "^3.0.0",
"yargs": "^17.5.1"
},
"devDependencies": {
"@types/express": "^4.17.13",
Expand Down
37 changes: 37 additions & 0 deletions backend/src/scripts/generate-alerts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import yargs from "yargs"
import { AlertType } from "@common/enums"
import { ALERT_TYPE_TO_RISK_SCORE } from "@common/maps"
import { AppDataSource } from "data-source"
import { Alert } from "models"
import { DatabaseService } from "services/database"

const generateAlert = async (alertType: string, apiEndpointUuid: string, description: string, context: any) => {
try{
const newAlert = new Alert()
newAlert.apiEndpointUuid = apiEndpointUuid
newAlert.type = AlertType[alertType]
newAlert.description = description
newAlert.riskScore = ALERT_TYPE_TO_RISK_SCORE[AlertType[alertType]]
newAlert.context = JSON.parse(context || "{}")
await DatabaseService.executeTransactions([[newAlert]], [], false)
} catch (err) {
console.error(`Error generating new alert from script: ${err}`)
}
}

const main = async () => {
const datasource = await AppDataSource.initialize()
if (!datasource.isInitialized) {
console.error("Couldn't initialize datasource...")
return
}
console.log("AppDataSource Initialized...")
const args = yargs.argv
const alertType = args["alertType"]
const apiEndpointUuid = args["endpointUuid"]
const description = args["description"]
const context = args["context"]
await generateAlert(alertType, apiEndpointUuid, description, context)
}

main()
2 changes: 1 addition & 1 deletion backend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4011,7 +4011,7 @@ yargs@^16.0.0, yargs@^16.2.0:
y18n "^5.0.5"
yargs-parser "^20.2.2"

yargs@^17.3.1:
yargs@^17.3.1, yargs@^17.5.1:
version "17.5.1"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.5.1.tgz#e109900cab6fcb7fd44b1d8249166feb0b36e58e"
integrity sha512-t6YAJcxDkNX7NFYiVtKvWUz8l+PaKTLiL63mJYWR2GnHq2gjEWISzsLp9wg3aY36dY1j+gfIEL3pIF+XlJJfbA==
Expand Down

0 comments on commit 48af715

Please sign in to comment.