diff --git a/functions/package.json b/functions/package.json index ec2d122..75168c8 100644 --- a/functions/package.json +++ b/functions/package.json @@ -1,6 +1,6 @@ { "name": "functions", - "version": "1.0.1", + "version": "1.0.2", "scripts": { "lint": "tslint --project tsconfig.json", "build": "tsc", diff --git a/functions/src/models/newsriver.ts b/functions/src/models/newsriver.ts index 6c8c690..8511114 100644 --- a/functions/src/models/newsriver.ts +++ b/functions/src/models/newsriver.ts @@ -4,6 +4,8 @@ import properties = require('../common/properties'); const databases: Record = {}; +const latestResults: Record> = {}; +const lastUpdateMsForLanguage: Record = {}; let initCalled = false; export async function initialize() { @@ -14,6 +16,8 @@ export async function initialize() { projectProperties.database, `${projectProperties.collection}_${language}` ); + lastUpdateMsForLanguage[language] = 0; + latestResults[language] = null; } } @@ -22,12 +26,16 @@ export async function newsForLanguage(language: string) { throw new Error('`initialize` not called'); if (language ! in properties.languages) throw new RangeError(`invalid language "${language}"`); + if (Math.floor((Date.now() - lastUpdateMsForLanguage[language]) / 60000) <= 15) + return latestResults[language]; + lastUpdateMsForLanguage[language] = Date.now(); const collection = databases[language].collection; - const snapshot = await collection.get(); + const snapshot = await collection.orderBy("discoverDate", "desc").get(); const data = new Array(); snapshot.forEach(item => { if (item.data() !== null) data.push(item.data() as NewsriverData) }); + latestResults[language] = data; return data; } diff --git a/functions/src/updater.ts b/functions/src/updater.ts index edf4dbd..5586948 100644 --- a/functions/src/updater.ts +++ b/functions/src/updater.ts @@ -12,7 +12,6 @@ export class Updater { private readonly language: string; private readonly auth: string; private _path: string | undefined; - private readonly network: AxiosInstance get path(): Promise { if (this._path === undefined) @@ -30,6 +29,20 @@ export class Updater { this._path = undefined; } + get network(): AxiosInstance { + return axios.create({ + baseURL: 'https://api.newsriver.io/v2/', + headers: { + 'Authorization': this.auth, + 'Content-Type': 'application/json' + }, + withCredentials: true, + responseType: 'json', + httpsAgent: new https.Agent({ keepAlive: true }), + timeout: 500000 + }); + } + constructor(db: FirebaseFirestore.Firestore | null, collectionName: string | null, searchTerms: Array, @@ -42,17 +55,6 @@ export class Updater { this.language = language; this.auth = auth; this.interval = intervalMins * 60 * 1000; - this.network = axios.create({ - baseURL: 'https://api.newsriver.io/v2/', - headers: { - 'Authorization': this.auth, - 'Content-Type': 'application/json' - }, - withCredentials: true, - responseType: 'json', - httpsAgent: new https.Agent({ keepAlive: true }), - timeout: 500000 - }); this.doRequest() .then(response => { this.updateData(response) @@ -78,16 +80,9 @@ export class Updater { try { for (const element of content) { try { - const document = await this.db.collection(this.collectionName).doc(element.id).get(); - console.debug(`Item with id ${element.id} ${document.exists ? 'exists' : 'does not exist'}`) - if (!document.exists) - firebaseHelper.firestore.createDocumentWithID(this.db, this.collectionName, element.id, element) - .then(created => console.debug(`Item with ID: ${element.id} was ${created ? 'created' : 'not created'}`)) - .catch(err => console.error(`Error while creating document ${err}`)); - else - firebaseHelper.firestore.updateDocument(this.db, this.collectionName, element.id, element) - .then(updated => console.debug(`Item with ID ${element.id} was ${updated ? 'updated' : 'not updated'}`)) - .catch(err => console.error(`Error while updating document ${err}`)); + firebaseHelper.firestore.createDocumentWithID(this.db, this.collectionName, element.id, element) + .then(created => console.debug(`Item with ID: ${element.id} was ${created ? 'created' : 'not created'}`)) + .catch(err => console.error(`Error while creating document ${err}`)); } catch (err) { console.warn(`Error while creating/updating document - ${err}`); }