Skip to content

Instantly share code, notes, and snippets.

@cdinu
Created July 12, 2018 10:48
Show Gist options
  • Save cdinu/d26c0b8506299c23be53abe14fe0a1f5 to your computer and use it in GitHub Desktop.
Save cdinu/d26c0b8506299c23be53abe14fe0a1f5 to your computer and use it in GitHub Desktop.
GetStatsForSession.js
function getStats(code) {
const flatten = (arr, depth = 1) => arr.reduce((a, v) => a.concat(depth > 1 && Array.isArray(v) ? flatten(v, depth - 1) : v), []);
const words = (str='', pattern = /[^a-zA-Z-]+/) => str.split(pattern).filter(Boolean);
const stats = {};
const { _id: session, document } = db.sessions.findOne({ code });
const { pages } = db.documents.findOne({ _id: document });
stats.pageCount = pages.length;
const pageObjects = db.pages.find({ _id: { $in: pages } });
const atoms = flatten(pageObjects.map(({ atoms }) => atoms));
stats.atomCount = atoms.length;
const answers = db.answers.find({ atom: { $in: atoms } }).toArray();
stats.answerCount = answers.length;
const notes = db.notes.find({ page: { $in: pages } }, '').toArray();
stats.noteCount = notes.length;
// stats.noteWordCount = words(notes.reduce((acc, { data }) => `${acc} ${data}`)).length;
const questions = db.questions.find({ session, state: { $ne: 'deleted' } }).toArray();
stats.questionCount = questions.length;
const claps = db.claps.find({ page: { $in: pages } }).toArray();
stats.clapCount = claps.length;
return stats;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment