Skip to content

Commit

Permalink
setting retention period on scenario upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeknovy committed Nov 30, 2023
1 parent 6d96734 commit 396c69e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/server/controllers/item/create-item-async-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { generateShareToken } from "./utils/generateShareToken"
import { upsertScenario } from "./shared/upsert-scenario"

export const createItemAsyncController = async (req: IGetUserAuthInfoRequest, res: Response) => {
const { environment, note, status = ItemStatus.None, hostname, name, resourcesLink } = req.body
const { environment, note, status = ItemStatus.None, hostname, name, resourcesLink, keepTestRunsPeriod } = req.body
const { scenarioName, projectName } = req.params

logger.info(`Creating new item for scenario: ${scenarioName}`)
try {
const scenarioExists = await upsertScenario(projectName, scenarioName)
const scenarioExists = await upsertScenario(projectName, scenarioName, keepTestRunsPeriod)
if (!scenarioExists) {
return res.status(StatusCode.NotFound).json({ message: "scenario not found" })
}
Expand Down
27 changes: 25 additions & 2 deletions src/server/controllers/item/create-item-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
TEST_NAME_MAX_LENGTH,
} from "./create-item-const"
import { AnalyticsEvent } from "../../utils/analytics/anyltics-event"
import {ALLOWED_PERDIOD} from "./shared/constants";

Check failure on line 27 in src/server/controllers/item/create-item-controller.ts

View workflow job for this annotation

GitHub Actions / build

A space is required after '{'

Check failure on line 27 in src/server/controllers/item/create-item-controller.ts

View workflow job for this annotation

GitHub Actions / build

A space is required before '}'

Check failure on line 27 in src/server/controllers/item/create-item-controller.ts

View workflow job for this annotation

GitHub Actions / build

Extra semicolon

const pg = pgp()

Expand All @@ -42,7 +43,7 @@ const SECONDS_DIVISOR = 1000
export const createItemController = (req: IGetUserAuthInfoRequest, res: Response, next: NextFunction) => {
// eslint-disable-next-line complexity
upload(req, res, async error => {
const { environment, note, status = ItemStatus.None, hostname, name, resourcesLink } = req.body
const { environment, note, status = ItemStatus.None, hostname, name, resourcesLink, keepTestRunsPeriod } = req.body
if (!req.files) {
return next(boom.badRequest())
}
Expand All @@ -60,6 +61,12 @@ export const createItemController = (req: IGetUserAuthInfoRequest, res: Response
if (!Object.values(ItemStatus).some(_ => _ === status)) {
return next(boom.badRequest("invalid status type"))
}
if (keepTestRunsPeriod) {
const keepTestRunsPeriodError = checkKeepTestRunsPeriod(keepTestRunsPeriod)
if (keepTestRunsPeriodError) {
return next(keepTestRunsPeriodError)
}
}
const failedValidations = checkFieldsLength([
{ name: "environment", value: environment, maxLength: ENVIRONMENT_MAX_LENGTH },
{ name: "name", value: name, maxLength: TEST_NAME_MAX_LENGTH },
Expand All @@ -79,7 +86,7 @@ export const createItemController = (req: IGetUserAuthInfoRequest, res: Response
const monitoringFileName = monitoring?.[0]?.path
let tempBuffer = []

await upsertScenario(projectName, scenarioName)
await upsertScenario(projectName, scenarioName, keepTestRunsPeriod)

const item = await db.one(createNewItem(
scenarioName,
Expand Down Expand Up @@ -227,3 +234,19 @@ const checkFieldsLength = (properties: Array<{ name: string; value: any; maxLeng
})
return failedValidations
}

export const checkKeepTestRunsPeriod = (keepTestRunsPeriod): boom => {
try {
const keepTestRunsPeriodNumber = parseInt(keepTestRunsPeriod, 10)
if (isNaN(keepTestRunsPeriodNumber)) {
return boom.badRequest("keepTestRunsPeriod - only numbers are allowed")
}
const allowedValue = ALLOWED_PERDIOD.find(val => val === keepTestRunsPeriodNumber)
if (!allowedValue) {
return boom.badRequest(`invalid value, allowed values are: ${ALLOWED_PERDIOD}`)
}
return
} catch(e) {
return boom.badRequest("keepTestRunsPeriod - only numbers are allowed")
}
}
1 change: 1 addition & 0 deletions src/server/controllers/item/shared/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ALLOWED_PERDIOD = [7, 14, 30, 90, 180]

Check failure on line 1 in src/server/controllers/item/shared/constants.ts

View workflow job for this annotation

GitHub Actions / build

No magic number: 7

Check failure on line 1 in src/server/controllers/item/shared/constants.ts

View workflow job for this annotation

GitHub Actions / build

No magic number: 14

Check failure on line 1 in src/server/controllers/item/shared/constants.ts

View workflow job for this annotation

GitHub Actions / build

No magic number: 30

Check failure on line 1 in src/server/controllers/item/shared/constants.ts

View workflow job for this annotation

GitHub Actions / build

No magic number: 90

Check failure on line 1 in src/server/controllers/item/shared/constants.ts

View workflow job for this annotation

GitHub Actions / build

No magic number: 180
4 changes: 2 additions & 2 deletions src/server/controllers/item/shared/upsert-scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getProject } from "../../../queries/projects"
import { createNewScenario, getScenario } from "../../../queries/scenario"
import { logger } from "../../../../logger"

export const upsertScenario = async (projectName, scenarioName) => {
export const upsertScenario = async (projectName, scenarioName, keepTestRunsPeriod: number) => {
const scenario = await db.oneOrNone(getScenario(projectName, scenarioName))
if (scenario) {
// scenario already exists
Expand All @@ -12,7 +12,7 @@ export const upsertScenario = async (projectName, scenarioName) => {
const project = await db.one(getProject(projectName))
if (project.upsertScenario) {
logger.info(`Creating new scenario "${scenarioName}" into project "${projectName}"`)
await db.query(createNewScenario(projectName, scenarioName))
await db.query(createNewScenario(projectName, scenarioName, keepTestRunsPeriod))
return true
}
return false
Expand Down
8 changes: 4 additions & 4 deletions src/server/queries/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ export const deleteScenario = (projectName, scenarioName) => {
}
}

export const createNewScenario = (projectName, scenarioName) => {
export const createNewScenario = (projectName, scenarioName, keepTestRunsPeriod = 0) => {
return {
text: `INSERT INTO jtl.scenario(name, project_id, analysis_enabled) VALUES($2, (
text: `INSERT INTO jtl.scenario(name, project_id, analysis_enabled, keep_test_runs_period) VALUES($2, (
SELECT id FROM jtl.projects WHERE project_name = $1
), $3)`,
values: [projectName, scenarioName, true],
), $3, $4)`,
values: [projectName, scenarioName, true, keepTestRunsPeriod],
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/server/schema-validator/item-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
NOTE_MAX_LENGTH,
RESOURCES_LINK_MAX_LENGTH, TEST_NAME_MAX_LENGTH,
} from "../controllers/item/create-item-const"
import {ALLOWED_PERDIOD} from "../controllers/item/shared/constants";

Check failure on line 8 in src/server/schema-validator/item-schema.ts

View workflow job for this annotation

GitHub Actions / build

A space is required after '{'

Check failure on line 8 in src/server/schema-validator/item-schema.ts

View workflow job for this annotation

GitHub Actions / build

A space is required before '}'


const projectName = Joi.string().required()
Expand Down Expand Up @@ -55,6 +56,7 @@ export const newAsyncItemStartBodySchema = Joi.object().keys({
hostname,
note,
resourcesLink,
keepTestRunsPeriod: Joi.number().valid(ALLOWED_PERDIOD),
})

export const newItemParamSchema = Joi.object().keys({
Expand Down

0 comments on commit 396c69e

Please sign in to comment.