Back to home page

sPhenix code displayed by LXR

 
 

    


Warning, file /macros/calibrations/tpc/gemcurrents/Prometheus_print.py was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 #!/usr/bin/env python3
0002 
0003 import sys
0004 import requests
0005 import pyodbc
0006 from collections import defaultdict
0007 
0008 PROMETHEUS = "http://130.199.6.28:9090/api/v1"
0009 PADDING = 120
0010 STEP = 10
0011 
0012 
0013 def get_run_times(runnumber):
0014     conn = pyodbc.connect("DSN=daq", readonly=True)
0015     cursor = conn.cursor()
0016 
0017     #cursor.execute(
0018     #    """
0019     #    SELECT
0020     #        EXTRACT(EPOCH FROM brtimestamp),
0021     #        EXTRACT(EPOCH FROM ertimestamp)
0022     #    FROM run
0023     #    WHERE runnumber = ?
0024     #    """,
0025     #    runnumber,
0026     #)
0027     cursor.execute(
0028         """
0029         SELECT
0030             EXTRACT(EPOCH FROM brtimestamp AT TIME ZONE 'America/New_York'),
0031             EXTRACT(EPOCH FROM ertimestamp AT TIME ZONE 'America/New_York')
0032         FROM run
0033         WHERE runnumber = ?
0034         """,
0035         runnumber,
0036     )
0037     row = cursor.fetchone()
0038 
0039     cursor.close()
0040     conn.close()
0041 
0042     if row is None:
0043         raise RuntimeError(f"Run {runnumber} not found")
0044 
0045     return int(row[0]), int(row[1])
0046 
0047 
0048 def prometheus_query(metric, begin, end):
0049     response = requests.get(
0050         f"{PROMETHEUS}/query_range",
0051         params={
0052             "query": metric,
0053             "start": begin,
0054             "end": end,
0055             "step": STEP,
0056         },
0057         timeout=60,
0058     )
0059 
0060     response.raise_for_status()
0061     payload = response.json()
0062 
0063     if payload["status"] != "success":
0064         raise RuntimeError(payload)
0065 
0066     return payload["data"]["result"]
0067 
0068 
0069 def organize(series):
0070     """
0071     Return:
0072         values[timestamp][channel] = value
0073     """
0074 
0075     values = defaultdict(dict)
0076 
0077     for item in series:
0078         labels = item["metric"]
0079 
0080         channel = labels.get("ch_name", "UNKNOWN")
0081 
0082         # Ignore the 12 non-TPC/reference extras here if desired later.
0083         for timestamp, value in item["values"]:
0084             values[int(float(timestamp))][channel] = float(value)
0085 
0086     return values
0087 
0088 
0089 def print_points(name, values):
0090     print()
0091     print("=" * 80)
0092     print(name)
0093     print("=" * 80)
0094 
0095     for timestamp in sorted(values):
0096         channels = values[timestamp]
0097 
0098         print()
0099         print(f"TIME {timestamp}   CHANNELS {len(channels)}")
0100 
0101         for channel in sorted(channels):
0102             print(f"{channel:20s} {channels[channel]:10.3f}")
0103 
0104 
0105 if __name__ == "__main__":
0106 
0107     if len(sys.argv) != 2:
0108         print(f"Usage: {sys.argv[0]} runnumber")
0109         sys.exit(1)
0110 
0111     runnumber = int(sys.argv[1])
0112 
0113     begin, end = get_run_times(runnumber)
0114 
0115     query_begin = begin - PADDING
0116     query_end = end + PADDING
0117 
0118     print(f"Run: {runnumber}")
0119     print(f"Run Begin: {begin}")
0120     print(f"Run End: {end}")
0121     print(f"Query Begin: {query_begin}")
0122     print(f"Query End: {query_end}")
0123 
0124     v0_series = prometheus_query(
0125         "sphenix_tpc_hv_caen_V0Set",
0126         query_begin,
0127         query_end,
0128     )
0129 
0130     vmon_series = prometheus_query(
0131         "sphenix_tpc_hv_caen_VMon",
0132         query_begin,
0133         query_end,
0134     )
0135 
0136     print(f"V0Set series: {len(v0_series)}")
0137     print(f"VMon series: {len(vmon_series)}")
0138 
0139     v0 = organize(v0_series)
0140     vmon = organize(vmon_series)
0141 
0142     print_points("V0Set", v0)
0143     print_points("VMon", vmon)