scripts/stats.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Thu, 02 Apr 2009 15:31:36 +0000
changeset 2052 a723a2509e21
parent 2047 7e9656691c8e
child 2058 773b13d86309
permissions -rw-r--r--
Add some additional functions for stats to stats.py script. This included getStudents, getUsers, getMentors, getOrgAdmins, countStudentsWithProposals and some style fixes. Patch by: Pawel Solyga Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""Starts an interactive shell with statistic helpers.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
]
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
import cPickle
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
import operator
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
import sys
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
import interactive
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
def addKey(target, fieldname):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
  """Adds the key of the specified field.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    36
  result = target.copy()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
  result['%s_key' % fieldname] = target[fieldname].key().name()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
  return result
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
def getOrgs():
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
  """Returns all orgs as dictionary.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
  from soc.models.organization import Organization
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
  gen = lambda: Organization.all()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
  it = interactive.deepFetch(gen)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
  orgs = [(i.key().name(), i) for i in it]
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
  return dict(orgs)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    53
def getUsers():
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    54
  """Returns all Users as dictionary.
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    55
  """
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    56
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    57
  from soc.models.user import User
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    58
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    59
  gen = lambda: User.all()
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    60
  it = interactive.deepFetch(gen)
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    61
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    62
  users = [(i.key().name(), i) for i in it]
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    63
  return dict(users)
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    64
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    65
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    66
def getStudents():
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    67
  """Returns all Students as dictionary.
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    68
  """
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    69
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    70
  from soc.models.student import Student
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    71
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    72
  gen = lambda: Student.all()
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    73
  it = interactive.deepFetch(gen)
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    74
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    75
  students = [(i.key().name(), i) for i in it]
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    76
  return dict(students)
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    77
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    78
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    79
def getMentors():
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    80
  """Returns all Mentors as dictionary.
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    81
  """
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    82
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    83
  from soc.models.mentor import Mentor
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    84
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    85
  gen = lambda: Mentor.all()
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    86
  it = interactive.deepFetch(gen)
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    87
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    88
  mentors = [(i.key().name(), i) for i in it]
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    89
  return dict(mentors)
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    90
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    91
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    92
def getOrgAdmins():
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    93
  """Returns all Organization Administrators as dictionary.
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    94
  """
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    95
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    96
  from soc.models.org_admin import OrgAdmin
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    97
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    98
  gen = lambda: OrgAdmin.all()
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    99
  it = interactive.deepFetch(gen)
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   100
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   101
  orgAdmins = [(i.key().name(), i) for i in it]
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   102
  return dict(orgAdmins)
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   103
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   104
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   105
def getProps():
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   106
  """Returns all proposals as a list of dictionaries.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   108
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   109
  key_order = [
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   110
      'link_id', 'scope_path', 'title', 'abstract', 'content',
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
      'additional_info', 'mentor', 'possible_mentors', 'score',
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
      'status', 'org']
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   114
  from soc.models.student_proposal import StudentProposal
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   115
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   116
  gen = lambda: StudentProposal.all()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
  it = interactive.deepFetch(gen)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   119
  proposals = [i.toDict(key_order) for i in it]
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   120
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   121
  return proposals
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   123
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   124
def getStudentProposals():
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   125
  """Returns all Student Proposals as dictionary.
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   126
  """
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   127
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   128
  from soc.models.student_proposal import StudentProposal
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   129
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   130
  gen = lambda: StudentProposal.all()
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   131
  it = interactive.deepFetch(gen)
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   132
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   133
  studentProposals = [(i.key().name(), i) for i in it]
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   134
  
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   135
  return dict(studentProposals)
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   136
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   137
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   138
def orgStats(target):
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
  """Retrieves org stats.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   140
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   142
  from soc.logic import dicts
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   143
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
  target = [addKey(i, 'org') for i in target]
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
  grouped = dicts.groupby(target, 'org_key')
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   146
  popularity = [(k, len(v)) for k,v in grouped.iteritems()]
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   147
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   148
  return grouped, dict(popularity)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   149
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   150
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   151
def countStudentsWithProposals():
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   152
  """Retrieves number of Students who have submitted at least one Student Proposal.
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   153
  """
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   154
  
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   155
  proposals = getStudentProposals()
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   156
  students = {}
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   157
  
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   158
  for proposal_key in proposals.keys():
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   159
    students[proposals[proposal_key].scope_path] = True
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   160
  
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   161
  return len(students)
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   162
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   163
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   164
def printPopularity(popularity):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   165
  """Prints the popularity for the specified proposals.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   166
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   167
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   168
  g = operator.itemgetter(1)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   169
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   170
  for item in sorted(popularity.iteritems(), key=g, reverse=True):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   171
    print "%s: %d" % item
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   172
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   173
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   174
def savePopularity(popularities):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   175
  """Saves the specified popularities.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   176
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   177
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   178
  import logging
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   179
  from google.appengine.ext import db
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   180
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   181
  from soc.models.organization import Organization
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   182
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   183
  def txn(key_name, popularity):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   184
    org = Organization.get_by_key_name(key_name)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   185
    org.nr_applications = popularity
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   186
    org.put()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   187
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   188
  for key, value in sorted(popularities.iteritems()):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   189
    print key
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   190
    db.run_in_transaction_custom_retries(10, txn, key, value)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   191
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   192
  print "done"
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   193
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   194
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   195
def loadPickle(name):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   196
  """Loads a pickle.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   197
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   198
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   199
  f = open(name + '.dat')
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   200
  return cPickle.load(f)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   201
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   202
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   203
def dumpPickle(target, name):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   204
  """Dumps a pickle"
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   205
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   206
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   207
  f = open("%s.dat" % name, 'w')
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   208
  cPickle.dump(target, f)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   209
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   210
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   211
def main(args):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   212
  """Main routine.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   213
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   214
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   215
  interactive.setup()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   216
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   217
  context = {
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   218
      'load': loadPickle,
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   219
      'dump': dumpPickle,
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   220
      'orgStats': orgStats,
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   221
      'printPopularity': printPopularity,
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   222
      'savePopularity': savePopularity,
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   223
      'getOrgs': getOrgs,
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   224
      'getProps': getProps,
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   225
      'getStudents': getStudents,
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   226
      'getMentors': getMentors,
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   227
      'getOrgAdmins': getOrgAdmins,
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   228
      'getUsers': getUsers,
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   229
      'getStudentProposals': getStudentProposals,
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   230
      'countStudentsWithProposals': countStudentsWithProposals
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   231
  }
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   232
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   233
  interactive.remote(args, context)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   234
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   235
if __name__ == '__main__':
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   236
  if len(sys.argv) < 2:
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   237
    print "Usage: %s app_id [host]" % (sys.argv[0],)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   238
    sys.exit(1)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   239
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   240
  main(sys.argv[1:])
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   241