Example #1
0
    def get(self, request, project, version):
        """
        Get a Project Release's Stats
        `````````````````````````````

        Returns the stats of a given release under a project.

        :pparam string organization_slug: the slug of the organization the
                                          release belongs to.
        :pparam string project_slug: the slug of the project to list the
                                     release files of.
        :pparam string version: the version identifier of the release.
        :auth: required
        """
        stats_type = request.GET.get("type") or "sessions"
        if stats_type not in ("users", "sessions"):
            return Response({"detail": "invalid stat"}, status=400)

        try:
            params = self.get_filter_params(request, project)
            rollup = self.get_rollup(request, params)
        except ProjectEventsError as e:
            return Response({"detail": six.text_type(e)}, status=400)

        stats = get_project_release_stats(
            project_id=params["project_id"][0],
            release=version,
            stat=stats_type,
            rollup=rollup,
            start=params["start"],
            end=params["end"],
            environments=params.get("environment"),
        )

        users_breakdown = {}
        for timespan, data in six.iteritems(
                get_crash_free_breakdown(
                    project_id=params["project_id"][0],
                    release=version,
                    environments=params.get("environment"),
                )):
            users_breakdown[timespan] = {
                "totalUsers": data["total_users"],
                "crashFreeUsers": data["crash_free_users"],
                "totalSessions": data["total_sessions"],
                "crashFreeSessions": data["crash_free_sessions"],
            }

        return Response(serialize({
            "stats": stats,
            "usersBreakdown": users_breakdown
        }),
                        status=200)
Example #2
0
    def get(self, request, project, version):
        """
        Get a Project Release's Stats
        `````````````````````````````

        Returns the stats of a given release under a project.

        :pparam string organization_slug: the slug of the organization the
                                          release belongs to.
        :pparam string project_slug: the slug of the project to list the
                                     release files of.
        :pparam string version: the version identifier of the release.
        :auth: required
        """
        stats_type = request.GET.get("type") or "sessions"
        if stats_type not in ("users", "sessions"):
            return Response({"detail": "invalid stat"}, status=400)

        try:
            params = self.get_filter_params(request, project)
            rollup = get_rollup_from_request(
                request,
                params,
                "24h",
                ProjectEventsError(
                    "Your interval and date range would create too many results. "
                    "Use a larger interval, or a smaller date range."),
            )
            # The minimum interval is one hour on the server
            rollup = max(rollup, 3600)
        except ProjectEventsError as e:
            return Response({"detail": six.text_type(e)}, status=400)

        release = upsert_missing_release(project, version)
        if release is None:
            raise ResourceDoesNotExist

        stats, totals = get_project_release_stats(
            project_id=params["project_id"][0],
            release=version,
            stat=stats_type,
            rollup=rollup,
            start=params["start"],
            end=params["end"],
            environments=params.get("environment"),
        )

        users_breakdown = []
        for data in get_crash_free_breakdown(
                project_id=params["project_id"][0],
                release=version,
                environments=params.get("environment"),
                start=release.date_added,
        ):
            users_breakdown.append({
                "date":
                data["date"],
                "totalUsers":
                data["total_users"],
                "crashFreeUsers":
                data["crash_free_users"],
                "totalSessions":
                data["total_sessions"],
                "crashFreeSessions":
                data["crash_free_sessions"],
            })

        return Response(
            serialize({
                "stats": stats,
                "statTotals": totals,
                "usersBreakdown": users_breakdown
            }),
            status=200,
        )