1
1
from flask import abort , Flask , jsonify , request
2
+ import concurrent .futures as cf
2
3
import requests
3
4
import logging
4
5
import utils
13
14
app = Flask (__name__ )
14
15
15
16
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'
16
22
17
23
18
24
###
19
25
# Slack API
20
26
###
21
-
22
27
@app .route ('/confirm' , methods = ['POST' ])
23
28
def confirm ():
24
29
req = request .form .to_dict ()
25
30
data = json .loads (req ["payload" ])
26
31
backend = data ["actions" ][0 ]["name" ]
27
32
value = data ["actions" ][0 ]["value" ]
28
33
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 )
37
45
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' ])
39
48
40
- return ""
49
+ return reply
41
50
42
51
43
52
###
@@ -48,63 +57,49 @@ def calibration():
48
57
data = request .form .to_dict ()
49
58
backend = data ['text' ].lower ()
50
59
51
- extension = '_multiqubut_err.png'
52
-
53
60
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 ..."
56
65
else :
57
- send_buttons (data ["response_url" ], extension )
58
-
59
- return ""
66
+ send_buttons (data ["response_url" ], extension_calibration )
67
+ return ''
60
68
61
69
62
70
@app .route ('/jobs' , methods = ['POST' ])
63
71
def jobs ():
64
72
data = request .form .to_dict ()
65
73
backend = data ['text' ].lower ()
66
74
67
- extension = '.png'
68
-
69
75
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 ..."
72
80
else :
73
- send_buttons (data ["response_url" ], extension )
74
-
75
- return ""
81
+ send_buttons (data ["response_url" ], extension_jobs )
82
+ return ''
76
83
77
84
78
85
@app .route ('/full' , methods = ['POST' ])
79
86
def full ():
80
87
data = request .form .to_dict ()
81
88
backend = data ['text' ].lower ()
82
89
83
- extension = '_to_send.png'
84
-
85
90
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 ..."
88
95
else :
89
- send_buttons (data ["response_url" ], extension )
90
-
91
- return ""
96
+ send_buttons (data ["response_url" ], extension_full )
97
+ return ''
92
98
93
99
94
100
###
95
101
# Helper Functions
96
102
###
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
-
108
103
def send_image (path , name , channel ):
109
104
my_file = {
110
105
'file' : (path , open (path , 'rb' ), 'png' )
0 commit comments