├── requirements.txt ├── img └── preview1.png ├── release-notes.md ├── README.md ├── db.example.conf ├── templates ├── home.html ├── base.html └── new.html ├── .gitignore ├── app.py └── LICENSE /requirements.txt: -------------------------------------------------------------------------------- 1 | flask -------------------------------------------------------------------------------- /img/preview1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NUAA-Open-Source/A2OS-Dashboard/master/img/preview1.png -------------------------------------------------------------------------------- /release-notes.md: -------------------------------------------------------------------------------- 1 | # Relase Notes 2 | 3 | ## 1.0.0 4 | 5 | - 简单的查询语句绘制图表 6 | 目前仅支持一张图表二维表示,如查询最近 7 天的总事件数 7 | ```sql 8 | SELECT DATE_FORMAT(DATE(created_at), "%Y-%m-%d"), COUNT(*) FROM events WHERE DATE(created_at) BETWEEN DATE(NOW())-6 AND DATE(now()) GROUP BY DATE(created_at); 9 | ``` 10 | - 创建新查询时图表预览和提交 11 | - 保存历史创建图表于首页 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A2OS Dashboard 2 | 3 | ## 项目介绍 4 | 5 | 该项目旨在为 SafeU 开发过程中引入的数据埋点服务 `behavior` 提供数据可视化服务 6 | 7 | ## 项目技术栈 8 | 9 | - Flask 10 | - ECharts 11 | 12 | ## Release Notes 13 | 14 | [Release Notes](./release-notes.md) 15 | 16 | ## 效果预览 17 | 18 | ![](./img/preview1.png) 19 | 20 | ## Notice 21 | 22 | 本项目使用的数据库配置存放在 `db.conf` 中,可自行根据 `db.example.conf` 的示例进行创建 23 | -------------------------------------------------------------------------------- /db.example.conf: -------------------------------------------------------------------------------- 1 | # Copyright 2019 SafeU Dev Team 2 | # Licensed under the Apache License, Version 2.0 (the "License"); 3 | # you may not use this file except in compliance with the License. 4 | # You may obtain a copy of the License at 5 | # 6 | # http://www.apache.org/licenses/LICENSE-2.0 7 | # 8 | # Unless required by applicable law or agreed to in writing, software 9 | # distributed under the License is distributed on an "AS IS" BASIS, 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | # See the License for the specific language governing permissions and 12 | # limitations under the License. 13 | 14 | [behavior_db] 15 | behavior_db = your_db 16 | behavior_db_host = your_db_host 17 | behavior_db_port = your_db_port 18 | behavior_db_user = your_db_user 19 | behavior_db_password = your_db_password 20 | behavior_db_charset = your_db_charset 21 | 22 | [a2os_dashboard_db] 23 | a2os_dashboard_db = your_db 24 | a2os_dashboard_db_host = your_db_host 25 | a2os_dashboard_db_port = your_db_port 26 | a2os_dashboard_db_user = your_db_user 27 | a2os_dashboard_db_password = your_db_password 28 | a2os_dashboard_db_charset = your_db_charset -------------------------------------------------------------------------------- /templates/home.html: -------------------------------------------------------------------------------- 1 | 15 | 16 | {% extends "base.html" %} 17 | {% block body %} 18 |
19 |
20 | {% for item in result %} 21 |
22 | {% endfor %} 23 |
24 |
25 | {% endblock body %} 26 | 27 | {% block script %} 28 | {% for item in result %} 29 | var myChart{{item[0]}} = echarts.init(document.getElementById('chart-{{item[0]}}')); 30 | var option = { 31 | title: { 32 | text: '{{item[1]}}' 33 | }, 34 | tooltip: {}, 35 | legend: { 36 | data:['{{item[3]}}'] 37 | }, 38 | xAxis: { 39 | data: {{item[4]|safe}} 40 | }, 41 | yAxis: {}, 42 | series: [{ 43 | name: '{{item[3]}}', 44 | type: 'line', 45 | data: {{item[5]}} 46 | }] 47 | }; 48 | myChart{{item[0]}}.setOption(option); 49 | {% endfor %} 50 | {% endblock script %} 51 | 52 | {% block style %} 53 | .content { 54 | margin-top: 30px; 55 | width: 90%; 56 | margin-left: 5%; 57 | } 58 | 59 | .charts { 60 | margin-top: 30px; 61 | width: 100%; 62 | display: flex; 63 | flex-direction: row; 64 | justify-content: flex-start; 65 | flex-wrap: wrap; 66 | } 67 | 68 | .chart { 69 | width: 50%; 70 | height: 300px; 71 | } 72 | {% endblock style %} -------------------------------------------------------------------------------- /templates/base.html: -------------------------------------------------------------------------------- 1 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | Document 29 | 30 | 31 | 43 | {% block body %} 44 | 45 | {% endblock body %} 46 | 47 | 48 | 58 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/flask,python 3 | # Edit at https://www.gitignore.io/?templates=flask,python 4 | 5 | ### Flask ### 6 | instance/* 7 | !instance/.gitignore 8 | .webassets-cache 9 | 10 | ### Flask.Python Stack ### 11 | # Byte-compiled / optimized / DLL files 12 | __pycache__/ 13 | *.py[cod] 14 | *$py.class 15 | 16 | # C extensions 17 | *.so 18 | 19 | # Distribution / packaging 20 | .Python 21 | build/ 22 | develop-eggs/ 23 | dist/ 24 | downloads/ 25 | eggs/ 26 | .eggs/ 27 | lib/ 28 | lib64/ 29 | parts/ 30 | sdist/ 31 | var/ 32 | wheels/ 33 | pip-wheel-metadata/ 34 | share/python-wheels/ 35 | *.egg-info/ 36 | .installed.cfg 37 | *.egg 38 | MANIFEST 39 | 40 | # PyInstaller 41 | # Usually these files are written by a python script from a template 42 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 43 | *.manifest 44 | *.spec 45 | 46 | # Installer logs 47 | pip-log.txt 48 | pip-delete-this-directory.txt 49 | 50 | # Unit test / coverage reports 51 | htmlcov/ 52 | .tox/ 53 | .nox/ 54 | .coverage 55 | .coverage.* 56 | .cache 57 | nosetests.xml 58 | coverage.xml 59 | *.cover 60 | .hypothesis/ 61 | .pytest_cache/ 62 | 63 | # Translations 64 | *.mo 65 | *.pot 66 | 67 | # Django stuff: 68 | *.log 69 | local_settings.py 70 | db.sqlite3 71 | 72 | # Flask stuff: 73 | instance/ 74 | 75 | # Scrapy stuff: 76 | .scrapy 77 | 78 | # Sphinx documentation 79 | docs/_build/ 80 | 81 | # PyBuilder 82 | target/ 83 | 84 | # Jupyter Notebook 85 | .ipynb_checkpoints 86 | 87 | # IPython 88 | profile_default/ 89 | ipython_config.py 90 | 91 | # pyenv 92 | .python-version 93 | 94 | # pipenv 95 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 96 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 97 | # having no cross-platform support, pipenv may install dependencies that don’t work, or not 98 | # install all needed dependencies. 99 | #Pipfile.lock 100 | 101 | # celery beat schedule file 102 | celerybeat-schedule 103 | 104 | # SageMath parsed files 105 | *.sage.py 106 | 107 | # Environments 108 | .env 109 | .venv 110 | env/ 111 | venv/ 112 | ENV/ 113 | env.bak/ 114 | venv.bak/ 115 | 116 | # Spyder project settings 117 | .spyderproject 118 | .spyproject 119 | 120 | # Rope project settings 121 | .ropeproject 122 | 123 | # mkdocs documentation 124 | /site 125 | 126 | # mypy 127 | .mypy_cache/ 128 | .dmypy.json 129 | dmypy.json 130 | 131 | # Pyre type checker 132 | .pyre/ 133 | 134 | ### Python ### 135 | # Byte-compiled / optimized / DLL files 136 | 137 | # C extensions 138 | 139 | # Distribution / packaging 140 | 141 | # PyInstaller 142 | # Usually these files are written by a python script from a template 143 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 144 | 145 | # Installer logs 146 | 147 | # Unit test / coverage reports 148 | 149 | # Translations 150 | 151 | # Django stuff: 152 | 153 | # Flask stuff: 154 | 155 | # Scrapy stuff: 156 | 157 | # Sphinx documentation 158 | 159 | # PyBuilder 160 | 161 | # Jupyter Notebook 162 | 163 | # IPython 164 | 165 | # pyenv 166 | 167 | # pipenv 168 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 169 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 170 | # having no cross-platform support, pipenv may install dependencies that don’t work, or not 171 | # install all needed dependencies. 172 | 173 | # celery beat schedule file 174 | 175 | # SageMath parsed files 176 | 177 | # Environments 178 | 179 | # Spyder project settings 180 | 181 | # Rope project settings 182 | 183 | # mkdocs documentation 184 | 185 | # mypy 186 | 187 | # Pyre type checker 188 | 189 | # End of https://www.gitignore.io/api/flask,python 190 | 191 | db.conf 192 | .vscode -------------------------------------------------------------------------------- /templates/new.html: -------------------------------------------------------------------------------- 1 | 15 | 16 | {% extends "base.html" %} 17 | 18 | {% block body %} 19 |
20 |
21 | 22 | 23 |
24 |
25 | 26 | 27 |
28 |
29 | 30 | 31 |
32 |
33 | 34 | 35 |
36 |
37 | 38 | 39 |
40 | {% endblock body %} 41 | 42 | {% block script %} 43 | $().ready(function() { 44 | $("#quicklook-chart").hide() 45 | }) 46 | 47 | function submit() { 48 | title = $("#title-input").val() 49 | x_title = $("#x-title-input").val() 50 | y_title = $("#y-title-input").val() 51 | sql = $("#sql-textarea").val() 52 | $.ajax({ 53 | type: "POST", 54 | url: "/new", 55 | data: { 56 | title: title, 57 | x_title: x_title, 58 | y_title: y_title, 59 | sql: sql, 60 | }, 61 | dataType: "json", 62 | async: true, 63 | success: function(data) { 64 | toastr.success("保存成功") 65 | } 66 | }) 67 | } 68 | 69 | var myChart = echarts.init(document.getElementById('quicklook-chart')); 70 | function quicklook() { 71 | title = $("#title-input").val() 72 | x_title = $("#x-title-input").val() 73 | y_title = $("#y-title-input").val() 74 | sql = $("#sql-textarea").val() 75 | $.ajax({ 76 | type: "POST", 77 | url: "/quicklook", 78 | data: { 79 | title: title, 80 | x_title: x_title, 81 | y_title: y_title, 82 | sql: sql, 83 | }, 84 | dataType: "json", 85 | async: true, 86 | success: function(data) { 87 | $("#quicklook-chart").show() 88 | var option = { 89 | title: { 90 | text: data["title"] 91 | }, 92 | tooltip: {}, 93 | legend: { 94 | data:[data["x_title"]] 95 | }, 96 | xAxis: { 97 | data: data["x_data"] 98 | }, 99 | yAxis: {}, 100 | series: [{ 101 | name: data["x_title"], 102 | type: 'line', 103 | data: data["y_data"] 104 | }] 105 | }; 106 | myChart.setOption(option); 107 | } 108 | }) 109 | } 110 | {% endblock script %} 111 | 112 | {% block style %} 113 | .form { 114 | width: 90%; 115 | margin-left: 5%; 116 | margin-top: 30px; 117 | } 118 | {% endblock style %} -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 SafeU Dev Team 2 | # Licensed under the Apache License, Version 2.0 (the "License"); 3 | # you may not use this file except in compliance with the License. 4 | # You may obtain a copy of the License at 5 | # 6 | # http://www.apache.org/licenses/LICENSE-2.0 7 | # 8 | # Unless required by applicable law or agreed to in writing, software 9 | # distributed under the License is distributed on an "AS IS" BASIS, 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | # See the License for the specific language governing permissions and 12 | # limitations under the License. 13 | 14 | # coding=utf-8 15 | from flask import Flask, render_template, request 16 | import pymysql 17 | import datetime 18 | import functools 19 | import configparser 20 | import json 21 | 22 | app = Flask(__name__) 23 | cf = configparser.ConfigParser() 24 | cf.read('db.conf') 25 | 26 | @app.route('/') 27 | def home(): 28 | dashboard_select_result = dashboard_sql_execute("select * from record") 29 | result = [] 30 | for record in dashboard_select_result: 31 | behavior_select_result = behavior_sql_execute(record[1]) 32 | x_data = [] 33 | y_data = [] 34 | for row in behavior_select_result: 35 | if (type(row[0]) == datetime.date): 36 | x_data.append(row[0].strftime("%Y-%m-%d")) 37 | y_data.append(row[1]) 38 | else: 39 | x_data.append(row[0]) 40 | y_data.append(row[1]) 41 | result.append((record[0], record[2], record[3], record[4], x_data, y_data)) 42 | return render_template("home.html", result=result) 43 | 44 | @app.route('/new', methods=["POST", "GET"]) 45 | def new(): 46 | if request.method == "GET": 47 | return render_template("new.html") 48 | elif request.method == "POST": 49 | title = request.form['title'] 50 | x_title = request.form['x_title'] 51 | y_title = request.form['y_title'] 52 | sql = request.form['sql'] 53 | print("insert into record (title, xtitle, ytitle, query) values ('%s', '%s', '%s', '%s')" % (title, x_title, y_title, pymysql.escape_string(sql))) 54 | dashboard_sql_execute("insert into record (title, xtitle, ytitle, query) values ('%s', '%s', '%s', '%s')" % (title, x_title, y_title, pymysql.escape_string(sql))) 55 | 56 | result = {"status": "success"} 57 | return json.dumps(result) 58 | 59 | @app.route('/quicklook', methods=["POST"]) 60 | def quicklook(): 61 | title = request.form['title'] 62 | x_title = request.form['x_title'] 63 | y_title = request.form['y_title'] 64 | sql = request.form['sql'] 65 | x_data = [] 66 | y_data = [] 67 | behavior_select_result = behavior_sql_execute(sql) 68 | for row in behavior_select_result: 69 | if (type(row[0]) == datetime.date): 70 | x_data.append(row[0].strftime("%Y-%m-%d")) 71 | y_data.append(row[1]) 72 | else: 73 | x_data.append(row[0]) 74 | y_data.append(row[1]) 75 | result = { 76 | "title": title, 77 | "x_title": x_title, 78 | "y_title": y_title, 79 | "x_data": x_data, 80 | "y_data": y_data 81 | } 82 | return json.dumps(result) 83 | 84 | def dashboard_sql_execute(sql): 85 | connection = pymysql.connect( 86 | host = cf.get('a2os_dashboard_db', 'a2os_dashboard_db_host'), 87 | port = cf.getint('a2os_dashboard_db', 'a2os_dashboard_db_port'), 88 | user = cf.get('a2os_dashboard_db', 'a2os_dashboard_db_user'), 89 | password = cf.get('a2os_dashboard_db', 'a2os_dashboard_db_password'), 90 | db = cf.get('a2os_dashboard_db', 'a2os_dashboard_db'), 91 | charset = cf.get('a2os_dashboard_db', 'a2os_dashboard_db_charset') 92 | ) 93 | cursor = connection.cursor() 94 | cursor.execute(sql) 95 | connection.commit() 96 | select_result = cursor.fetchall() 97 | cursor.close() 98 | connection.close() 99 | return select_result 100 | 101 | def behavior_sql_execute(sql): 102 | connection = pymysql.connect( 103 | host=cf.get('behavior_db', 'behavior_db_host'), 104 | port=cf.getint('behavior_db', 'behavior_db_port'), 105 | user=cf.get('behavior_db', 'behavior_db_user'), 106 | password=cf.get('behavior_db', 'behavior_db_password'), 107 | db=cf.get('behavior_db', 'behavior_db'), 108 | charset=cf.get('behavior_db', 'behavior_db_charset') 109 | ) 110 | cursor = connection.cursor() 111 | cursor.execute(sql) 112 | connection.commit() 113 | select_result = cursor.fetchall() 114 | cursor.close() 115 | connection.close() 116 | return select_result -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | 3 | Version 2.0, January 2004 4 | 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 16 | 17 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 18 | 19 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 20 | 21 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 22 | 23 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 24 | 25 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 26 | 27 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 28 | 29 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 30 | 31 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 32 | 33 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 34 | 35 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 36 | 37 | You must give any other recipients of the Work or Derivative Works a copy of this License; and 38 | You must cause any modified files to carry prominent notices stating that You changed the files; and 39 | You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 40 | If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 41 | 42 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 43 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 44 | 45 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 46 | 47 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 48 | 49 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 50 | 51 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 52 | 53 | END OF TERMS AND CONDITIONS --------------------------------------------------------------------------------