@@ -194,18 +194,39 @@ class JSONResultsGenerator:
194194 issues.
195195 """
196196
197- def __init__ (self , project ):
197+ def __init__ (self , project , sections = None ):
198+ """
199+ `sections` is an optional iterable restricting which of the
200+ packages/dependencies/files/relations arrays are included.
201+ Defaults to including all of them.
202+ """
198203 self .project = project
204+ self .sections = sections
199205
200206 def __iter__ (self ):
201207 yield "{\n "
202- yield from self .serialize (label = "headers" , generator = self .get_headers )
203- yield from self .serialize (label = "packages" , generator = self .get_packages )
204- yield from self .serialize (label = "dependencies" , generator = self .get_dependencies )
205- yield from self .serialize (label = "files" , generator = self .get_files )
208+
209+ sections = [
210+ ("packages" , self .get_packages ),
211+ ("dependencies" , self .get_dependencies ),
212+ ("files" , self .get_files ),
213+ ("relations" , self .get_relations ),
214+ ]
215+ if self .sections is not None :
216+ sections = [
217+ (label , generator )
218+ for label , generator in sections
219+ if label in self .sections
220+ ]
221+
206222 yield from self .serialize (
207- label = "relations " , generator = self .get_relations , latest = True
223+ label = "headers " , generator = self .get_headers , latest = not sections
208224 )
225+ for index , (label , generator ) in enumerate (sections ):
226+ yield from self .serialize (
227+ label = label , generator = generator , latest = index == len (sections ) - 1
228+ )
229+
209230 yield "}"
210231
211232 def serialize (self , label , generator , latest = False ):
@@ -257,7 +278,9 @@ def get_headers(self, project):
257278
258279 def encode_queryset (self , project , model_name , serializer ):
259280 queryset = get_queryset (project , model_name )
260- for obj in queryset .iterator (chunk_size = 2000 ):
281+ # A larger chunk_size reduces how often prefetch_related() re-runs its
282+ # queries, since iterator() re-executes prefetching once per chunk.
283+ for obj in queryset .iterator (chunk_size = 10000 ):
261284 yield self .encode (serializer (obj ).data )
262285
263286 def get_packages (self , project ):
@@ -289,13 +312,15 @@ def get_relations(self, project):
289312 )
290313
291314
292- def to_json (project ):
315+ def to_json (project , sections = None ):
293316 """
294317 Generate output for the provided `project` in JSON format.
295318 The output file is created in the `project` output/ directory.
296319 Return the path of the generated output file.
320+ `sections` is an optional iterable restricting which of the
321+ packages/dependencies/files/relations arrays are included.
297322 """
298- results_generator = JSONResultsGenerator (project )
323+ results_generator = JSONResultsGenerator (project , sections = sections )
299324 output_file = project .get_output_file_path ("results" , "json" )
300325
301326 with output_file .open ("w" ) as file :
0 commit comments