diff --git a/src/__mocks__/api/v3/client.ts b/src/__mocks__/api/v3/client.ts index b539237..0f86d63 100644 --- a/src/__mocks__/api/v3/client.ts +++ b/src/__mocks__/api/v3/client.ts @@ -2,6 +2,7 @@ import { default as company } from '~/__mocks__/fixtures/v3/company.js' import { default as daily } from '~/__mocks__/fixtures/v3/daily.js' import { default as quarter } from '~/__mocks__/fixtures/v3/quarter.js' import { Daily } from '~/entities/v3/daily' +import { Quarter } from '~/entities/v3/quarter' export class BuffettCodeApiClientV3 { public mockCompany = jest.fn() @@ -18,9 +19,8 @@ export class BuffettCodeApiClientV3 { return this.mockCompany()['data'] } - quarter(): object | null { - const quarter = this.mockQuarter()['data'] - return quarter ? quarter : null + quarter(): Quarter { + return Quarter.fromResponse(this.mockQuarter()) } daily(): Daily { @@ -31,8 +31,7 @@ export class BuffettCodeApiClientV3 { return Daily.fromResponse(this.mockDaily()) } - ondemandQuarter(): object | null { - const quarter = this.mockQuarter()['data'] - return quarter ? quarter : null + ondemandQuarter(): Quarter { + return Quarter.fromResponse(this.mockQuarter()) } } diff --git a/src/__mocks__/services/quarter-cache.ts b/src/__mocks__/services/quarter-cache.ts index 6e85f00..b9a4472 100644 --- a/src/__mocks__/services/quarter-cache.ts +++ b/src/__mocks__/services/quarter-cache.ts @@ -1,19 +1,44 @@ +import { Quarter } from '~/entities/v3/quarter' import { YearQuarter } from '~/fiscal-periods/year-quarter' export class QuarterCache { static readonly cache = {} - static get(ticker: string, yearQuarter: YearQuarter): object | null { - const cached = QuarterCache.cache[`${ticker}-${yearQuarter}`] + static get(ticker: string, yearQuarter: YearQuarter): Quarter | null { + const cachedData = this.getData(ticker, yearQuarter) + const cachedColumnDescription = this.getColumnDescription() + if (cachedData == undefined || cachedColumnDescription == undefined) { + return null + } + + return new Quarter(cachedData, cachedColumnDescription) + } + + static getData(ticker: string, yearQuarter: YearQuarter): object | null { + const cached = this.cache[`${ticker}-${yearQuarter}`] + return cached === undefined ? null : cached + } + + static getColumnDescription(): object | null { + const cached = this.cache['column-description'] return cached === undefined ? null : cached } - static put(ticker: string, quarter: object): void { - QuarterCache.cache[ + static put(ticker: string, quarter: Quarter): void { + this.putData(ticker, quarter.data) + this.putColumnDescription(quarter.columnDescription) + } + + static putData(ticker: string, quarter: object): void { + this.cache[ `${ticker}-${quarter['fiscal_year']}Q${quarter['fiscal_quarter']}` ] = quarter } + static putColumnDescription(columnDescription: object): void { + this.cache['column-description'] = columnDescription + } + // for testing static clearAll(): void { Object.keys(this.cache).forEach(key => delete this.cache[key]) diff --git a/src/api/v2/caching-client.test.ts b/src/api/v2/caching-client.test.ts index 63f4c1c..99bc879 100644 --- a/src/api/v2/caching-client.test.ts +++ b/src/api/v2/caching-client.test.ts @@ -79,7 +79,7 @@ describe('quarter', () => { const period = new YearQuarterParam(2018, 1) test('(uncached)', () => { - expect(QuarterCache.get(ticker, period.toYearQuarter())).toBeNull() + expect(QuarterCache.getData(ticker, period.toYearQuarter())).toBeNull() const client = new CachingBuffettCodeApiClientV2('token') const res = client.quarter(ticker, period) @@ -87,11 +87,11 @@ describe('quarter', () => { expect(res['fiscal_year']).toBe(period.year) expect(res['fiscal_quarter']).toBe(period.quarter) - expect(QuarterCache.get(ticker, period.toYearQuarter())).toEqual(res) + expect(QuarterCache.getData(ticker, period.toYearQuarter())).toEqual(res) }) test('(cached)', () => { - const cached = QuarterCache.get(ticker, period.toYearQuarter()) + const cached = QuarterCache.getData(ticker, period.toYearQuarter()) expect(cached).not.toBeNull() const client = new CachingBuffettCodeApiClientV2('token') @@ -100,7 +100,9 @@ describe('quarter', () => { expect(res['fiscal_year']).toBe(period.year) expect(res['fiscal_quarter']).toBe(period.quarter) - expect(QuarterCache.get(ticker, period.toYearQuarter())).toEqual(cached) + expect(QuarterCache.getData(ticker, period.toYearQuarter())).toEqual( + cached + ) }) }) @@ -112,7 +114,7 @@ describe('quarter', () => { const period = new YearQuarterParam('LY', 'LQ') test('(uncached)', () => { - expect(QuarterCache.get(ticker, new YearQuarter(2018, 1))).toBeNull() + expect(QuarterCache.getData(ticker, new YearQuarter(2018, 1))).toBeNull() const client = new CachingBuffettCodeApiClientV2('token') const res = client.quarter(ticker, period) @@ -120,7 +122,9 @@ describe('quarter', () => { expect(res['fiscal_year']).toBe(2018) expect(res['fiscal_quarter']).toBe(1) - expect(QuarterCache.get(ticker, new YearQuarter(2018, 1))).toEqual(res) + expect(QuarterCache.getData(ticker, new YearQuarter(2018, 1))).toEqual( + res + ) }) }) }) @@ -136,24 +140,26 @@ describe('ondemandQuarter', () => { const period = new YearQuarterParam(2018, 1) test('(uncached)', () => { - expect(QuarterCache.get(ticker, period.toYearQuarter())).toBeNull() + expect(QuarterCache.getData(ticker, period.toYearQuarter())).toBeNull() const client = new CachingBuffettCodeApiClientV2('token') const res = client.ondemandQuarter(ticker, period) expect(res).not.toBeNull() - expect(QuarterCache.get(ticker, period.toYearQuarter())).toEqual(res) + expect(QuarterCache.getData(ticker, period.toYearQuarter())).toEqual(res) }) test('(cached)', () => { - const cached = QuarterCache.get(ticker, period.toYearQuarter()) + const cached = QuarterCache.getData(ticker, period.toYearQuarter()) expect(cached).not.toBeNull() const client = new CachingBuffettCodeApiClientV2('token') const res = client.ondemandQuarter(ticker, period) expect(res).toEqual(cached) - expect(QuarterCache.get(ticker, period.toYearQuarter())).toEqual(cached) + expect(QuarterCache.getData(ticker, period.toYearQuarter())).toEqual( + cached + ) }) }) @@ -165,7 +171,7 @@ describe('ondemandQuarter', () => { const period = new YearQuarterParam('LY', 'LQ') test('(uncached)', () => { - expect(QuarterCache.get(ticker, new YearQuarter(2018, 1))).toBeNull() + expect(QuarterCache.getData(ticker, new YearQuarter(2018, 1))).toBeNull() const client = new CachingBuffettCodeApiClientV2('token') const res = client.ondemandQuarter(ticker, period) @@ -173,7 +179,9 @@ describe('ondemandQuarter', () => { expect(res['fiscal_year']).toBe(2018) expect(res['fiscal_quarter']).toBe(1) - expect(QuarterCache.get(ticker, new YearQuarter(2018, 1))).toEqual(res) + expect(QuarterCache.getData(ticker, new YearQuarter(2018, 1))).toEqual( + res + ) }) }) }) diff --git a/src/api/v2/caching-client.ts b/src/api/v2/caching-client.ts index 0e3a57f..b566ddb 100644 --- a/src/api/v2/caching-client.ts +++ b/src/api/v2/caching-client.ts @@ -39,7 +39,7 @@ export class CachingBuffettCodeApiClientV2 extends BuffettCodeApiClientV2 { quarter(ticker: string, period: YearQuarterParam): object | null { if (period.convertibleToYearQuarter()) { - const cached = QuarterCache.get(ticker, period.toYearQuarter()) + const cached = QuarterCache.getData(ticker, period.toYearQuarter()) if (cached) { return cached } @@ -50,14 +50,14 @@ export class CachingBuffettCodeApiClientV2 extends BuffettCodeApiClientV2 { return null } - QuarterCache.put(ticker, quarter) + QuarterCache.putData(ticker, quarter) return quarter } ondemandQuarter(ticker: string, period: YearQuarterParam): object | null { if (period.convertibleToYearQuarter()) { - const cached = QuarterCache.get(ticker, period.toYearQuarter()) + const cached = QuarterCache.getData(ticker, period.toYearQuarter()) if (cached) { return cached } @@ -68,7 +68,7 @@ export class CachingBuffettCodeApiClientV2 extends BuffettCodeApiClientV2 { return null } - QuarterCache.put(ticker, quarter) + QuarterCache.putData(ticker, quarter) return quarter } diff --git a/src/api/v3/caching-client.test.ts b/src/api/v3/caching-client.test.ts index 9fd106c..c9c77df 100644 --- a/src/api/v3/caching-client.test.ts +++ b/src/api/v3/caching-client.test.ts @@ -87,8 +87,8 @@ describe('quarter', () => { const client = new CachingBuffettCodeApiClientV3('token') const res = client.quarter(ticker, period) expect(res).not.toBeNull() - expect(res['fiscal_year']).toBe(period.year) - expect(res['fiscal_quarter']).toBe(period.quarter) + expect(res.data['fiscal_year']).toBe(period.year) + expect(res.data['fiscal_quarter']).toBe(period.quarter) expect(QuarterCache.get(ticker, period.toYearQuarter())).toEqual(res) }) @@ -100,8 +100,8 @@ describe('quarter', () => { const client = new CachingBuffettCodeApiClientV3('token') const res = client.quarter(ticker, period) expect(res).toEqual(cached) - expect(res['fiscal_year']).toBe(period.year) - expect(res['fiscal_quarter']).toBe(period.quarter) + expect(res.data['fiscal_year']).toBe(period.year) + expect(res.data['fiscal_quarter']).toBe(period.quarter) expect(QuarterCache.get(ticker, period.toYearQuarter())).toEqual(cached) }) @@ -120,8 +120,8 @@ describe('quarter', () => { const client = new CachingBuffettCodeApiClientV3('token') const res = client.quarter(ticker, period) expect(res).not.toBeNull() - expect(res['fiscal_year']).toBe(2018) - expect(res['fiscal_quarter']).toBe(1) + expect(res.data['fiscal_year']).toBe(2018) + expect(res.data['fiscal_quarter']).toBe(1) expect(QuarterCache.get(ticker, new YearQuarter(2018, 1))).toEqual(res) }) @@ -173,8 +173,8 @@ describe('ondemandQuarter', () => { const client = new CachingBuffettCodeApiClientV3('token') const res = client.ondemandQuarter(ticker, period) expect(res).not.toBeNull() - expect(res['fiscal_year']).toBe(2018) - expect(res['fiscal_quarter']).toBe(1) + expect(res.data['fiscal_year']).toBe(2018) + expect(res.data['fiscal_quarter']).toBe(1) expect(QuarterCache.get(ticker, new YearQuarter(2018, 1))).toEqual(res) }) diff --git a/src/api/v3/caching-client.ts b/src/api/v3/caching-client.ts index 367d656..0d38fa4 100644 --- a/src/api/v3/caching-client.ts +++ b/src/api/v3/caching-client.ts @@ -1,5 +1,6 @@ import { BuffettCodeApiClientV3 } from '~/api/v3/client' import { Daily } from '~/entities/v3/daily' +import { Quarter } from '~/entities/v3/quarter' import { DateParam } from '~/fiscal-periods/date-param' import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param' import { CompanyCache } from '~/services/company-cache' @@ -35,7 +36,7 @@ export class CachingBuffettCodeApiClientV3 extends BuffettCodeApiClientV3 { return daily } - quarter(ticker: string, period: YearQuarterParam): object | null { + quarter(ticker: string, period: YearQuarterParam): Quarter { if (period.convertibleToYearQuarter()) { const cached = QuarterCache.get(ticker, period.toYearQuarter()) if (cached) { @@ -44,10 +45,6 @@ export class CachingBuffettCodeApiClientV3 extends BuffettCodeApiClientV3 { } const quarter = super.quarter(ticker, period) - if (!quarter) { - return null - } - QuarterCache.put(ticker, quarter) return quarter @@ -65,7 +62,7 @@ export class CachingBuffettCodeApiClientV3 extends BuffettCodeApiClientV3 { return daily } - ondemandQuarter(ticker: string, period: YearQuarterParam): object | null { + ondemandQuarter(ticker: string, period: YearQuarterParam): Quarter { if (period.convertibleToYearQuarter()) { const cached = QuarterCache.get(ticker, period.toYearQuarter()) if (cached) { @@ -74,10 +71,6 @@ export class CachingBuffettCodeApiClientV3 extends BuffettCodeApiClientV3 { } const quarter = super.ondemandQuarter(ticker, period) - if (!quarter) { - return null - } - QuarterCache.put(ticker, quarter) return quarter diff --git a/src/api/v3/client.test.ts b/src/api/v3/client.test.ts index 139d74e..b30c498 100644 --- a/src/api/v3/client.test.ts +++ b/src/api/v3/client.test.ts @@ -5,6 +5,7 @@ import { HttpError } from '~/api/http-error' import { useMockedUrlFetchApp } from '~/api/test-helper' import { BuffettCodeApiClientV3 } from '~/api/v3/client' import { Daily } from '~/entities/v3/daily' +import { Quarter } from '~/entities/v3/quarter' import { DateParam } from '~/fiscal-periods/date-param' import { DateRange } from '~/fiscal-periods/date-range' import { YearQuarter } from '~/fiscal-periods/year-quarter' @@ -102,7 +103,9 @@ describe('BuffettCodeApiClientV3', () => { const client = new BuffettCodeApiClientV3('foo') const ticker = '2371' const period = new YearQuarterParam(2018, 1) - expect(client.quarter(ticker, period)).toEqual(quarter['data']) + expect(client.quarter(ticker, period)).toEqual( + Quarter.fromResponse(quarter) + ) expect(mockFetch.mock.calls.length).toBe(1) expect(mockFetch.mock.calls[0].length).toBe(2) expect(mockFetch.mock.calls[0][0]).toBe( @@ -115,7 +118,13 @@ describe('BuffettCodeApiClientV3', () => { }) test('bulkQuarter', () => { - const mockFetch = useMockedUrlFetchApp(200, JSON.stringify(quarter)) + const bulkQuarter = { + data: { + '2020-09-06': quarter['data'] + }, + column_description: quarter['column_description'] // eslint-disable-line @typescript-eslint/camelcase + } + const mockFetch = useMockedUrlFetchApp(200, JSON.stringify(bulkQuarter)) const client = new BuffettCodeApiClientV3('foo') const ticker = '2371' @@ -123,7 +132,9 @@ describe('BuffettCodeApiClientV3', () => { new YearQuarter(2018, 1), new YearQuarter(2018, 4) ) - expect(client.bulkQuarter(ticker, range)).toEqual(quarter['data']) + expect(client.bulkQuarter(ticker, range)).toEqual( + Quarter.fromBulkResponse(bulkQuarter) + ) expect(mockFetch.mock.calls.length).toBe(1) expect(mockFetch.mock.calls[0].length).toBe(2) expect(mockFetch.mock.calls[0][0]).toBe( @@ -141,7 +152,9 @@ describe('BuffettCodeApiClientV3', () => { const client = new BuffettCodeApiClientV3('foo') const ticker = '2371' const period = new YearQuarterParam(2018, 1) - expect(client.ondemandQuarter(ticker, period)).toEqual(quarter['data']) + expect(client.ondemandQuarter(ticker, period)).toEqual( + Quarter.fromResponse(quarter) + ) expect(mockFetch.mock.calls.length).toBe(1) expect(mockFetch.mock.calls[0].length).toBe(2) expect(mockFetch.mock.calls[0][0]).toBe( diff --git a/src/api/v3/client.ts b/src/api/v3/client.ts index 3adea13..0e028cd 100644 --- a/src/api/v3/client.ts +++ b/src/api/v3/client.ts @@ -1,6 +1,7 @@ import { HttpError } from '~/api/http-error' import { UrlBuilder } from '~/api/url-builder' import { Daily } from '~/entities/v3/daily' +import { Quarter } from '~/entities/v3/quarter' import { DateParam } from '~/fiscal-periods/date-param' import { DateRange } from '~/fiscal-periods/date-range' import { YearQuarterParam } from '~/fiscal-periods/year-quarter-param' @@ -58,7 +59,7 @@ export class BuffettCodeApiClientV3 { return res[ticker] } - public quarter(ticker: string, period: YearQuarterParam): object { + public quarter(ticker: string, period: YearQuarterParam): Quarter { const endpoint = BuffettCodeApiClientV3.baseUrl + '/quarter' const builder = new UrlBuilder(endpoint, { ticker, @@ -69,10 +70,10 @@ export class BuffettCodeApiClientV3 { const options = this.defaultOptions() const res = BuffettCodeApiClientV3.request(url, options) - return res['data'] + return Quarter.fromResponse(res) } - public bulkQuarter(ticker: string, range: YearQuarterRange): object[] { + public bulkQuarter(ticker: string, range: YearQuarterRange): Quarter[] { const endpoint = BuffettCodeApiClientV3.baseUrl + '/bulk/quarter' const builder = new UrlBuilder(endpoint, { ticker, @@ -83,10 +84,10 @@ export class BuffettCodeApiClientV3 { const options = this.defaultOptions() const res = BuffettCodeApiClientV3.request(url, options) - return res['data'] + return Quarter.fromBulkResponse(res) } - public ondemandQuarter(ticker: string, period: YearQuarterParam): object { + public ondemandQuarter(ticker: string, period: YearQuarterParam): Quarter { const endpoint = BuffettCodeApiClientV3.baseUrl + '/ondemand/quarter' const builder = new UrlBuilder(endpoint, { ticker, @@ -97,7 +98,7 @@ export class BuffettCodeApiClientV3 { const options = this.defaultOptions() const res = BuffettCodeApiClientV3.request(url, options) - return res['data'] + return Quarter.fromResponse(res) } public daily(ticker: string, date: DateParam): Daily { diff --git a/src/custom-functions/v2/bcode-quarter.test.ts b/src/custom-functions/v2/bcode-quarter.test.ts index 8493694..cb2cae8 100644 --- a/src/custom-functions/v2/bcode-quarter.test.ts +++ b/src/custom-functions/v2/bcode-quarter.test.ts @@ -31,14 +31,14 @@ describe('bcodeQuarter', () => { test('(quarter, FY, FQ)', () => { const ticker = '2371' const yearQuarter = new YearQuarter(2018, 1) - expect(QuarterCache.get(ticker, yearQuarter)).toBeNull() + expect(QuarterCache.getData(ticker, yearQuarter)).toBeNull() expect(QuarterPropertyCache.get()).toBeNull() const client = new CachingBuffettCodeApiClientV2('token') const result = bcodeQuarter(client, ticker, 2018, 1, 'net_sales', false) expect(result).toEqual(new BcodeResult(12513000000.0, '百万円')) - expect(QuarterCache.get(ticker, yearQuarter)['net_sales']).toBe( + expect(QuarterCache.getData(ticker, yearQuarter)['net_sales']).toBe( 12513000000.0 ) expect(QuarterPropertyCache.get()).not.toBeNull() @@ -47,14 +47,16 @@ describe('bcodeQuarter', () => { test('(quarter, LY, LQ)', () => { const ticker = '2371' const period = new YearQuarter(2018, 1) - expect(QuarterCache.get(ticker, period)).toBeNull() + expect(QuarterCache.getData(ticker, period)).toBeNull() expect(QuarterPropertyCache.get()).toBeNull() const client = new CachingBuffettCodeApiClientV2('token') const result = bcodeQuarter(client, ticker, 'LY', 'LQ', 'net_sales', false) expect(result).toEqual(new BcodeResult(12513000000.0, '百万円')) - expect(QuarterCache.get(ticker, period)['net_sales']).toBe(12513000000.0) + expect(QuarterCache.getData(ticker, period)['net_sales']).toBe( + 12513000000.0 + ) expect(QuarterPropertyCache.get()).not.toBeNull() }) }) diff --git a/src/entities/v3/quarter.ts b/src/entities/v3/quarter.ts new file mode 100644 index 0000000..51f90f5 --- /dev/null +++ b/src/entities/v3/quarter.ts @@ -0,0 +1,15 @@ +export class Quarter { + constructor(readonly data: object, readonly columnDescription: object) { + // noop + } + + static fromResponse(response: object): Quarter { + return new Quarter(response['data'], response['column_description']) + } + + static fromBulkResponse(response: object): Quarter[] { + return Object.keys(response['data']).map(key => { + return new Quarter(response['data'][key], response['column_description']) + }) + } +} diff --git a/src/services/csv-exporter.test.ts b/src/services/csv-exporter.test.ts index f6219ab..fa5a316 100644 --- a/src/services/csv-exporter.test.ts +++ b/src/services/csv-exporter.test.ts @@ -30,7 +30,7 @@ describe('generateData', () => { const from = '2018Q1' const to = '2018Q1' const today = new Date('2020-09-23') - expect(QuarterCache.get(ticker, new YearQuarter(2018, 1))).toBeNull() + expect(QuarterCache.getData(ticker, new YearQuarter(2018, 1))).toBeNull() const data = CsvExporter.generateData(ticker, from, to, today) @@ -50,7 +50,7 @@ describe('generateData', () => { 1.0 ]) expect( - QuarterCache.get(ticker, new YearQuarter(2018, 1))['net_sales'] + QuarterCache.getData(ticker, new YearQuarter(2018, 1))['net_sales'] ).toBe(12513000000.0) }) @@ -59,7 +59,9 @@ describe('generateData', () => { const from = '2018Q1' const to = '2018Q1' const today = new Date('2020-09-23') - expect(QuarterCache.get(ticker, new YearQuarter(2018, 1))).not.toBeNull() + expect( + QuarterCache.getData(ticker, new YearQuarter(2018, 1)) + ).not.toBeNull() const data = CsvExporter.generateData(ticker, from, to, today) @@ -79,7 +81,7 @@ describe('generateData', () => { 1.0 ]) expect( - QuarterCache.get(ticker, new YearQuarter(2018, 1))['net_sales'] + QuarterCache.getData(ticker, new YearQuarter(2018, 1))['net_sales'] ).toBe(12513000000.0) }) }) diff --git a/src/services/quarter-cache.test.ts b/src/services/quarter-cache.test.ts index 3ebde77..3b1c609 100644 --- a/src/services/quarter-cache.test.ts +++ b/src/services/quarter-cache.test.ts @@ -10,24 +10,33 @@ test('key', () => { }) const quarter = quarterFixture['2371'][0] +const columnDescription = quarterFixture['column_description'] const yearQuarter = new YearQuarter(2018, 1) beforeEach(() => { jest.clearAllMocks() }) -test('get', () => { +test('getData', () => { getMock.mockReturnValueOnce(JSON.stringify(quarter)) - expect(QuarterCache.get('2371', yearQuarter)).toEqual(quarter) - expect(QuarterCache.get('9999', yearQuarter)).toBeNull() + expect(QuarterCache.getData('2371', yearQuarter)).toEqual(quarter) + expect(QuarterCache.getData('9999', yearQuarter)).toBeNull() expect(getMock).toBeCalledTimes(2) expect(getMock).nthCalledWith(1, 'quarter-2371-2018Q1') expect(getMock).nthCalledWith(2, 'quarter-9999-2018Q1') }) -test('put', () => { - QuarterCache.put('2371', quarter) +test('getColumnDescription', () => { + getMock.mockReturnValueOnce(JSON.stringify(columnDescription)) + expect(QuarterCache.getColumnDescription()).toEqual(columnDescription) + + expect(getMock).toBeCalledTimes(1) + expect(getMock).toBeCalledWith('quarter-column-description') +}) + +test('putData', () => { + QuarterCache.putData('2371', quarter) expect(putMock).toBeCalledTimes(1) expect(putMock).toBeCalledWith( @@ -36,3 +45,14 @@ test('put', () => { 21600 ) }) + +test('putColumnDescription', () => { + QuarterCache.putColumnDescription(columnDescription) + + expect(putMock).toBeCalledTimes(1) + expect(putMock).toBeCalledWith( + 'quarter-column-description', + JSON.stringify(columnDescription), + 21600 + ) +}) diff --git a/src/services/quarter-cache.ts b/src/services/quarter-cache.ts index d6593f7..1166e8b 100644 --- a/src/services/quarter-cache.ts +++ b/src/services/quarter-cache.ts @@ -1,3 +1,4 @@ +import { Quarter } from '~/entities/v3/quarter' import { YearQuarter } from '~/fiscal-periods/year-quarter' export class QuarterCache { @@ -11,7 +12,11 @@ export class QuarterCache { return `${this.prefix}-${ticker}-${yearQuarter}` } - static get(ticker: string, yearQuarter: YearQuarter): object[] | null { + static columnDescriptionKey(): string { + return `${this.prefix}-column-description` + } + + static getData(ticker: string, yearQuarter: YearQuarter): object | null { const cache = CacheService.getUserCache() const key = this.key(ticker, yearQuarter) const cached = cache.get(key) @@ -22,7 +27,27 @@ export class QuarterCache { return JSON.parse(cached) } - static put( + static getColumnDescription(): object | null { + const cache = CacheService.getUserCache() + const cached = cache.get(this.columnDescriptionKey()) + if (!cached) { + return null + } + + return JSON.parse(cached) + } + + static get(ticker: string, yearQuarter: YearQuarter): Quarter | null { + const cachedData = this.getData(ticker, yearQuarter) + const cachedColumnDescription = this.getColumnDescription() + if (!cachedData || !cachedColumnDescription) { + return null + } + + return new Quarter(cachedData, cachedColumnDescription) + } + + static putData( ticker: string, quarter: object, expirationInSeconds = 21600 @@ -35,4 +60,25 @@ export class QuarterCache { const key = this.key(ticker, yearQuarter) cache.put(key, JSON.stringify(quarter), expirationInSeconds) } + + static putColumnDescription( + columnDescription: object, + expirationInSeconds = 21600 + ): void { + const cache = CacheService.getUserCache() + cache.put( + this.columnDescriptionKey(), + JSON.stringify(columnDescription), + expirationInSeconds + ) + } + + static put( + ticker: string, + quarter: Quarter, + expirationInSeconds = 21600 + ): void { + this.putData(ticker, quarter.data, expirationInSeconds) + this.putColumnDescription(quarter.columnDescription, expirationInSeconds) + } }