Return-Path: X-Original-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3719DDAFC for ; Tue, 14 May 2013 18:03:31 +0000 (UTC) Received: (qmail 48146 invoked by uid 500); 14 May 2013 18:03:31 -0000 Delivered-To: apmail-incubator-allura-commits-archive@incubator.apache.org Received: (qmail 48062 invoked by uid 500); 14 May 2013 18:03:30 -0000 Mailing-List: contact allura-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: allura-dev@incubator.apache.org Delivered-To: mailing list allura-commits@incubator.apache.org Received: (qmail 47979 invoked by uid 99); 14 May 2013 18:03:30 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 14 May 2013 18:03:30 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 9097A87AA; Tue, 14 May 2013 18:03:30 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: johnsca@apache.org To: allura-commits@incubator.apache.org Date: Tue, 14 May 2013 18:03:31 -0000 Message-Id: <75e9c2b4bc864dffa4c4cf35e7b3f343@git.apache.org> In-Reply-To: <31394a6013ab49e8b402fc710f7ba99e@git.apache.org> References: <31394a6013ab49e8b402fc710f7ba99e@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [02/13] git commit: [#6212] Support configurable start date for counting user stats [#6212] Support configurable start date for counting user stats Signed-off-by: Cory Johns Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/9912f312 Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/9912f312 Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/9912f312 Branch: refs/heads/cj/5571 Commit: 9912f31257c3be2bb6a6cc35f1801245071fd1c0 Parents: bffea80 Author: Cory Johns Authored: Mon May 13 19:30:40 2013 +0000 Committer: Dave Brondsema Committed: Mon May 13 21:56:19 2013 +0000 ---------------------------------------------------------------------- Allura/allura/model/stats.py | 15 +++++++++++++-- .../forgeuserstats/controllers/userstats.py | 2 +- ForgeUserStats/forgeuserstats/tests/test_model.py | 13 ++++++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9912f312/Allura/allura/model/stats.py ---------------------------------------------------------------------- diff --git a/Allura/allura/model/stats.py b/Allura/allura/model/stats.py index cff1abc..166933e 100644 --- a/Allura/allura/model/stats.py +++ b/Allura/allura/model/stats.py @@ -15,9 +15,11 @@ # specific language governing permissions and limitations # under the License. +from datetime import datetime import pymongo from pylons import tmpl_context as c, app_globals as g from pylons import request +from tg import config import bson from ming import schema as S @@ -79,8 +81,17 @@ class Stats(MappedClass): programming_languages=[S.ObjectId], lines=int)])) + @property + def start_date(self): + """Date from which stats should be calculated. + + The user may have registered before stats were collected, + making calculations based on registration date unfair.""" + min_date = config.get('userstats.start_date', '0001-1-1') + return max(datetime.strptime(min_date,'%Y-%m-%d'), self.registration_date) + def getCodeContribution(self): - days=(datetime.today() - self.registration_date).days + days=(datetime.today() - self.start_date).days if not days: days=1 for val in self['general']: @@ -94,7 +105,7 @@ class Stats(MappedClass): return 0 def getDiscussionContribution(self): - days=(datetime.today() - self.registration_date).days + days=(datetime.today() - self.start_date).days if not days: days=1 for val in self['general']: http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9912f312/ForgeUserStats/forgeuserstats/controllers/userstats.py ---------------------------------------------------------------------- diff --git a/ForgeUserStats/forgeuserstats/controllers/userstats.py b/ForgeUserStats/forgeuserstats/controllers/userstats.py index 85b26df..0624a31 100644 --- a/ForgeUserStats/forgeuserstats/controllers/userstats.py +++ b/ForgeUserStats/forgeuserstats/controllers/userstats.py @@ -212,7 +212,7 @@ def _getDataForCategory(category, stats): averagetime = lm_tickets.get('averagesolvingtime') - days = (datetime.utcnow() - stats.registration_date).days + days = (datetime.utcnow() - stats.start_date).days if days >= 30: pmartifacts = dict( created = round(totartifacts['created']*30.0/days,2), http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/9912f312/ForgeUserStats/forgeuserstats/tests/test_model.py ---------------------------------------------------------------------- diff --git a/ForgeUserStats/forgeuserstats/tests/test_model.py b/ForgeUserStats/forgeuserstats/tests/test_model.py index 4b123eb..2da99e1 100644 --- a/ForgeUserStats/forgeuserstats/tests/test_model.py +++ b/ForgeUserStats/forgeuserstats/tests/test_model.py @@ -20,14 +20,18 @@ import unittest from datetime import datetime, timedelta from pylons import tmpl_context as c +from tg import config from alluratest.controller import setup_basic_test, setup_global_objects from allura.tests import decorators as td from allura.model import User, Project, TroveCategory +from allura.lib import helpers as h from allura import model as M from forgegit.tests import with_git +from forgeuserstats.model import stats as USM + class TestUserStats(unittest.TestCase): def setUp(self): @@ -339,7 +343,7 @@ class TestUserStats(unittest.TestCase): assert init_commits['number'] == 4 init_lmcommits = self.user.stats.getLastMonthCommits() assert init_lmcommits['number'] == 4 - + p.trove_topic = [topic._id] self.user.stats.addCommit(commit, datetime.utcnow(), p) commits = self.user.stats.getCommits() @@ -389,3 +393,10 @@ class TestUserStats(unittest.TestCase): assert lm_logins == init_lm_logins + 1 assert abs(self.user.stats.last_login - login_datetime) < timedelta(seconds=1) + def test_start_date(self): + stats = USM.UserStats(registration_date=datetime(2012,04,01)) + self.assertEqual(stats.start_date, datetime(2012,04,01)) + with h.push_config(config, **{'userstats.start_date': '2013-04-01'}): + self.assertEqual(stats.start_date, datetime(2013,04,01)) + with h.push_config(config, **{'userstats.start_date': '2011-04-01'}): + self.assertEqual(stats.start_date, datetime(2012,04,01))