prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Add completed orders to order-statistics endpoint
  **Is your feature request related to a problem? Please describe.**
  Currently order statistics endpoint does not return completed orders.
  
  **Describe the solution you'd like**
  It should return completed orders and also fix sales accordingly. sales should return order values calculated from completed orders only.
  
  **Additional context**
  Needed in FE.
  
  
  </issue>
  <code>
  [start of app/api/order_statistics/events.py]
  1 from flask_rest_jsonapi import ResourceDetail
  2 from marshmallow_jsonapi import fields
  3 from marshmallow_jsonapi.flask import Schema
  4 from sqlalchemy import func
  5 
  6 from app.api.bootstrap import api
  7 from app.api.helpers.db import get_count
  8 from app.api.helpers.db import safe_query
  9 from app.api.helpers.utilities import dasherize
  10 from app.models import db
  11 from app.models.event import Event
  12 from app.models.order import Order, OrderTicket
  13 
  14 
  15 class OrderStatisticsEventSchema(Schema):
  16     """
  17     Api schema for general statistics of event
  18     """
  19 
  20     class Meta:
  21         """
  22         Meta class
  23         """
  24         type_ = 'order-statistics-event'
  25         self_view = 'v1.order_statistics_event_detail'
  26         self_view_kwargs = {'id': '<id>'}
  27         inflect = dasherize
  28 
  29     id = fields.Str()
  30     identifier = fields.Str()
  31     tickets = fields.Method("tickets_count")
  32     orders = fields.Method("orders_count")
  33     sales = fields.Method("sales_count")
  34 
  35     def tickets_count(self, obj):
  36         obj_id = obj.id
  37         total = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(
  38             Order.event_id == obj_id).scalar()
  39         draft = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(
  40             Order.event_id == obj_id, Order.status == 'draft').scalar()
  41         cancelled = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(
  42             Order.event_id == obj_id, Order.status == 'cancelled').scalar()
  43         pending = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(
  44             Order.event_id == obj_id, Order.status == 'pending').scalar()
  45         expired = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(
  46             Order.event_id == obj_id, Order.status == 'expired').scalar()
  47         placed = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(
  48             Order.event_id == obj_id, Order.status == 'placed').scalar()
  49         result = {
  50             'total': total or 0,
  51             'draft': draft or 0,
  52             'cancelled': cancelled or 0,
  53             'pending': pending or 0,
  54             'expired': expired or 0,
  55             'placed': placed or 0
  56         }
  57         return result
  58 
  59     def orders_count(self, obj):
  60         obj_id = obj.id
  61         total = get_count(db.session.query(Order).filter(Order.event_id == obj_id))
  62         draft = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'draft'))
  63         cancelled = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'cancelled'))
  64         pending = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'pending'))
  65         expired = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'expired'))
  66         placed = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'placed'))
  67         result = {
  68             'total': total or 0,
  69             'draft': draft or 0,
  70             'cancelled': cancelled or 0,
  71             'pending': pending or 0,
  72             'expired': expired or 0,
  73             'placed': placed or 0
  74         }
  75         return result
  76 
  77     def sales_count(self, obj):
  78         obj_id = obj.id
  79         total = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id).scalar()
  80         draft = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,
  81                                                                              Order.status == 'draft').scalar()
  82         cancelled = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,
  83                                                                                  Order.status == 'cancelled').scalar()
  84         pending = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,
  85                                                                                Order.status == 'pending').scalar()
  86         expired = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,
  87                                                                                Order.status == 'expired').scalar()
  88         placed = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,
  89                                                                               Order.status == 'placed').scalar()
  90         result = {
  91             'total': total or 0,
  92             'draft': draft or 0,
  93             'cancelled': cancelled or 0,
  94             'pending': pending or 0,
  95             'expired': expired or 0,
  96             'placed': placed or 0
  97         }
  98         return result
  99 
  100 
  101 class OrderStatisticsEventDetail(ResourceDetail):
  102     """
  103     Event statistics detail by id
  104     """
  105 
  106     def before_get_object(self, view_kwargs):
  107         if view_kwargs.get('identifier'):
  108             event = safe_query(self, Event, 'identifier', view_kwargs['identifier'], 'identifier')
  109             view_kwargs['id'] = event.id
  110 
  111     methods = ['GET']
  112     decorators = (api.has_permission('is_coorganizer', fetch="id", fetch_as="event_id", model=Event),)
  113     schema = OrderStatisticsEventSchema
  114     data_layer = {'session': db.session,
  115                   'model': Event,
  116                   'methods': {
  117                       'before_get_object': before_get_object
  118                   }}
  119
  [end of app/api/order_statistics/events.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/app/api/order_statistics/events.py b/app/api/order_statistics/events.py
  --- a/app/api/order_statistics/events.py
  +++ b/app/api/order_statistics/events.py
  @@ -46,13 +46,16 @@
               Order.event_id == obj_id, Order.status == 'expired').scalar()
           placed = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(
               Order.event_id == obj_id, Order.status == 'placed').scalar()
  +        completed = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(
  +            Order.event_id == obj_id, Order.status == 'completed').scalar()
           result = {
               'total': total or 0,
               'draft': draft or 0,
               'cancelled': cancelled or 0,
               'pending': pending or 0,
               'expired': expired or 0,
  -            'placed': placed or 0
  +            'placed': placed or 0,
  +            'completed': completed or 0
           }
           return result
   
  @@ -64,13 +67,15 @@
           pending = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'pending'))
           expired = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'expired'))
           placed = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'placed'))
  +        completed = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'completed'))
           result = {
               'total': total or 0,
               'draft': draft or 0,
               'cancelled': cancelled or 0,
               'pending': pending or 0,
               'expired': expired or 0,
  -            'placed': placed or 0
  +            'placed': placed or 0,
  +            'completed': completed or 0
           }
           return result
   
  @@ -87,13 +92,16 @@
                                                                                  Order.status == 'expired').scalar()
           placed = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,
                                                                                 Order.status == 'placed').scalar()
  +        completed = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,
  +                                                                                 Order.status == 'completed').scalar()
           result = {
               'total': total or 0,
               'draft': draft or 0,
               'cancelled': cancelled or 0,
               'pending': pending or 0,
               'expired': expired or 0,
  -            'placed': placed or 0
  +            'placed': placed or 0,
  +            'completed': completed or 0
           }
           return result
  
