From 414808ed322cf4ab0c811476b4a2aad1deaf5d3b Mon Sep 17 00:00:00 2001 From: Mees Fix Date: Tue, 20 Aug 2024 12:11:08 -0400 Subject: [PATCH 1/3] Update NRS TA monitors to not save plot and generate them live. --- .../ta_monitors/msata_monitor.py | 26 +++++++++---------- .../ta_monitors/wata_monitor.py | 23 +++++++++------- jwql/website/apps/jwql/monitor_views.py | 18 ++++++------- jwql/website/apps/jwql/static/js/jwql.js | 6 ++--- 4 files changed, 36 insertions(+), 37 deletions(-) diff --git a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/msata_monitor.py b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/msata_monitor.py index 3f0fade13..52484721a 100755 --- a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/msata_monitor.py +++ b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/msata_monitor.py @@ -1431,7 +1431,6 @@ def mk_plt_layout(self, plot_data): self.setup_date_range() # set the output html file name and create the plot grid - output_file(self.output_file_name) p1 = self.plt_status() p2 = self.plt_residual_offsets() p3 = self.plt_res_offsets_corrected() @@ -1452,11 +1451,9 @@ def mk_plt_layout(self, plot_data): merge_tools=False, ) box_layout = layout(children=[self.date_range, grid]) - save(box_layout) # return the needed components for embedding the results in the MSATA html template - script, div = components(box_layout) - return script, div + self.script, self.div = components(box_layout) def identify_tables(self): """Determine which database tables to use for a run of the TA monitor.""" @@ -1720,6 +1717,17 @@ def add_msata_data(self): logging.info("\tUpdated the MSATA statistics table") + def plots_for_app(self): + """Utility function to access div and script objects for + embedding bokeh in JWQL application. + """ + # Query results and convert into pandas df. + self.query_results = pd.DataFrame( + list(NIRSpecMsataStats.objects.all().values()) + ) + # Generate plot + self.mk_plt_layout(self.query_results) + @log_fail @log_info def run(self): @@ -1743,7 +1751,6 @@ def run(self): # Locate the record of most recent time the monitor was run self.query_start = self.most_recent_search() - self.output_file_name = os.path.join(self.output_dir, "msata_layout.html") # Use the current time as the end time for MAST query self.query_end = Time.now().mjd @@ -1827,17 +1834,10 @@ def run(self): # Generate plot self.mk_plt_layout(self.query_results) - logging.info( - "\tNew output plot file will be written as: {}".format( - self.output_file_name - ) - ) # Once data is added to database table and plots are made, the # monitor has run successfully. monitor_run = True - logging.info( - "\tOutput html plot file created: {}".format(self.output_file_name) - ) + msata_files_used4plots = len(self.msata_data["visit_id"]) logging.info( "\t{} MSATA files were used to make plots.".format( diff --git a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py index a2f3301e5..95f903aea 100755 --- a/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py +++ b/jwql/instrument_monitors/nirspec_monitors/ta_monitors/wata_monitor.py @@ -820,7 +820,6 @@ def mk_plt_layout(self, plot_data): self.setup_date_range() # set the output html file name and create the plot grid - output_file(self.output_file_name) p1 = self.plt_status() p2 = self.plt_residual_offsets() p3 = self.plt_v2offset_time() @@ -831,11 +830,9 @@ def mk_plt_layout(self, plot_data): # make grid grid = gridplot([p1, p2, p3, p4, p5, p6], ncols=2, merge_tools=False) box_layout = layout(children=[self.date_range, grid]) - save(box_layout) # return the needed components for embeding the results in the WATA html template - script, div = components(box_layout) - return script, div + self.script, self.div = components(box_layout) def file_exists_in_database(self, filename): """Checks if an entry for filename exists in the wata stats @@ -1052,6 +1049,17 @@ def add_wata_data(self): logging.info("\tUpdated the WATA statistics table") + def plots_for_app(self): + """Utility function to access div and script objects for + embedding bokeh in JWQL application. + """ + # Query results and convert into pandas df. + self.query_results = pd.DataFrame( + list(NIRSpecWataStats.objects.all().values()) + ) + # Generate plot + self.mk_plt_layout(self.query_results) + @log_fail @log_info def run(self): @@ -1075,7 +1083,6 @@ def run(self): # Locate the record of most recent time the monitor was run self.query_start = self.most_recent_search() - self.output_file_name = os.path.join(self.output_dir, "wata_layout.html") # Use the current time as the end time for MAST query self.query_end = Time.now().mjd @@ -1153,11 +1160,7 @@ def run(self): self.query_results = pd.DataFrame(list(NIRSpecWataStats.objects.all().values())) # Generate plot. self.mk_plt_layout(self.query_results) - logging.info( - "\tNew output plot file will be written as: {}".format( - self.output_file_name - ) - ) + # Once data is added to database table and plots are made, the # monitor has run successfully. monitor_run = True diff --git a/jwql/website/apps/jwql/monitor_views.py b/jwql/website/apps/jwql/monitor_views.py index f5408dd89..b0943c54a 100644 --- a/jwql/website/apps/jwql/monitor_views.py +++ b/jwql/website/apps/jwql/monitor_views.py @@ -342,13 +342,12 @@ def msata_monitoring_ajax(request): JsonResponse object Outgoing response sent to the webpage """ - # retrieve existing monitor html content + # Make plots and extract visualization components monitor = msata_monitor.MSATA() - div, script1, script2 = monitor.read_existing_html() + monitor.plots_for_app() - context = {'script1': script1, - 'script2': script2, - 'div': div} + context = {'script': monitor.script, + 'div': monitor.div} return JsonResponse(context, json_dumps_params={'indent': 2}) @@ -391,12 +390,11 @@ def wata_monitoring_ajax(request): JsonResponse object Outgoing response sent to the webpage """ - # retrieve existing monitor html content + # Make plots and extract visualization components monitor = wata_monitor.WATA() - div, script1, script2 = monitor.read_existing_html() + monitor.plots_for_app() - context = {'script1': script1, - 'script2': script2, - 'div': div} + context = {'script': monitor.script, + 'div': monitor.div} return JsonResponse(context, json_dumps_params={'indent': 2}) diff --git a/jwql/website/apps/jwql/static/js/jwql.js b/jwql/website/apps/jwql/static/js/jwql.js index 96896f4c3..895da4fea 100644 --- a/jwql/website/apps/jwql/static/js/jwql.js +++ b/jwql/website/apps/jwql/static/js/jwql.js @@ -1042,8 +1042,7 @@ function update_msata_page(base_url) { // Build div content var content = data["div"]; - content += data["script1"]; - content += data["script2"]; + content += data["script"]; /* Add the content to the div * Note: