Skip to content

Commit 1f7c890

Browse files
committed
Clean up variant analyses directory
1 parent 8ae6543 commit 1f7c890

4 files changed

Lines changed: 83 additions & 19 deletions

File tree

extensions/ql-vscode/src/pure/files.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@ export function pathsEqual(
6767
}
6868
return path1 === path2;
6969
}
70+
71+
export async function readDirFullPaths(path: string): Promise<string[]> {
72+
const baseNames = await readdir(path);
73+
return baseNames.map((baseName) => join(path, baseName));
74+
}

extensions/ql-vscode/src/query-history/query-history-scrubber.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { pathExists, readdir, stat, remove, readFile } from "fs-extra";
1+
import { pathExists, stat, remove, readFile } from "fs-extra";
22
import { EOL } from "os";
33
import { join } from "path";
44
import { Disposable, ExtensionContext } from "vscode";
55
import { extLogger } from "../common";
6+
import { readDirFullPaths } from "../pure/files";
67
import { QueryHistoryDirs } from "./query-history-dirs";
78
import { QueryHistoryManager } from "./query-history-manager";
89

@@ -75,18 +76,33 @@ async function scrubQueries(
7576
let scrubCount = 0; // total number of directories deleted
7677
try {
7778
counter?.increment();
78-
void extLogger.log("Scrubbing query directory. Removing old queries.");
79+
void extLogger.log(
80+
"Cleaning up query history directories. Removing old entries.",
81+
);
82+
7983
if (!(await pathExists(queryHistoryDirs.localQueriesDirPath))) {
8084
void extLogger.log(
81-
`Cannot scrub. Query directory does not exist: ${queryHistoryDirs.localQueriesDirPath}`,
85+
`Cannot clean up query history directories. Local queries directory does not exist: ${queryHistoryDirs.localQueriesDirPath}`,
86+
);
87+
return;
88+
}
89+
if (!(await pathExists(queryHistoryDirs.variantAnalysesDirPath))) {
90+
void extLogger.log(
91+
`Cannot clean up query history directories. Variant analyses directory does not exist: ${queryHistoryDirs.variantAnalysesDirPath}`,
8292
);
8393
return;
8494
}
8595

86-
const baseNames = await readdir(queryHistoryDirs.localQueriesDirPath);
96+
const localQueryDirPaths = await readDirFullPaths(
97+
queryHistoryDirs.localQueriesDirPath,
98+
);
99+
const variantAnalysisDirPaths = await readDirFullPaths(
100+
queryHistoryDirs.variantAnalysesDirPath,
101+
);
102+
const allDirPaths = [...localQueryDirPaths, ...variantAnalysisDirPaths];
103+
87104
const errors: string[] = [];
88-
for (const baseName of baseNames) {
89-
const dir = join(queryHistoryDirs.localQueriesDirPath, baseName);
105+
for (const dir of allDirPaths) {
90106
const scrubResult = await scrubDirectory(dir, now, maxQueryTime);
91107
if (scrubResult.errorMsg) {
92108
errors.push(scrubResult.errorMsg);

extensions/ql-vscode/test/unit-tests/pure/files.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
gatherQlFiles,
55
getDirectoryNamesInsidePath,
66
pathsEqual,
7+
readDirFullPaths,
78
} from "../../../src/pure/files";
89

910
describe("files", () => {
@@ -100,6 +101,20 @@ describe("files", () => {
100101
expect(result).toEqual(["sub-folder"]);
101102
});
102103
});
104+
105+
describe("readDirFullPaths", () => {
106+
it("should return all files with full path", async () => {
107+
const file1 = join(dataDir, "compute-default-strings.ql");
108+
const file2 = join(dataDir, "multiple-result-sets.ql");
109+
const file3 = join(dataDir, "query.ql");
110+
111+
const paths = await readDirFullPaths(dataDir);
112+
113+
expect(paths.some((path) => path === file1)).toBe(true);
114+
expect(paths.some((path) => path === file2)).toBe(true);
115+
expect(paths.some((path) => path === file3)).toBe(true);
116+
});
117+
});
103118
});
104119

105120
describe("pathsEqual", () => {

extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-scrubber.test.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ describe("query history scrubber", () => {
2323
// We don't want our times to align exactly with the hour,
2424
// so we can better mimic real life
2525
const LESS_THAN_ONE_DAY = ONE_DAY_IN_MS - 1000;
26-
const tmpDir = dirSync({
26+
const tmpLocalQueriesDir = dirSync({
27+
unsafeCleanup: true,
28+
});
29+
const tmpVariantAnalysesDir = dirSync({
2730
unsafeCleanup: true,
2831
});
2932

@@ -62,7 +65,7 @@ describe("query history scrubber", () => {
6265
});
6366

6467
it("should not throw an error when the query directory does not exist", async () => {
65-
registerScrubber("idontexist");
68+
registerScrubber("idontexist1", "idontexist2");
6669

6770
jest.advanceTimersByTime(ONE_HOUR_IN_MS);
6871
await wait();
@@ -89,21 +92,35 @@ describe("query history scrubber", () => {
8992
);
9093
});
9194

92-
it("should scrub directories", async () => {
95+
it("should scrub local query directories", async () => {
9396
// create two query directories that are right around the cut off time
94-
const queryDir = createMockQueryDir(
97+
const localQueriesDir = createMockQueryDir(
98+
tmpLocalQueriesDir.name,
9599
ONE_HOUR_IN_MS,
96100
TWO_HOURS_IN_MS,
97101
THREE_HOURS_IN_MS,
98102
);
99-
registerScrubber(queryDir);
103+
const variantAnalysesDir = createMockQueryDir(
104+
tmpVariantAnalysesDir.name,
105+
ONE_HOUR_IN_MS,
106+
TWO_HOURS_IN_MS,
107+
THREE_HOURS_IN_MS,
108+
);
109+
registerScrubber(localQueriesDir, variantAnalysesDir);
100110

101111
jest.advanceTimersByTime(TWO_HOURS_IN_MS);
102112
await wait();
103113

104114
// should have deleted only the invalid locations
105115
expectDirectories(
106-
queryDir,
116+
localQueriesDir,
117+
toQueryDirName(ONE_HOUR_IN_MS),
118+
toQueryDirName(TWO_HOURS_IN_MS),
119+
toQueryDirName(THREE_HOURS_IN_MS),
120+
);
121+
122+
expectDirectories(
123+
variantAnalysesDir,
107124
toQueryDirName(ONE_HOUR_IN_MS),
108125
toQueryDirName(TWO_HOURS_IN_MS),
109126
toQueryDirName(THREE_HOURS_IN_MS),
@@ -114,7 +131,14 @@ describe("query history scrubber", () => {
114131

115132
// nothing should have happened...yet
116133
expectDirectories(
117-
queryDir,
134+
localQueriesDir,
135+
toQueryDirName(ONE_HOUR_IN_MS),
136+
toQueryDirName(TWO_HOURS_IN_MS),
137+
toQueryDirName(THREE_HOURS_IN_MS),
138+
);
139+
140+
expectDirectories(
141+
variantAnalysesDir,
118142
toQueryDirName(ONE_HOUR_IN_MS),
119143
toQueryDirName(TWO_HOURS_IN_MS),
120144
toQueryDirName(THREE_HOURS_IN_MS),
@@ -126,23 +150,24 @@ describe("query history scrubber", () => {
126150
// should have deleted the two older directories
127151
// even though they have different time stamps,
128152
// they both expire during the same scrubbing period
129-
expectDirectories(queryDir, toQueryDirName(THREE_HOURS_IN_MS));
153+
expectDirectories(localQueriesDir, toQueryDirName(THREE_HOURS_IN_MS));
154+
expectDirectories(variantAnalysesDir, toQueryDirName(THREE_HOURS_IN_MS));
130155

131156
// Wait until the next scrub time and the final directory is deleted
132157
jest.advanceTimersByTime(TWO_HOURS_IN_MS);
133158
await wait();
134159

135160
// should have deleted everything
136-
expectDirectories(queryDir);
161+
expectDirectories(localQueriesDir);
162+
expectDirectories(variantAnalysesDir);
137163
});
138164

139165
function expectDirectories(queryDir: string, ...dirNames: string[]) {
140166
const files = readdirSync(queryDir);
141167
expect(files.sort()).toEqual(dirNames.sort());
142168
}
143169

144-
function createMockQueryDir(...timestamps: number[]) {
145-
const dir = tmpDir.name;
170+
function createMockQueryDir(dir: string, ...timestamps: number[]) {
146171
const queryDir = join(dir, "query");
147172
// create qyuery directory and fill it with some query directories
148173
mkdirSync(queryDir);
@@ -175,12 +200,15 @@ describe("query history scrubber", () => {
175200
return `query-${timestamp}`;
176201
}
177202

178-
function registerScrubber(dir: string) {
203+
function registerScrubber(
204+
localQueriesDirPath: string,
205+
variantAnalysesDirPath: string,
206+
) {
179207
deregister = registerQueryHistoryScrubber(
180208
ONE_HOUR_IN_MS,
181209
TWO_HOURS_IN_MS,
182210
LESS_THAN_ONE_DAY,
183-
{ localQueriesDirPath: dir, variantAnalysesDirPath: dir },
211+
{ localQueriesDirPath, variantAnalysesDirPath },
184212
{
185213
removeDeletedQueries: () => {
186214
return Promise.resolve();

0 commit comments

Comments
 (0)