You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by jo...@apache.org on 2013/05/13 22:46:48 UTC
[1/2] git commit: [#6212] Support configurable start date for
counting user stats
Updated Branches:
refs/heads/cj/6212 f7f814460 -> 870f9e6e2
[#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/486a6e7e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/486a6e7e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/486a6e7e
Branch: refs/heads/cj/6212
Commit: 486a6e7ea5cd3bc19c1cec9c8d4ed21b45f207b4
Parents: f7f8144
Author: Cory Johns <cj...@slashdotmedia.com>
Authored: Mon May 13 19:30:40 2013 +0000
Committer: Cory Johns <cj...@slashdotmedia.com>
Committed: Mon May 13 19:45:07 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/486a6e7e/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/486a6e7e/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/486a6e7e/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))
[2/2] git commit: [#6212] Support config option to disable counting
lines-of-code
Posted by jo...@apache.org.
[#6212] Support config option to disable counting lines-of-code
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/870f9e6e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/870f9e6e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/870f9e6e
Branch: refs/heads/cj/6212
Commit: 870f9e6e2ac5cbcc9c0cea9089fe551a876deb86
Parents: 486a6e7
Author: Cory Johns <cj...@slashdotmedia.com>
Authored: Mon May 13 20:44:53 2013 +0000
Committer: Cory Johns <cj...@slashdotmedia.com>
Committed: Mon May 13 20:44:53 2013 +0000
----------------------------------------------------------------------
Allura/allura/model/stats.py | 28 ++++++++-------
ForgeUserStats/forgeuserstats/tests/test_model.py | 30 ++++++++++++++++
2 files changed, 45 insertions(+), 13 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/870f9e6e/Allura/allura/model/stats.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/stats.py b/Allura/allura/model/stats.py
index 166933e..3946557 100644
--- a/Allura/allura/model/stats.py
+++ b/Allura/allura/model/stats.py
@@ -20,6 +20,7 @@ import pymongo
from pylons import tmpl_context as c, app_globals as g
from pylons import request
from tg import config
+from paste.deploy.converters import asbool
import bson
from ming import schema as S
@@ -501,19 +502,20 @@ class Stats(MappedClass):
oldcommit = newcommit.repo.commit(newcommit.parent_ids[0])
totlines = 0
- for changed in d.changed:
- newblob = newcommit.tree.get_blob_by_path(changed)
- oldblob = oldcommit.tree.get_blob_by_path(changed)
- totlines+=_computeLines(newblob, oldblob)
-
- for copied in d.copied:
- newblob = newcommit.tree.get_blob_by_path(copied['new'])
- oldblob = oldcommit.tree.get_blob_by_path(copied['old'])
- totlines+=_computeLines(newblob, oldblob)
-
- for added in d.added:
- newblob = newcommit.tree.get_blob_by_path(added)
- totlines+=_computeLines(newblob)
+ if asbool(config.get('userstats.count_lines_of_code', True)):
+ for changed in d.changed:
+ newblob = newcommit.tree.get_blob_by_path(changed)
+ oldblob = oldcommit.tree.get_blob_by_path(changed)
+ totlines+=_computeLines(newblob, oldblob)
+
+ for copied in d.copied:
+ newblob = newcommit.tree.get_blob_by_path(copied['new'])
+ oldblob = oldcommit.tree.get_blob_by_path(copied['old'])
+ totlines+=_computeLines(newblob, oldblob)
+
+ for added in d.added:
+ newblob = newcommit.tree.get_blob_by_path(added)
+ totlines+=_computeLines(newblob)
_addCommitData(self, topics, languages, totlines)
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/870f9e6e/ForgeUserStats/forgeuserstats/tests/test_model.py
----------------------------------------------------------------------
diff --git a/ForgeUserStats/forgeuserstats/tests/test_model.py b/ForgeUserStats/forgeuserstats/tests/test_model.py
index 2da99e1..dd77b32 100644
--- a/ForgeUserStats/forgeuserstats/tests/test_model.py
+++ b/ForgeUserStats/forgeuserstats/tests/test_model.py
@@ -21,6 +21,7 @@ from datetime import datetime, timedelta
from pylons import tmpl_context as c
from tg import config
+import mock
from alluratest.controller import setup_basic_test, setup_global_objects
from allura.tests import decorators as td
@@ -400,3 +401,32 @@ class TestUserStats(unittest.TestCase):
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))
+
+ @mock.patch('allura.model.stats.difflib.unified_diff')
+ def test_count_loc(self, unified_diff):
+ stats = USM.UserStats()
+ newcommit = mock.Mock(
+ parent_ids=['deadbeef'],
+ diffs=mock.Mock(
+ changed=[mock.MagicMock()],
+ copied=[mock.MagicMock()],
+ added=[mock.MagicMock()],
+ ),
+ )
+ unified_diff.return_value = ['+++','---','+line']
+ newcommit.tree.get_blob_by_path.return_value = mock.MagicMock()
+ newcommit.tree.get_blob_by_path.return_value.__iter__.return_value = ['one']
+ newcommit.repo.commit().tree.get_blob_by_path.return_value = mock.MagicMock()
+ newcommit.repo.commit().tree.get_blob_by_path.return_value.__iter__.return_value = ['two']
+ commit_datetime = datetime.now()
+ project = mock.Mock(
+ trove_topic=[],
+ trove_language=[],
+ )
+ stats.addCommit(newcommit, commit_datetime, project)
+ self.assertEqual(stats.general[0].commits[0], {'lines': 3, 'number': 1, 'language': None})
+ unified_diff.reset_mock()
+ with h.push_config(config, **{'userstats.count_lines_of_code': 'false'}):
+ stats.addCommit(newcommit, commit_datetime, project)
+ self.assertEqual(stats.general[0].commits[0], {'lines': 3, 'number': 2, 'language': None})
+ unified_diff.assert_not_called()