Improvements to buildSchoolGraph to open a spreadsheet

This commit is contained in:
Nick Winter 2015-12-16 08:36:54 -08:00 committed by Rob
parent 68f2a169dd
commit 7d7513eb3f

View file

@ -5,6 +5,8 @@ mongoose = require 'mongoose'
log = require 'winston'
async = require 'async'
moment = require 'moment'
fs = require 'fs'
exec = require('child_process').exec
### SET UP ###
do (setupLodash = this) ->
@ -35,6 +37,7 @@ nextPrompt = (users, question, userToSchool, suggestions) ->
unless userToSchool
return console.log('Done.') or process.exit() unless [userToSchool, suggestions] = findUserToSchool users
question ?= formatSuggestions userToSchool, suggestions
openTSV userToSchool, suggestions
prompt question, (answer) ->
answer = answer.trim()
return console.log('Bye.') or process.exit() if answer in ['q', 'quit']
@ -98,11 +101,14 @@ findNumbers = (answer, max) ->
console.log "Incorrect number #{number} higher than max: #{max}"
numbers
formatUser = (user, relativeToUser) ->
# TODO: replace date string with relative time since signup compared to target user, and actually make suggestions based on students that signed up at almost the same time
props = _.pick(user, ['name', 'emailLower', 'ageRange', 'dateCreated', 'lastLevel', 'points', 'referrer', 'hourOfCode'])
props.dateCreated = if relativeToUser then moment(props.dateCreated).from(relativeToUser.dateCreated) else moment(props.dateCreated).fromNow()
_.values(props).join(' ')
formatUser = (user, relativeToUser, separator=' ') ->
values = []
for key in ['name', 'emailLower', 'ageRange', 'dateCreated', 'lastLevel', 'points', 'referrer', 'hourOfCode']
val = user[key]
if key is 'dateCreated'
val = if relativeToUser then moment(val).from(relativeToUser.dateCreated) else moment(val).fromNow()
values.push val
values.join separator
formatSuggestions = (userToSchool, suggestions) ->
suggestionPrompts = ("#{_.str.rpad(i + 1, 3)} #{_.str.rpad(s.schoolName, 50)} #{s.reasons.length} #{if s.reasons.length > 1 then 'Matches' else 'Match'}: #{s.reasons.join(', ')}\tfrom user: #{formatUser(s.user, userToSchool)}" for s, i in suggestions).join('\n')
@ -114,6 +120,18 @@ formatSuggestions = (userToSchool, suggestions) ->
Choose a number, type a name, enter to skip, or q to quit.
> """
openTSV = (userToSchool, suggestions) ->
header = ['#', 'School Name', 'Matches', 'Name', 'Email', 'Age', 'Signup', 'Last Level', 'Points', 'Referrer', 'HoC'].join '\t'
rows = [[0, userToSchool.schoolName, '', formatUser(userToSchool, null, '\t')].join '\t']
for s, i in suggestions
matches = s.reasons.length + ' ' + if s.reasons.length > 1 then 'Matches' else 'Match' + ': ' + s.reasons.join(', ')
rows.push [i + 1, s.schoolName, matches, formatUser(s.user, userToSchool, '\t')].join '\t'
contents = [header].concat(rows).join('\n') + '\n'
path = "#{process.env.HOME}/Downloads/#{userToSchool.emailLower}.tsv"
fs.writeFile path, contents, {flags: 'w'}, (err) ->
console.log 'Error writing school suggestions TSV:', err if err
exec "open -a /Applications/Numbers.app #{path}"
checkedTopGroups = {}
findUserToSchool = (users) ->
# We find the top user from the top group that we can make the most reasoned suggestions about what the school name would be.
@ -147,6 +165,7 @@ findUserToSchool = (users) ->
findSuggestions = (target) ->
# Look for other users with the same IP, course instances, clans, or similar school names or non-common shared email domains.
# TODO: Actually make suggestions based on students that signed up at almost the same time
suggestions = []
t0 = new Date()
if debugging then console.log ' Checking suggestions for', target.emailLower, target.schoolName, (new Date()) - t0
@ -208,7 +227,7 @@ scoreSuggestions = (suggestions, target) ->
when 'IP' then 40
when 'Referrer' then 20
when 'Name' then 15
when 'Domain' then (if getDomain(target) in ['cps.edu', 'mynewcaneyisd.org', 'fsusd.org'] then 1 else 10)
when 'Domain' then (if getDomain(target) in ['cps.edu', 'mynewcaneyisd.org', 'fsusd.org', 'edison.k12.nj.us'] then 1 else 10)
when 'Clans' then 0.01
sum
), 0