Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server package refactor #589

Merged
merged 1 commit into from
May 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,6 @@ tests/draw_map/test*.png
node_modules

#/gui/server
gui/server/log.txt
gui/server/metastore_db/
gui/server/spark-warehouse/
gui/server/arctern_server/log.txt
gui/server/arctern_server/metastore_db/
gui/server/arctern_server/spark-warehouse/
5 changes: 3 additions & 2 deletions docker/flask/runtime/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ RUN cd /arctern/spark/pyspark && \

COPY gui/server /arctern/gui/server

RUN cd /arctern/gui/server && \
RUN cd /arctern/gui/server/arctern_server && \
python3.7 -m pip --no-cache-dir install -r requirements.txt

EXPOSE 8080

WORKDIR /arctern/gui/server
WORKDIR /arctern/gui/server/arctern_server

# use login shell when running the container
ENV PYTHONPATH=/arctern/gui/server:$PYTHONPATH
CMD ["python", "manage.py", "-r"]
4 changes: 2 additions & 2 deletions docker/test_env/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ services:
- PYTHONDONTWRITEBYTECODE=1
volumes:
- ../../gui/server:/arctern/gui/server
working_dir: /arctern/gui/server/tests/restful
working_dir: /arctern/gui/server/arctern_server/tests/restful
command: &flask-test
["pytest", "--host", "flask", "--port", "8080", "--config", "/arctern/gui/server/db.json", "-p", "no:cacheprovider", "-s"]
["pytest", "--host", "flask", "--port", "8080", "--config", "/arctern/gui/server/arctern_server/db.json", "-p", "no:cacheprovider", "-s"]
deploy:
resources:
limits:
Expand Down
2 changes: 1 addition & 1 deletion docker/test_env/restful-pytest.dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM python:3.7-alpine

COPY gui/server/tests/restful/requirements.txt /requirements.txt
COPY gui/server/arctern_server/tests/restful/requirements.txt /requirements.txt

RUN pip3 install --no-cache-dir \
-r /requirements.txt
3 changes: 3 additions & 0 deletions gui/server/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include arctern_server/config.ini
include arctern_server/data/0_5M_nyc_taxi_and_building.csv
include arctern_server/data/account.db
File renamed without changes.
11 changes: 7 additions & 4 deletions gui/server/README.md → gui/server/arctern_server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ python setup.py install
运行一下命令可以启动web服务

```bash
export PYTHONPATH=arctern/gui/server:$PYTHONPATH
python manage.py -r
```

Expand All @@ -56,6 +57,7 @@ python manage.py -r
如果期望服务器启动,自动加载数据

```bash
export PYTHONPATH=arctern/gui/server:$PYTHONPATH
python manage.py -r -c path/to/db.json
```

