Skip to content

Commit

Permalink
Refactors the bad design
Browse files Browse the repository at this point in the history
  • Loading branch information
scourgemancer authored and nicholastmosher committed Feb 19, 2020
1 parent fde61c6 commit f2a9bbe
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 45 deletions.
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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} 🔥`);
});
});
47 changes: 47 additions & 0 deletions src/utils/data_fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {GeneMap, IAnnotation, GroupedAnnotations, IGene, readData} from "./ingest";

let mostRecentGeneMap: GeneMap;
let mostRecentAnnotations: IAnnotation[];
let mostRecentGroupedAnnotations: GroupedAnnotations<Set<IGene>>;

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<Set<IGene>> => { return mostRecentGroupedAnnotations; };
44 changes: 0 additions & 44 deletions src/utils/ingest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Set<IGene>> => { return mostRecentGroupedAnnotations; };

0 comments on commit f2a9bbe

Please sign in to comment.