verification_info: {"golden_diff": "diff --git a/app/api/order_statistics/events.py b/app/api/order_statistics/events.py\n--- a/app/api/order_statistics/events.py\n+++ b/app/api/order_statistics/events.py\n@@ -46,13 +46,16 @@\n             Order.event_id == obj_id, Order.status == 'expired').scalar()\n         placed = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(\n             Order.event_id == obj_id, Order.status == 'placed').scalar()\n+        completed = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(\n+            Order.event_id == obj_id, Order.status == 'completed').scalar()\n         result = {\n             'total': total or 0,\n             'draft': draft or 0,\n             'cancelled': cancelled or 0,\n             'pending': pending or 0,\n             'expired': expired or 0,\n-            'placed': placed or 0\n+            'placed': placed or 0,\n+            'completed': completed or 0\n         }\n         return result\n \n@@ -64,13 +67,15 @@\n         pending = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'pending'))\n         expired = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'expired'))\n         placed = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'placed'))\n+        completed = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'completed'))\n         result = {\n             'total': total or 0,\n             'draft': draft or 0,\n             'cancelled': cancelled or 0,\n             'pending': pending or 0,\n             'expired': expired or 0,\n-            'placed': placed or 0\n+            'placed': placed or 0,\n+            'completed': completed or 0\n         }\n         return result\n \n@@ -87,13 +92,16 @@\n                                                                                Order.status == 'expired').scalar()\n         placed = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,\n                                                                               Order.status == 'placed').scalar()\n+        completed = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,\n+                                                                                 Order.status == 'completed').scalar()\n         result = {\n             'total': total or 0,\n             'draft': draft or 0,\n             'cancelled': cancelled or 0,\n             'pending': pending or 0,\n             'expired': expired or 0,\n-            'placed': placed or 0\n+            'placed': placed or 0,\n+            'completed': completed or 0\n         }\n         return result\n", "issue": "Add completed orders to order-statistics endpoint\n**Is your feature request related to a problem? Please describe.**\r\nCurrently order statistics endpoint does not return completed orders.\r\n\r\n**Describe the solution you'd like**\r\nIt should return completed orders and also fix sales accordingly. sales should return order values calculated from completed orders only.\r\n\r\n**Additional context**\r\nNeeded in FE.\r\n\n", "before_files": [{"content": "from flask_rest_jsonapi import ResourceDetail\nfrom marshmallow_jsonapi import fields\nfrom marshmallow_jsonapi.flask import Schema\nfrom sqlalchemy import func\n\nfrom app.api.bootstrap import api\nfrom app.api.helpers.db import get_count\nfrom app.api.helpers.db import safe_query\nfrom app.api.helpers.utilities import dasherize\nfrom app.models import db\nfrom app.models.event import Event\nfrom app.models.order import Order, OrderTicket\n\n\nclass OrderStatisticsEventSchema(Schema):\n    \"\"\"\n    Api schema for general statistics of event\n    \"\"\"\n\n    class Meta:\n        \"\"\"\n        Meta class\n        \"\"\"\n        type_ = 'order-statistics-event'\n        self_view = 'v1.order_statistics_event_detail'\n        self_view_kwargs = {'id': '<id>'}\n        inflect = dasherize\n\n    id = fields.Str()\n    identifier = fields.Str()\n    tickets = fields.Method(\"tickets_count\")\n    orders = fields.Method(\"orders_count\")\n    sales = fields.Method(\"sales_count\")\n\n    def tickets_count(self, obj):\n        obj_id = obj.id\n        total = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(\n            Order.event_id == obj_id).scalar()\n        draft = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(\n            Order.event_id == obj_id, Order.status == 'draft').scalar()\n        cancelled = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(\n            Order.event_id == obj_id, Order.status == 'cancelled').scalar()\n        pending = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(\n            Order.event_id == obj_id, Order.status == 'pending').scalar()\n        expired = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(\n            Order.event_id == obj_id, Order.status == 'expired').scalar()\n        placed = db.session.query(func.sum(OrderTicket.quantity.label('sum'))).join(Order.order_tickets).filter(\n            Order.event_id == obj_id, Order.status == 'placed').scalar()\n        result = {\n            'total': total or 0,\n            'draft': draft or 0,\n            'cancelled': cancelled or 0,\n            'pending': pending or 0,\n            'expired': expired or 0,\n            'placed': placed or 0\n        }\n        return result\n\n    def orders_count(self, obj):\n        obj_id = obj.id\n        total = get_count(db.session.query(Order).filter(Order.event_id == obj_id))\n        draft = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'draft'))\n        cancelled = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'cancelled'))\n        pending = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'pending'))\n        expired = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'expired'))\n        placed = get_count(db.session.query(Order).filter(Order.event_id == obj_id, Order.status == 'placed'))\n        result = {\n            'total': total or 0,\n            'draft': draft or 0,\n            'cancelled': cancelled or 0,\n            'pending': pending or 0,\n            'expired': expired or 0,\n            'placed': placed or 0\n        }\n        return result\n\n    def sales_count(self, obj):\n        obj_id = obj.id\n        total = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id).scalar()\n        draft = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,\n                                                                             Order.status == 'draft').scalar()\n        cancelled = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,\n                                                                                 Order.status == 'cancelled').scalar()\n        pending = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,\n                                                                               Order.status == 'pending').scalar()\n        expired = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,\n                                                                               Order.status == 'expired').scalar()\n        placed = db.session.query(func.sum(Order.amount.label('sum'))).filter(Order.event_id == obj_id,\n                                                                              Order.status == 'placed').scalar()\n        result = {\n            'total': total or 0,\n            'draft': draft or 0,\n            'cancelled': cancelled or 0,\n            'pending': pending or 0,\n            'expired': expired or 0,\n            'placed': placed or 0\n        }\n        return result\n\n\nclass OrderStatisticsEventDetail(ResourceDetail):\n    \"\"\"\n    Event statistics detail by id\n    \"\"\"\n\n    def before_get_object(self, view_kwargs):\n        if view_kwargs.get('identifier'):\n            event = safe_query(self, Event, 'identifier', view_kwargs['identifier'], 'identifier')\n            view_kwargs['id'] = event.id\n\n    methods = ['GET']\n    decorators = (api.has_permission('is_coorganizer', fetch=\"id\", fetch_as=\"event_id\", model=Event),)\n    schema = OrderStatisticsEventSchema\n    data_layer = {'session': db.session,\n                  'model': Event,\n                  'methods': {\n                      'before_get_object': before_get_object\n                  }}\n", "path": "app/api/order_statistics/events.py"}]}