-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
238872e
commit 9448a2f
Showing
10 changed files
with
459 additions
and
35 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
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
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,36 @@ | ||
import express from "express"; | ||
import { StructuredData } from "../../ingest"; | ||
import { getDataset } from "../../data_fetcher"; | ||
|
||
const serveFile = (filename: string, callback?: () => any) => { | ||
const app = express(); | ||
app.use("/annotations", express.static(filename)); | ||
return app.listen(8080, () => { | ||
console.log(`Serving file ${filename}`); | ||
if (callback) { callback(); } | ||
}); | ||
}; | ||
|
||
test("data updates when files on the server change", async (done) => { | ||
const path = require("path"); | ||
process.env["UPDATE_INTERVAL"] = "5000"; | ||
process.env["UPDATE_AT_MIDNIGHT"] = "false"; | ||
process.env["SERVER_LIFETIME_LENGTH"] = "20000"; | ||
process.env["FILE_URL"] = "http://localhost:8080/annotations/tair.gaf.gz"; | ||
const januaryServer = await serveFile(path.join(__dirname + "/january"), | ||
() => { | ||
const backend = require(path.join(__dirname + "/../../index.ts")); | ||
const januaryData: StructuredData = getDataset(); | ||
|
||
setTimeout(async () => { | ||
januaryServer.close(); | ||
let februaryServer = await serveFile(path.join(__dirname + "/february")); | ||
setTimeout(() => februaryServer.close(), 10000); | ||
}, 10000); | ||
setTimeout(() => { | ||
const februaryData: StructuredData = getDataset(); | ||
expect(januaryData).not.toEqual(februaryData); | ||
done(); | ||
}, 12000); | ||
}); | ||
}, 30000); |
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
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,71 @@ | ||
import express from "express"; | ||
import { StructuredData } from "../ingest"; | ||
import {startPeriodicallyCalling, getDataset} from "../data_fetcher"; | ||
|
||
describe("Periodically calling functions", () => { | ||
|
||
it("should call once for each period", () => { | ||
let foo = jest.fn(); | ||
startPeriodicallyCalling(foo, 300, new Date()); | ||
setTimeout(() => expect(foo).toHaveBeenCalledTimes(3), 1000); | ||
}); | ||
|
||
it("should only start calling after the start date", () => { | ||
let bar = jest.fn(); | ||
const startDate = new Date(); | ||
startDate.setMilliseconds(startDate.getMilliseconds() + 500); | ||
startPeriodicallyCalling(bar, 200, startDate); | ||
setTimeout(() => expect(bar).toHaveBeenCalledTimes(0), 400); | ||
setTimeout(() => expect(bar).toHaveBeenCalledTimes(2), 1000); | ||
}); | ||
|
||
it("should stop running when a lifetime is provided", () => { | ||
let foobar = jest.fn(); | ||
startPeriodicallyCalling(foobar, 200, new Date(), 300); | ||
setTimeout(() => expect(foobar).toHaveBeenCalledTimes(1), 500); | ||
}); | ||
}); | ||
|
||
test("The data getter reflects updates when the server changes", | ||
async () => { | ||
const path = require("path"); | ||
|
||
process.env["UPDATE_INTERVAL"] = "5000"; | ||
process.env["UPDATE_AT_MIDNIGHT"] = "false"; | ||
process.env["SERVER_LIFETIME_LENGTH"] = "20000"; | ||
process.env["FILE_URL"] = "http://localhost:8080/annotations/tair.gaf.gz"; | ||
|
||
const januaryServer = serveFile(path.join(__dirname + "/january")); | ||
|
||
await new Promise(() => setTimeout(() => { | ||
januaryServer.close(); | ||
let februaryServer = serveFile(path.join(__dirname + "/february")); | ||
setTimeout(() => februaryServer.close(), 10000); | ||
}, 10000)); | ||
|
||
await new Promise(() => setTimeout(async () => { | ||
const backend = require(path.join(__dirname + "/../../src/index.ts")); | ||
|
||
const januaryData: StructuredData = getDataset(); | ||
await new Promise(() => setTimeout(() => { | ||
const februaryData: StructuredData = getDataset(); | ||
console.log(januaryData == februaryData); | ||
console.log(januaryData === februaryData); | ||
console.log(JSON.stringify(januaryData).split('at').length - 1); | ||
console.log(JSON.stringify(februaryData).split('at').length - 1); | ||
expect(januaryData).not.toEqual(februaryData); | ||
expect(januaryData).toEqual(februaryData); | ||
}, 12000)); | ||
}, 5000)); | ||
}, 25000); | ||
|
||
const serveFile = (filename: string, callback?: () => any) => { | ||
const app = express(); | ||
app.use("/annotations", express.static(filename)); | ||
return app.listen(8080, () => { | ||
console.log(`Serving file ${filename}`); | ||
if (callback) { | ||
callback(); | ||
} | ||
}); | ||
}; |
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,73 @@ | ||
import {readFileSync} from "fs"; | ||
import {resolve} from "path"; | ||
import {ingestData, StructuredData, UnstructuredText} from "./ingest"; | ||
|
||
let mostRecentDataset: StructuredData; | ||
|
||
export const getDataset = (): StructuredData => { return mostRecentDataset; }; | ||
|
||
export const downloadData = (dataUrl: string) => { | ||
const { spawnSync } = require("child_process"); | ||
const path = require("path"); | ||
const filepath = path.join(__dirname + "/../assets/"); | ||
spawnSync("cd " + filepath + " ; rm tair.gaf"); | ||
spawnSync("cd " + filepath + " ; wget " + dataUrl); | ||
spawnSync("cd " + filepath + " ; gunzip *.gz"); | ||
} | ||
|
||
export const updateData = () => { | ||
const file_url = process.env["FILE_URL"] || "http://current.geneontology.org/annotations/tair.gaf.gz"; | ||
downloadData(file_url); | ||
console.log("Begin reading data"); | ||
const genesText = readFileSync(resolve("assets/gene-types.txt")).toString(); | ||
const annotationsText = readFileSync(resolve("assets/tair.gaf")).toString(); | ||
const unstructuredText: UnstructuredText = {genesText, annotationsText}; | ||
const maybeDataset = ingestData(unstructuredText); | ||
if (!maybeDataset) throw new Error("failed to parse data"); | ||
const dataset: StructuredData = maybeDataset; | ||
mostRecentDataset = dataset; | ||
console.log("Finished parsing data"); | ||
}; | ||
|
||
export const startPeriodicallyCalling = (fn: (...args: any[]) => void, interval: number = (1000 * 60 * 60 * 24), startDate: Date = new Date(), lifetime?: number) => { | ||
const now = new Date(); | ||
|
||
const update_at_midnight: string = process.env["UPDATE_AT_MIDNIGHT"] || "true"; | ||
if (update_at_midnight === "true") { | ||
startDate = getTomorrowMorning(); | ||
} | ||
|
||
let timer; | ||
if (isSameTimeOfDay(startDate, now)) { | ||
timer = setInterval(fn, interval); | ||
} else { | ||
let difference = startDate.getTime() - now.getTime(); | ||
if (difference > 0) { | ||
if (lifetime) { | ||
setTimeout(startPeriodicallyCalling, difference, fn, interval, startDate, lifetime); | ||
} else { | ||
setTimeout(startPeriodicallyCalling, difference, fn, interval, startDate); | ||
} | ||
return; | ||
} else { | ||
timer = setInterval(fn, interval); // Just begins the updates now if the startDate is in the past | ||
} | ||
} | ||
|
||
if (lifetime) { | ||
setTimeout(clearInterval, lifetime, timer); | ||
} | ||
}; | ||
|
||
export 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 isSameTimeOfDay = (time1, time2) => { | ||
return time1.getHours() === time2.getHours() && time1.getMinutes() === time2.getMinutes(); | ||
} |
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