99from aboutcode .pipeline import LoopProgress
1010from django .db import transaction
1111
12- from insights .graphs import GRAPHS
12+ from insights .charts import CHARTS
1313from insights .models import DailySnapshot
1414from insights .models import ImporterInsight
1515from insights .models import PackageCWEInsight
@@ -27,12 +27,12 @@ class InsightsSnapshotPipeline(VulnerableCodePipeline):
2727 @classmethod
2828 def steps (cls ):
2929 return (
30- cls .compute_graph_analytics ,
30+ cls .compute_chart_analytics ,
3131 cls .save_snapshot ,
3232 )
3333
34- def compute_graph_analytics (self ):
35- """Run graph collect_fns to compute analytics."""
34+ def compute_chart_analytics (self ):
35+ """Run chart collect_fns to compute analytics."""
3636
3737 # List all package types
3838 self .packages = list (PackageV2 .objects .order_by ().values_list ("type" , flat = True ).distinct ())
@@ -42,49 +42,44 @@ def compute_graph_analytics(self):
4242 self .package_cwes = []
4343 self .importer_insights = []
4444 self .severity_insight = None
45-
46- active_graphs = {
47- graph_id : graph_def for graph_id , graph_def in GRAPHS .items () if graph_def .collect_fn
48- }
45+ active_charts = [chart_def for chart_def in CHARTS if chart_def .collect_fn ]
4946
5047 # Count steps for progress bar
5148 total_steps = sum (
52- len (self .packages ) if graph_def .is_per_package else 1
53- for graph_def in active_graphs .values ()
49+ len (self .packages ) if chart_def .is_per_package else 1 for chart_def in active_charts
5450 )
5551
5652 progress = LoopProgress (total_iterations = total_steps , logger = self .log , progress_step = 1 )
5753 progress_iter = iter (progress .iter (range (total_steps )))
5854
59- for graph_id , graph_def in active_graphs . items () :
60- self .log (f"Running collect_fn for { graph_id } " )
55+ for chart_def in active_charts :
56+ self .log (f"Running collect_fn for { chart_def . id } " )
6157
62- if graph_def .is_per_package :
58+ if chart_def .is_per_package :
6359 for pkg in self .packages :
6460 next (progress_iter , None )
65- graph_def .collect_fn (self , pkg )
61+ chart_def .collect_fn (self , pkg )
6662 else :
6763 next (progress_iter , None )
68- graph_def .collect_fn (self )
64+ chart_def .collect_fn (self )
6965
7066 @transaction .atomic
7167 def save_snapshot (self ):
7268 self .log ("Saving snapshot" )
7369 snapshot = DailySnapshot .objects .create ()
7470
7571 for insight in self .package_insights .values ():
76- insight .snapshot = snapshot
72+ insight .snapshot_id = snapshot . id
7773
7874 for insight in self .importer_insights :
79- insight .snapshot = snapshot
75+ insight .snapshot_id = snapshot . id
8076
8177 if self .severity_insight :
82- self .severity_insight .snapshot = snapshot
78+ self .severity_insight .snapshot_id = snapshot . id
8379 self .severity_insight .save ()
8480
8581 PackageInsight .objects .bulk_create (self .package_insights .values (), batch_size = 5000 )
8682 PackageNameInsight .objects .bulk_create (self .package_names , batch_size = 5000 )
8783 PackageCWEInsight .objects .bulk_create (self .package_cwes , batch_size = 5000 )
8884 ImporterInsight .objects .bulk_create (self .importer_insights , batch_size = 5000 )
89-
9085 self .log ("Snapshot saved successfully." )
0 commit comments