prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  repair paydays table
  This isn't really used much yet but it should be (nice historical data). Two repairs needed:
  - ~~recalculate nparticipants for paydays <= #3~~
  - recalculate exchange_volume and exchange_fees_volume for paydays <= #8 (see #202)
  
  
  </issue>
  <code>
  [start of gittip/models/__init__.py]
  1 """
  2 
  3 The most important object in the Gittip object model is Participant, and the
  4 second most important one is Ccommunity. There are a few others, but those are
  5 the most important two. Participant, in particular, is at the center of
  6 everything on Gittip.
  7 
  8 """
  9 from postgres import Postgres
  10 
  11 class GittipDB(Postgres):
  12 
  13     def self_check(self):
  14         """
  15         Runs all available self checks on the database.
  16         """
  17         self._check_balances()
  18         self._check_tips()
  19         self._check_orphans()
  20         self._check_orphans_no_tips()
  21 
  22     def _check_tips(self):
  23         """
  24         Checks that there are no rows in tips with duplicate (tipper, tippee, mtime).
  25 
  26         https://github.com/gittip/www.gittip.com/issues/1704
  27         """
  28         conflicting_tips = self.one("""
  29             SELECT count(*)
  30               FROM
  31                  (
  32                     SELECT * FROM tips
  33                     EXCEPT
  34                     SELECT DISTINCT ON(tipper, tippee, mtime) *
  35                       FROM tips
  36                   ORDER BY tipper, tippee, mtime
  37                   ) AS foo
  38         """)
  39         assert conflicting_tips == 0
  40 
  41     def _check_balances(self):
  42         """
  43         Recalculates balances for all participants from transfers and exchanges.
  44 
  45         https://github.com/gittip/www.gittip.com/issues/1118
  46         """
  47         with self.get_cursor() as cursor:
  48             if cursor.one("select exists (select * from paydays where ts_end < ts_start) as running"):
  49                 # payday is running and the query bellow does not account for pending
  50                 return
  51             b = cursor.one("""
  52                 select count(*)
  53                   from (
  54                     select username, sum(a) as balance
  55                       from (
  56                               select participant as username, sum(amount) as a
  57                                 from exchanges
  58                                where amount > 0
  59                             group by participant
  60 
  61                                union
  62 
  63                               select participant as username, sum(amount-fee) as a
  64                                 from exchanges
  65                                where amount < 0
  66                             group by participant
  67 
  68                                union
  69 
  70                               select tipper as username, sum(-amount) as a
  71                                 from transfers
  72                             group by tipper
  73 
  74                                union
  75 
  76                               select tippee as username, sum(amount) as a
  77                                 from transfers
  78                             group by tippee
  79                             ) as foo
  80                     group by username
  81 
  82                     except
  83 
  84                     select username, balance
  85                     from participants
  86                   ) as foo2
  87             """)
  88         assert b == 0, "conflicting balances: {}".format(b)
  89 
  90     def _check_orphans(self):
  91         """
  92         Finds participants that
  93             * does not have corresponding elsewhere account
  94             * have not been absorbed by other participant
  95 
  96         These are broken because new participants arise from elsewhere
  97         and elsewhere is detached only by take over which makes a note
  98         in absorptions if it removes the last elsewhere account.
  99 
  100         Especially bad case is when also claimed_time is set because
  101         there must have been elsewhere account attached and used to sign in.
  102 
  103         https://github.com/gittip/www.gittip.com/issues/617
  104         """
  105         orphans = self.all("""
  106             select username
  107                from participants
  108               where not exists (select * from elsewhere where elsewhere.participant=username)
  109                 and not exists (select * from absorptions where archived_as=username)
  110         """)
  111         assert len(orphans) == 0, "missing elsewheres: {}".format(list(orphans))
  112 
  113     def _check_orphans_no_tips(self):
  114         """
  115         Finds participants
  116             * without elsewhere account attached
  117             * having non zero outstanding tip
  118 
  119         This should not happen because when we remove the last elsewhere account
  120         in take_over we also zero out all tips.
  121         """
  122         tips_with_orphans = self.all("""
  123             WITH orphans AS (
  124                 SELECT username FROM participants
  125                 WHERE NOT EXISTS (SELECT 1 FROM elsewhere WHERE participant=username)
  126             ), valid_tips AS (
  127                   SELECT * FROM (
  128                             SELECT DISTINCT ON (tipper, tippee) *
  129                               FROM tips
  130                           ORDER BY tipper, tippee, mtime DESC
  131                       ) AS foo
  132                   WHERE amount > 0
  133             )
  134             SELECT id FROM valid_tips
  135             WHERE tipper IN (SELECT * FROM orphans)
  136             OR tippee IN (SELECT * FROM orphans)
  137         """)
  138         known = set([25206]) # '4c074000c7bc', 'naderman', '3.00'
  139         real = set(tips_with_orphans) - known
  140         assert len(real) == 0, real
  141 #
  142
  [end of gittip/models/__init__.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/gittip/models/__init__.py b/gittip/models/__init__.py
  --- a/gittip/models/__init__.py
  +++ b/gittip/models/__init__.py
  @@ -18,6 +18,7 @@
           self._check_tips()
           self._check_orphans()
           self._check_orphans_no_tips()
  +        self._check_paydays_volumes()
   
       def _check_tips(self):
           """
  @@ -138,4 +139,72 @@
           known = set([25206]) # '4c074000c7bc', 'naderman', '3.00'
           real = set(tips_with_orphans) - known
           assert len(real) == 0, real
  +
  +    def _check_paydays_volumes(self):
  +        """
  +        Recalculate *_volume fields in paydays table using exchanges table.
  +        """
  +        charge_volume = self.all("""
  +            select * from (
  +                select id, ts_start, charge_volume, (
  +                        select sum(amount+fee)
  +                        from exchanges
  +                        where timestamp > ts_start
  +                        and timestamp < ts_end
  +                        and amount > 0
  +                    ) as ref
  +                from paydays
  +                order by id
  +            ) as foo
  +            where charge_volume != ref
  +        """)
  +        assert len(charge_volume) == 0
  +
  +        charge_fees_volume = self.all("""
  +            select * from (
  +                select id, ts_start, charge_fees_volume, (
  +                        select sum(fee)
  +                        from exchanges
  +                        where timestamp > ts_start
  +                        and timestamp < ts_end
  +                        and amount > 0
  +                    ) as ref
  +                from paydays
  +                order by id
  +            ) as foo
  +            where charge_fees_volume != ref
  +        """)
  +        assert len(charge_fees_volume) == 0
  +
  +        ach_volume = self.all("""
  +            select * from (
  +                select id, ts_start, ach_volume, (
  +                        select sum(amount)
  +                        from exchanges
  +                        where timestamp > ts_start
  +                        and timestamp < ts_end
  +                        and amount < 0
  +                    ) as ref
  +                from paydays
  +                order by id
  +            ) as foo
  +            where ach_volume != ref
  +        """)
  +        assert len(ach_volume) == 0
  +
  +        ach_fees_volume = self.all("""
  +            select * from (
  +                select id, ts_start, ach_fees_volume, (
  +                        select sum(fee)
  +                        from exchanges
  +                        where timestamp > ts_start
  +                        and timestamp < ts_end
  +                        and amount < 0
  +                    ) as ref
  +                from paydays
  +                order by id
  +            ) as foo
  +            where ach_fees_volume != ref
  +        """)
  +        assert len(ach_fees_volume) == 0
   #
  
verification_info: {"golden_diff": "diff --git a/gittip/models/__init__.py b/gittip/models/__init__.py\n--- a/gittip/models/__init__.py\n+++ b/gittip/models/__init__.py\n@@ -18,6 +18,7 @@\n         self._check_tips()\n         self._check_orphans()\n         self._check_orphans_no_tips()\n+        self._check_paydays_volumes()\n \n     def _check_tips(self):\n         \"\"\"\n@@ -138,4 +139,72 @@\n         known = set([25206]) # '4c074000c7bc', 'naderman', '3.00'\n         real = set(tips_with_orphans) - known\n         assert len(real) == 0, real\n+\n+    def _check_paydays_volumes(self):\n+        \"\"\"\n+        Recalculate *_volume fields in paydays table using exchanges table.\n+        \"\"\"\n+        charge_volume = self.all(\"\"\"\n+            select * from (\n+                select id, ts_start, charge_volume, (\n+                        select sum(amount+fee)\n+                        from exchanges\n+                        where timestamp > ts_start\n+                        and timestamp < ts_end\n+                        and amount > 0\n+                    ) as ref\n+                from paydays\n+                order by id\n+            ) as foo\n+            where charge_volume != ref\n+        \"\"\")\n+        assert len(charge_volume) == 0\n+\n+        charge_fees_volume = self.all(\"\"\"\n+            select * from (\n+                select id, ts_start, charge_fees_volume, (\n+                        select sum(fee)\n+                        from exchanges\n+                        where timestamp > ts_start\n+                        and timestamp < ts_end\n+                        and amount > 0\n+                    ) as ref\n+                from paydays\n+                order by id\n+            ) as foo\n+            where charge_fees_volume != ref\n+        \"\"\")\n+        assert len(charge_fees_volume) == 0\n+\n+        ach_volume = self.all(\"\"\"\n+            select * from (\n+                select id, ts_start, ach_volume, (\n+                        select sum(amount)\n+                        from exchanges\n+                        where timestamp > ts_start\n+                        and timestamp < ts_end\n+                        and amount < 0\n+                    ) as ref\n+                from paydays\n+                order by id\n+            ) as foo\n+            where ach_volume != ref\n+        \"\"\")\n+        assert len(ach_volume) == 0\n+\n+        ach_fees_volume = self.all(\"\"\"\n+            select * from (\n+                select id, ts_start, ach_fees_volume, (\n+                        select sum(fee)\n+                        from exchanges\n+                        where timestamp > ts_start\n+                        and timestamp < ts_end\n+                        and amount < 0\n+                    ) as ref\n+                from paydays\n+                order by id\n+            ) as foo\n+            where ach_fees_volume != ref\n+        \"\"\")\n+        assert len(ach_fees_volume) == 0\n #\n", "issue": "repair paydays table\nThis isn't really used much yet but it should be (nice historical data). Two repairs needed:\n- ~~recalculate nparticipants for paydays <= #3~~\n- recalculate exchange_volume and exchange_fees_volume for paydays <= #8 (see #202)\n\n", "before_files": [{"content": "\"\"\"\n\nThe most important object in the Gittip object model is Participant, and the\nsecond most important one is Ccommunity. There are a few others, but those are\nthe most important two. Participant, in particular, is at the center of\neverything on Gittip.\n\n\"\"\"\nfrom postgres import Postgres\n\nclass GittipDB(Postgres):\n\n    def self_check(self):\n        \"\"\"\n        Runs all available self checks on the database.\n        \"\"\"\n        self._check_balances()\n        self._check_tips()\n        self._check_orphans()\n        self._check_orphans_no_tips()\n\n    def _check_tips(self):\n        \"\"\"\n        Checks that there are no rows in tips with duplicate (tipper, tippee, mtime).\n\n        https://github.com/gittip/www.gittip.com/issues/1704\n        \"\"\"\n        conflicting_tips = self.one(\"\"\"\n            SELECT count(*)\n              FROM\n                 (\n                    SELECT * FROM tips\n                    EXCEPT\n                    SELECT DISTINCT ON(tipper, tippee, mtime) *\n                      FROM tips\n                  ORDER BY tipper, tippee, mtime\n                  ) AS foo\n        \"\"\")\n        assert conflicting_tips == 0\n\n    def _check_balances(self):\n        \"\"\"\n        Recalculates balances for all participants from transfers and exchanges.\n\n        https://github.com/gittip/www.gittip.com/issues/1118\n        \"\"\"\n        with self.get_cursor() as cursor:\n            if cursor.one(\"select exists (select * from paydays where ts_end < ts_start) as running\"):\n                # payday is running and the query bellow does not account for pending\n                return\n            b = cursor.one(\"\"\"\n                select count(*)\n                  from (\n                    select username, sum(a) as balance\n                      from (\n                              select participant as username, sum(amount) as a\n                                from exchanges\n                               where amount > 0\n                            group by participant\n\n                               union\n\n                              select participant as username, sum(amount-fee) as a\n                                from exchanges\n                               where amount < 0\n                            group by participant\n\n                               union\n\n                              select tipper as username, sum(-amount) as a\n                                from transfers\n                            group by tipper\n\n                               union\n\n                              select tippee as username, sum(amount) as a\n                                from transfers\n                            group by tippee\n                            ) as foo\n                    group by username\n\n                    except\n\n                    select username, balance\n                    from participants\n                  ) as foo2\n            \"\"\")\n        assert b == 0, \"conflicting balances: {}\".format(b)\n\n    def _check_orphans(self):\n        \"\"\"\n        Finds participants that\n            * does not have corresponding elsewhere account\n            * have not been absorbed by other participant\n\n        These are broken because new participants arise from elsewhere\n        and elsewhere is detached only by take over which makes a note\n        in absorptions if it removes the last elsewhere account.\n\n        Especially bad case is when also claimed_time is set because\n        there must have been elsewhere account attached and used to sign in.\n\n        https://github.com/gittip/www.gittip.com/issues/617\n        \"\"\"\n        orphans = self.all(\"\"\"\n            select username\n               from participants\n              where not exists (select * from elsewhere where elsewhere.participant=username)\n                and not exists (select * from absorptions where archived_as=username)\n        \"\"\")\n        assert len(orphans) == 0, \"missing elsewheres: {}\".format(list(orphans))\n\n    def _check_orphans_no_tips(self):\n        \"\"\"\n        Finds participants\n            * without elsewhere account attached\n            * having non zero outstanding tip\n\n        This should not happen because when we remove the last elsewhere account\n        in take_over we also zero out all tips.\n        \"\"\"\n        tips_with_orphans = self.all(\"\"\"\n            WITH orphans AS (\n                SELECT username FROM participants\n                WHERE NOT EXISTS (SELECT 1 FROM elsewhere WHERE participant=username)\n            ), valid_tips AS (\n                  SELECT * FROM (\n                            SELECT DISTINCT ON (tipper, tippee) *\n                              FROM tips\n                          ORDER BY tipper, tippee, mtime DESC\n                      ) AS foo\n                  WHERE amount > 0\n            )\n            SELECT id FROM valid_tips\n            WHERE tipper IN (SELECT * FROM orphans)\n            OR tippee IN (SELECT * FROM orphans)\n        \"\"\")\n        known = set([25206]) # '4c074000c7bc', 'naderman', '3.00'\n        real = set(tips_with_orphans) - known\n        assert len(real) == 0, real\n#\n", "path": "gittip/models/__init__.py"}]}