Skip to content

Commit

Permalink
Merge pull request #131 from kkamkou/issue-130
Browse files Browse the repository at this point in the history
status_detect is not triggered in some cases, closes #130
  • Loading branch information
kkamkou committed Nov 18, 2015
2 parents 87c24a8 + 44de34d commit f0fa5e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
20 changes: 10 additions & 10 deletions gitmostwanted/tasks/repo_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sqlalchemy.sql import expression
from statistics import variance, mean
from datetime import datetime, timedelta
from types import GeneratorType


@celery.task()
Expand All @@ -13,19 +14,14 @@ def status_detect(num_days, num_segments):
.filter(RepoStars.repo_id == repo.id)\
.order_by(expression.asc(RepoStars.day))\
.all()
if not result:
continue

means = []
chunks = result_split(list(result_normalize(result, num_days)), num_segments)
for chunk in chunks:
means.append(1 if variance(chunk) >= 1000 else mean(chunk))
val = 0 if not result else result_mean(
result_split(list(result_normalize(result, num_days)), num_segments)
)

mean_val = mean(means)
repo.status = 'hopeless' if val < 1 else 'promising'

repo.status = 'hopeless' if mean_val < 1 else 'promising'

db.session.add(RepoMean(repo=repo, value=mean_val))
db.session.add(RepoMean(repo=repo, value=val))
db.session.commit()


Expand All @@ -43,6 +39,10 @@ def status_refresh(num_days):
db.session.commit()


def result_mean(chunks: GeneratorType):
return mean([1 if variance(chunk) >= 1000 else mean(chunk) for chunk in chunks])


def result_normalize(lst: list, num_days: int):
fst = lst[0][0]
lst = dict(lst)
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/tasks/test_repo_status.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from gitmostwanted.tasks.repo_status import result_normalize, result_split
from gitmostwanted.tasks.repo_status import result_normalize, result_split, result_mean
from types import GeneratorType
from unittest import TestCase
from itertools import chain


class TasksRepoStatusTestCase(TestCase):
Expand All @@ -20,3 +21,14 @@ def test_split_result(self):
lst = list(result)
self.assertEquals(len(lst), 4)
self.assertEquals(len(lst[0]), 7)

def test_calculate_mean(self):
def gn_normal():
yield [10, 20]

def gn_mixed():
yield [10, 99999, 10]

self.assertEquals(result_mean(gn_normal()), 15)
self.assertEquals(result_mean(gn_mixed()), 1)
self.assertEquals(result_mean(chain(gn_mixed(), gn_normal())), 8)

0 comments on commit f0fa5e3

Please sign in to comment.