scripts/stats.py
author Daniel Diniz <ajaksu@gmail.com>
Wed, 08 Jul 2009 10:40:46 +0200
changeset 2570 851640749319
parent 2345 f78caf12f32d
child 2592 040359ccd20b
permissions -rwxr-xr-x
Several Survey UI fixes. Fixes: Too narrow fieldsets in new question/option dialogs. Survey submit (on take view) and save/export/etc. buttons at weird places, instead of at bottom. Weird placement of radio buttons in Opera. Too narrow selects in IE. Broken images in edit view in IE and Opera. Reviewed by: Lennard de Rijk (Only on IE)
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
2079
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
    26
import datetime
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
import operator
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
import sys
2218
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
    29
import time
2047
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
import interactive
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
2058
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    34
def dateFetch(queryGen, last=None, batchSize=100):
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    35
  """Iterator that yields an entity in batches.
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    36
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    37
  Args:
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    38
    queryGen: should return a Query object
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    39
    last: used to .filter() for last_modified_on
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    40
    batchSize: how many entities to retrieve in one datastore call
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
  Retrieved from http://tinyurl.com/d887ll (AppEngine cookbook).
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    43
  """
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
  from google.appengine.ext import db
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    46
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    47
   # AppEngine will not fetch more than 1000 results
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    48
  batchSize = min(batchSize,1000)
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    49
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    50
  query = None
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    51
  done = False
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    52
  count = 0
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    53
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    54
  while not done:
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    55
    print count
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    56
    query = queryGen()
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    57
    query.order('last_modified_on')
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    58
    if last:
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    59
      query.filter("last_modified_on > ", last)
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    60
    results = query.fetch(batchSize)
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    61
    for result in results:
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    62
      count += 1
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    63
      yield result
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    64
    if batchSize > len(results):
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    65
      done = True
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    66
    else:
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    67
      last = results[-1].last_modified_on
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    68
773b13d86309 Add dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2052
diff changeset
    69
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
def addKey(target, fieldname):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    71
  """Adds the key of the specified field.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
  result = target.copy()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
  result['%s_key' % fieldname] = target[fieldname].key().name()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
  return result
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    79
def getEntities(model):
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    80
  """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
    81
  """
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
    82
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    83
  def wrapped():
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    84
    gen = lambda: model.all()
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    85
    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
    86
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    87
    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
    88
    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
    89
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
    90
  return wrapped
2052
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
2062
9c739b37c367 Refactor getProps to use dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2061
diff changeset
    93
def getProps(last=None):
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
  """Returns all proposals as a list of dictionaries.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
  key_order = [
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
      'link_id', 'scope_path', 'title', 'abstract', 'content',
2062
9c739b37c367 Refactor getProps to use dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2061
diff changeset
    99
      'additional_info', '_mentor', 'possible_mentors', 'score',
9c739b37c367 Refactor getProps to use dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2061
diff changeset
   100
      'status', '_org', 'created_on', 'last_modified_on']
2047
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
  from soc.models.student_proposal import StudentProposal
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   103
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   104
  gen = lambda: StudentProposal.all()
2062
9c739b37c367 Refactor getProps to use dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2061
diff changeset
   105
9c739b37c367 Refactor getProps to use dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2061
diff changeset
   106
  it = dateFetch(gen, last)
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   107
2062
9c739b37c367 Refactor getProps to use dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2061
diff changeset
   108
  proposals = [(i.key().name(), i.toDict(key_order)) for i in it]
2079
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   109
  if proposals:
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   110
    last = i.last_modified_on # last modified entity
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   111
  else:
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   112
    last = datetime.datetime.now()
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   113
2079
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   114
  return dict(proposals), last
2047
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
2079
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   117
def orgStats(target, orgs):
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   118
  """Retrieves org stats.
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
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   121
  from soc.logic import dicts
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   122
2079
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   123
  orgs = [(v.key(), v) for k, v in orgs.iteritems()]
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   124
  orgs = dict(orgs)
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   125
2062
9c739b37c367 Refactor getProps to use dateFetch
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2061
diff changeset
   126
  grouped = dicts.groupby(target.values(), '_org')
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   127
2079
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   128
  grouped = [(orgs[k], v) for k, v in grouped.iteritems()]
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   129
  popularity = [(k.link_id, len(v)) for k, v in grouped]
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   130
086c60aad3d8 Minor tweaks to orgStats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2063
diff changeset
   131
  return dict(grouped), dict(popularity)
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   132
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   133
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   134
def countStudentsWithProposals():
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   135
  """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
   136
  """
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   137
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   138
  proposals = getStudentProposals()
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   139
  students = {}
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   140
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   141
  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
   142
    students[proposals[proposal_key].scope_path] = True
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   143
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   144
  return len(students)
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   145
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   146
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   147
def printPopularity(popularity):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   148
  """Prints the popularity for the specified proposals.
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
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   151
  g = operator.itemgetter(1)
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
  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
   154
    print "%s: %d" % item
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   155
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   156
2148
0345046ed7a5 Make it possible to save any value with saveValues
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2090
diff changeset
   157
