Merge pull request #2286 from ing-bank/fix/providence-cache

Fix/providence cache
This commit is contained in:
Thijs Louisse 2024-05-16 14:25:50 +02:00 committed by GitHub
commit b205a1e372
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 24 additions and 21 deletions

View file

@ -0,0 +1,6 @@
---
'providence-analytics': patch
---
- feat: expose ReportService to allow config of outputPath
- fix: cache and performance improvements

View file

@ -3,3 +3,4 @@ export { QueryService } from './program/core/QueryService.js';
export { LogService } from './program/core/LogService.js'; export { LogService } from './program/core/LogService.js';
export { InputDataService } from './program/core/InputDataService.js'; export { InputDataService } from './program/core/InputDataService.js';
export { AstService } from './program/core/AstService.js'; export { AstService } from './program/core/AstService.js';
export { ReportService } from './program/core/ReportService.js';

View file

@ -526,7 +526,7 @@ export class InputDataService {
npmGlobs.push(...getNpmPackagePaths(startPath)); npmGlobs.push(...getNpmPackagePaths(startPath));
} }
const combinedGlobs = [...cfg.allowlist, ...npmGlobs, ...negativeGitGlobs]; const combinedGlobs = Array.from(new Set([...cfg.allowlist, ...npmGlobs, ...negativeGitGlobs]));
const hasProvidedPositiveGlob = cfg.allowlist.some(glob => !glob.startsWith('!')); const hasProvidedPositiveGlob = cfg.allowlist.some(glob => !glob.startsWith('!'));
// We need to expand // We need to expand
@ -538,6 +538,7 @@ export class InputDataService {
const globbyCfg = { const globbyCfg = {
expandDirectories: false, expandDirectories: false,
fs: fsAdapter.fs,
onlyFiles: true, onlyFiles: true,
absolute: true, absolute: true,
cwd: startPath, cwd: startPath,

View file

@ -2,9 +2,6 @@ import path from 'path';
import { hash } from '../utils/hash.js'; import { hash } from '../utils/hash.js';
import { fsAdapter } from '../utils/fs-adapter.js'; import { fsAdapter } from '../utils/fs-adapter.js';
import { memoize } from '../utils/memoize.js';
// const memoize = fn => fn;
/** /**
* @typedef {import('../../../types/index.js').AnalyzerQueryResult} AnalyzerQueryResult * @typedef {import('../../../types/index.js').AnalyzerQueryResult} AnalyzerQueryResult
* @typedef {import('../../../types/index.js').PathFromSystemRoot} PathFromSystemRoot * @typedef {import('../../../types/index.js').PathFromSystemRoot} PathFromSystemRoot
@ -132,5 +129,3 @@ export class ReportService {
fsAdapter.fs.writeFileSync(filePath, JSON.stringify(file, null, 2), { flag: 'w' }); fsAdapter.fs.writeFileSync(filePath, JSON.stringify(file, null, 2), { flag: 'w' });
} }
} }
ReportService.createIdentifier = memoize(ReportService.createIdentifier);
ReportService.getCachedResult = memoize(ReportService.getCachedResult);

View file

@ -24,16 +24,16 @@ function createCachableArg(arg) {
/** /**
* @template T * @template T
* @type {<T>(functionToMemoize:T, opts?:{ storage?:object; serializeObjects?: boolean }) => T} * @type {<T extends Function>(functionToMemoize:T, opts?:{ storage?:object; }) => T}
*/ */
export function memoize(functionToMemoize, { storage = {}, serializeObjects = false } = {}) { export function memoize(functionToMemoize, { storage = {} } = {}) {
// @ts-expect-erro
// eslint-disable-next-line func-names
return /** @type {* & T} */ ( return /** @type {* & T} */ (
function memoizedFn() { function memoizedFn() {
// eslint-disable-next-line prefer-rest-params // eslint-disable-next-line prefer-rest-params
const args = [...arguments]; const args = [...arguments];
const cachableArgs = !serializeObjects ? args : args.map(createCachableArg); const shouldSerialize = args.some(isObject);
const cachableArgs = shouldSerialize ? args.map(createCachableArg) : args;
// Allow disabling of cache for testing purposes // Allow disabling of cache for testing purposes
// @ts-expect-error // @ts-expect-error
if (shouldCache && cachableArgs in storage) { if (shouldCache && cachableArgs in storage) {
@ -56,6 +56,7 @@ export function memoize(functionToMemoize, { storage = {}, serializeObjects = fa
memoize.disableCaching = () => { memoize.disableCaching = () => {
shouldCache = false; shouldCache = false;
}; };
/** /**
* Once testing is done, it is possible to restore caching. * Once testing is done, it is possible to restore caching.
* @param {boolean} [initialValue] * @param {boolean} [initialValue]
@ -65,8 +66,6 @@ memoize.restoreCaching = initialValue => {
}; };
Object.defineProperty(memoize, 'isCacheEnabled', { Object.defineProperty(memoize, 'isCacheEnabled', {
// writable: false,
// enumerable: true,
get() { get() {
return shouldCache; return shouldCache;
}, },

View file

@ -141,7 +141,7 @@ async function getAllFilesFromStartPath(
const direntsForLvl = await fs.promises.readdir(startPath, { withFileTypes: true }); const direntsForLvl = await fs.promises.readdir(startPath, { withFileTypes: true });
for (const dirent of direntsForLvl) { for (const dirent of direntsForLvl) {
// @ts-expect-error // @ts-expect-error
dirent.parentPath = startPath; dirent.parentPath = dirent.path = startPath; // eslint-disable-line no-multi-assign
dirents.push(dirent); dirents.push(dirent);
if (dirent.isDirectory()) { if (dirent.isDirectory()) {
@ -189,7 +189,7 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
options.onlyDirectories = true; options.onlyDirectories = true;
} }
const globs = Array.isArray(globOrGlobs) ? globOrGlobs : [globOrGlobs]; const globs = Array.isArray(globOrGlobs) ? Array.from(new Set(globOrGlobs)) : [globOrGlobs];
/** @type {RegExp[]} */ /** @type {RegExp[]} */
const matchRegexesNegative = []; const matchRegexesNegative = [];
@ -224,12 +224,10 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
}); });
const allDirEntsRelativeToCwd = allDirentsRelativeToStartPath.map(dirent => ({ const allDirEntsRelativeToCwd = allDirentsRelativeToStartPath.map(dirent => ({
// @ts-expect-error relativeToCwdPath: toPosixPath(
relativeToCwdPath: toPosixPath(path.join(dirent.parentPath, dirent.name)).replace( // @ts-expect-error
`${toPosixPath(options.cwd)}/`, path.join(dirent.parentPath || dirent.path, dirent.name),
'', ).replace(`${toPosixPath(options.cwd)}/`, ''),
),
dirent, dirent,
})); }));

View file

@ -1,5 +1,6 @@
import { expect } from 'chai'; import { expect } from 'chai';
import { it } from 'mocha'; import { it } from 'mocha';
import { providence } from '../../../src/program/providence.js'; import { providence } from '../../../src/program/providence.js';
import { QueryService } from '../../../src/program/core/QueryService.js'; import { QueryService } from '../../../src/program/core/QueryService.js';
import { setupAnalyzerTest } from '../../../test-helpers/setup-analyzer-test.js'; import { setupAnalyzerTest } from '../../../test-helpers/setup-analyzer-test.js';

View file

@ -119,6 +119,7 @@ describe('Memoize', () => {
}); });
}); });
}); });
describe('With non primitives', () => { describe('With non primitives', () => {
describe('Arrays', () => { describe('Arrays', () => {
it(`returns cached result when called with same parameters`, async () => { it(`returns cached result when called with same parameters`, async () => {
@ -246,6 +247,7 @@ describe('Memoize', () => {
expect(sumCalled).to.equal(1); expect(sumCalled).to.equal(1);
// Outside world can edit returned reference // Outside world can edit returned reference
// @ts-expect-error
resultCached.x = 3; resultCached.x = 3;
// Return from cache // Return from cache
const lastResult = sumMemoized({ x: 1 }, { y: 2 }); const lastResult = sumMemoized({ x: 1 }, { y: 2 });