-
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.
Merge pull request #101 from brown-ccv/firebase-count-function
feat: year aggregation cloud function
- Loading branch information
Showing
7 changed files
with
5,694 additions
and
1 deletion.
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
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,2 @@ | ||
node_modules/ | ||
*.local |
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,15 @@ | ||
exports.calculateYearCounts = (publications) => { | ||
const yearCounts = {}; | ||
|
||
publications.forEach((publication) => { | ||
const year = publication.year; | ||
if (year) { | ||
yearCounts[year] = (yearCounts[year] || 0) + 1; | ||
} | ||
}); | ||
|
||
return Object.entries(yearCounts).map(([label, count]) => ({ | ||
label, | ||
count, | ||
})); | ||
}; |
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,45 @@ | ||
const { calculateYearCounts } = require('./aggregator'); | ||
|
||
describe('calculateYearCounts', () => { | ||
test('correctly aggregates counts by year', () => { | ||
const testData = [ | ||
{ year: 2021, title: 'Paper 1' }, | ||
{ year: 2021, title: 'Paper 2' }, | ||
{ year: 2022, title: 'Paper 3' }, | ||
{ year: 2023, title: 'Paper 4' }, | ||
{ year: 2024, title: 'Paper 5' }, | ||
{ year: 2024, title: 'Paper 6' }, | ||
]; | ||
|
||
const result = calculateYearCounts(testData); | ||
|
||
expect(result).toEqual([ | ||
{ label: '2021', count: 2 }, | ||
{ label: '2022', count: 1 }, | ||
{ label: '2023', count: 1 }, | ||
{ label: '2024', count: 2 }, | ||
]); | ||
}); | ||
|
||
test('handles empty input', () => { | ||
const result = calculateYearCounts([]); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
test('handles missing year data', () => { | ||
const testData = [ | ||
{ year: 2021, title: 'Paper 1' }, | ||
{ title: 'Paper 2' }, | ||
{ year: 2021, title: 'Paper 3' }, | ||
{ year: null, title: 'Paper 4' }, | ||
{ year: 2023, title: 'Paper 5' }, | ||
]; | ||
|
||
const result = calculateYearCounts(testData); | ||
|
||
expect(result).toEqual([ | ||
{ label: '2021', count: 2 }, | ||
{ label: '2023', count: 1 }, | ||
]); | ||
}); | ||
}); |
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,42 @@ | ||
const { onDocumentWritten } = require('firebase-functions/v2/firestore'); | ||
const { initializeApp } = require('firebase-admin/app'); | ||
const { getFirestore } = require('firebase-admin/firestore'); | ||
const { calculateYearCounts } = require('./aggregator'); | ||
|
||
// Initialize Firebase Admin SDK | ||
initializeApp(); | ||
const db = getFirestore(); | ||
|
||
// Calculate and save aggregations | ||
async function calculateAggregatedCounts() { | ||
const publicationsSnapshot = await db.collection('publications').get(); | ||
const publications = publicationsSnapshot.docs.map(doc => doc.data()); | ||
|
||
// Count of docs by year | ||
const aggregatedCounts = calculateYearCounts(publications); | ||
|
||
// Save the aggregation(s) | ||
await db.collection('aggregations').doc('publicationCounts').set({ | ||
counts: aggregatedCounts, | ||
}); | ||
} | ||
|
||
// onDocumentWritten triggers on create, update, or delete | ||
exports.aggregatePublicationsOnWrite = onDocumentWritten( | ||
'publications/{docId}', | ||
async (event) => { | ||
try { | ||
// log before and after data | ||
const beforeData = event.data.before.data(); | ||
const afterData = event.data.after.data(); | ||
|
||
console.log(`Document written: ${event.params.docId}`); | ||
console.log('Before data:', beforeData); | ||
console.log('After data:', afterData); | ||
|
||
await calculateAggregatedCounts(); | ||
} catch (error) { | ||
console.error('Error recalculating counts on document write:', error); | ||
} | ||
} | ||
); |
Oops, something went wrong.