def saveValues(values, saver):
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   158
  """Saves the specified popularities.
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
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   161
  import logging
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   162
  from google.appengine.ext import db
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
  from soc.models.organization import Organization
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   165
2148
0345046ed7a5 Make it possible to save any value with saveValues
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2090
diff changeset
   166
  def txn(key, value):
0345046ed7a5 Make it possible to save any value with saveValues
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2090
diff changeset
   167
    org = Organization.get_by_key_name(key)
0345046ed7a5 Make it possible to save any value with saveValues
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2090
diff changeset
   168
    saver(org, value)
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   169
    org.put()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   170
2148
0345046ed7a5 Make it possible to save any value with saveValues
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2090
diff changeset
   171
  for key, value in sorted(values.iteritems()):
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   172
    print key
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   173
    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
   174
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   175
  print "done"
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
2090
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   178
def addFollower(follower, proposals, add_public=True, add_private=True):
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   179
  """Adds a user as follower to the specified proposals.
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   180
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   181
  Args:
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   182
    follower: the User to add as follower
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   183
    proposals: a list with the StudnetProposals that should be subscribed to
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   184
    add_public: whether the user is subscribed to public updates
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   185
    add_private: whether the user should be subscribed to private updates
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   186
  """
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   187
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   188
  from soc.models.review_follower import ReviewFollower
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   189
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   190
  result = []
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   191
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   192
  for i in proposals:
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   193
     properties = {
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   194
       'user': follower,
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   195
       'link_id': follower.link_id,
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   196
       'scope': i,
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   197
       'scope_path': i.key().name(),
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   198
       'key_name': '%s/%s' % (i.key().name(), follower.link_id),
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   199
       'subscribed_public': add_public,
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   200
       'subscribed_private': add_private,
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   201
     }
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   202
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   203
     entity = ReviewFollower(**properties)
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   204
     result.append(entity)
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   205
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   206
  return result
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   207
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   208
2218
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   209
def convertProposals(org):
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   210
  """Convert all proposals for the specified organization.
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   211
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   212
  Args:
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   213
    org: the organization for which all proposals will be converted
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   214
  """
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   215
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   216
  from soc.logic.models.student_proposal import logic as proposal_logic
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   217
  from soc.logic.models.student_project import logic as project_logic
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   218
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   219
  proposals = proposal_logic.getProposalsToBeAcceptedForOrg(org)
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   220
2262
5b0576dcc107 Set batch size to 10
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2245
diff changeset
   221
  print "accepting %d proposals, with %d slots" % (len(proposals), org.slots)
5b0576dcc107 Set batch size to 10
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2245
diff changeset
   222
2218
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   223
  for proposal in proposals:
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   224
    fields = {
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   225
        'link_id': 't%i' % (int(time.time()*100)),
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   226
        'scope_path': proposal.org.key().id_or_name(),
2222
a91d55e9c9cd Various improvements to convertProposals
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2218
diff changeset
   227
        'scope': proposal.org,
2218
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   228
        'program': proposal.program,
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   229
        'student': proposal.scope,
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   230
        'title': proposal.title,
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   231
        'abstract': proposal.abstract,
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   232
        'mentor': proposal.mentor,
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   233
        }
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   234
2222
a91d55e9c9cd Various improvements to convertProposals
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2218
diff changeset
   235
    project = project_logic.updateOrCreateFromFields(fields, silent=True)
2218
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   236
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   237
    fields = {
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   238
        'status':'accepted',
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   239
        }
2222
a91d55e9c9cd Various improvements to convertProposals
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2218
diff changeset
   240
a91d55e9c9cd Various improvements to convertProposals
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2218
diff changeset
   241
    proposal_logic.updateEntityProperties(proposal, fields, silent=True)
2218
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   242
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   243
  fields = {
2222
a91d55e9c9cd Various improvements to convertProposals
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2218
diff changeset
   244
      'status': ['new', 'pending'],
2218
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   245
      'org': org,
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   246
      }
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   247
2222
a91d55e9c9cd Various improvements to convertProposals
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2218
diff changeset
   248
  querygen = lambda: proposal_logic.getQueryForFields(fields)
2262
5b0576dcc107 Set batch size to 10
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2245
diff changeset
   249
  proposals = [i for i in interactive.deepFetch(querygen, batchSize=10)]
2222
a91d55e9c9cd Various improvements to convertProposals
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2218
diff changeset
   250
a91d55e9c9cd Various improvements to convertProposals
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2218
diff changeset
   251
  print "rejecting %d proposals" % len(proposals)
2218
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   252
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   253
  fields = {
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   254
      'status': 'rejected',
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   255
      }
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   256
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   257
  for proposal in proposals:
2222
a91d55e9c9cd Various improvements to convertProposals
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2218
diff changeset
   258
    proposal_logic.updateEntityProperties(proposal, fields, silent=True)
2218
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   259
84b0ce492cf5 Add a script to convert proposals.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2151
diff changeset
   260
2234
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   261
def startSpam():
2270
543f11d7b0e4 Fix missing dot in stats.py docstring.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2265
diff changeset
   262
  """Creates the job that is responsible for sending mails.
