-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
272 lines (227 loc) · 7.89 KB
/
app.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
from flask import *
from flask_sqlalchemy import SQLAlchemy
import os
import logging
from flask.logging import default_handler
import pytz
from datetime import datetime
db = SQLAlchemy()
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL')
db.init_app(app)
gunicorn_error_logger = logging.getLogger('gunicorn.error')
app.logger.handlers.extend(gunicorn_error_logger.handlers)
app.logger.setLevel(logging.DEBUG)
app.logger.removeHandler(default_handler)
app.logger.info('Starting the Flask User Management App...')
@app.route('/')
def index():
current_app.logger.info(f"DATABASE_URL environment variable: {os.getenv('DATABASE_URL')}")
return 'atstk'
@app.route('/api/c', methods=['POST'])
def create_order():
content = request.get_json(silent=True)
current_app.logger.info(f'content: {content}')
message = content['m']
current_app.logger.info(f'message: {message}')
message_split = message.split('|')
# Valid input
if len(message_split) != 6:
return f'Bad request!', 400
# 2|2723|1|Sell|Fix|120.0
order_id = int(message_split[0])
stock_id = message_split[1]
lot_num = int(message_split[2]) # 1
order_type = message_split[3]
price_type = message_split[4]
price = float(message_split[5])
current_app.logger.info(f'order_id: {order_id}, stock_id: {stock_id}, lot_num: {lot_num}, order_type: {order_type}, price_type: {price_type}, price: {price}')
if len(stock_id) != 4 or order_type not in ['Buy', 'Sell'] or price_type not in ['Fix', 'Market']:
return f'Bad request!', 400
sql_cmd = '''
INSERT INTO STOCK_ORDER
VALUES (%s, %s, %s, %s, %s, %s, %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
'''
result = db.engine.execute(sql_cmd, (order_id, stock_id, lot_num, order_type, price_type, price, 'New'))
return 'OK'
@app.route('/api/g')
def get_order():
sql_cmd = '''
UPDATE HEALTH_CHECK
SET last_check_time = CURRENT_TIMESTAMP
WHERE name = %s;
'''
result = db.engine.execute(sql_cmd, 'Agent')
sql_cmd = '''
SELECT *
FROM STOCK_ORDER
WHERE status = 'New'
ORDER BY create_time
FETCH FIRST ROW ONLY
'''
row = db.engine.execute(sql_cmd).first()
current_app.logger.info(f'row: {row}')
if not row:
return 'Not found!', 404
order_id = row['id']
stock_id = row['stock_id']
lot_num = row['lot_num']
order_type = row['type']
price_type = row['price_type']
price = row['price']
current_app.logger.info(f'order_id: {order_id}')
current_app.logger.info(f'stock_id: {stock_id}')
current_app.logger.info(f'lot_num: {lot_num}')
current_app.logger.info(f'order_type: {order_type}')
current_app.logger.info(f'price_type: {price_type}')
current_app.logger.info(f'price: {price}')
sql_cmd = '''
UPDATE STOCK_ORDER
SET status = 'Accept', update_time = CURRENT_TIMESTAMP
WHERE id = %s;
'''
result = db.engine.execute(sql_cmd, order_id)
return f'{order_id}|{stock_id}|{lot_num}|{order_type}|{price_type}|{price}'
@app.route('/api/h')
def get_health_check():
sql_cmd = '''
SELECT last_check_time
FROM HEALTH_CHECK
WHERE name = %s;
'''
row = db.engine.execute(sql_cmd, 'Agent').first()
last_check_time = row['last_check_time']
tz = pytz.timezone('Asia/Taipei')
last_check_time = last_check_time.replace(tzinfo=pytz.utc).astimezone(tz)
return last_check_time.strftime('%Y-%m-%d %H:%M:%S %z')
@app.route('/api/cc', methods=['POST'])
def create_close_order():
sql_cmd = '''
UPDATE CLOSE_ORDER
SET enable_flag = 1, update_time = CURRENT_TIMESTAMP
WHERE name = %s;
'''
result = db.engine.execute(sql_cmd, 'Agent')
return 'OK'
@app.route('/api/gc')
def get_close_order():
# sql_cmd = '''
# UPDATE HEALTH_CHECK
# SET last_check_time = CURRENT_TIMESTAMP
# WHERE name = %s;
# '''
# result = db.engine.execute(sql_cmd, 'Agent')
sql_cmd = '''
SELECT *
FROM CLOSE_ORDER
WHERE name = %s
AND enable_flag = 1
'''
row = db.engine.execute(sql_cmd, 'Agent').first()
current_app.logger.info(f'row: {row}')
if not row:
return 'Not found!', 404
sql_cmd = '''
UPDATE CLOSE_ORDER
SET enable_flag = 0, update_time = CURRENT_TIMESTAMP
WHERE name = %s;
'''
result = db.engine.execute(sql_cmd, 'Agent')
return 'OK'
@app.route('/api/cf', methods=['POST'])
def create_order_fail_reason():
content = request.get_json(silent=True)
current_app.logger.info(f'content: {content}')
message = content['m']
current_app.logger.info(f'message: {message}')
sql_cmd = '''
INSERT INTO ORDER_FAIL_REASON
VALUES (%s, CURRENT_TIMESTAMP)
'''
result = db.engine.execute(sql_cmd, message)
return 'OK'
@app.route('/api/gf')
def get_order_fail_reason():
result = 'Result:'
sql_cmd = '''
SELECT message, update_time
FROM ORDER_FAIL_REASON
ORDER BY update_time DESC
'''
rows = db.engine.execute(sql_cmd, 'Agent').fetchall()
for row in rows:
current_app.logger.info(f'row: {row}')
message = row['message']
update_time = row['update_time']
# if update_time.date() == datetime.today().date():
tz = pytz.timezone('Asia/Taipei')
update_time = update_time.replace(tzinfo=pytz.utc).astimezone(tz)
update_time = update_time.strftime('%Y-%m-%d %H:%M:%S %z')
result = result + '\n' + update_time + ' | ' + message
return result
@app.route('/api/cr', methods=['POST'])
def create_report():
content = request.get_json(silent=True)
current_app.logger.info(f'content: {content}')
text = content['t']
current_app.logger.info(f'message: {text}')
sql_cmd = '''
UPDATE ORDER_TEXT
SET content = %s, lock = false, upload_time = CURRENT_TIMESTAMP
WHERE name = %s;
'''
result = db.engine.execute(sql_cmd, (text, 'Agent'))
return "OK"
@app.route('/api/gr', methods=['POST'])
def get_report():
sql_cmd = '''
SELECT content
FROM ORDER_TEXT
WHERE name = 'Agent'
AND lock = false
FETCH FIRST ROW ONLY
'''
row = db.engine.execute(sql_cmd).first()
current_app.logger.info(f'row: {row}')
if not row:
text = 'OK'
else:
text = row['content']
sql_cmd = '''
UPDATE ORDER_TEXT
SET lock = true
WHERE name = %s;
'''
result = db.engine.execute(sql_cmd, 'Agent')
return text
@app.route('/api/cs', methods=['POST'])
def create_status_request():
sql_cmd = '''
UPDATE ORDER_STATUS
SET request = true, request_time = CURRENT_TIMESTAMP
WHERE name = 'Server'
'''
result = db.engine.execute(sql_cmd)
return 'OK'
@app.route('/api/gs')
def get_status_request():
sql_cmd = '''
SELECT *
FROM ORDER_STATUS
WHERE name = 'Server'
AND request = true
FETCH FIRST ROW ONLY
'''
row = db.engine.execute(sql_cmd).first()
current_app.logger.info(f'row: {row}')
if not row:
return 'Not found!', 404
sql_cmd = '''
UPDATE ORDER_STATUS
SET request = false
WHERE name = 'Server'
'''
result = db.engine.execute(sql_cmd)
return 'OK'
if __name__ == '__main__':
app.run(debug=True)