-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
264 lines (231 loc) · 7.74 KB
/
server.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
#!/usr/bin/env python
# coding=utf-8
"""
@Description: Do not edit
@Date: 2021-08-15 13:01:11
@LastEditors: wanghaijie01
@LastEditTime: 2021-09-26 00:20:53
"""
from datetime import datetime
import logging
import sys
from flask import Flask, make_response, request
from qtools.calculator import IRR, cf, generate_investment_data, nfv, nper, sa
from fund_company import company, scale, company_yield, work_year, awards, scale_ratio, company_filter
from cron import crontask
from qtools import index as qindex
from qtools import debt as qdebt
app = Flask(__name__)
RUN_ENV = "dev"
@app.after_request
def after_request(response):
allow_origin = "http://localhost:3000"
if RUN_ENV == "release":
allow_origin = "https://afreecoder.cn"
response.headers["Access-Control-Allow-Credentials"] = "true"
response.headers["Access-Control-Allow-Headers"] = "Content-Type"
response.headers["Access-Control-Allow-Origin"] = allow_origin
return response
@app.route('/calculator/investment/fixed', methods=["get"])
def fix_investment_calculator():
f = lambda key, default: request.args.get(key) if (request.args.get(key) != None and request.args.get(key) != "") else default
start_amount = float(f("start_amount", 0))
year = float(f("year", 0))
irr = float(f("irr", 0))/100
frequently = f("frequently", "m")
cash_flow = float(f("cash_flow", 0))
end_amount = float(f("end_amount", 0))
cal_type = f("cal_type", "end_amount_cal")
if cal_type == "end_amount_cal":
end_amount = nfv(start_amount, year, irr, frequently, cash_flow)
elif cal_type == "irr_cal":
irr = IRR(start_amount, year, frequently, cash_flow, end_amount)
elif cal_type == "cash_flow_cal":
cash_flow = cf(start_amount, year, irr, frequently, end_amount)
elif cal_type == "start_amount_cal":
start_amount = sa(year, irr, frequently, cash_flow, end_amount)
elif cal_type == "year_cal":
year = nper(start_amount, irr, frequently, cash_flow, end_amount)
summary, cordinate = generate_investment_data(start_amount, year, irr, frequently, cash_flow, end_amount)
res = {
"invest_res": summary,
"cordinate": cordinate
}
return res
@app.route("/finance/fund-company/base", methods=["get"])
def get_company_base_info():
orderby = request.args.get("orderBy")
orderdir = request.args.get("orderDir")
data = company.get_company_base(orderby, orderdir)
res = {
"errno": 0,
"message": "success",
"data": {
"items": data
}
}
resp = make_response(res)
return resp
@app.route("/finance/fund-company/scale", methods=["get"])
def get_scale_info():
fund_type = request.args.get("fund_type")
orderby = request.args.get("orderBy")
orderdir = request.args.get("orderDir")
df = scale.format_scale_info(fund_type, orderby, orderdir)
res = {
"errno": 0,
"message": "success",
"data": {
"items": df.to_dict("records")
}
}
resp = make_response(res)
return resp
@app.route("/finance/fund-company/yield", methods=["get"])
def get_yield_info():
fund_type = request.args.get("fund_type")
orderby = request.args.get("orderBy")
orderdir = request.args.get("orderDir")
df = company_yield.get_yield_rank(fund_type, orderby, orderdir)
res = {
"errno": 0,
"message": "success",
"data": {
"items": df.to_dict("records")
}
}
resp = make_response(res)
return resp
@app.route("/finance/fund-company/work-year", methods=["get"])
def get_work_year_info():
orderdir = request.args.get("orderDir")
df = work_year.get_work_year_info(orderdir)
res = {
"errno": 0,
"message": "success",
"data": {
"items": df.to_dict("records")
}
}
resp = make_response(res)
return resp
@app.route("/finance/fund-company/awards", methods=["get"])
def get_awards_info():
orderdir = request.args.get("orderDir")
df = awards.get_awards_info(orderdir)
res = {
"errno": 0,
"message": "success",
"data": {
"items": df.to_dict("records")
}
}
resp = make_response(res)
return resp
@app.route("/finance/fund-company/scale-ratio", methods=["get"])
def get_scale_ratio_info():
company_code = request.args.get("company_code")
data = scale_ratio.get_scale_ratio(company_code)
res = {
"errno": 0,
"message": "success",
"data": {
"scale_ratio": data
}
}
resp = make_response(res)
return resp
@app.route("/finance/fund-company/yield-detail", methods=["get"])
def get_yield_detail():
company_code = request.args.get("company_code")
fund_type = request.args.get("fund_type")
data = company_yield.get_yield_detail(company_code, fund_type)
res = {
"errno": 0,
"message": "success",
"data": {
"yield_detail": data
}
}
resp = make_response(res)
return resp
@app.route("/finance/fund-company/filter-res", methods=["post"])
def get_fund_company_filter_res():
json_body = request.get_json()
df = company_filter.apply_condition_expression(json_body["conditions"])
res = {
"errno": 0,
"message": "success",
"data": {
"items": df.to_dict("records")
}
}
resp = make_response(res)
return resp
@app.route("/index/fundmental/percentile", methods=["get"])
def get_index_fundmental_percentile():
code = request.args.get("stock_code")
index = qindex.Index()
data = index.get_latest_index_fundmental_percentile_data(code)
res = {
"errno": 0,
"message": "success",
"data": data
}
resp = make_response(res)
return resp
@app.route("/index/fundmental/percentile-list", methods=["get"])
def get_latest_index_fundmental_percentile_list():
codes = request.args.get("stock_codes")
granularity = request.args.get("granularity")
stock = qindex.Index()
data = stock.get_latest_index_fundmental_percentile_list(codes.split(","), granularity)
res = {
"errno": 0,
"message": "success",
"data": data
}
resp = make_response(res)
return resp
@app.route("/index/fundmental/line-plot-data", methods=["get"])
def get_index_fundmental_line_plot_data():
codes = request.args.get("stock_codes")
metrics = request.args.get("metrics")
index = qindex.Index()
data = index.get_index_line_plot_data(codes, metrics)
res = {
"errno": 0,
"message": "success",
"data": data
}
resp = make_response(res)
return resp
@app.route("/debt/mix-metrics/fed", methods=["get"])
def get_debt_fed_data():
code = request.args.get("stock_code")
compute_method = request.args.get("compute_method")
debt = qdebt.Debt()
data = debt.compute_fed(code, compute_method)
res = {
"errno": 0,
"message": "success",
"data": data
}
resp = make_response(res)
return resp
if __name__ == '__main__':
if len(sys.argv) >= 2:
RUN_ENV = sys.argv[1]
# 设置日志
today = datetime.now().strftime("%Y%m%d")
logging.basicConfig(level=logging.INFO,#控制台打印的日志级别
filename='logs/service-' + today + '.log',
filemode='a',##模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志
#a是追加模式,默认如果不写的话,就是追加模式
format=
'%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
#日志格式
)
crontask.start()
app.config['JSON_AS_ASCII'] = False
app.run(host="0.0.0.0", port="8000")