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

ta3 (simulation/calibration/optimization)result cache #4224

Merged
merged 6 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { RunResults } from '@/types/SimulateConfig';
import * as EventService from '@/services/event';
import { useProjects } from '@/composables/project';
import { subscribe, unsubscribe } from '@/services/ClientEventService';
import { FIFOCache } from '@/utils/FifoCache';

export type DataArray = Record<string, any>[];

Expand Down Expand Up @@ -120,25 +121,32 @@ export async function getRunResult(runId: string, filename: string) {
}
}

const dataArrayCache = new FIFOCache<Promise<string>>(100);
export async function getRunResultCSV(
runId: string,
filename: string,
renameFn?: (s: string) => string
): Promise<DataArray> {
try {
const resp = await API.get(`simulations/${runId}/result`, {
params: { filename }
});
const cacheKey = `${runId}:${filename}`;

let promise = dataArrayCache.get(cacheKey);
if (!promise) {
promise = API.get(`simulations/${runId}/result`, {
params: { filename }
}).then((res) => res.data);

dataArrayCache.set(cacheKey, promise);
}

// If a rename function is defined, loop over the first row
let dataStr = resp.data;
let dataStr = await promise;
if (renameFn) {
const lines = dataStr.split(/\n/);
const line0 = lines[0].split(/,/).map(renameFn).join(',');
lines[0] = line0;
dataStr = lines.join('\n');
}

const output = csvParse(dataStr, autoType);
return output;
} catch (err) {
Expand Down
25 changes: 25 additions & 0 deletions packages/client/hmi-client/src/utils/FifoCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Simple FIFO-based cache
* */
export class FIFOCache<T> {
private limit: number;

private cache: Map<string, T> = new Map();

constructor(size = 50) {
this.limit = size;
}

get(key: string) {
return this.cache.get(key);
}

set(key: string, value: T) {
this.cache.set(key, value);

// Expunge, keys are in insertion order thus making it fifo
if (this.cache.size > this.limit) {
this.cache.delete(this.cache.keys().next().value);
}
}
}
27 changes: 27 additions & 0 deletions packages/client/hmi-client/tests/unit/utils/fifocache.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { FIFOCache } from '@/utils/FifoCache';
import { describe, expect, it } from 'vitest';

describe('fifo cache test', () => {
it('can store value', () => {
const cache = new FIFOCache<string>(3);
cache.set('a', 'foo');
cache.set('b', 'bar');

expect(cache.get('a')).to.eq('foo');
expect(cache.get('b')).to.eq('bar');
expect(cache.get('c')).toBeUndefined();
});

it('expunge policy', () => {
const cache = new FIFOCache<string>(3);
for (let i = 0; i < 10; i++) {
cache.set(`${i}`, `${i}`);
}

console.log(cache);

expect(cache.get('1')).toBeUndefined();
expect(cache.get('6')).toBeUndefined();
expect(cache.get('8')).to.eq('8');
});
});
Loading