diff --git a/src/browsergym/workarena/tasks/dashboard.py b/src/browsergym/workarena/tasks/dashboard.py index c15f163..be6a271 100644 --- a/src/browsergym/workarena/tasks/dashboard.py +++ b/src/browsergym/workarena/tasks/dashboard.py @@ -29,14 +29,18 @@ # - We currently don't support maps because they are clickable and would require a more evolved cheat function SUPPORTED_PLOT_TYPES = ["area", "bar", "column", "line", "pie", "spline"] -# Get report filter config -config = SNowInstance().report_filter_config -if config is None: - REPORT_DATE_FILTER = REPORT_TIME_FILTER = None -else: - REPORT_DATE_FILTER = config["report_date_filter"] - REPORT_TIME_FILTER = config["report_time_filter"] -del config +# Fetching the instance config is expensive. +# Cache it here on first use. +_CACHED_INSTANCE_CONFIG: dict | None = None + + +def _get_config() -> dict | None: + global _CACHED_INSTANCE_CONFIG + if _CACHED_INSTANCE_CONFIG is None: + _CACHED_INSTANCE_CONFIG = SNowInstance().report_filter_config + if _CACHED_INSTANCE_CONFIG is None: + return None + return _CACHED_INSTANCE_CONFIG class DashboardRetrievalTask(AbstractServiceNowTask, ABC): @@ -53,6 +57,13 @@ def __init__( self.fixed_config = fixed_config self.__dict__.update(kwargs) + self.report_date_filter = None + self.report_time_filter = None + config = _get_config() + if config is not None: + self.report_date_filter = config["report_date_filter"] + self.report_time_filter = config["report_time_filter"] + @abstractmethod def all_configs(self) -> List[dict]: pass @@ -307,7 +318,7 @@ def setup_goal(self, page: playwright.sync_api.Page) -> Tuple[str | dict]: super().setup_goal(page=page) # Check that the report filters are properly setup - if REPORT_DATE_FILTER is None or REPORT_TIME_FILTER is None: + if self.report_date_filter is None or self.report_time_filter is None: raise RuntimeError( "The report date and time filters are not set. Please run the install script to set them." ) @@ -320,8 +331,8 @@ def setup_goal(self, page: playwright.sync_api.Page) -> Tuple[str | dict]: # ... set start URL based on config # ...... some of the reports have need a date filter to be applied so we do this by patching a placeholder in the URL self.start_url = self.instance.snow_url + self.config["url"].replace( - "REPORT_DATE_FILTER", REPORT_DATE_FILTER - ).replace("REPORT_TIME_FILTER", REPORT_TIME_FILTER) + "REPORT_DATE_FILTER", self.report_date_filter + ).replace("REPORT_TIME_FILTER", self.report_time_filter) # Produce goal string based on question type chart_locator = ( @@ -633,7 +644,7 @@ def _generate_random_config( """ # Check that the report filters are properly setup - if REPORT_DATE_FILTER is None or REPORT_TIME_FILTER is None: + if self.report_date_filter is None or self.report_time_filter is None: raise RuntimeError( "The report date and time filters are not set. Please run the install script to set them." ) @@ -700,7 +711,7 @@ def _generate_random_config( # On the fly generated report if not report.get("sys_id", None): # ... these receive a filter that is added through the URL - url = f"/now/nav/ui/classic/params/target/sys_report_template.do%3Fsysparm_field%3D{report['field']}%26sysparm_type%3D{report['type']}%26sysparm_table%3D{report['table']}%26sysparm_from_list%3Dtrue%26sysparm_chart_size%3Dlarge%26sysparm_manual_labor%3Dtrue%26sysparm_query=sys_created_on