2265
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   263
  """
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   264
2234
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   265
  from soc.logic.models.job import logic as job_logic
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   266
  from soc.logic.models.priority_group import logic as priority_logic
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   267
  from soc.logic.models.program import logic as program_logic
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   268
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   269
  program_entity = program_logic.getFromKeyName('google/gsoc2009')
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   270
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   271
  priority_group = priority_logic.getGroup(priority_logic.EMAIL)
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   272
  job_fields = {
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   273
      'priority_group': priority_group,
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   274
      'task_name': 'setupStudentProposalMailing',
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   275
      'key_data': [program_entity.key()]}
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   276
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   277
  job_logic.updateOrCreateFromFields(job_fields)
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   278
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   279
2345
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   280
def startUniqueUserIdConversion():
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   281
  """Creates the job that is responsible for adding unique user ids.
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   282
  """
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   283
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   284
  from soc.logic.models.job import logic as job_logic
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   285
  from soc.logic.models.priority_group import logic as priority_logic
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   286
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   287
  priority_group = priority_logic.getGroup(priority_logic.CONVERT)
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   288
  job_fields = {
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   289
      'priority_group': priority_group,
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   290
      'task_name': 'setupUniqueUserIdAdder'}
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   291
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   292
  job_logic.updateOrCreateFromFields(job_fields)
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   293
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   294
2265
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   295
def reviveJobs(amount):
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   296
  """Sets jobs that are stuck in 'aborted' to waiting.
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   297
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   298
  Args:
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   299
    amount: the amount of jobs to revive
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   300
  """
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   301
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   302
  from soc.models.job import Job
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   303
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   304
  query = Job.all().filter('status', 'aborted')
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   305
  jobs = query.fetch(amount)
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   306
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   307
  if not jobs:
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   308
    print "no dead jobs"
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   309
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   310
  for job in jobs:
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   311
     job.status = 'waiting'
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   312
     job.put()
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   313
     print "restarted %d" % job.key().id()
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   314
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   315
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   316
def deidleJobs(amount):
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   317
  """Sets jobs that are stuck in 'started' to waiting.
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   318
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   319
  Args:
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   320
    amount: the amount of jobs to deidle
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   321
  """
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   322
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   323
  from soc.models.job import Job
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   324
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   325
  query = Job.all().filter('status', 'started')
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   326
  jobs = query.fetch(amount)
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   327
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   328
  if not jobs:
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   329
    print "no idle jobs"
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   330
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   331
  for job in jobs:
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   332
     job.status = 'waiting'
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   333
     job.put()
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   334
     print "restarted %d" % job.key().id()
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   335
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   336
2245
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   337
def deleteEntities(model, step_size=25):
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   338
  """Deletes all entities of the specified type
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   339
  """
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   340
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   341
  print "Deleting..."
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   342
  count = 0
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   343
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   344
  while True:
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   345
    entities = model.all().fetch(step_size)
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   346
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   347
    if not entities:
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   348
      break
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   349
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   350
    for entity in entities:
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   351
      entity.delete()
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   352
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   353
    count += step_size
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   354
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   355
    print "deleted %d entities" % count
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   356
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   357
  print "Done"
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   358
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   359
def loadPickle(name):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   360
  """Loads a pickle.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   361
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   362
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   363
  f = open(name + '.dat')
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   364
  return cPickle.load(f)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   365
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   366
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   367
def dumpPickle(target, name):
2061
c7ba473b091e Style fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2060
diff changeset
   368
  """Dumps a pickle.
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   369
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   370
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   371
  f = open("%s.dat" % name, 'w')
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   372
  cPickle.dump(target, f)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   373
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   374
2322
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   375
def acceptedStudentsCSVExport(csv_filename, program_key_name):
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   376
  """Exports all accepted Students for particular program into CSV file.
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   377
  """
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   378
  # TODO(Pawel.Solyga): Add additional Program parameter to this method 
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   379
  # so we export students from different programs
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   380
  # TODO(Pawel.SOlyga): Make it universal so it works with both GHOP 
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   381
  # and GSoC programs
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   382
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   383
  from soc.models.student_project import StudentProject
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   384
  from soc.models.student import Student
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   385
  from soc.models.organization import Organization
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   386
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   387
  getStudentProjects = getEntities(StudentProject)
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   388
  student_projects = getStudentProjects()
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   389
  student_projects_amount = len(student_projects)
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   390
  print "Fetched %d Student Projects." % student_projects_amount
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   391
  print "Fetching Student entities from Student Projects."
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   392
  accepted_students = {}
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   393
  student_organization = {}
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   394
  counter = 0
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   395
  for sp_key in student_projects.keys():
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   396
    key = student_projects[sp_key].student.key().name()
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   397
    accepted_students[key] = student_projects[sp_key].student
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   398
    org_name = student_projects[sp_key].scope.name
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   399
    student_organization[key] = org_name
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   400
    counter += 1
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   401
    print str(counter) + '/' + str(student_projects_amount) + ' ' + key + ' (' + org_name + ')'
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   402
  print "All Student entities fetched."
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   403
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   404
  students_key_order = ['link_id', 'given_name', 'surname', 
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   405
      'name_on_documents', 'email', 'res_street', 'res_city', 'res_state',
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   406
      'res_country', 'res_postalcode', 'phone', 'ship_street', 'ship_city',
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   407
      'ship_state', 'ship_country', 'ship_postalcode', 'birth_date', 
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   408
      'tshirt_size', 'tshirt_style', 'name', 'school_name', 'school_country',
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   409
      'major', 'degree']
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   410
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   411
  print "Preparing Students data for export."
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   412
  students_data = [accepted_students[i].toDict(students_key_order) for i in accepted_students.keys()]
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   413
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   414
  print "Adding organization name to Students data."
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   415
  for student in students_data:
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   416
    student['organization'] = student_organization[program_key_name + '/' + student['link_id']]
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   417
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   418
  students_key_order.append('organization')
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   419
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   420
  saveDataToCSV(csv_filename, students_data, students_key_order)
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   421
  print "Accepted Students exported to %s file." % csv_filename
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   422
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   423
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   424
def saveDataToCSV(csv_filename, data, key_order):
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   425
  """Saves data in order into CSV file.
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   426
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   427
  This is a helper function used with acceptedStudentsCSVExport().
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   428
  """
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   429
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   430
  import csv
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   431
  import StringIO
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   432
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   433
  from soc.logic import dicts
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   434
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   435
  file_handler = StringIO.StringIO()
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   436
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   437
  writer = csv.DictWriter(file_handler, key_order, dialect='excel')
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   438
  writer.writerow(dicts.identity(key_order))
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   439
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   440
  # encode the data to UTF-8 to ensure compatibiliy
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   441
  for row_dict in data:
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   442
    for key in row_dict.keys():
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   443
      value = row_dict[key]
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   444
      if isinstance(value, basestring):
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   445
        row_dict[key] = value.encode("utf-8")
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   446
      else:
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   447
        row_dict[key] = str(value)
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   448
    writer.writerow(row_dict)
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   449
  
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   450
  csv_data = file_handler.getvalue()
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   451
  csv_file = open(csv_filename, 'w')
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   452
  csv_file.write(csv_data)
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   453
  csv_file.close()
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   454
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   455
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   456
def main(args):
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   457
  """Main routine.
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   458
  """
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   459
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   460
  interactive.setup()
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   461
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   462
  from soc.models.organization import Organization
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   463
  from soc.models.user import User
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   464
  from soc.models.student import Student
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   465
  from soc.models.mentor import Mentor
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   466
  from soc.models.org_admin import OrgAdmin
2245
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   467
  from soc.models.job import Job
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   468
  from soc.models.student_proposal import StudentProposal
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   469
  from soc.models.student_project import StudentProject
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   470
2151
f58515b0b2e1 Style fix in function names in stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2148
diff changeset
   471
  def slotSaver(org, value):
2148
0345046ed7a5 Make it possible to save any value with saveValues
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2090
diff changeset
   472
    org.slots = value
2151
f58515b0b2e1 Style fix in function names in stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2148
diff changeset
   473
  def popSaver(org, value):
2148
0345046ed7a5 Make it possible to save any value with saveValues
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2090
diff changeset
   474
    org.nr_applications = value
2151
f58515b0b2e1 Style fix in function names in stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2148
diff changeset
   475
  def rawSaver(org, value):
2148
0345046ed7a5 Make it possible to save any value with saveValues
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2090
diff changeset
   476
    org.slots_calculated = value
0345046ed7a5 Make it possible to save any value with saveValues
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2090
diff changeset
   477
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   478
  context = {
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   479
      'load': loadPickle,
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   480
      'dump': dumpPickle,
2052
a723a2509e21 Add some additional functions for stats to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2047
diff changeset
   481
      'orgStats': orgStats,
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   482
      'printPopularity': printPopularity,
2148
0345046ed7a5 Make it possible to save any value with saveValues
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2090
diff changeset
   483
      'saveValues': saveValues,
2245
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   484
      'getEntities': getEntities,
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   485
      'deleteEntities': deleteEntities,
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   486
      'getOrgs': getEntities(Organization),
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   487
      'getUsers': getEntities(User),
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   488
      'getStudents': getEntities(Student),
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   489
      'getMentors': getEntities(Mentor),
2063
319d9f05955a Fixed a typo in stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2062
diff changeset
   490
      'getOrgAdmins': getEntities(OrgAdmin),
2274
1885cda38bb7 Add getStudentsProjects to context in stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2270
diff changeset
   491
      'getStudentProjects': getEntities(StudentProject),
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   492
      'getProps': getProps,
2060
45029d87be4a Add and use a getEntities method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2058
diff changeset
   493
      'countStudentsWithProposals': countStudentsWithProposals,
2222
a91d55e9c9cd Various improvements to convertProposals
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2218
diff changeset
   494
      'convertProposals': convertProposals,
2090
a07ff1e1bd1f Add a script to subscribe someone to all updates
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2089
diff changeset
   495
      'addFollower': addFollower,
2089
b8a9691da6e7 Add the entities to the context too
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2079
diff changeset
   496
      'Organization': Organization,
2245
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   497
      'Job': Job,
2089
b8a9691da6e7 Add the entities to the context too
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2079
diff changeset
   498
      'User': User,
b8a9691da6e7 Add the entities to the context too
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2079
diff changeset
   499
      'Student': Student,
b8a9691da6e7 Add the entities to the context too
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2079
diff changeset
   500
      'Mentor': Mentor,
b8a9691da6e7 Add the entities to the context too
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2079
diff changeset
   501
      'OrgAdmin': OrgAdmin,
2245
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   502
      'StudentProject': StudentProject,
46e490606f3d Added deleteEntities methods to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2234
diff changeset
   503
      'StudentProposal': StudentProposal,
2151
f58515b0b2e1 Style fix in function names in stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2148
diff changeset
   504
      'slotSaver': slotSaver,
f58515b0b2e1 Style fix in function names in stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2148
diff changeset
   505
      'popSaver': popSaver,
f58515b0b2e1 Style fix in function names in stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2148
diff changeset
   506
      'rawSaver': rawSaver,
2234
69539a5ca63a Add a startSpam method to stats
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2222
diff changeset
   507
      'startSpam': startSpam,
2265
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   508
      'reviveJobs': reviveJobs,
bfaadb6ab559 Added reviveJobs and deidleJobs to stats.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2262
diff changeset
   509
      'deidleJobs': deidleJobs,
2322
98fe07a5542f Add acceptedStudentsCSVExport function to stats.py script.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2274
diff changeset
   510
      'acceptedStudentsCSVExport': acceptedStudentsCSVExport,
2345
f78caf12f32d Add helper functions, model update and jobs for unique user ids.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2322
diff changeset
   511
      'startUniqueUserIdConversion': startUniqueUserIdConversion,
2047
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   512
  }
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   513
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   514
  interactive.remote(args, context)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   515
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   516
if __name__ == '__main__':
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   517
  if len(sys.argv) < 2:
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   518
    print "Usage: %s app_id [host]" % (sys.argv[0],)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   519
    sys.exit(1)
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   520
7e9656691c8e Added a stats module to scripts
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   521
  main(sys.argv[1:])