-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
136 lines (125 loc) · 5.97 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from logger import LOG_DIR
GOOGLE_SHEET_SCOPE = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
GOOGLE_SHEET_CREDENTIALS_FILENAME = "trex-slowcontrolHV-credentials.json"
GOOGLE_SHEET_NAME = "TREX-DM run lists"
def append_row_to_google_sheet(row, worksheet_number=3):
try:
creds = ServiceAccountCredentials.from_json_keyfile_name(GOOGLE_SHEET_CREDENTIALS_FILENAME, GOOGLE_SHEET_SCOPE)
client = gspread.authorize(creds)
sheet = client.open(GOOGLE_SHEET_NAME)
page = sheet.get_worksheet(worksheet_number) # starts from 0
result = page.append_row(row, value_input_option='USER_ENTERED', table_range='A5')
print(f"Row appended in range {result['updates']['updatedRange']}")
except Exception as e:
print(f"Error while appending row to Google Sheet: {e}")
finally:
with open(LOG_DIR + "/run_list.txt", "a") as file:
file.write(str(row))
def create_row_for_google_sheet(run_number, start_date, run_type, other_columns):
try:
creds = ServiceAccountCredentials.from_json_keyfile_name(GOOGLE_SHEET_CREDENTIALS_FILENAME, GOOGLE_SHEET_SCOPE)
client = gspread.authorize(creds)
sheet = client.open(GOOGLE_SHEET_NAME)
page = sheet.get_worksheet(3) # starts from 0
column_names = page.row_values(2)
except Exception as e:
column_names = ['Run', 'Date', 'Type', 'Time', 'Vmesh Left (V)', 'Eg-mm (V/cm*bar)', 'Vgem(top-bott) (V)',
'Vgembottom', 'Vgemtop', 'Vlastring', 'Ec-g(V/cm*bar)', 'Vcathode (V)', 'Ec-mm (V/cm*bar)',
'Vmesh Right (V)', 'Pressure (bar)', 'Flow (ln/h)', 'Gain (FEC units)', 'Shape time (FEC units)',
'Clock (FEC units/MHz)', 'Threshold Left (daq+thr)', 'Multiplicity Left', 'Threshold Right (daq+thr)',
'Multiplicity Right', 'Trigg_delay (hexad/decimal)', 'trip info', 'Notes',
]
print(f"Error, {e}, while fetching column names from Google Sheet. Using default column names")
column_names = [c.replace(' ', '').lower() for c in column_names]
row = ['' for _ in range(len(column_names))]
row[0] = run_number
row[1] = start_date
row[2] = run_type
for ch, v in other_columns.items():
ch = ch.replace(' ', '').lower()
column_found = False
for column in column_names:
if ch in column:
row[column_names.index(column)] = v
column_found = True
break
if not column_found:
print(f"Column for channel {ch} not found in Google Sheet")
return row
def get_last_run_number_from_google_sheet(worksheet_number=3):
try:
creds = ServiceAccountCredentials.from_json_keyfile_name(GOOGLE_SHEET_CREDENTIALS_FILENAME, GOOGLE_SHEET_SCOPE)
client = gspread.authorize(creds)
sheet = client.open(GOOGLE_SHEET_NAME)
page = sheet.get_worksheet(worksheet_number) # starts from 0
column_values = page.col_values(1) # 1 refers to column A (run number)
run_numbers = [val for val in column_values if val]
print(f"Last run number from Google Sheet: {run_numbers[-1]}")
return run_numbers[-1]
except Exception as e:
print(f"Error while fetching last run number from Google Sheet: {e}")
return -1
class GoogleSheetClient:
def __init__(self, scope, credentials_filename, sheet_name):
self.scope = scope
self.credentials_filename = credentials_filename
self.sheet_name = sheet_name
self.client = None
self.sheet = None
def __enter__(self):
try:
creds = ServiceAccountCredentials.from_json_keyfile_name(self.credentials_filename, self.scope)
self.client = gspread.authorize(creds)
self.sheet = self.client.open(self.sheet_name)
return self.sheet
except Exception as e:
print(f"Error while connecting to Google Sheet: {e}")
return None
def __exit__(self, exc_type, exc_value, traceback):
pass
import requests
import json
import os
import datetime as dt
def write_to_log_file(message:str, log_filename:str, print_message:bool=True):
if print_message:
print(message)
if log_filename:
filename = LOG_DIR + "/" + log_filename
if not os.path.isfile(filename):
try:
# create the file if it does not exist
with open(filename, 'w') as file:
pass
print("Writing to new file:", filename)
except:
raise Exception("Invalid file or directory:", filename)
with open(filename, 'a') as file:
time = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
file.write(time + " " + message + '\n')
def send_slack_message(message:str, log_filename="", print_message=True):
#### webhook to Alvaro chat
webhook_url = ""
#### webhook to trex-operations channel
#webhook_url = ""
write_to_log_file(message, log_filename, print_message=print_message)
write_to_log_file(message, "slack.log", print_message=False) # always write to slack.log too
slack_data = {'text': message}
try:
requests.post(webhook_url, data=json.dumps(slack_data), headers={'Content-Type': 'application/json'})
except Exception as e:
print(e)
import threading
class ExceptionThread(threading.Thread):
def __init__(self, target=None, args=(), kwargs=None, **thread_kwargs):
super().__init__(target=target, args=args, kwargs=kwargs, **thread_kwargs)
self.exception = None
def run(self):
try:
# Run the target function, if provided
if self._target:
self._target(*self._args, **(self._kwargs or {}))
except Exception as e:
self.exception = e