scripts/stats.py
author Sverre Rabbelier <srabbelier@gmail.com>
Thu, 02 Apr 2009 23:06:39 +0000
changeset 2061 c7ba473b091e
parent 2060 45029d87be4a
child 2062 9c739b37c367
permissions -rwxr-xr-x
Style fixes Patch by: Sverre Rabbelier
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
2058
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    32
def dateFetch(queryGen, last=None, batchSize=100):
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    33
  """Iterator that yields an entity in batches.
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    34
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    35
  Args:
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    36
    queryGen: should return a Query object
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    37
    last: used to .filter() for last_modified_on
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    38
    batchSize: how many entities to retrieve in one datastore call
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    39
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    40
  Retrieved from http://tinyurl.com/d887ll (AppEngine cookbook).
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    41
  """
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    42
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    43
  from google.appengine.ext import db
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    44
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    45
   # AppEngine will not fetch more than 1000 results
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    46
  batchSize = min(batchSize,1000)
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    47
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    48
  query = None
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    49
  done = False
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    50
  count = 0
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    51
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    52
  while not done:
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    53
    print count
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    54
    query = queryGen()
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    55
    query.order('last_modified_on')
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    56
    if last:
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    57
      query.filter("last_modified_on > ", last)
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    58
    results = query.fetch(batchSize)
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    59
    for result in results:
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    60
      count += 1
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    61
      yield result
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    62
    if batchSize > len(results):
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    63
      done = True
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    64
    else:
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    65
      last = results[-1].last_modified_on
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    66
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    67
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
def addKey(target, fieldname):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
  """Adds the key of the specified field.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
  result = target.copy()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
  result['%s_key' % fieldname] = target[fieldname].key().name()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
  return result
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    77
def getEntities(model):
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    78
  """Returns all users as dictionary.
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    79
  """
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    80
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    81
  def wrapped():
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    82
    gen = lambda: model.all()
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    83
    it = interactive.deepFetch(gen)
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    84
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    85
    entities = [(i.key().name(), i) for i in it]
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    86
    return dict(entities)
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    87
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    88
  return wrapped
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    89
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    90
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    91
def getProps():
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
  """Returns all proposals as a list of dictionaries.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
  key_order = [
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
      'link_id', 'scope_path', 'title', 'abstract', 'content',
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
      'additional_info', 'mentor', 'possible_mentors', 'score',
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
      'status', 'org']
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
  from soc.models.student_proposal import StudentProposal
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
  gen = lambda: StudentProposal.all()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   103
  it = interactive.deepFetch(gen)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   104
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   105
  proposals = [i.toDict(key_order) for i in it]
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   106
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
  return proposals
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
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   110
def orgStats(target):
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   111
  """Retrieves org stats.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   112
  """
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.logic import dicts
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
  target = [addKey(i, 'org') for i in target]
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   117
  grouped = dicts.groupby(target, 'org_key')
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
  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
   119
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   120
  return grouped, dict(popularity)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   121
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   123
def countStudentsWithProposals():
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   124
  """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
   125
  """
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   126
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   127
  proposals = getStudentProposals()
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   128
  students = {}
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   129
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   130
  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
   131
    students[proposals[proposal_key].scope_path] = True
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   132
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   133
  return len(students)
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
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   136
def printPopularity(popularity):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   137
  """Prints the popularity for the specified proposals.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   138
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   139
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   140
  g = operator.itemgetter(1)
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
  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
   143
    print "%s: %d" % item
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   146
def savePopularity(popularities):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   147
  """Saves the specified popularities.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   148
  """
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
  import logging
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   151
  from google.appengine.ext import db
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   152
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   153
  from soc.models.organization import Organization
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   154
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   155
  def txn(key_name, popularity):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   156
    org = Organization.get_by_key_name(key_name)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   157
    org.nr_applications = popularity
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   158
    org.put()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   159
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   160
  for key, value in sorted(popularities.iteritems()):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   161
    print key
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   162
    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
   163
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   164
  print "done"
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   165
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
def loadPickle(name):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   168
  """Loads a pickle.
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
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   171
  f = open(name + '.dat')
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   172
  return cPickle.load(f)
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
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   175
def dumpPickle(target, name):
2061
c7ba473b091e Style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2060
diff changeset
   176
  """Dumps a pickle.
2047
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
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   179
  f = open("%s.dat" % name, 'w')
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   180
  cPickle.dump(target, f)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   181
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 main(args):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   184
  """Main routine.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   185
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   186
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   187
  interactive.setup()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   188
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   189
  from soc.models.organization import Organization
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   190
  from soc.models.user import User
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   191
  from soc.models.student import Student
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   192
  from soc.models.mentor import Mentor
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   193
  from soc.models.org_admin import OrgAdmin
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   194
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   195
  context = {
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   196
      'load': loadPickle,
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   197
      'dump': dumpPickle,
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   198
      'orgStats': orgStats,
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   199
      'printPopularity': printPopularity,
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   200
      'savePopularity': savePopularity,
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   201
      'getOrgs': getEntities(Organization),
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   202
      'getUsers': getEntities(User),
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   203
      'getStudents': getEntities(Student),
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   204
      'getMentors': getEntities(Mentor),
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   205
      'getOrgAdmins': getEntities(OrgAdmins),
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   206
      'getProps': getProps,
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   207
      'countStudentsWithProposals': countStudentsWithProposals,
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   208
  }
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
  interactive.remote(args, context)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   211
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   212
if __name__ == '__main__':
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   213
  if len(sys.argv) < 2:
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   214
    print "Usage: %s app_id [host]" % (sys.argv[0],)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   215
    sys.exit(1)
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
  main(sys.argv[1:])
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   218