Skip to content

Commit

Permalink
fix: dedup changes (#115)
Browse files Browse the repository at this point in the history
Signed-off-by: shivamsouravjha <[email protected]>
  • Loading branch information
shivamsouravjha authored Jul 15, 2024
1 parent 03bae50 commit 27b2bf3
Showing 1 changed file with 50 additions and 58 deletions.
108 changes: 50 additions & 58 deletions v2/dedup/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ const yaml = require('js-yaml');

const filePath = 'dedupData.yaml';


// middleware
export default function middleware(

): (req: Request, res: Response, next: NextFunction) => void {
export default function middleware(): (req: Request, res: Response, next: NextFunction) => void {
// console.log("Inside middleware...");

// @ts-ignore
Expand All @@ -22,15 +19,12 @@ export default function middleware(
});
return (req: Request, res: Response, next: NextFunction) => {
res.on("finish", () => {

afterMiddleware(req, res);
});
next();

};
}


export function afterMiddleware(req: Request, res: Response) {
let id = req.get("KEPLOY-TEST-ID");
if (!id) {
Expand All @@ -44,7 +38,6 @@ export function afterMiddleware(req: Request, res: Response) {
executedLinesByFile: executedLinesByFile
};


let existingData = [];

try {
Expand All @@ -55,7 +48,6 @@ export function afterMiddleware(req: Request, res: Response) {
console.error("Error reading existing file:", error);
}


if (!Array.isArray(existingData)) {
console.error('Expected an array for existingData, but got:', typeof existingData);
existingData = []; // Reset to an empty array or handle accordingly
Expand All @@ -75,58 +67,58 @@ export function afterMiddleware(req: Request, res: Response) {
// console.log("Data has been appended and logged to", filePath);
}

// isJsonValid checks whether o is a valid JSON or not

let count = 0;
const executedLinebyEachTest = new Array();
function GetCoverage() {
console.log("Calculating per request coverage...");
count++;
let executedLinesByFile = {};
// iterate over global.__coverage__
// @ts-ignore
for (const filename in global.__coverage__) {
// console.log("FIlenamae", filename);
// while (1) {
// @ts-ignore
let coverageData = global.__coverage__[filename];
// console.log("Inside GetCoverage " + count);
// console.log(coverageData);


// for (const filePath of Object.keys(coverageData)) {
const executedLines = new Set();
const fileCoverage = coverageData;
const statementMap = fileCoverage.statementMap;
const hitCounts = fileCoverage.s;
if (count > 1) {
// iterate over hitcounts and subtract the previous hitcounts
// @ts-ignore
var prevHitCounts = executedLinebyEachTest[count - 2];

for (const statementId in hitCounts) {
hitCounts[statementId] = Math.abs(
hitCounts[statementId] - prevHitCounts[statementId]
);
}
}
type HitCounts = { [statementId: string]: number };
type CoverageData = { [filename: string]: { statementMap: any, s: HitCounts } };
type ExecutedLineByEachTest = { [filename: string]: HitCounts }[];

const executedLinebyEachTest: ExecutedLineByEachTest = [];
const executedLinesByFile: { [filename: string]: number[] } = {};

for (const statementId in statementMap) {
if (hitCounts[statementId] > 0) {
const executedLine = statementMap[statementId].start.line;
executedLines.add(executedLine);
}
declare const global: { __coverage__: CoverageData };

function GetCoverage() {
count++;

for (const filename in global.__coverage__) {
let coverageData = global.__coverage__[filename];
const executedLines = new Set<number>();
const fileCoverage = coverageData;
const statementMap = fileCoverage.statementMap;
const hitCounts = fileCoverage.s;
// console.log("hitcounts", hitCounts);

if (count > 1) {
if (!executedLinebyEachTest[count - 2]) {
executedLinebyEachTest[count - 2] = {};
}
const prevHitCounts = executedLinebyEachTest[count - 2][filename] || {};
for (const statementId in hitCounts) {
const currentHitCount = isNaN(hitCounts[statementId]) ? 0 : hitCounts[statementId];
const previousHitCount = isNaN(prevHitCounts[statementId]) ? 0 : prevHitCounts[statementId];
hitCounts[statementId] = Math.abs(currentHitCount - previousHitCount);
}
}

for (const statementId in statementMap) {
if (hitCounts[statementId] > 0) {
const executedLine = statementMap[statementId].start.line;
// console.log("executedLine", executedLine);
executedLines.add(executedLine);
}
}

executedLinesByFile[filename] = Array.from(executedLines).sort((a, b) => a - b);

if (!executedLinebyEachTest[count - 1]) {
executedLinebyEachTest[count - 1] = {};
}

executedLinebyEachTest[count - 1][filename] = { ...hitCounts };

// console.log("Executed lines by file executedLinebyEachTest:", executedLinebyEachTest);
}
// @ts-ignore
executedLinesByFile[filename] = Array.from(executedLines).sort((a, b) => a - b);
// }
// @ts-ignore
executedLinebyEachTest.push({ ...hitCounts });

// console.log("Executed lines by file:", executedLinesByFile);
// extract s from the coverage data
}
return executedLinesByFile;
return executedLinesByFile;
}

module.exports = middleware;

0 comments on commit 27b2bf3

Please sign in to comment.