Skip to content

Commit

Permalink
Preload cache on converter create
Browse files Browse the repository at this point in the history
  • Loading branch information
dickwolff committed Feb 9, 2024
1 parent 91c9ce7 commit e22bb11
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
10 changes: 6 additions & 4 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { SwissquoteConverter } from "./converters/swissquoteConverter";
import { FinpensionConverter } from "./converters/finpensionConverter";
import { YahooFinanceService } from "./yahooFinanceService";


export function createAndRunConverter(converterType: string, inputFilePath: string, outputFilePath: string, completionCallback: CallableFunction, errorCallback: CallableFunction) {
export async function createAndRunConverter(converterType: string, inputFilePath: string, outputFilePath: string, completionCallback: CallableFunction, errorCallback: CallableFunction) {

// Verify if Ghostolio account ID is set (because without it there can be no valid output).
if (!process.env.GHOSTFOLIO_ACCOUNT_ID) {
Expand All @@ -22,7 +21,7 @@ export function createAndRunConverter(converterType: string, inputFilePath: stri
const converterTypeLc = converterType.toLocaleLowerCase();

// Determine convertor type.
const converter = createConverter(converterTypeLc);
const converter = await createConverter(converterTypeLc);

// Map the file to a Ghostfolio import.
converter.readAndProcessFile(inputFilePath, (result: GhostfolioExport) => {
Expand All @@ -41,10 +40,13 @@ export function createAndRunConverter(converterType: string, inputFilePath: stri
}, (error) => errorCallback(error));
}

function createConverter(converterType: string): AbstractConverter {
async function createConverter(converterType: string): Promise<AbstractConverter> {

const yahooFinanceService = new YahooFinanceService();

const cacheSize = await yahooFinanceService.loadCache();
console.log(`[i] Restored ${cacheSize[0]} ISIN-symbol pairs and ${cacheSize[1]} symbols from cache..`);

let converter: AbstractConverter;

switch (converterType) {
Expand Down
54 changes: 28 additions & 26 deletions src/yahooFinanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ export class YahooFinanceService {

// Retrieve prefered exchange postfix if set in .env
this.preferedExchangePostfix = process.env.DEGIRO_PREFERED_EXCHANGE_POSTFIX;

// Preload the cache from disk.
this.preloadCache().then((cacheSize) => console.log(`\n[i] Restored ${cacheSize[0]} ISIN-symbol pairs and ${cacheSize[1]} symbols from cache..`));
}

/**
Expand All @@ -53,7 +50,7 @@ export class YahooFinanceService {
if (symbol) {

const symbolMatch = this.symbolCache.has(symbol);

// If a match was found, return the security.
if (symbolMatch) {
this.logDebug(`Retrieved symbol ${symbol} from cache!`, progress);
Expand Down Expand Up @@ -134,6 +131,33 @@ export class YahooFinanceService {
return null;
}

/**
* Load the cache with ISIN and symbols.
*
* @returns The size of the loaded cache
*/
public async loadCache(): Promise<[number, number]> {

// Verify if there is data in the ISIN-Symbol cache. If so, restore to the local variable.
const isinSymbolCacheExist = await cacache.get.info(cachePath, "isinSymbolCache");
if (isinSymbolCacheExist) {
const cache = await cacache.get(cachePath, "isinSymbolCache");
const cacheAsJson = JSON.parse(cache.data.toString(), this.mapReviver);
this.isinSymbolCache = cacheAsJson;
}

// Verify if there is data in the Symbol cache. If so, restore to the local variable.
const symbolCacheExists = await cacache.get.info(cachePath, "symbolCache");
if (symbolCacheExists) {
const cache = await cacache.get(cachePath, "symbolCache");
const cacheAsJson = JSON.parse(cache.data.toString(), this.mapReviver);
this.symbolCache = cacheAsJson;
}

// Return cache sizes.
return [this.isinSymbolCache.size, this.symbolCache.size];
}

/**
* Get symbols for a security by a given key.
*
Expand Down Expand Up @@ -237,28 +261,6 @@ export class YahooFinanceService {
return symbolMatch;
}

private async preloadCache(): Promise<[number, number]> {

// Verify if there is data in the ISIN-Symbol cache. If so, restore to the local variable.
const isinSymbolCacheExist = await cacache.get.info(cachePath, "isinSymbolCache");
if (isinSymbolCacheExist) {
const cache = await cacache.get(cachePath, "isinSymbolCache");
const cacheAsJson = JSON.parse(cache.data.toString(), this.mapReviver);
this.isinSymbolCache = cacheAsJson;
}

// Verify if there is data in the Symbol cache. If so, restore to the local variable.
const symbolCacheExists = await cacache.get.info(cachePath, "symbolCache");
if (symbolCacheExists) {
const cache = await cacache.get(cachePath, "symbolCache");
const cacheAsJson = JSON.parse(cache.data.toString(), this.mapReviver);
this.symbolCache = cacheAsJson;
}

// Return cache sizes.
return [this.isinSymbolCache.size, this.symbolCache.size];
}

private async saveInCache(isin?: string, symbol?: string, value?: any) {

// Save ISIN-value combination to cache if given.
Expand Down

0 comments on commit e22bb11

Please sign in to comment.