1
- import asyncio
2
1
import copy
3
- import random
4
2
import re
5
- from typing import Dict , List , Optional , Tuple
3
+ from typing import Any , Dict , List
6
4
7
5
import backoff
8
6
import kinto_http
@@ -49,23 +47,15 @@ async def get_records(self, *args, **kwargs) -> List[Dict]:
49
47
return await self ._client .get_records (* args , ** kwargs )
50
48
51
49
@retry_timeout
52
- async def get_monitor_changes (self , bust_cache = False , ** kwargs ) -> List [Dict ]:
53
- if bust_cache :
54
- if "_expected" in kwargs :
55
- raise ValueError ("Pick one of `bust_cache` and `_expected` parameters" )
56
- random_cache_bust = random .randint (999999000000 , 999999999999 )
57
- kwargs ["_expected" ] = random_cache_bust
58
- return await self .get_records (bucket = "monitor" , collection = "changes" , ** kwargs )
50
+ async def get_monitor_changes (self , ** kwargs ) -> List [Dict ]:
51
+ resp = await self .get_changeset (
52
+ bucket = "monitor" , collection = "changes" , ** kwargs
53
+ )
54
+ return resp ["changes" ]
59
55
60
56
@retry_timeout
61
- async def get_changeset (self , bucket , collection , ** kwargs ) -> List [Dict ]:
62
- endpoint = f"/buckets/{ bucket } /collections/{ collection } /changeset"
63
- kwargs .setdefault ("_expected" , random .randint (999999000000 , 999999999999 ))
64
- loop = asyncio .get_event_loop ()
65
- body , _ = await loop .run_in_executor (
66
- None , lambda : self ._client .session .request ("get" , endpoint , params = kwargs )
67
- )
68
- return body
57
+ async def get_changeset (self , * args , ** kwargs ) -> Dict [str , Any ]:
58
+ return await self ._client .get_changeset (* args , ** kwargs )
69
59
70
60
@retry_timeout
71
61
async def get_record (self , * args , ** kwargs ) -> Dict :
@@ -110,9 +100,7 @@ async def fetch_signed_resources(server_url: str, auth: str) -> List[Dict[str, D
110
100
preview_buckets .add (resource ["preview" ]["bucket" ])
111
101
112
102
resources = []
113
- monitored = await client .get_records (
114
- bucket = "monitor" , collection = "changes" , _sort = "bucket,collection"
115
- )
103
+ monitored = await client .get_monitor_changes (_sort = "bucket,collection" )
116
104
for entry in monitored :
117
105
bid = entry ["bucket" ]
118
106
cid = entry ["collection" ]
@@ -138,60 +126,35 @@ async def fetch_signed_resources(server_url: str, auth: str) -> List[Dict[str, D
138
126
return resources
139
127
140
128
141
- def records_equal (a , b ):
142
- """Compare records, ignoring timestamps."""
143
- ignored_fields = ("last_modified" , "schema" )
144
- ra = {k : v for k , v in a .items () if k not in ignored_fields }
145
- rb = {k : v for k , v in b .items () if k not in ignored_fields }
146
- return ra == rb
147
-
148
-
149
- def compare_collections (
150
- a : List [Dict ], b : List [Dict ]
151
- ) -> Optional [Tuple [List [str ], List [str ], List [str ]]]:
152
- """Compare two lists of records. Returns empty list if equal."""
153
- b_by_id = {r ["id" ]: r for r in b }
154
- missing = []
155
- differ = []
156
- for ra in a :
157
- rb = b_by_id .pop (ra ["id" ], None )
158
- if rb is None :
159
- missing .append (ra ["id" ])
160
- elif not records_equal (ra , rb ):
161
- differ .append (ra ["id" ])
162
- extras = list (b_by_id .keys ())
163
-
164
- if missing or differ or extras :
165
- return (missing , differ , extras )
166
-
167
- return None
168
-
169
-
170
129
def human_diff (
171
130
left : str ,
172
131
right : str ,
173
- missing : List [str ],
174
- differ : List [str ],
175
- extras : List [str ],
132
+ missing : List [dict ],
133
+ differ : List [tuple [ dict , dict ] ],
134
+ extras : List [dict ],
176
135
show_ids : int = 5 ,
177
136
) -> str :
137
+ missing_ids = [r ["id" ] for r in missing ]
138
+ differ_ids = [r ["id" ] for _ , r in differ ]
139
+ extras_ids = [r ["id" ] for r in extras ]
140
+
178
141
def ellipse (line ):
179
142
return ", " .join (repr (r ) for r in line [:show_ids ]) + (
180
143
"..." if len (line ) > show_ids else ""
181
144
)
182
145
183
146
details = []
184
- if missing :
147
+ if missing_ids :
185
148
details .append (
186
- f"{ len (missing )} record{ 's' if len (missing ) > 1 else '' } present in { left } but missing in { right } ({ ellipse (missing )} )"
149
+ f"{ len (missing_ids )} record{ 's' if len (missing_ids ) > 1 else '' } present in { left } but missing in { right } ({ ellipse (missing_ids )} )"
187
150
)
188
- if differ :
151
+ if differ_ids :
189
152
details .append (
190
- f"{ len (differ )} record{ 's' if len (differ ) > 1 else '' } differ between { left } and { right } ({ ellipse (differ )} )"
153
+ f"{ len (differ_ids )} record{ 's' if len (differ_ids ) > 1 else '' } differ between { left } and { right } ({ ellipse (differ_ids )} )"
191
154
)
192
- if extras :
155
+ if extras_ids :
193
156
details .append (
194
- f"{ len (extras )} record{ 's' if len (extras ) > 1 else '' } present in { right } but missing in { left } ({ ellipse (extras )} )"
157
+ f"{ len (extras_ids )} record{ 's' if len (extras_ids ) > 1 else '' } present in { right } but missing in { left } ({ ellipse (extras_ids )} )"
195
158
)
196
159
return ", " .join (details )
197
160
0 commit comments