Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise existing broken link count function #764

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions api/functions/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,35 @@ const incrementString = (str) => {
};

const getExistingBrokenLinkCount = async (runId) => {
const unscannableLinks = await exports.getUnscannableLinks();
const scan = await exports.getSummaryById(runId);
const filter = odata`PartitionKey eq ${scan.partitionKey} and src ge ${scan.url} and src le ${incrementString(scan.url)}`;
const filterDays = 90;
let filter;
const startDate = new Date(scan.buildDate);
startDate.setDate(startDate.getDate() - filterDays);

if (scan.scanResultVersion === 2) {
filter = `PartitionKey eq '${scan.apiKey}-${slug(scan.url)}' and buildDate ge datetime'${startDate.toISOString()}' and buildDate le datetime'${scan.buildDate.toISOString()}'`;
} else {
filter = odata`PartitionKey eq ${scan.partitionKey} and src ge ${scan.url} and src le ${incrementString(scan.url)} and buildDate ge datetime'${startDate.toISOString()}' and buildDate le datetime'${scan.buildDate.toISOString()}'`;
}

const entity = new TableClient(azureUrl, TABLE.ScanResults, credential).listEntities({
queryOptions: { filter }
});
const result = [];
for await (const item of entity) {
result.push(item);
}
const previousFailures = new Map();
const previousFailures = new Set();
const sortedResult = result.sort((a, b) => a.buildDate - b.buildDate);

const existingCount = sortedResult.reduce((count, item) => {
if (previousFailures.has(item.dst) && item.runId === runId) {
count++;
} else {
previousFailures.add(item.dst);
}

const existingCount = result.reduce((count, item) => {
if (item.runId === runId) {
if (!previousFailures.has(item.dst) && !unscannableLinks.some((i) => item.dst.includes(i))) {
const hasPrevious = result.some((i) => i.dst === item.dst && i.buildDate < item.buildDate);
previousFailures.set(item.dst, hasPrevious);

if (hasPrevious) {
count++;
}
}
}
return count;
}, 0);

Expand Down
Loading