Elfsong commited on
Commit
980944f
·
verified ·
1 Parent(s): c2d6726

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -1
app.py CHANGED
@@ -1,8 +1,11 @@
1
  import os
2
  import streamlit as st
 
3
  import googleapiclient.discovery
 
4
  from google.oauth2 import service_account
5
 
 
6
  def auth():
7
  with open("/tmp/token.json", "w") as token_f:
8
  google_key = os.getenv("GOOGLE_KEY")
@@ -25,6 +28,13 @@ def get_status(credentials, instance_name):
25
  status = True if item["status"] == "RUNNING" else False
26
  return status, ip_address
27
 
 
 
 
 
 
 
 
28
  def get_server(credentials, instance_name):
29
  service = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)
30
  request = service.instances().get(project='nus-cisco-corp-lab-wp1', zone='asia-southeast1-c', instance=instance_name)
@@ -69,7 +79,11 @@ if not status:
69
  else:
70
  st.write(f"You can access Lucky Reactor via http://{ip_address}:7860")
71
  info = get_server(credentials, instance_name)
72
- st.write(info)
 
 
 
 
73
  with st.form("deactivate_form"):
74
  token = st.text_input("Token")
75
  submitted = st.form_submit_button("Terminate 🕊️")
 
1
  import os
2
  import streamlit as st
3
+ from dateutil import parser
4
  import googleapiclient.discovery
5
+ from datetime import datetime, timedelta
6
  from google.oauth2 import service_account
7
 
8
+
9
  def auth():
10
  with open("/tmp/token.json", "w") as token_f:
11
  google_key = os.getenv("GOOGLE_KEY")
 
28
  status = True if item["status"] == "RUNNING" else False
29
  return status, ip_address
30
 
31
+ def get_time_remaining(last_start_timestamp, run_duration_seconds):
32
+ last_start_dt = parser.isoparse(last_start_timestamp)
33
+ end_run_dt = last_start_dt + timedelta(seconds=run_duration_seconds)
34
+ now_dt = datetime.now(tz=last_start_dt.tzinfo)
35
+ delta = (end_run_dt - now_dt).total_seconds()
36
+ return max(delta, 0.0)
37
+
38
  def get_server(credentials, instance_name):
39
  service = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)
40
  request = service.instances().get(project='nus-cisco-corp-lab-wp1', zone='asia-southeast1-c', instance=instance_name)
 
79
  else:
80
  st.write(f"You can access Lucky Reactor via http://{ip_address}:7860")
81
  info = get_server(credentials, instance_name)
82
+ max_run_duration = info['scheduling']['maxRunDuration']['seconds']
83
+ last_start_timestamp = info['lastStartTimestamp']
84
+ get_time_remaining(last_start_timestamp, run_duration_seconds)
85
+ st.write(f'Remaining seconds: {max_run_duration} s')
86
+
87
  with st.form("deactivate_form"):
88
  token = st.text_input("Token")
89
  submitted = st.form_submit_button("Terminate 🕊️")