-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: sample queries cache (#2239)
- Loading branch information
Showing
13 changed files
with
166 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import localforage from 'localforage'; | ||
import { IHistoryItem } from '../../types/history'; | ||
|
||
const historyStorage = localforage.createInstance({ | ||
storeName: 'history', | ||
name: 'GE_V4' | ||
}); | ||
|
||
function historyItemHasExpired(createdAt: string): boolean { | ||
const ageInDays: number = 30; | ||
const dateToCompare = new Date(); | ||
dateToCompare.setDate(dateToCompare.getDate() - ageInDays); | ||
const expiryDate = dateToCompare.getTime(); | ||
const createdTime = new Date(createdAt).getTime(); | ||
return (createdTime < expiryDate); | ||
} | ||
|
||
export const historyCache = (function () { | ||
|
||
const writeHistoryData = (historyItem: IHistoryItem) => { | ||
historyStorage.setItem(historyItem.createdAt, historyItem); | ||
} | ||
|
||
const readHistoryData = async (): Promise<IHistoryItem[]> => { | ||
let historyData: IHistoryItem[] = []; | ||
const keys = await historyStorage.keys(); | ||
for (const creationTime of keys) { | ||
if (historyItemHasExpired(creationTime)) { | ||
historyStorage.removeItem(creationTime); | ||
} else { | ||
const historyItem = await historyStorage.getItem(creationTime)! as IHistoryItem; | ||
historyData = [...historyData, historyItem]; | ||
} | ||
} | ||
return historyData; | ||
} | ||
|
||
const removeHistoryData = async (historyItem: IHistoryItem) => { | ||
await historyStorage.removeItem(historyItem.createdAt); | ||
return true; | ||
}; | ||
|
||
const bulkRemoveHistoryData = async (listOfKeys: string[]) => { | ||
historyStorage.iterate((_value, key) => { | ||
if (listOfKeys.includes(key)) { | ||
historyStorage.removeItem(key); | ||
} | ||
}).then(() => { | ||
return true; | ||
}); | ||
}; | ||
|
||
return { | ||
writeHistoryData, | ||
bulkRemoveHistoryData, | ||
removeHistoryData, | ||
readHistoryData | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ISampleQuery } from '../../types/query-runner'; | ||
import { samplesCache } from './samples.cache'; | ||
|
||
jest.mock('localforage', () => ({ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
config: () => { }, | ||
createInstance: () => ({ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
getItem: () => { }, | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
setItem: () => { } | ||
}) | ||
})); | ||
|
||
const queries: ISampleQuery[] = []; | ||
describe('Samples Cache should', () => { | ||
it('return the same queries after queries added', async () => { | ||
expect(queries.length).toBe(0); | ||
queries.push( | ||
{ | ||
category: 'Getting Started', | ||
method: 'GET', | ||
humanName: 'my profile', | ||
requestUrl: '/v1.0/me', | ||
docLink: 'https://learn.microsoft.com/en-us/graph/api/user-get', | ||
skipTest: false | ||
}, | ||
); | ||
samplesCache.saveSamples(queries); | ||
const samplesData = await samplesCache.readSamples(); | ||
expect(samplesData).not.toBe(queries); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import localforage from 'localforage'; | ||
import { ISampleQuery } from '../../types/query-runner'; | ||
|
||
const samplesStorage = localforage.createInstance({ | ||
storeName: 'samples', | ||
name: 'GE_V4' | ||
}); | ||
|
||
const SAMPLE_KEY = 'sample-queries'; | ||
|
||
export const samplesCache = (function () { | ||
|
||
const saveSamples = async (queries: ISampleQuery[]) => { | ||
await samplesStorage.setItem(SAMPLE_KEY, JSON.stringify(queries)); | ||
} | ||
|
||
const readSamples = async (): Promise<ISampleQuery[]> => { | ||
const items = await samplesStorage.getItem(SAMPLE_KEY) as string; | ||
if (items) { | ||
return JSON.parse(items); | ||
} | ||
return []; | ||
} | ||
|
||
return { | ||
saveSamples, | ||
readSamples | ||
} | ||
})(); |