From f2a9bbe88847d1de121b51dee7a749b5cf087a7a Mon Sep 17 00:00:00 2001 From: Timothy Geary Date: Mon, 27 Jan 2020 18:25:24 -0500 Subject: [PATCH] Refactors the bad design --- src/index.ts | 8 ++++++- src/utils/data_fetcher.ts | 47 +++++++++++++++++++++++++++++++++++++++ src/utils/ingest.ts | 44 ------------------------------------ 3 files changed, 54 insertions(+), 45 deletions(-) create mode 100644 src/utils/data_fetcher.ts diff --git a/src/index.ts b/src/index.ts index 89610b8..94e7652 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,13 @@ import { app } from './server'; +import { updateData, startPeriodicUpdates } from './utils/data_fetcher'; + +updateData(); +const default_interval: string = (1000 * 60 * 60 * 24).toString(); +const interval: string = process.env["UPDATE_INTERVAL"] || default_interval; +startPeriodicUpdates(parseInt(interval)); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`🚢 Now listening on 0.0.0.0:${PORT} 🔥`); -}); \ No newline at end of file +}); diff --git a/src/utils/data_fetcher.ts b/src/utils/data_fetcher.ts new file mode 100644 index 0000000..a781d31 --- /dev/null +++ b/src/utils/data_fetcher.ts @@ -0,0 +1,47 @@ +import {GeneMap, IAnnotation, GroupedAnnotations, IGene, readData} from "./ingest"; + +let mostRecentGeneMap: GeneMap; +let mostRecentAnnotations: IAnnotation[]; +let mostRecentGroupedAnnotations: GroupedAnnotations>; + +export const updateData = () => { + const { execSync } = require("child_process"); + const stdout = execSync("wget http://current.geneontology.org/annotations/tair.gaf.gz ; gunzip tair.gaf.gz"); + + const { geneMap, annotations, groupedAnnotations } = readData(); + mostRecentGeneMap = geneMap; + mostRecentAnnotations = annotations; + mostRecentGroupedAnnotations = groupedAnnotations; +}; + +export const startPeriodicUpdates = (interval: number = (1000 * 60 * 60 * 24), startDate: Date = getTomorrowMorning()) => { + const now = new Date(); + if (sameTimeOfDay(startDate, now)) { + setInterval(updateData, interval); + } else { + let difference = startDate.getTime() - now.getTime(); + if (difference > 0) { + setTimeout(startPeriodicUpdates, difference, interval, startDate); + } + setInterval(updateData, interval); // Just begins the updates now if the startDate is in the past + } +}; + +const getTomorrowMorning = (): Date => { + const tomorrowMorning: Date = new Date(); + tomorrowMorning.setDate(tomorrowMorning.getDate() + 1); + tomorrowMorning.setHours(0); + tomorrowMorning.setMinutes(0); + tomorrowMorning.setSeconds(0); + return tomorrowMorning; +}; + +const sameTimeOfDay = (time1, time2) => { + return time1.getHours() === time2.getHours() && time1.getMinutes() === time2.getMinutes(); +} + +export const getGeneMap = (): GeneMap => { return mostRecentGeneMap; }; + +export const getAnnotations = (): IAnnotation[] => { return mostRecentAnnotations; }; + +export const getGroupedAnnotations = (): GroupedAnnotations> => { return mostRecentGroupedAnnotations; }; diff --git a/src/utils/ingest.ts b/src/utils/ingest.ts index 7a2086d..251f106 100644 --- a/src/utils/ingest.ts +++ b/src/utils/ingest.ts @@ -462,47 +462,3 @@ export const ingestData = (raw: UnstructuredText): StructuredData | null => { // Return all structured data return {genes, annotations, raw}; }; - - -const { geneMap, annotations, groupedAnnotations } = readData(); -let mostRecentGeneMap = geneMap; -let mostRecentAnnotations = annotations; -let mostRecentGroupedAnnotations = groupedAnnotations; - -export const updateData = () => { - const { execSync } = require("child_process"); - const stdout = execSync("wget http://current.geneontology.org/annotations/tair.gaf.gz ; gunzip tair.gaf.gz"); - - const { geneMap, annotations, groupedAnnotations } = readData(); - mostRecentGeneMap = geneMap; - mostRecentAnnotations = annotations; - mostRecentGroupedAnnotations = groupedAnnotations; -}; - -export const startPeriodicUpdates = () => { - let nextDate = new Date(); - // Calls updateData daily, by default, if its the first minute of the day - if (nextDate.getMinutes() === 0) { - let interval = process.env["UPDATE_INTERVAL"]; - if (interval) { - const runningInterval = setInterval(updateData, parseInt(interval)); - } else { - const runningInterval = setInterval(updateData, (1000 * 60 * 60 * 24)); // Default interval is one day in ms - } - } else { - // Otherwise wait until the start of the next day to begin the periodic updates - nextDate.setDate(nextDate.getDate() + 1); - nextDate.setHours(0); - nextDate.setMinutes(0); - nextDate.setSeconds(0); - let difference = nextDate.getTime() - new Date().getTime(); - setTimeout(startPeriodicUpdates, difference); - } -}; -startPeriodicUpdates(); - -export const getGeneMap = (): GeneMap => { return mostRecentGeneMap; }; - -export const getAnnotations = (): IAnnotation[] => { return mostRecentAnnotations; }; - -export const getGroupedAnnotations = (): GroupedAnnotations> => { return mostRecentGroupedAnnotations; };