You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2013/05/17 00:30:15 UTC
[24/50] [abbrv] 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 <cj...@slashdotmedia.com>
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/db/6208
Commit: 9912f31257c3be2bb6a6cc35f1801245071fd1c0
Parents: bffea80
Author: Cory Johns <cj...@slashdotmedia.com>
Authored: Mon May 13 19:30:40 2013 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
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))