-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
477 lines (435 loc) · 16.5 KB
/
app.py
File metadata and controls
477 lines (435 loc) · 16.5 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/env python3
"""
가맹점수 분석 차트 API 서버
FE에서 차트 사양을 가져올 수 있는 REST API를 제공합니다.
"""
from flask import Flask, jsonify, request
from flask_cors import CORS
from flask_restx import Api, Resource, fields
from chart_specs import (
AGE_GENDER_DATA,
AREA_POPULATION_DATA,
BAR_CHART_DATA,
CLOSING_RATE_DATA,
GENDER_PIE_DATA,
GROWTH_RATE_DATA,
LINE_CHART_DATA,
NET_GROWTH_RATE_DATA,
OPENING_CLOSING_RATE_DATA,
TIME_PERIOD_DATA,
YEARLY_TREND_DATA,
get_chartjs_age_gender_config,
get_chartjs_area_population_config,
get_chartjs_bar_chart_config,
get_chartjs_closing_rate_config,
get_chartjs_growth_rate_config,
get_chartjs_line_chart_config,
get_chartjs_net_growth_rate_config,
get_chartjs_opening_closing_rate_config,
get_chartjs_pie_chart_config,
get_chartjs_time_period_config,
get_chartjs_yearly_trend_config,
get_echarts_bar_chart_option,
get_echarts_line_chart_option,
get_echarts_pie_chart_option,
get_plotly_bar_chart_figure,
get_plotly_line_chart_figure,
get_plotly_pie_chart_figure,
get_vega_lite_bar_chart_spec,
get_vega_lite_line_chart_spec,
get_vega_lite_pie_chart_spec,
)
app = Flask(__name__)
CORS(app) # CORS 활성화
# Swagger API 설정
api = Api(
app,
version="1.0",
title="가맹점수 분석 차트 API",
description="가맹점수 분석 결과를 차트 사양(spec) 형태로 제공하는 REST API",
doc="/docs",
default="charts",
default_label="차트 관련 API",
authorizations={"apikey": {"type": "apiKey", "in": "header", "name": "X-API-KEY"}},
)
# API 모델 정의
chart_response_model = api.model(
"ChartResponse",
{
"message": fields.String(description="응답 메시지"),
"data": fields.Raw(description="차트 사양 데이터"),
},
)
error_model = api.model(
"Error",
{
"error": fields.String(description="에러 메시지"),
"status": fields.Integer(description="HTTP 상태 코드"),
},
)
# 차트 네임스페이스
charts_ns = api.namespace("api/charts", description="차트 사양 관련 API")
data_ns = api.namespace("api/data", description="원본 데이터 관련 API")
@app.route("/")
def index():
"""API 루트 엔드포인트"""
return jsonify(
{
"message": "가맹점수 분석 차트 API 서버",
"version": "1.0.0",
"endpoints": {
"charts": "/api/charts",
"data": "/api/data",
"vega_lite": "/api/charts/vega_lite",
"echarts": "/api/charts/echarts",
"plotly": "/api/charts/plotly",
"chartjs": "/api/charts/chartjs",
},
}
)
@charts_ns.route("/")
class AllCharts(Resource):
@api.doc("get_all_charts")
@api.response(200, "Success", chart_response_model)
def get(self):
"""모든 차트 라이브러리의 사양을 반환"""
return {
"message": "모든 차트 사양 조회 성공",
"data": {
"vega_lite": {
"line_chart": get_vega_lite_line_chart_spec(),
"bar_chart": get_vega_lite_bar_chart_spec(),
"pie_chart": get_vega_lite_pie_chart_spec(),
},
"echarts": {
"line_chart": get_echarts_line_chart_option(),
"bar_chart": get_echarts_bar_chart_option(),
"pie_chart": get_echarts_pie_chart_option(),
},
"plotly": {
"line_chart": get_plotly_line_chart_figure(),
"bar_chart": get_plotly_bar_chart_figure(),
"pie_chart": get_plotly_pie_chart_figure(),
},
"chartjs": {
"line_chart": get_chartjs_line_chart_config(),
"bar_chart": get_chartjs_bar_chart_config(),
"pie_chart": get_chartjs_pie_chart_config(),
"area_population": get_chartjs_area_population_config(),
"age_gender": get_chartjs_age_gender_config(),
"time_period": get_chartjs_time_period_config(),
"yearly_trend": get_chartjs_yearly_trend_config(),
"growth_rate": get_chartjs_growth_rate_config(),
"closing_rate": get_chartjs_closing_rate_config(),
"opening_closing_rate": get_chartjs_opening_closing_rate_config(),
"net_growth_rate": get_chartjs_net_growth_rate_config(),
},
},
}
@charts_ns.route("/vega_lite")
class VegaLiteCharts(Resource):
@api.doc("get_vega_lite_charts")
@api.param(
"type", "차트 타입 (line, bar, pie, all)", enum=["line", "bar", "pie", "all"]
)
@api.response(200, "Success")
def get(self):
"""Vega-Lite 차트 사양만 반환"""
chart_type = request.args.get("type", "all")
if chart_type == "line":
return get_vega_lite_line_chart_spec()
elif chart_type == "bar":
return get_vega_lite_bar_chart_spec()
elif chart_type == "pie":
return get_vega_lite_pie_chart_spec()
else:
return {
"line_chart": get_vega_lite_line_chart_spec(),
"bar_chart": get_vega_lite_bar_chart_spec(),
"pie_chart": get_vega_lite_pie_chart_spec(),
}
@charts_ns.route("/echarts")
class EChartsCharts(Resource):
@api.doc("get_echarts_charts")
@api.param(
"type", "차트 타입 (line, bar, pie, all)", enum=["line", "bar", "pie", "all"]
)
@api.response(200, "Success")
def get(self):
"""ECharts 차트 옵션만 반환"""
chart_type = request.args.get("type", "all")
if chart_type == "line":
return get_echarts_line_chart_option()
elif chart_type == "bar":
return get_echarts_bar_chart_option()
elif chart_type == "pie":
return get_echarts_pie_chart_option()
else:
return {
"line_chart": get_echarts_line_chart_option(),
"bar_chart": get_echarts_bar_chart_option(),
"pie_chart": get_echarts_pie_chart_option(),
}
@charts_ns.route("/plotly")
class PlotlyCharts(Resource):
@api.doc("get_plotly_charts")
@api.param(
"type", "차트 타입 (line, bar, pie, all)", enum=["line", "bar", "pie", "all"]
)
@api.response(200, "Success")
def get(self):
"""Plotly 차트 figure만 반환"""
chart_type = request.args.get("type", "all")
if chart_type == "line":
return get_plotly_line_chart_figure()
elif chart_type == "bar":
return get_plotly_bar_chart_figure()
elif chart_type == "pie":
return get_plotly_pie_chart_figure()
else:
return {
"line_chart": get_plotly_line_chart_figure(),
"bar_chart": get_plotly_bar_chart_figure(),
"pie_chart": get_plotly_pie_chart_figure(),
}
@charts_ns.route("/chartjs")
class ChartJSCharts(Resource):
@api.doc("get_chartjs_charts")
@api.param(
"type",
"차트 타입 (line, bar, pie, area_population, age_gender, "
"time_period, yearly_trend, growth_rate, closing_rate, "
"opening_closing_rate, net_growth_rate, all)",
enum=[
"line",
"bar",
"pie",
"area_population",
"age_gender",
"time_period",
"yearly_trend",
"growth_rate",
"closing_rate",
"opening_closing_rate",
"net_growth_rate",
"all",
],
)
@api.response(200, "Success")
def get(self):
"""Chart.js 차트 설정만 반환"""
chart_type = request.args.get("type", "all")
if chart_type == "line":
return get_chartjs_line_chart_config()
elif chart_type == "bar":
return get_chartjs_bar_chart_config()
elif chart_type == "pie":
return get_chartjs_pie_chart_config()
elif chart_type == "area_population":
return get_chartjs_area_population_config()
elif chart_type == "age_gender":
return get_chartjs_age_gender_config()
elif chart_type == "time_period":
return get_chartjs_time_period_config()
elif chart_type == "yearly_trend":
return get_chartjs_yearly_trend_config()
elif chart_type == "growth_rate":
return get_chartjs_growth_rate_config()
elif chart_type == "closing_rate":
return get_chartjs_closing_rate_config()
elif chart_type == "opening_closing_rate":
return get_chartjs_opening_closing_rate_config()
elif chart_type == "net_growth_rate":
return get_chartjs_net_growth_rate_config()
else:
return {
"line_chart": get_chartjs_line_chart_config(),
"bar_chart": get_chartjs_bar_chart_config(),
"pie_chart": get_chartjs_pie_chart_config(),
"area_population": get_chartjs_area_population_config(),
"age_gender": get_chartjs_age_gender_config(),
"time_period": get_chartjs_time_period_config(),
"yearly_trend": get_chartjs_yearly_trend_config(),
"growth_rate": get_chartjs_growth_rate_config(),
"closing_rate": get_chartjs_closing_rate_config(),
"opening_closing_rate": get_chartjs_opening_closing_rate_config(),
"net_growth_rate": get_chartjs_net_growth_rate_config(),
}
@data_ns.route("/")
class ChartData(Resource):
@api.doc("get_chart_data")
@api.param(
"type",
"데이터 타입 (line, bar, pie, area_population, age_gender, "
"time_period, yearly_trend, growth_rate, closing_rate, "
"opening_closing_rate, net_growth_rate, all)",
enum=[
"line",
"bar",
"pie",
"area_population",
"age_gender",
"time_period",
"yearly_trend",
"growth_rate",
"closing_rate",
"opening_closing_rate",
"net_growth_rate",
"all",
],
)
@api.response(200, "Success")
def get(self):
"""원본 차트 데이터 반환"""
data_type = request.args.get("type", "all")
if data_type == "line":
return LINE_CHART_DATA
elif data_type == "bar":
return BAR_CHART_DATA
elif data_type == "pie":
return GENDER_PIE_DATA
elif data_type == "area_population":
return AREA_POPULATION_DATA
elif data_type == "age_gender":
return AGE_GENDER_DATA
elif data_type == "time_period":
return TIME_PERIOD_DATA
elif data_type == "yearly_trend":
return YEARLY_TREND_DATA
elif data_type == "growth_rate":
return GROWTH_RATE_DATA
elif data_type == "closing_rate":
return CLOSING_RATE_DATA
elif data_type == "opening_closing_rate":
return OPENING_CLOSING_RATE_DATA
elif data_type == "net_growth_rate":
return NET_GROWTH_RATE_DATA
else:
return {
"line_chart_data": LINE_CHART_DATA,
"bar_chart_data": BAR_CHART_DATA,
"pie_chart_data": GENDER_PIE_DATA,
"area_population_data": AREA_POPULATION_DATA,
"age_gender_data": AGE_GENDER_DATA,
"time_period_data": TIME_PERIOD_DATA,
"yearly_trend_data": YEARLY_TREND_DATA,
"growth_rate_data": GROWTH_RATE_DATA,
"closing_rate_data": CLOSING_RATE_DATA,
"opening_closing_rate_data": OPENING_CLOSING_RATE_DATA,
"net_growth_rate_data": NET_GROWTH_RATE_DATA,
}
@charts_ns.route("/<library>/<chart_type>")
class SpecificChart(Resource):
@api.doc("get_specific_chart")
@api.param(
"library", "차트 라이브러리", enum=["vega_lite", "echarts", "plotly", "chartjs"]
)
@api.param(
"chart_type",
"차트 타입",
enum=[
"line",
"bar",
"pie",
"area_population",
"age_gender",
"time_period",
"yearly_trend",
"growth_rate",
"closing_rate",
"opening_closing_rate",
"net_growth_rate",
],
)
@api.response(200, "Success")
@api.response(400, "Bad Request", error_model)
@api.response(500, "Internal Server Error", error_model)
def get(self, library, chart_type):
"""특정 라이브러리의 특정 차트 타입 사양 반환"""
chart_functions = {
"vega_lite": {
"line": get_vega_lite_line_chart_spec,
"bar": get_vega_lite_bar_chart_spec,
"pie": get_vega_lite_pie_chart_spec,
},
"echarts": {
"line": get_echarts_line_chart_option,
"bar": get_echarts_bar_chart_option,
"pie": get_echarts_pie_chart_option,
},
"plotly": {
"line": get_plotly_line_chart_figure,
"bar": get_plotly_bar_chart_figure,
"pie": get_plotly_pie_chart_figure,
},
"chartjs": {
"line": get_chartjs_line_chart_config,
"bar": get_chartjs_bar_chart_config,
"pie": get_chartjs_pie_chart_config,
"area_population": get_chartjs_area_population_config,
"age_gender": get_chartjs_age_gender_config,
"time_period": get_chartjs_time_period_config,
"yearly_trend": get_chartjs_yearly_trend_config,
"growth_rate": get_chartjs_growth_rate_config,
"closing_rate": get_chartjs_closing_rate_config,
"opening_closing_rate": get_chartjs_opening_closing_rate_config,
"net_growth_rate": get_chartjs_net_growth_rate_config,
},
}
if library not in chart_functions:
api.abort(400, f"지원하지 않는 라이브러리: {library}")
if chart_type not in chart_functions[library]:
api.abort(400, f"지원하지 않는 차트 타입: {chart_type}")
try:
chart_spec = chart_functions[library][chart_type]()
return chart_spec
except Exception as e:
api.abort(500, f"차트 사양 생성 실패: {str(e)}")
@app.route("/health")
def health_check():
"""헬스 체크 엔드포인트"""
return jsonify({"status": "healthy", "service": "chart-api-server"})
@app.route("/redoc")
def redoc():
"""ReDoc API 문서"""
return """
<!DOCTYPE html>
<html>
<head>
<title>가맹점수 분석 차트 API - ReDoc</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link
href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700"
rel="stylesheet">
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<redoc spec-url="/swagger.json"></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@2.0.0/bundles/redoc.standalone.js">
</script>
</body>
</html>
"""
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="가맹점수 분석 차트 API 서버")
parser.add_argument("--host", default="0.0.0.0", help="호스트 주소 (기본값: 0.0.0.0)")
parser.add_argument("--port", type=int, default=5001, help="포트 번호 (기본값: 5001)")
parser.add_argument("--debug", action="store_true", help="디버그 모드 활성화")
args = parser.parse_args()
print("🚀 가맹점수 분석 차트 API 서버 시작...")
print("📊 사용 가능한 엔드포인트:")
print(" - GET /api/charts : 모든 차트 사양")
print(" - GET /api/charts/{library} : 특정 라이브러리의 차트 사양")
print(" - GET /api/charts/{library}/{type} : 특정 차트 사양")
print(" - GET /api/data : 원본 데이터")
print(" - GET /health : 헬스 체크")
print(f"\n🌐 서버가 http://{args.host}:{args.port} 에서 실행됩니다.")
app.run(debug=args.debug, host=args.host, port=args.port)
# 테스트 주석 추가