Task #3716: [/api/v1/stats/discover] Show stats for discover secction
parent
3d3e8f2d49
commit
19bbe1c512
|
@ -17,25 +17,40 @@ from collections import OrderedDict
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.views.decorators.cache import cache_page
|
from django.views.decorators.cache import cache_page
|
||||||
|
|
||||||
from taiga.base.api import viewsets
|
|
||||||
from taiga.base import response
|
from taiga.base import response
|
||||||
|
from taiga.base.api import viewsets
|
||||||
|
|
||||||
from . import permissions
|
from . import permissions
|
||||||
from . import services
|
from . import services
|
||||||
|
|
||||||
|
|
||||||
class SystemStatsViewSet(viewsets.ViewSet):
|
CACHE_TIMEOUT = getattr(settings, "STATS_CACHE_TIMEOUT", 0)
|
||||||
|
|
||||||
|
|
||||||
|
class BaseStatsViewSet(viewsets.ViewSet):
|
||||||
|
@property
|
||||||
|
def _cache_timeout(self):
|
||||||
|
return CACHE_TIMEOUT
|
||||||
|
|
||||||
|
def dispatch(self, *args, **kwargs):
|
||||||
|
return cache_page(self._cache_timeout)(super().dispatch)(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class SystemStatsViewSet(BaseStatsViewSet):
|
||||||
permission_classes = (permissions.SystemStatsPermission,)
|
permission_classes = (permissions.SystemStatsPermission,)
|
||||||
|
|
||||||
def list(self, request, **kwargs):
|
def list(self, request, **kwargs):
|
||||||
stats = OrderedDict()
|
stats = OrderedDict()
|
||||||
stats["users"] = services.get_users_stats()
|
stats["users"] = services.get_users_public_stats()
|
||||||
stats["projects"] = services.get_projects_stats()
|
stats["projects"] = services.get_projects_public_stats()
|
||||||
stats["userstories"] = services.get_user_stories_stats()
|
stats["userstories"] = services.get_user_stories_public_stats()
|
||||||
return response.Ok(stats)
|
return response.Ok(stats)
|
||||||
|
|
||||||
def _get_cache_timeout(self):
|
|
||||||
return getattr(settings, "STATS_CACHE_TIMEOUT", 0)
|
|
||||||
|
|
||||||
def dispatch(self, *args, **kwargs):
|
class DiscoverStatsViewSet(BaseStatsViewSet):
|
||||||
return cache_page(self._get_cache_timeout())(super().dispatch)(*args, **kwargs)
|
permission_classes = (permissions.DiscoverStatsPermission,)
|
||||||
|
|
||||||
|
def list(self, request, **kwargs):
|
||||||
|
stats = OrderedDict()
|
||||||
|
stats["projects"] = services.get_projects_discover_stats(user=request.user)
|
||||||
|
return response.Ok(stats)
|
||||||
|
|
|
@ -14,7 +14,6 @@
|
||||||
|
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
from django.conf import settings
|
|
||||||
from django.conf.urls import include, url
|
from django.conf.urls import include, url
|
||||||
|
|
||||||
from .routers import router
|
from .routers import router
|
||||||
|
@ -25,6 +24,5 @@ class StatsAppConfig(AppConfig):
|
||||||
verbose_name = "Stats"
|
verbose_name = "Stats"
|
||||||
|
|
||||||
def ready(self):
|
def ready(self):
|
||||||
if settings.STATS_ENABLED:
|
from taiga.urls import urlpatterns
|
||||||
from taiga.urls import urlpatterns
|
urlpatterns.append(url(r'^api/v1/', include(router.urls)))
|
||||||
urlpatterns.append(url(r'^api/v1/', include(router.urls)))
|
|
||||||
|
|
|
@ -18,3 +18,7 @@ from taiga.base.api import permissions
|
||||||
|
|
||||||
class SystemStatsPermission(permissions.TaigaResourcePermission):
|
class SystemStatsPermission(permissions.TaigaResourcePermission):
|
||||||
global_perms = permissions.AllowAny()
|
global_perms = permissions.AllowAny()
|
||||||
|
|
||||||
|
|
||||||
|
class DiscoverStatsPermission(permissions.TaigaResourcePermission):
|
||||||
|
global_perms = permissions.AllowAny()
|
||||||
|
|
|
@ -12,9 +12,16 @@
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
from taiga.base import routers
|
from taiga.base import routers
|
||||||
|
|
||||||
from . import api
|
from . import api
|
||||||
|
|
||||||
|
|
||||||
router = routers.DefaultRouter(trailing_slash=False)
|
router = routers.DefaultRouter(trailing_slash=False)
|
||||||
router.register(r"stats/system", api.SystemStatsViewSet, base_name="system-stats")
|
|
||||||
|
if settings.STATS_ENABLED:
|
||||||
|
router.register(r"stats/system", api.SystemStatsViewSet, base_name="system-stats")
|
||||||
|
|
||||||
|
router.register(r"stats/discover", api.DiscoverStatsViewSet, base_name="discover-stats")
|
||||||
|
|
|
@ -22,9 +22,13 @@ from datetime import timedelta
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
|
||||||
def get_users_stats():
|
###########################################################################
|
||||||
|
# Public Stats
|
||||||
|
###########################################################################
|
||||||
|
|
||||||
|
def get_users_public_stats():
|
||||||
model = apps.get_model("users", "User")
|
model = apps.get_model("users", "User")
|
||||||
queryset = model.objects.filter(is_active=True, is_system=False)
|
queryset = model.objects.filter(is_active=True, is_system=False)
|
||||||
stats = OrderedDict()
|
stats = OrderedDict()
|
||||||
|
|
||||||
today = timezone.now()
|
today = timezone.now()
|
||||||
|
@ -71,9 +75,9 @@ def get_users_stats():
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
|
|
||||||
def get_projects_stats():
|
def get_projects_public_stats():
|
||||||
model = apps.get_model("projects", "Project")
|
model = apps.get_model("projects", "Project")
|
||||||
queryset = model.objects.all()
|
queryset = model.objects.all()
|
||||||
stats = OrderedDict()
|
stats = OrderedDict()
|
||||||
|
|
||||||
today = timezone.now()
|
today = timezone.now()
|
||||||
|
@ -109,9 +113,9 @@ def get_projects_stats():
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
|
|
||||||
def get_user_stories_stats():
|
def get_user_stories_public_stats():
|
||||||
model = apps.get_model("userstories", "UserStory")
|
model = apps.get_model("userstories", "UserStory")
|
||||||
queryset = model.objects.all()
|
queryset = model.objects.all()
|
||||||
stats = OrderedDict()
|
stats = OrderedDict()
|
||||||
|
|
||||||
today = timezone.now()
|
today = timezone.now()
|
||||||
|
@ -130,3 +134,20 @@ def get_user_stories_stats():
|
||||||
.count()) / 5
|
.count()) / 5
|
||||||
|
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
|
###########################################################################
|
||||||
|
# Discover Stats
|
||||||
|
###########################################################################
|
||||||
|
|
||||||
|
def get_projects_discover_stats(user=None):
|
||||||
|
model = apps.get_model("projects", "Project")
|
||||||
|
queryset = model.objects.all()
|
||||||
|
stats = OrderedDict()
|
||||||
|
|
||||||
|
# Get Public (visible) projects
|
||||||
|
queryset = queryset.filter(Q(is_private=False) |
|
||||||
|
Q(is_private=True, anon_permissions__contains=["view_project"]))
|
||||||
|
|
||||||
|
stats["total"] = queryset.count()
|
||||||
|
|
||||||
|
return stats
|
||||||
|
|
Loading…
Reference in New Issue