Skip to content

Commit

Permalink
thresholds reworked (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeknovy authored Apr 5, 2023
1 parent eb7cfe0 commit 5d67c77
Show file tree
Hide file tree
Showing 7 changed files with 464 additions and 195 deletions.
15 changes: 9 additions & 6 deletions src/server/controllers/item/shared/item-data-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import {
calculateApdexValues,
updateItemApdexSettings,
chartOverviewStatusCodesQuery,
responseTimePerLabelHistogram, findRawData,
responseTimePerLabelHistogram, findRawData, getBaselineItemWithStats,
} from "../../../queries/items"
import { ReportStatus } from "../../../queries/items.model"
import { getScenarioSettings, currentScenarioMetrics } from "../../../queries/scenario"
import { getScenarioSettings } from "../../../queries/scenario"
import { sendNotifications } from "../../../utils/notifications/send-notification"
import { scenarioThresholdsCalc } from "../utils/scenario-thresholds-calc"
import { extraIntervalMilliseconds } from "./extra-intervals-mapping"
Expand Down Expand Up @@ -122,10 +122,13 @@ export const itemDataProcessing = async ({ projectName, scenarioName, itemId })
overview.maxVu = Math.max(...chartData.threads.map(([, vu]) => vu))

if (scenarioSettings.thresholdEnabled) {
const scenarioMetrics = await db.one(currentScenarioMetrics(projectName, scenarioName, overview.maxVu))
const thresholdResult = scenarioThresholdsCalc(overview, scenarioMetrics, scenarioSettings)
if (thresholdResult) {
await db.none(saveThresholdsResult(projectName, scenarioName, itemId, thresholdResult))
logger.info("threshold comparison enabled, fetching baseline report")
const baselineReport = await db.oneOrNone(getBaselineItemWithStats(projectName, scenarioName))
if (baselineReport) {
const thresholdResult = scenarioThresholdsCalc(labelStats, baselineReport.stats, scenarioSettings)
if (thresholdResult) {
await db.none(saveThresholdsResult(projectName, scenarioName, itemId, thresholdResult))
}
}
}

Expand Down
68 changes: 53 additions & 15 deletions src/server/controllers/item/update-item-controller.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@
import { Request, Response, NextFunction } from "express"
import { updateTestItemInfo, removeCurrentBaseFlag, setBaseFlag } from "../../queries/items"
import {
updateTestItemInfo,
removeCurrentBaseFlag,
setBaseFlag,
findItemStats, saveThresholdsResult, findItemsWithThresholds,
} from "../../queries/items"
import { db } from "../../../db/db"
import { StatusCode } from "../../utils/status-code"
import { scenarioThresholdsCalc } from "./utils/scenario-thresholds-calc"
import { getScenarioSettings } from "../../queries/scenario"
import { logger } from "../../../logger"


export const updateItemController = async (req: Request, res: Response, next: NextFunction) => {
const { projectName, scenarioName, itemId } = req.params
const { note, environment, hostname, base, name } = req.body
try {
await db.query("BEGIN")
await db.none(updateTestItemInfo(itemId, scenarioName, projectName, note, environment, hostname, name))
if (base) {
await db.none(removeCurrentBaseFlag(scenarioName, projectName))
await db.none(setBaseFlag(itemId, scenarioName, projectName))
const { projectName, scenarioName, itemId } = req.params
const { note, environment, hostname, base, name } = req.body
try {
await db.query("BEGIN")
await db.none(updateTestItemInfo(itemId, scenarioName, projectName, note, environment, hostname, name))
if (base) {
await db.none(removeCurrentBaseFlag(scenarioName, projectName))
await db.none(setBaseFlag(itemId, scenarioName, projectName))
const scenarioSettings = await db.one(getScenarioSettings(projectName, scenarioName))
if (scenarioSettings.thresholdEnabled) {
logger.info("Thresholds enabled, searching for any connected reports")
// items
const itemsWithThresholds = await db.manyOrNone(findItemsWithThresholds(projectName, scenarioName))

if (itemsWithThresholds && itemsWithThresholds.length > 0) {
logger.info("Items with thresholds that need to be updated found")
const { stats: newBaseLineReport } = await db.one(findItemStats(itemId))

// eslint-disable-next-line max-depth
for (const itemIdToBeRecalculated of itemsWithThresholds) {
logger.info(`About to re-calculate threshold for item: ${itemIdToBeRecalculated.id}`)

const { stats: statsToBeRecalculatedItem } = await db.one(
findItemStats(itemIdToBeRecalculated.id))

const updatedThreshold = scenarioThresholdsCalc(statsToBeRecalculatedItem,
newBaseLineReport, scenarioSettings)
// eslint-disable-next-line max-depth
if (updatedThreshold) {
// eslint-disable-next-line max-len
logger.info(`About to save re-calculated threshold values for item: ${itemIdToBeRecalculated.id}`)
await db.none(saveThresholdsResult(projectName, scenarioName,
itemIdToBeRecalculated.id, updatedThreshold))
}
}
}
}

}
await db.query("COMMIT")
res.status(StatusCode.NoContent).send()
} catch(error) {
await db.query("ROLLBACK")
return next(error)
}
await db.query("COMMIT")
res.status(StatusCode.NoContent).send()
} catch(error) {
await db.query("ROLLBACK")
return next(error)
}
}
Loading

0 comments on commit 5d67c77

Please sign in to comment.