@@ -198,33 +198,39 @@ def get_context_data(self, **kwargs):
198198 )
199199
200200 # Fetch usage data from XDMoD
201+ def _has_data_same_as_before (data ):
202+ return data is not None
203+
204+ def build_usage_plots (slurm_account_name : str ):
205+ # metric_name, has_key, plot_key
206+ METRICS = [
207+ ("avg_cpu_hours" , "has_cpu_usage" , "cpu_usage_plots" ),
208+ ("avg_gpu_hours" , "has_gpu_usage" , "gpu_usage_plots" ),
209+ ("avg_waitduration_hours" , "has_wait_usage" , "wait_usage_plots" ),
210+ ]
211+
212+ ctx_updates = {}
213+ first_plot = True
214+
215+ for metric_name , has_key , plot_key in METRICS :
216+ data = get_usage_data (metric_name , slurm_account_name )
217+ has_data = _has_data_same_as_before (data )
218+ ctx_updates [has_key ] = has_data
219+
220+ if has_data :
221+ fig = px .bar (data )
222+ div = plot (
223+ fig ,
224+ output_type = "div" ,
225+ include_plotlyjs = "cdn" if first_plot else False # include JS once
226+ )
227+ first_plot = False
228+ ctx_updates [plot_key ] = div
229+
230+ return ctx_updates
231+
201232 slurm_account_name = allocation_obj .get_attribute ("slurm_account_name" )
202- usage_cpu_hours = get_usage_data ("avg_cpu_hours" , slurm_account_name )
203- if usage_cpu_hours is not None :
204- plot_cpu_hours = px .bar (usage_cpu_hours )
205- plot_div_cpu_hours = plot (plot_cpu_hours , output_type = "div" )
206- context ["has_cpu_usage" ] = True
207- context ["cpu_usage_plots" ] = plot_div_cpu_hours
208- else :
209- context ["has_cpu_usage" ] = False
210-
211- usage_gpu_hours = get_usage_data ("avg_gpu_hours" , slurm_account_name )
212- if usage_gpu_hours is not None :
213- plot_gpu_hours = px .bar (usage_gpu_hours )
214- plot_div_gpu_hours = plot (plot_gpu_hours , output_type = "div" )
215- context ["has_gpu_usage" ] = True
216- context ["gpu_usage_plots" ] = plot_div_gpu_hours
217- else :
218- context ["has_gpu_usage" ] = False
219-
220- usage_wait_hours = get_usage_data ("avg_waitduration_hours" , slurm_account_name )
221- if usage_wait_hours is not None :
222- plot_wait_hours = px .bar (usage_wait_hours )
223- plot_div_wait_hours = plot (plot_wait_hours , output_type = "div" )
224- context ["has_wait_usage" ] = True
225- context ["wait_usage_plots" ] = plot_div_wait_hours
226- else :
227- context ["has_wait_usage" ] = False
233+ context .update (build_usage_plots (slurm_account_name ))
228234
229235 return context
230236
0 commit comments