Edited wiki page through web user interface. git-svn-id: http://soc.googlecode.com/svn/wiki@3239 5fae56a1-394a-0410-ba46-5107137fd53f
diff --git a/StatisticsModule.wiki b/StatisticsModule.wiki index aec044c..1ff8927 100644 --- a/StatisticsModule.wiki +++ b/StatisticsModule.wiki
@@ -170,6 +170,28 @@ calculated_on = db.DateTimeProperty(auto_now=True) }}} +==== Adding new stats ==== +If we want to add a new statistic that takes data only from one model, the first thing we need to do is to add it to the data model. +Of course we have to create a new Statistic entity. Let's say that we want to add Mentors Per Coutry stat, thus the new entity has properties like: +{{{ + properties = { + 'id' : 'MentorsPerCountry', + 'name' : 'Mentors Per Country', + 'link_id' : 'mentors_per_country', + 'scope' : program_entity, # each statistic binded with a program + 'records_completed' : 0, + 'scope_path' : 'google/gsoc' + } +}}} +The important field for later is id. Now, when we refresh statistics list page, we see the new one, but clicking 'Collect' causes an exception. Module that is responsible for updating statistics is {{{soc.logic.models.statistic}}}. The first function that is invoked is {{{updateStat}}}, but it is just a dispatcher which calls a right function depending on id. Therefore, if id is {{{MentorsPerCountry}}}, we have to provide function {{{updateMentorsPerCountry(self, scope)}}} (or update+id in general). + +We may want to write a custom updating function, but if we want to get data only from one model, it may be faster to use {{{updateStatEntity}}} (the name will probably change) function, but we need to pass some arguments. It is easier to understand it by an example, so let's look again at our MentorsPerCountry example. + +To get {{{statistic}}} entity, we have to know two fields that determines it unambiguously. Those fileds are for example id and scope. +We deals with mentors, so all data is collected based on mentor model. In order to get entities from that model, we have to provide logic, so {{{logic=soc.logic.models.mentor}}}. +The next thing is a list of {{{choices}}} for our stat. In this example it's a list of all countries. +The last but certainly not least param is {{{choice_selector}}}. It is a function that takes params as its argument. This function should simply take an entity and return its choice (the entity is passed to this function by {{{params['entity']}}}). For example, if there is a mentor whose {{{res_coutry}}} is Poland, the choice_selector should return 'Poland'. In our example (when the choice is explicitly in model), we may use {{{defaultChoiceSelector}}}, but sometimes the right choice has to be calculated (see {{{studentsPerAgeChoiceSelector}}}) + === Later Goals for Job Control === * _...insert text here_