scripts/stats.py
changeset 2047 7e9656691c8e
child 2052 a723a2509e21
equal deleted inserted replaced
2046:1fb1c628ad60 2047:7e9656691c8e
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2009 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Starts an interactive shell with statistic helpers.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22 ]
       
    23 
       
    24 
       
    25 import cPickle
       
    26 import operator
       
    27 import sys
       
    28 
       
    29 import interactive
       
    30 
       
    31 
       
    32 def addKey(target, fieldname):
       
    33   """Adds the key of the specified field.
       
    34   """
       
    35 
       
    36   result = target.copy()
       
    37   result['%s_key' % fieldname] = target[fieldname].key().name()
       
    38   return result
       
    39 
       
    40 def getOrgs():
       
    41   """Returns all orgs as dictionary.
       
    42   """
       
    43 
       
    44   from soc.models.organization import Organization
       
    45 
       
    46   gen = lambda: Organization.all()
       
    47   it = interactive.deepFetch(gen)
       
    48 
       
    49   orgs = [(i.key().name(), i) for i in it]
       
    50   return dict(orgs)
       
    51 
       
    52 
       
    53 def getProps():
       
    54   """Returns all proposals as a list of dictionaries.
       
    55   """
       
    56 
       
    57   key_order = [
       
    58       'link_id', 'scope_path', 'title', 'abstract', 'content',
       
    59       'additional_info', 'mentor', 'possible_mentors', 'score',
       
    60       'status', 'org']
       
    61 
       
    62   from soc.models.student_proposal import StudentProposal
       
    63 
       
    64   gen = lambda: StudentProposal.all()
       
    65   it = interactive.deepFetch(gen)
       
    66 
       
    67   proposals = [i.toDict(key_order) for i in it]
       
    68 
       
    69   return proposals
       
    70 
       
    71 
       
    72 def orgstats(target):
       
    73   """Retrieves org stats.
       
    74   """
       
    75 
       
    76   from soc.logic import dicts
       
    77 
       
    78   target = [addKey(i, 'org') for i in target]
       
    79   grouped = dicts.groupby(target, 'org_key')
       
    80   popularity = [(k, len(v)) for k,v in grouped.iteritems()]
       
    81 
       
    82   return grouped, dict(popularity)
       
    83 
       
    84 
       
    85 def printPopularity(popularity):
       
    86   """Prints the popularity for the specified proposals.
       
    87   """
       
    88 
       
    89   g = operator.itemgetter(1)
       
    90 
       
    91   for item in sorted(popularity.iteritems(), key=g, reverse=True):
       
    92     print "%s: %d" % item
       
    93 
       
    94 
       
    95 def savePopularity(popularities):
       
    96   """Saves the specified popularities.
       
    97   """
       
    98 
       
    99   import logging
       
   100   from google.appengine.ext import db
       
   101 
       
   102   from soc.models.organization import Organization
       
   103 
       
   104   def txn(key_name, popularity):
       
   105     org = Organization.get_by_key_name(key_name)
       
   106     org.nr_applications = popularity
       
   107     org.put()
       
   108 
       
   109   for key, value in sorted(popularities.iteritems()):
       
   110     print key
       
   111     db.run_in_transaction_custom_retries(10, txn, key, value)
       
   112 
       
   113   print "done"
       
   114 
       
   115 
       
   116 def loadPickle(name):
       
   117   """Loads a pickle.
       
   118   """
       
   119 
       
   120   f = open(name + '.dat')
       
   121   return cPickle.load(f)
       
   122 
       
   123 
       
   124 def dumpPickle(target, name):
       
   125   """Dumps a pickle"
       
   126   """
       
   127 
       
   128   f = open("%s.dat" % name, 'w')
       
   129   cPickle.dump(target, f)
       
   130 
       
   131 
       
   132 def main(args):
       
   133   """Main routine.
       
   134   """
       
   135 
       
   136   interactive.setup()
       
   137 
       
   138   context = {
       
   139       'load': loadPickle,
       
   140       'dump': dumpPickle,
       
   141       'orgstats': orgstats,
       
   142       'printPopularity': printPopularity,
       
   143       'savePopularity': savePopularity,
       
   144       'getOrgs': getOrgs,
       
   145       'getProps': getProps,
       
   146   }
       
   147 
       
   148   interactive.remote(args, context)
       
   149 
       
   150 if __name__ == '__main__':
       
   151   if len(sys.argv) < 2:
       
   152     print "Usage: %s app_id [host]" % (sys.argv[0],)
       
   153     sys.exit(1)
       
   154 
       
   155   main(sys.argv[1:])
       
   156