From 5d2ad62fb98dfa68fd6ec1fb6e4672c023025682 Mon Sep 17 00:00:00 2001 From: Matt Lott Date: Thu, 4 Feb 2016 17:31:25 -0800 Subject: [PATCH] Add top school counts to analytics dashboard --- app/templates/admin/analytics.jade | 16 ++++++++++++++++ app/views/admin/AnalyticsView.coffee | 15 ++++++++++++++- server/users/user_handler.coffee | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/app/templates/admin/analytics.jade b/app/templates/admin/analytics.jade index a5377e902..f00cdf5b4 100644 --- a/app/templates/admin/analytics.jade +++ b/app/templates/admin/analytics.jade @@ -57,6 +57,22 @@ block content else div Loading ... + h3 School Counts + .small Only including schools with #{view.minSchoolCount}+ counts + if view.schoolCounts + table.table.table-striped.table-condensed + tr + th + th School Name + th User Count + each val, i in view.schoolCounts + tr + td= i + 1 + td= val.schoolName + td= val.count + else + div Loading ... + h1 Active Classes table.table.table-striped.table-condensed tr diff --git a/app/views/admin/AnalyticsView.coffee b/app/views/admin/AnalyticsView.coffee index 64542d998..553d77d78 100644 --- a/app/views/admin/AnalyticsView.coffee +++ b/app/views/admin/AnalyticsView.coffee @@ -12,6 +12,7 @@ module.exports = class AnalyticsView extends RootView template: template furthestCourseDayRange: 30 lineColors: ['red', 'blue', 'green', 'purple', 'goldenrod', 'brown', 'darkcyan'] + minSchoolCount: 20 constructor: (options) -> super options @@ -118,7 +119,19 @@ module.exports = class AnalyticsView extends RootView @updateRevenueChartData() @render?() }, 0).load() - + + @supermodel.addRequestResource('school_counts', { + url: '/db/user/-/school_counts' + method: 'POST' + data: {minCount: @minSchoolCount} + success: (@schoolCounts) => + @schoolCounts?.sort (a, b) -> + return -1 if a.count > b.count + return 0 if a.count is b.count + 1 + @render?() + }, 0).load() + @courses = new CocoCollection([], { url: "/db/course", model: Course}) @courses.comparator = "_id" @listenToOnce @courses, 'sync', @onCoursesSync diff --git a/server/users/user_handler.coffee b/server/users/user_handler.coffee index 6d6375994..3ca2d79b0 100644 --- a/server/users/user_handler.coffee +++ b/server/users/user_handler.coffee @@ -309,6 +309,7 @@ UserHandler = class UserHandler extends Handler return @getByIDs(req, res) if args[1] is 'users' return @getNamesByIDs(req, res) if args[1] is 'names' return @getPrepaidCodes(req, res) if args[1] is 'prepaid_codes' + return @getSchoolCounts(req, res) if args[1] is 'school_counts' return @nameToID(req, res, args[0]) if args[1] is 'nameToID' return @getLevelSessionsForEmployer(req, res, args[0]) if args[1] is 'level.sessions' and args[2] is 'employer' return @getLevelSessions(req, res, args[0]) if args[1] is 'level.sessions' @@ -464,6 +465,27 @@ UserHandler = class UserHandler extends Handler Prepaid.find({}).or(orQuery).exec (err, documents) => @sendSuccess(res, documents) + getSchoolCounts: (req, res) -> + return @sendSuccess(res, []) unless req.user?.isAdmin() + minCount = req.body.minCount ? 20 + query = {$and: [ + {anonymous: false}, + {schoolName: {$exists: true}}, + {schoolName: {$ne: ''}} + ]} + User.find(query, {schoolName: 1}).exec (err, documents) => + return @sendDatabaseError(res, err) if err + schoolCountMap = {} + for doc in documents + schoolName = doc.get('schoolName') + schoolCountMap[schoolName] ?= 0; + schoolCountMap[schoolName]++; + schoolCounts = [] + for schoolName, count of schoolCountMap + continue unless count >= minCount + schoolCounts.push schoolName: schoolName, count: count + @sendSuccess(res, schoolCounts) + agreeToCLA: (req, res) -> return @sendForbiddenError(res) unless req.user doc =