Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect aggregation #102

Merged
merged 6 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,33 @@ const { calculateYearCounts } = require('./aggregator');
initializeApp();
const db = getFirestore();

// Calculate and save aggregations
// Calculate and save aggregations
async function calculateAggregatedCounts() {
const publicationsSnapshot = await db.collection('publications').get();
const publications = publicationsSnapshot.docs.map(doc => doc.data());
const publications = publicationsSnapshot.docs.map((doc) => doc.data());

// Count of docs by year
// Count of docs by year
const aggregatedCounts = calculateYearCounts(publications);

// Save the aggregation(s)
await db.collection('aggregations').doc('publicationCounts').set({
await db.collection('aggregations').doc('publicationsByYear').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();
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);
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);
}
await calculateAggregatedCounts();
} catch (error) {
console.error('Error recalculating counts on document write:', error);
}
);
});
83 changes: 45 additions & 38 deletions src/components/BarChart.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import { Vega } from 'react-vega';
import { getAggregation } from '../utils/firebase.ts';

const bar_color = '#00c398'; // ccv green
const bar_hover_color = '#ffc72c'; // ccv yellow
Expand Down Expand Up @@ -431,44 +432,50 @@ const generateBarPlotWithCumuSum = (dataJson, xLabel) => {
return spec;
};

const inputJson = [
{ label: '2008', count: 1 },
{ label: '2009', count: 2 },
{ label: '2010', count: 6 },
{ label: '2011', count: 3 },
{ label: '2012', count: 8 },
{ label: '2013', count: 18 },
{ label: '2014', count: 35 },
{ label: '2015', count: 41 },
{ label: '2016', count: 35 },
{ label: '2017', count: 60 },
{ label: '2018', count: 70 },
{ label: '2019', count: 60 },
{ label: '2020', count: 70 },
{ label: '2021', count: 58 },
{ label: '2022', count: 62 },
{ label: '2023', count: 80 },
{ label: '2024', count: 5 },
];

export function CountsByYearPlot({ type }) {
let vegaSpec = {};
export const CountsByYearPlot = ({ type }) => {
const xLabel = 'Year';
if (type === 'bar') {
vegaSpec = generateBarPlot({
data: inputJson,
xLabel: xLabel,
yLabel: 'Publications',
});
} else if (type === 'cumu-line') {
vegaSpec = generateCumuSumPlot({
data: inputJson,
xLabel: xLabel,
yLabel: 'Cumulative Publications',
});
} else if (type === 'bar-cumu-line') {
vegaSpec = generateBarPlotWithCumuSum(inputJson, xLabel);
const [vegaSpec, setVegaSpec] = useState(null);
const [loading, setLoading] = useState(true);

useEffect(() => {
const fetchData = async () => {
try {
const fetchedData = await getAggregation({ documentName: 'publicationsByYear' });
// console.log(fetchedData);
let spec;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment! can delete

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleted!

if (type === 'bar') {
spec = generateBarPlot({
data: fetchedData,
xLabel: xLabel,
yLabel: 'Publications',
});
} else if (type === 'cumu-line') {
spec = generateCumuSumPlot({
data: fetchedData,
xLabel: xLabel,
yLabel: 'Cumulative Publications',
});
} else if (type === 'bar-cumu-line') {
spec = generateBarPlotWithCumuSum(fetchedData, xLabel);
}
setVegaSpec(spec);
} catch (error) {
console.error('Error fetching document:', error);
} finally {
setLoading(false);
}
};

fetchData();
}, [type]);

if (loading) {
return <div>Loading...</div>;
}

if (!vegaSpec) {
return <div>Error: Invalid plot type specified</div>;
}

return <Vega spec={vegaSpec} />;
}
};
12 changes: 12 additions & 0 deletions src/utils/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,15 @@ const getUser = async (email): Promise<User> => {

return docSnap.data() as User;
};

export async function getAggregation({ documentName }) {
const docRef = doc(db, 'aggregations', documentName);
const docSnap = await getDoc(docRef);

if (docSnap.exists()) {
return docSnap.data()['counts'];
} else {
console.log('Document not found.');
return null;
}
}
Loading