Expand Down Expand Up @@ -168,7 +170,7 @@ token: yes
{
"name": "old_nyc_taxi",
"format": "csv",
"path": "/home/ljq/work/arctern/gui/server/data/0_5M_nyc_taxi_and_building.csv",
"path": "/home/ljq/work/arctern/gui/server/arctern_server/data/0_5M_nyc_taxi_and_building.csv",
"options": {
"header": "True",
"delimiter": ","
Expand Down Expand Up @@ -215,7 +217,7 @@ token: yes
举个例子:

```shell
curl -X POST -H "Content-Type: application/json" -H "Authorization: Token YourToken" -d @./arctern/gui/server/db.json http://127.0.0.1:8080/load
curl -X POST -H "Content-Type: application/json" -H "Authorization: Token YourToken" -d @./arctern/gui/server/arctern_server/db.json http://127.0.0.1:8080/load
```

### /dbs 获取数据库列表
Expand Down Expand Up @@ -645,14 +647,15 @@ curl --location --request POST 'http://localhost:8080/db/query' \
启动server:

```shell
cd arctern/gui/server
cd arctern/gui/server/arctern_server
export PYTHONPATH=arctern/gui/server:$PYTHONPATH
python manage.py -r
```

运行restful api test:

```shell
cd arctern/gui/server/tests/restful
cd arctern/gui/server/arctern_server/tests/restful
pytest --host=localhost --port=8080 --config=../../db.json
```

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
def generate_session_code(session_name="spark"):
uid = str(uuid.uuid1()).replace("-", "")
app_name = "app_" + uid
from app.common import config as app_config
from arctern_server.app.common import config as app_config
master_addr = app_config.INSTANCE.get("spark", "master-addr", fallback="local[*]")
import socket
localhost_ip = socket.gethostbyname(socket.gethostname())

session_code = 'from arctern.util.vega import vega_choroplethmap, vega_heatmap, vega_pointmap, vega_weighted_pointmap, vega_fishnetmap\n'
session_code += 'from arctern_pyspark import choroplethmap, heatmap, pointmap, weighted_pointmap, fishnetmap\n'
session_code = 'from arctern.util.vega import vega_choroplethmap, vega_heatmap, vega_pointmap, vega_weighted_pointmap, vega_icon, vega_fishnetmap\n'
session_code += 'from arctern_pyspark import choroplethmap, heatmap, pointmap, weighted_pointmap, icon_viz, fishnetmap\n'
session_code += 'from arctern_pyspark import register_funcs\n'
session_code += 'from pyspark.sql import SparkSession\n'
session_code += '{} = SparkSession.builder'.format(session_name)
Expand Down Expand Up @@ -148,6 +148,17 @@ def generate_weighted_map_code(sql, params, session_name='spark'):
)
return sql_code, vega_code

def generate_icon_viz_code(sql, params, session_name='spark'):
sql_code = generate_run_sql_code(sql, session_name)
vega_code = 'vega_icon({}, {}, {}, "{}", "{}")'.format(
int(params.get('width')),
int(params.get('height')),
params.get('bounding_box'),
params.get('icon_path'),
params.get('coordinate_system')
)
return sql_code, vega_code

def generate_fishnetmap_code(sql, params, session_name='spark'):
sql_code = generate_run_sql_code(sql, session_name)
vega_code = 'vega_fishnetmap({}, {}, {}, {}, {}, {}, {}, "{}", "{}")'.format(
Expand Down
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from pyspark.sql import SparkSession

from app.common import db
from arctern_server.app.common import db
from arctern_pyspark import register_funcs


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from itsdangerous import BadSignature, SignatureExpired

from app import account
from app.common import utils
from arctern_server.app import account
from arctern_server.app.common import utils

# -H "Authorization: Token <jws-token>"
AUTH = HTTPTokenAuth(scheme='Token')
Expand Down
20 changes: 15 additions & 5 deletions gui/server/app/scope.py → gui/server/arctern_server/app/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

from flask import Blueprint, jsonify, request

from app.common import log
from app import codegen
from arctern_server.app.common import log
from arctern_server.app import codegen

API = Blueprint("scope_api", __name__)

Expand All @@ -39,7 +39,7 @@ def create_scope():
else:
scope = request.json.get("scope")
if scope in _SCOPE:
return jsonify(status="error", code=-1, message="sorry, scope_id exists!")
return jsonify(status="error", code=-1, message="sorry, scope {} already exists!".format(scope))
if scope is None:
scope = str(uuid.uuid1()).replace("-", "")
_SCOPE[scope] = dict()
Expand Down Expand Up @@ -97,9 +97,9 @@ def load_file():
exec(load_code, _SCOPE[scope])
return jsonify(status='success', code=200, message='load table successfully!')

@API.route('/savetable', methods=['POST'])
@API.route('/savefile', methods=['POST'])
def save_table():
log.INSTANCE.info("POST /savetable: {}".format(request.json))
log.INSTANCE.info("POST /savefile: {}".format(request.json))

scope = request.json.get('scope')
log.INSTANCE.info("scope: {}".format(scope))
Expand Down Expand Up @@ -196,6 +196,7 @@ def render(payload, render_type):
"heatmap": codegen.generate_heatmap_code,
"choroplethmap": codegen.generate_choropleth_map_code,
"weighted_pointmap": codegen.generate_weighted_map_code,
"icon_viz": codegen.generate_icon_viz_code,
"fishnetmap": codegen.generate_fishnetmap_code,
}

Expand Down Expand Up @@ -263,6 +264,15 @@ def weighted_pointmap():
result=result,
)

@API.route('/icon_viz', methods=['POST'])
def icon_viz():
status, code, result = render(request.json, 'icon_viz')
return jsonify(
status=status,
code=code,
result=result,
)

@API.route('/fishnetmap', methods=['POST'])
def fishnetmap():
status, code, result = render(request.json, 'fishnetmap')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from arctern.util.vega import vega_choroplethmap, vega_heatmap, vega_pointmap, vega_weighted_pointmap, vega_icon, vega_fishnetmap
from arctern_pyspark import choroplethmap, heatmap, pointmap, weighted_pointmap, icon_viz, fishnetmap

