-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdb_utils.py
executable file
·105 lines (95 loc) · 4.48 KB
/
db_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
#!/usr/bin/python3
from teradataml import *
import pandas as pd
from time import sleep
from IPython.display import clear_output , display as ipydisplay
def check_and_connect(host, username, password, compute_group):
# check for existing connection
if not get_context():
try:
eng = create_context(host=host, username=username, password=password)
execute_sql(f'SET SESSION COMPUTE GROUP {compute_group};')
except Exception as e:
if 'Failure sending Start Request message' in str(e):
eng = create_context(host=host, username=username, password=password)
execute_sql(f'SET SESSION COMPUTE GROUP {compute_group};')
pass
else:
raise # OperationalError from None
else: #check for existing connection
try:
eng = create_context(host=host, username=username, password=password)
execute_sql(f'SET SESSION COMPUTE GROUP {compute_group};')
except Exception as e:
if 'Failure sending Start Request message' in str(e):
eng = create_context(host=host, username=username, password=password)
execute_sql(f'SET SESSION COMPUTE GROUP {compute_group};')
pass
else:
raise
return eng
def check_cluster_start(compute_group):
try:
df_status = DataFrame.from_query(f'''SELECT * FROM DBC.ComputeStatusV WHERE ComputeGroupName = '{compute_group}';''').to_pandas()
ipydisplay(df_status)
l_status = df_status['CurrentState'].to_list()
l_req = df_status['LastReqState'].to_list()
wt = 'N'
if any('NOTDEPLOYED' in x for x in l_status) | any('DEPLOYING' in x for x in l_status):
print('Analytic Cluster Deploying')
wt = input('Wait for Cluster? y/N? ')
elif any('ACTIVE' in x for x in l_status):
print('Analytic Cluster Available')
elif any('STARTING' in x for x in l_status) | any('RESUME' in x for x in l_req):
print('Analytic Cluster Starting')
wt = input('Wait for Cluster? y/N? ')
else:
print('No Analytic Cluster Available')
print('Enter Number of the Cluster to start, 0 to exit:')
i = 0
l_clusters = df_status.groupby('ComputeProfileName').count().index.to_list()
for n in l_clusters:
i += 1
print(f'{str(i)}. {n}')
sel = input('Enter your selection: ')
if sel != '0':
wt = input('Wait for Cluster? y/N? ')
execute_sql(f'RESUME COMPUTE FOR COMPUTE PROFILE {l_clusters[i-1]} IN {compute_group};')
if wt == 'y':
while True:
df = DataFrame.from_query(f'''SELECT * FROM DBC.ComputeStatusV WHERE ComputeGroupName = '{compute_group}' ;''').to_pandas()
clear_output()
ipydisplay(df)
if any('ACTIVE' in x for x in df['CurrentState'].to_list()):
break
sleep(5)
except Exception as e:
raise
return e
return True
def check_cluster_stop(compute_group):
try:
df_status = DataFrame.from_query(f'''SELECT * FROM DBC.ComputeStatusV WHERE ComputeGroupName = '{compute_group}' ;''').to_pandas()
ipydisplay(df_status)
l_status = df_status['CurrentState'].to_list()
l_req = df_status['LastReqState'].to_list()
if (any('ACTIVE' in x for x in l_status) | any('STARTING' in x for x in l_status) | any('RESUME' in x for x in l_req)) & any('SUSPEND' not in x for x in l_req):
print('Analytic Cluster Active or Resuming')
print('Enter Number of the Cluster to shut down, 0 to exit:')
i = 0
l_clusters = df_status.groupby('ComputeProfileName').count().index.to_list()
for n in l_clusters:
i += 1
print(f'{str(i)}. {n}')
sel = input('Enter your selection: ')
if sel != '0':
execute_sql(f'SUSPEND COMPUTE FOR COMPUTE PROFILE {l_clusters[i-1]} IN {compute_group};')
df = DataFrame.from_query(f'''SELECT * FROM DBC.ComputeStatusV WHERE ComputeGroupName = '{compute_group}' ;''').to_pandas()
clear_output()
ipydisplay(df)
else:
print('Cluster Suspended or Suspend request already submitted.')
return True
except Exception as e:
raise
return e