Skip to content

Commit 7860859

Browse files
committed
Add more commands and exceptions handling
1 parent 64da5d7 commit 7860859

File tree

3 files changed

+183
-69
lines changed

3 files changed

+183
-69
lines changed

main_dumper.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import time
66
import pickle
77
import logging
8+
import requests
89
import os
910
import utils
1011

@@ -36,12 +37,20 @@ def dumper(delay):
3637
# Load.
3738
data = utils.load_data()
3839

40+
# Store data for 24 hours only.
41+
day_back = max([x[0] for x in data]) - 86400
42+
data = list(filter(lambda x: x[0] >= day_back, data))
43+
3944
# Append.
4045
# remote_backends = discover_remote_backends(api)
4146
remote_backends = utils.backends
42-
device_status = [api.backend_status(backend) for backend in remote_backends]
43-
44-
data.append((time.time(), device_status))
47+
try:
48+
device_status = [api.backend_status(backend) for backend in remote_backends]
49+
data.append((time.time(), device_status))
50+
except requests.exceptions.ConnectionError as e:
51+
print(e)
52+
except Exception as e:
53+
pass
4554

4655
# Store.
4756
if step == 0:
@@ -57,15 +66,15 @@ def dumper(delay):
5766
#############
5867
# Make Plots.
5968
for backend in utils.backends:
60-
utils.create_statistics(backend, api)
69+
utils.plot_full(backend, api)
6170
###
6271

6372
# Sleep.
6473
time.sleep(delay)
6574

6675

6776
def main():
68-
delay = 60 # Seconds.
77+
delay = 30 # Seconds.
6978
dumper(delay)
7079

7180

main_slack.py

+38-43
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flask import abort, Flask, jsonify, request
2+
import concurrent.futures as cf
23
import requests
34
import logging
45
import utils
@@ -13,31 +14,39 @@
1314
app = Flask(__name__)
1415

1516
slack_token = utils.get_token('res/token_slack.json')
17+
pool = cf.ThreadPoolExecutor(4)
18+
19+
extension_calibration = '_calibration_full.png'
20+
extension_jobs = '_jobs_full.png'
21+
extension_full = '_full.png'
1622

1723

1824
###
1925
# Slack API
2026
###
21-
2227
@app.route('/confirm', methods=['POST'])
2328
def confirm():
2429
req = request.form.to_dict()
2530
data = json.loads(req["payload"])
2631
backend = data["actions"][0]["name"]
2732
value = data["actions"][0]["value"]
2833

29-
payload = {
30-
"text": "Ok :slightly_smiling_face:",
31-
}
32-
headers = {
33-
'content-type': "application/json",
34-
}
35-
response = requests.request("POST", data['response_url'], data=json.dumps(payload), headers=headers)
36-
print(response.text)
34+
reply = None
35+
name = None
36+
if value.endswith(extension_jobs):
37+
name = 'Pending jobs for {}'.format(backend)
38+
reply = '*Pending jobs* for {} will be sent soon ...'.format(backend)
39+
elif value.endswith(extension_calibration):
40+
name = 'Calibration info for {}'.format(backend)
41+
reply = '*Calibration info* for {} will be sent soon ...'.format(backend)
42+
elif value.endswith(extension_full):
43+
name = 'Full statistics for {}'.format(backend)
44+
reply = '*Full statistics* for {} will be sent soon ...'.format(backend)
3745

38-
send_image('tmp/{}'.format(value), backend, data['channel']['id'])
46+
if name is not None:
47+
pool.submit(send_image, 'tmp/{}'.format(value), name, data['channel']['id'])
3948

40-
return ""
49+
return reply
4150

4251

4352
###
@@ -48,63 +57,49 @@ def calibration():
4857
data = request.form.to_dict()
4958
backend = data['text'].lower()
5059

51-
extension = '_multiqubut_err.png'
52-
5360
if backend in utils.backends:
54-
quick_response(data['response_url'])
55-
send_image('tmp/{}{}'.format(backend, extension), backend, data['channel_id'])
61+
name = 'Calibration info for {}'.format(backend)
62+
pool.submit(send_image, 'tmp/{}{}'.format(backend, extension_calibration),
63+
name, data['channel_id'])
64+
return "Wait a sec ..."
5665
else:
57-
send_buttons(data["response_url"], extension)
58-
59-
return ""
66+
send_buttons(data["response_url"], extension_calibration)
67+
return ''
6068

6169

6270
@app.route('/jobs', methods=['POST'])
6371
def jobs():
6472
data = request.form.to_dict()
6573
backend = data['text'].lower()
6674

67-
extension = '.png'
68-
6975
if backend in utils.backends:
70-
quick_response(data['response_url'])
71-
send_image('tmp/{}{}'.format(backend, extension), backend, data['channel_id'])
76+
name = 'Pending jobs for {}'.format(backend)
77+
pool.submit(send_image, 'tmp/{}{}'.format(backend, extension_jobs),
78+
name, data['channel_id'])
79+
return "Wait a sec ..."
7280
else:
73-
send_buttons(data["response_url"], extension)
74-
75-
return ""
81+
send_buttons(data["response_url"], extension_jobs)
82+
return ''
7683

7784

7885
@app.route('/full', methods=['POST'])
7986
def full():
8087
data = request.form.to_dict()
8188
backend = data['text'].lower()
8289

83-
extension = '_to_send.png'
84-
8590
if backend in utils.backends:
86-
quick_response(data['response_url'])
87-
send_image('tmp/{}{}'.format(backend, extension), backend, data['channel_id'])
91+
name = 'Full statistics for {}'.format(backend)
92+
pool.submit(send_image, 'tmp/{}{}'.format(backend, extension_full),
93+
name, data['channel_id'])
94+
return "Wait a sec ..."
8895
else:
89-
send_buttons(data["response_url"], extension)
90-
91-
return ""
96+
send_buttons(data["response_url"], extension_full)
97+
return ''
9298

9399

94100
###
95101
# Helper Functions
96102
###
97-
def quick_response(response_url):
98-
payload = {
99-
"text": "Wait a sec :hourglass_flowing_sand:",
100-
}
101-
headers = {
102-
'content-type': "application/json",
103-
}
104-
response = requests.request("POST", response_url, data=json.dumps(payload), headers=headers)
105-
print(response.text)
106-
107-
108103
def send_image(path, name, channel):
109104
my_file = {
110105
'file': (path, open(path, 'rb'), 'png')

0 commit comments

Comments
 (0)