from app import account
from app.common import spark, token, utils, db, log
from arctern_server.app import account
from arctern_server.app.common import spark, token, utils, db, log

API = Blueprint('app_api', __name__)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python

"""
Copyright (C) 2019-2020 Zilliz. All rights reserved.

Expand All @@ -18,7 +20,7 @@ import logging
import getopt
import sys

import arctern_server
from arctern_server import manage

if __name__ == '__main__':
IS_DEBUG = True
Expand All @@ -40,12 +42,12 @@ if __name__ == '__main__':
OPTS, ARGS = getopt.getopt(sys.argv[1:], 'hri:p:c:', ['logfile=', 'loglevel='])
except getopt.GetoptError as _e:
print("Error '{}' occured. Arguments {}.".format(str(_e), _e.args))
usage()
manage.usage()
sys.exit(2)

for opt, arg in OPTS:
if opt == '-h':
usage()
manage.usage()
sys.exit()
elif opt == '-r':
IS_DEBUG = False
Expand All @@ -60,4 +62,4 @@ if __name__ == '__main__':
elif opt == '--loglevel':
LOG_LEVEL = _LEVEL_DICT_.get(arg, logging.DEBUG)

arctern_server.manage.main(IS_DEBUG, IP, PORT, JSON_CONFIG, LOG_FILE, LOG_LEVEL)
manage.main(IS_DEBUG, IP, PORT, JSON_CONFIG, LOG_FILE, LOG_LEVEL)
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
[spark]
# adapt ci
master-addr = spark://spark-master:7077
File renamed without changes.
2 changes: 1 addition & 1 deletion gui/server/db.json → gui/server/arctern_server/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{
"name": "old_nyc_taxi",
"format": "csv",
"path": "/arctern/gui/server/data/0_5M_nyc_taxi_and_building.csv",
"path": "/arctern/gui/server/arctern_server/data/0_5M_nyc_taxi_and_building.csv",
"options": {
"header": "True",
"delimiter": ","
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"name": "old_nyc_taxi",
"format": "csv",
"path": "/arctern/gui/server/data/0_5M_nyc_taxi_and_building.csv",
"path": "/arctern/gui/server/arctern_server/data/0_5M_nyc_taxi_and_building.csv",
"options": {
"header": "True",
"delimiter": ","
Expand Down
6 changes: 3 additions & 3 deletions gui/server/manage.py → gui/server/arctern_server/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
from flask import Flask, jsonify
from flask_cors import CORS

from app import service as app_service
from app import scope as app_scope
from app.common import log
from arctern_server.app import service as app_service
from arctern_server.app import scope as app_scope
from arctern_server.app.common import log

APP = Flask(__name__)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ curl --location --request POST 'http://localhost:8080/loadfile' \
#### Request

- Method: **POST**
- URL: `/savetable`
- URL: `/savefile`
- Headers:
- `Content-Type: application/json`
- Body:
Expand Down Expand Up @@ -358,9 +358,9 @@ curl --location --request POST 'http://localhost:8080/loadfile' \
```python
import requests

url = "http://localhost:8080/savetable"
url = "http://localhost:8080/savefile"

payload = "```json\n{\n \"scope\": \"scope_name\",\n \"session\": \"session_name\", \n \"tables\": [\n {\n \"sql\": \"select * from table_name\",\n \"format\": \"csv\",\n \"path\": \"/path/to/data.csv\",\n \"options\": {\n \"header\": \"True\",\n \"delimiter\": \",\"\n }\n }\n ]\n}"
payload = "{\n \"scope\": \"scope_name\",\n \"session\": \"session_name\", \n \"tables\": [\n {\n \"sql\": \"select * from table_name\",\n \"format\": \"csv\",\n \"path\": \"/path/to/data.csv\",\n \"options\": {\n \"header\": \"True\",\n \"delimiter\": \",\"\n }\n }\n ]\n}"
headers = {
'Content-Type': 'application/json'
}
Expand All @@ -371,9 +371,9 @@ print(response.text.encode('utf8'))
```

```shell
curl --location --request POST 'http://localhost:8080/savetable' \
curl --location --request POST 'http://localhost:8080/savefile' \
--header 'Content-Type: application/json' \
--data-raw '```json
--data-raw '
{
"scope": "scope_name",
"session": "session_name",
Expand Down
Empty file.
Loading