Skip to content

Instantly share code, notes, and snippets.

@stuartf
Created March 4, 2019 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartf/4d15784ccb386ab8e02e6966db5954d5 to your computer and use it in GitHub Desktop.
Save stuartf/4d15784ccb386ab8e02e6966db5954d5 to your computer and use it in GitHub Desktop.
Get Canvas page views for a list of users between 2 dates by sis_user_id
const parse = require('csv-parse/lib/sync');
const fs = require('fs');
const Canvas = require('canvas-lms-api');
const config = require('../config.json');
const promiseRetryify = require('promise-retryify');
const canvas = new Canvas(config.canvas.url, {'accessToken': config.canvas.token});
canvas.qsStringifyOptions = { arrayFormat: 'repeat' };
const rc = promiseRetryify(canvas);
const data = parse(fs.readFileSync('./gtids.txt'), {
delimiter: '\t',
columns: true,
skip_empty_lines: true
});
const sids = [...new Set(data.map(entry => entry.SPRIDEN_ID))];
const getPageViews = function(sid, start_time, end_time) {
return rc.get(`users/sis_user_id:${sid}/page_views`, {start_time, end_time, per_page:100}).catch(err => err.code==404 ? 'No such user in Canvas': console.log(err))
};
Promise.all(sids.map(sid => getPageViews(sid, '2019-02-22T16:00:00Z', '2019-02-25T17:00:00Z'))).then(logs => {
const report = sids.reduce((acc, sid, idx) => {acc[sid] = logs[idx]; return acc;}, {});
console.log(JSON.stringify(report));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment