from cornflake import fields, serializers

from radar.api.serializers.common import GroupField
from radar.api.serializers.stats import DataPointListSerializer, PatientsByGroupListSerializer
from radar.api.views.generics import response_json, ApiView, parse_args
from radar.models.groups import Group, GROUP_TYPE
from radar.stats import patients_by_group, recruitment_by_month, patients_by_recruited_group


class RecruitmentByMonthRequestSerializer(serializers.Serializer):
    group = GroupField(required=False)


class PatientsByGroupRequestSerializer(serializers.Serializer):
    group = GroupField(required=False)
    group_type = fields.EnumField(GROUP_TYPE, required=False)


class PatientsByRecruitedGroupRequestSerializer(serializers.Serializer):
    group = GroupField(required=False)


class RecruitmentByMonthView(ApiView):
    @response_json(DataPointListSerializer)
    def get(self):
        args = parse_args(RecruitmentByMonthRequestSerializer)

        if args['group'] is not None:
            group = args['group']
        else:
            group = Group.get_radar()

        points = recruitment_by_month(group)

        return {'points': points}


class PatientsByGroupView(ApiView):
    @response_json(PatientsByGroupListSerializer)
    def get(self):
        args = parse_args(PatientsByGroupRequestSerializer)

        counts = patients_by_group(args['group'], args['group_type'])
        counts = [{'group': group, 'count': count} for group, count in counts]

        return {'counts': counts}


class PatientsByRecruitedGroupView(ApiView):
    @response_json(PatientsByGroupListSerializer)
    def get(self):
        args = parse_args(PatientsByRecruitedGroupRequestSerializer)

        if args['group'] is not None:
            group = args['group']
        else:
            group = Group.get_radar()

        counts = patients_by_recruited_group(group)
        counts = [{'group': x, 'count': y} for x, y in counts]

        return {'counts': counts}


def register_views(app):
    app.add_url_rule('/recruitment-by-month', view_func=RecruitmentByMonthView.as_view('recruitment_by_month'))
    app.add_url_rule('/patients-by-group', view_func=PatientsByGroupView.as_view('patients_by_group'))
    app.add_url_rule('/patients-by-recruited-group', view_func=PatientsByRecruitedGroupView.as_view('patients_by_recruited_group'))

@@ -34,16 +34,81 @@
 ializers
+%0Afrom cornflake.validators import in_%0Afrom flask import Blueprint
 %0A%0Afrom r
@@ -474,39 +474,32 @@
 lass Recruitment
-ByMonth
 RequestSerialize
@@ -556,32 +556,112 @@
 required=False)%0A
+    interval = fields.StringField(default='month', validators=%5Bin_(%5B'month'%5D)%5D)%0A
 %0A%0Aclass Patients
@@ -940,31 +940,24 @@
  Recruitment
-ByMonth
 View(ApiView
@@ -1059,23 +1059,16 @@
 ruitment
-ByMonth
 RequestS
@@ -2124,27 +2124,70 @@
 s(app):%0A    
-app
+stats = Blueprint('stats', __name__)%0A    stats
 .add_url_rul
@@ -2205,17 +2205,8 @@
 ment
--by-month
 ', v
@@ -2229,15 +2229,8 @@
 ment
-ByMonth
 View
@@ -2254,25 +2254,16 @@
 ment
-_by_month
 '))%0A    
 app.
@@ -2250,35 +2250,37 @@
 ruitment'))%0A    
-app
+stats
 .add_url_rule('/
@@ -2363,19 +2363,21 @@
 '))%0A    
-app
+stats
 .add_url
@@ -2485,16 +2485,71 @@
 ruited_group'))%0A
+    app.register_blueprint(stats, url_prefix='/stats')%0A

Add stats blueprint