Repository: dmwm/wmcore
Issue: ReqMgr2 web UI responding very slowly (fetch API)

**Impact of the bug**
ReqMgr2 (web UI, REST APIs are not affected)

**Describe the bug**
While browsing through a few workflows in the ReqMgr2 UI, I see some of those query strings are taking many minutes to respond, e.g.:
https://cmsweb.cern.ch/reqmgr2/fetch?rid=pdmvserv_task_EXO-RunIISummer16NanoAODv6-08201__v1_T_200325_092206_950
which took 4.5min to get served.

while this:
https://cmsweb.cern.ch/reqmgr2/data/request?name=pdmvserv_task_EXO-RunIISummer16NanoAODv6-08201__v1_T_200325_092206_950

responds in less than a second.
Thus, there is some extra information coming from the `fetch` API that is slowing the whole thing.

**How to reproduce it**
Just open the fetch link above

**Expected behavior**
First we need to identify what is causing the slowness in this response (I've seen cases where cmssdt affects it, for instance). Then we need to evaluate whether we need such information.
Finally, this API should respond very quickly (<1sec), given that it's specific to one single workflow.

**Additional context and error message**
none


Changed files:
src/python/WMCore/ReqMgr/Web/ReqMgrService.py

diff --git a/src/python/WMCore/ReqMgr/Web/ReqMgrService.py b/src/python/WMCore/ReqMgr/Web/ReqMgrService.py
index 554e0e0db..4f617156d 100644
--- a/src/python/WMCore/ReqMgr/Web/ReqMgrService.py
+++ b/src/python/WMCore/ReqMgr/Web/ReqMgrService.py
@@ -45,6 +45,7 @@ from WMCore.Services.LogDB.LogDB import LogDB
 from WMCore.Services.ReqMgr.ReqMgr import ReqMgr
 from WMCore.Services.pycurl_manager import RequestHandler
 from WMCore.WMSpec.StdSpecs.StdBase import StdBase
+from WMCore.Cache.GenericDataCache import MemoryCacheStruct
 
 
 def getdata(url, params, headers=None):
@@ -378,15 +379,18 @@ class ReqMgrService(TemplatedPage):
         self.wmstatsurl = cdict.get('wmstats_url', '%s/wmstatsserver' % base_url)
         if not self.wmstatsurl:
             raise Exception('ReqMgr2 configuration file does not provide wmstats url')
-        self.team_cache = []
+        # cache team information for 2 hours to limit wmstatsserver API calls
+        self.TEAM_CACHE = MemoryCacheStruct(7200, self.refreshTeams)
 
         # fetch assignment arguments specification from StdBase
         self.assignArgs = StdBase().getWorkloadAssignArgs()
         self.assignArgs = {key: val['default'] for key, val in self.assignArgs.items()}
 
     def getTeams(self):
-        "Helper function to get teams from wmstats or local cache"
-        teams = self.team_cache
+        return self.TEAM_CACHE.getData()
+
+    def refreshTeams(self):
+        "Helper function to cache team info from wmstats"
         url = '%s/data/teams' % self.wmstatsurl
         params = {}
         headers = {'Accept': 'application/json'}
@@ -396,11 +400,10 @@ class ReqMgrService(TemplatedPage):
                 print("WARNING: fail to get teams from %s" % url)
                 print(data)
             teams = data.get('result', [])
-            self.team_cache = teams
+            return teams
         except Exception as exp:
             print("WARNING: fail to get teams from %s" % url)
             print(str(exp))
-        return teams
 
     def update_scripts(self, force=False):
         "Update scripts dict"