(request, body)
return JsonResponse({"status": True, "data": data}, safe=False)
Fetch the number of analysis over a
        given period for the "failed" and
        "successful" states. Values are
        returned in months.
        :param days: integer; the amount of days to go back in time starting from today.
        :return: A list of months and their statistics
Fetch the number of analysis over a
        given period for the "failed" and
        "successful" states. Values are
        returned in months.
        :param days: integer; the amount of days to go back in time starting from today.
        :return: A list of months and their statistics
Fetch the number of analysis over a given period for the failed and successful states . Values are returned in months . : param days : integer ; the amount of days to go back in time starting from today . : return : A list of months and their statistics
def tasks_stats(request, body):
        """
        Fetch the number of analysis over a
        given period for the "failed" and
        "successful" states. Values are
        returned in months.
        :param days: integer; the amount of days to go back in time starting from today.
        :return: A list of months and their statistics
        """
        now = datetime.datetime.now()
        days = body.get("days", 365)

        if not isinstance(days, int):
            return json_error_response("parameter \"days\" not an integer")

        q = db.Session().query(Task)
        q = q.filter(Task.added_on.between(
            now - datetime.timedelta(days=days), now)
        )
        q = q.order_by(sqlalchemy.asc(Task.added_on))
        tasks = q.all()

        def _rtn_structure(start):
            _data = []

            for i in range(0, 12):
                if (now - start).total_seconds() < 0:
                    return _data

                _data.append({
                    "month": start.month,
                    "year": start.year,
                    "month_human": calendar.month_name[start.month],
                    "num": 0
                })

                start = start + dateutil.relativedelta.relativedelta(months=1)

            return _data

        if not tasks:
            return json_error_response("No tasks found")

        data = {
            "analysis": _rtn_structure(tasks[0].added_on),
            "failed": _rtn_structure(tasks[0].added_on)
        }

        for task in tasks:
            added_on = task.added_on
            success = "analysis" if task.status == "reported" else "failed"

            entry = next((z for z in data[success] if
                          z["month"] == added_on.month and
                          z["year"] == added_on.year), None)
            if entry:
                entry["num"] += 1

        return JsonResponse({"status": True, "data": data}, safe=False)
def tasks_stats ( request , body ) : now = datetime . datetime . now ( ) days = body . get ( "days" , 365 ) if not isinstance ( days , int ) : return json_error_response ( "parameter \"days\" not an integer" ) q = db . Session ( ) . query ( Task ) q = q . filter ( Task . added_on . between ( now - datetime . timedelta ( days = days ) , now ) ) q = q . order_by ( sqlalchemy . asc ( Task . added_on ) ) tasks = q . all ( ) def _rtn_structure ( start ) : _data = [ ] for i in range ( 0 , 12 ) : if ( now - start ) . total_seconds ( ) < 0 : return _data _data . append ( { "month" : start . month , "year" : start . year , "month_human" : calendar . month_name [ start . month ] , "num" : 0 } ) start = start + dateutil . relativedelta . relativedelta ( months = 1 ) return _data if not tasks : return json_error_response ( "No tasks found" ) data = { "analysis" : _rtn_structure ( tasks [ 0 ] . added_on ) , "failed" : _rtn_structure ( tasks [ 0 ] . added_on ) } for task in tasks : added_on = task . added_on success = "analysis" if task . status == "reported" else "failed" entry = next ( ( z for z in data [ success ] if z [ "month" ] == added_on . month and z [ "year" ] == added_on . year ) , None ) if entry : entry [ "num" ] += 1 return JsonResponse ( { "status" : True , "data" : data } , safe = False )