Created
March 4, 2019 22:03
-
-
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
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
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