Working on backlog stats api
parent
fe8da58db3
commit
eb85534813
|
@ -1,8 +1,11 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from django.db.models import Q
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import detail_route
|
||||
|
||||
from greenmine.base import filters
|
||||
from greenmine.base.api import ModelCrudViewSet, ModelListViewSet
|
||||
|
@ -19,6 +22,53 @@ class ProjectViewSet(ModelCrudViewSet):
|
|||
list_serializer_class = serializers.ProjectSerializer
|
||||
permission_classes = (IsAuthenticated, permissions.ProjectPermission)
|
||||
|
||||
@detail_route(methods=['get'])
|
||||
def stats(self, request, pk=None):
|
||||
project = get_object_or_404(models.Project, pk=pk)
|
||||
project_stats = {
|
||||
'name': project.name,
|
||||
'total_milestones': project.total_milestones,
|
||||
'total_points': project.total_story_points,
|
||||
'milestones': []
|
||||
}
|
||||
|
||||
current_milestone = 0
|
||||
current_evolution = 0
|
||||
current_team_increment = 0
|
||||
current_client_increment = 0
|
||||
optimal_points_per_sprint = project.total_story_points / (project.total_milestones - 1)
|
||||
|
||||
for ml in project.milestones.all():
|
||||
optimal_points = project.total_story_points - (optimal_points_per_sprint * current_milestone)
|
||||
project_stats['milestones'].append({
|
||||
'name': ml.name,
|
||||
'optimal': optimal_points,
|
||||
'evolution': project.total_story_points - current_evolution,
|
||||
'team-increment': current_team_increment,
|
||||
'client-increment': current_client_increment,
|
||||
})
|
||||
current_milestone += 1
|
||||
current_evolution += sum(ml.closed_points.values())
|
||||
current_team_increment += sum(ml.team_increment_points.values())
|
||||
current_client_increment += sum(ml.client_increment_points.values())
|
||||
|
||||
if project.total_milestones > project.milestones.all().count():
|
||||
for x in range(project.milestones.all().count(), project.total_milestones):
|
||||
optimal_points = project.total_story_points - (optimal_points_per_sprint * current_milestone)
|
||||
if current_evolution is not None:
|
||||
current_evolution = project.total_story_points - current_evolution
|
||||
project_stats['milestones'].append({
|
||||
'name': "Future sprint",
|
||||
'optimal': optimal_points,
|
||||
'evolution': current_evolution,
|
||||
'team-increment': current_team_increment + sum(project.future_team_increment.values()),
|
||||
'client-increment': current_client_increment + sum(project.future_client_increment.values()),
|
||||
})
|
||||
current_milestone += 1
|
||||
current_evolution = None
|
||||
|
||||
return Response(project_stats)
|
||||
|
||||
def get_queryset(self):
|
||||
qs = super(ProjectViewSet, self).get_queryset()
|
||||
qs = qs.filter(Q(owner=self.request.user) |
|
||||
|
|
|
@ -13,9 +13,12 @@ from picklefield.fields import PickledObjectField
|
|||
|
||||
from greenmine.base.utils.slug import slugify_uniquely
|
||||
from greenmine.base.notifications.models import WatchedMixin
|
||||
from greenmine.projects.userstories.models import UserStory
|
||||
from . import choices
|
||||
|
||||
import reversion
|
||||
import itertools
|
||||
import copy
|
||||
|
||||
|
||||
def get_attachment_file_path(instance, filename):
|
||||
|
@ -157,6 +160,72 @@ class Project(models.Model):
|
|||
rp_query = rp_query.exclude(role__id__in=roles.values_list("id", flat=True))
|
||||
rp_query.delete()
|
||||
|
||||
def _get_user_stories_points(self, user_stories):
|
||||
role_points = [us.role_points.all() for us in user_stories]
|
||||
flat_role_points = itertools.chain(*role_points)
|
||||
|
||||
result = {}
|
||||
for role_point in flat_role_points:
|
||||
if role_point.points.value is not None:
|
||||
if role_point.role_id in result:
|
||||
result[role_point.role_id] += float(role_point.points.value)
|
||||
else:
|
||||
result[role_point.role_id] = float(role_point.points.value)
|
||||
|
||||
return result
|
||||
|
||||
def _dict_sum(self, dict1, dict2):
|
||||
dict_result = copy.copy(dict2)
|
||||
for key, value in dict1.items():
|
||||
if key in dict_result:
|
||||
dict_result[key] += value
|
||||
else:
|
||||
dict_result[key] = value
|
||||
return dict_result
|
||||
|
||||
|
||||
@property
|
||||
def future_team_increment(self):
|
||||
user_stories = UserStory.objects.none()
|
||||
last_milestones = self.milestones.order_by('-estimated_finish')
|
||||
last_milestone = last_milestones[1] if last_milestones else None
|
||||
user_stories = UserStory.objects.filter(
|
||||
created_date__gte=last_milestone.estimated_finish if last_milestones else None,
|
||||
project_id=self.id,
|
||||
client_requirement=False,
|
||||
team_requirement=True
|
||||
)
|
||||
team_increment = self._get_user_stories_points(user_stories)
|
||||
shared_increment = {key: value/2 for key, value in self.future_shared_increment.items()}
|
||||
return self._dict_sum(team_increment, shared_increment)
|
||||
|
||||
@property
|
||||
def future_client_increment(self):
|
||||
user_stories = UserStory.objects.none()
|
||||
last_milestones = self.milestones.order_by('-estimated_finish')
|
||||
last_milestone = last_milestones[1] if last_milestones else None
|
||||
user_stories = UserStory.objects.filter(
|
||||
created_date__gte=last_milestone.estimated_finish if last_milestones else None,
|
||||
project_id=self.id,
|
||||
client_requirement=True,
|
||||
team_requirement=False
|
||||
)
|
||||
client_increment = self._get_user_stories_points(user_stories)
|
||||
shared_increment = {key: value/2 for key, value in self.future_shared_increment.items()}
|
||||
return self._dict_sum(client_increment, shared_increment)
|
||||
|
||||
@property
|
||||
def future_shared_increment(self):
|
||||
user_stories = UserStory.objects.none()
|
||||
last_milestones = self.milestones.order_by('-estimated_finish')
|
||||
last_milestone = last_milestones[1] if last_milestones else None
|
||||
user_stories = UserStory.objects.filter(
|
||||
created_date__gte=last_milestone.estimated_finish if last_milestones else None,
|
||||
project_id=self.id,
|
||||
client_requirement=True,
|
||||
team_requirement=True
|
||||
)
|
||||
return self._get_user_stories_points(user_stories)
|
||||
|
||||
# User Stories common Models
|
||||
|
||||
|
|
Loading…
Reference in New Issue