moved active/offline pipeline logic out; removed show_offline_pipelines CLI switch, instead show active/offline in one list indicating by symbol and text what the state is

This commit is contained in:
gac-maloja
2022-10-19 19:33:04 +02:00
parent cdbe675818
commit e0ae4f06ad
2 changed files with 133 additions and 33 deletions

96
pipelines.py Normal file
View File

@ -0,0 +1,96 @@
try:
from cam_server_client import PipelineClient
except ImportError:
from fake import PipelineClient
class Pipelines:
def __init__(self, addr="http://sf-daqsync-01:8889"):
self.pc = PipelineClient(addr)
self.refresh()
def refresh(self):
pc = self.pc
all_pls = pc.get_pipelines()
si = pc.get_server_info()
active_pls = si["active_instances"]
offline_pls = set(all_pls) - set(active_pls)
# spec_pls = (i for i in pls if "spec_db" in i)
self.all = sorted(all_pls)
self.active = sorted(active_pls)
self.offline = sorted(offline_pls)
def __iter__(self):
return (Pipeline(n, self) for n in self.all)
def __getitem__(self, name):
if name not in self.all:
raise RuntimeError(f"pipeline \"{name}\" does not exist")
return Pipeline(name, self)
class Pipeline:
def __init__(self, name, pls):
self.name = name
self.pls = pls
def __repr__(self):
return f"\"{self.name}\" is \"{self.state}\""
@property
def state(self):
pls = self.pls
name = self.name
pls.refresh()
if name not in pls.all:
raise RuntimeError(f"pipeline \"{name}\" does not exist")
if name in pls.active:
return "active"
if name in pls.offline:
return "offline"
raise RuntimeError(f"existing pipeline \"{name}\" is neither active nor offline")
def get(self):
pc = self.pls.pc
name = self.name
state = self.state
if state == "active":
return pc.get_instance_config(name)
if state == "offline":
return pc.get_config(name)
raise ValueError(f"state \"{state}\" for pipeline \"{name}\" not understood")
def set(self, cfg):
pc = self.pls.pc
name = self.name
state = self.state
if state == "active":
return pc.set_instance_config(name, cfg)
if state == "offline":
current = pc.get_config(name)
current.update(cfg)
return pc.set_config(name, current)
raise ValueError(f"state \"{state}\" for pipeline \"{name}\" not understood")