Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
When querying data, it is now sorted by discover date. In addition, t…
…he updater now "creates" the document always
  • Loading branch information
Javinator9889 committed Jul 1, 2020
1 parent e2de789 commit bb304f6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion 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",
Expand Down
10 changes: 9 additions & 1 deletion functions/src/models/newsriver.ts
Expand Up @@ -4,6 +4,8 @@ import properties = require('../common/properties');


const databases: Record<string, Database> = {};
const latestResults: Record<string, Array<NewsriverData>> = {};
const lastUpdateMsForLanguage: Record<string, number> = {};
let initCalled = false;

export async function initialize() {
Expand All @@ -14,6 +16,8 @@ export async function initialize() {
projectProperties.database,
`${projectProperties.collection}_${language}`
);
lastUpdateMsForLanguage[language] = 0;
latestResults[language] = null;
}
}

Expand All @@ -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<NewsriverData>();
snapshot.forEach(item => {
if (item.data() !== null)
data.push(item.data() as NewsriverData)
});
latestResults[language] = data;
return data;
}
39 changes: 17 additions & 22 deletions functions/src/updater.ts
Expand Up @@ -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<string> {
if (this._path === undefined)
Expand All @@ -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<string>,
Expand All @@ -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)
Expand All @@ -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}`);
}
Expand Down

0 comments on commit bb304f6

Please sign in to comment.