From ae7bd4964694a5d42451d5b44085b34d1631344f Mon Sep 17 00:00:00 2001 From: licong Date: Thu, 3 May 2018 15:01:02 +0800 Subject: [PATCH] fix zh_cn bugs and add conda control script --- api/curve/v1/api/data_dataName.py | 2 +- api/curve/v1/models/data_abstract.py | 6 +- api/curve/v1/utils.py | 3 +- api/requirements.txt | 16 +- control_conda.sh | 230 +++++++++++++++++++++++++++ 5 files changed, 244 insertions(+), 13 deletions(-) create mode 100755 control_conda.sh diff --git a/api/curve/v1/api/data_dataName.py b/api/curve/v1/api/data_dataName.py index 48a2b57..6be9bc0 100644 --- a/api/curve/v1/api/data_dataName.py +++ b/api/curve/v1/api/data_dataName.py @@ -125,7 +125,7 @@ def post(self, dataName): 'traceId': '', 'data': { 'data': [(point[0] * 1000, point[1]) for point in thumb], - 'name': data_name, + 'name': 'thumb', 'type': 'line' } }, ensure_ascii=False)) diff --git a/api/curve/v1/models/data_abstract.py b/api/curve/v1/models/data_abstract.py index c3053e6..68c5d89 100644 --- a/api/curve/v1/models/data_abstract.py +++ b/api/curve/v1/models/data_abstract.py @@ -7,6 +7,8 @@ :copyright: (c) 2017-2018 by Baidu, Inc. :license: Apache, see LICENSE for more details. """ +import urllib + from app import db from .common import ( auto_init, @@ -45,8 +47,8 @@ def view(self): """ return { 'id': self.id, - 'name': self.name, - 'uri': '/v1/data/%s' % self.name, + 'name': urllib.unquote(self.name.encode('utf-8')), + 'uri': '/v1/data/%s' % urllib.unquote(self.name.encode('utf-8')), 'createTime': self.create_time * 1000, 'updateTime': self.update_time * 1000, 'labelRatio': self.label_ratio, diff --git a/api/curve/v1/utils.py b/api/curve/v1/utils.py index 6e84a91..f15e24b 100644 --- a/api/curve/v1/utils.py +++ b/api/curve/v1/utils.py @@ -158,8 +158,7 @@ def str2time(time_str): :param time_str: :return: """ - return int( - time.mktime(time.strptime(time_str[:10] + ' ' + time_str[11:19], RFCTimeFormat.time_format))) + return int(time.mktime(time.strptime(time_str[:10] + ' ' + time_str[11:19], RFCTimeFormat.time_format))) E_TIME_FORMATTER = enum(unix=UnixTimeFormat, short=ShortTimeFormat, rfc=RFCTimeFormat) diff --git a/api/requirements.txt b/api/requirements.txt index 5984f22..fe6b06a 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -1,9 +1,9 @@ requests==2.12.4 # don't change the order, latest requests lib has a critical bug -Flask-SQLAlchemy==2.3.2 -Flask-RESTful==0.3.6 -Flask-Cors==3.0.3 -flask-compress==1.4.0 -jsonschema==2.6.0 -numpy==1.14.2 -GitHub-Flask==3.2.0 -uwsgi==2.0.17 +Flask-SQLAlchemy>=2.3.2 +Flask-RESTful>=0.3.6 +Flask-Cors>=3.0.3 +flask-compress>=1.4.0 +jsonschema>=2.6.0 +numpy>=1.14.0 +GitHub-Flask>=3.2.0 +uwsgi>=2.0.17 diff --git a/control_conda.sh b/control_conda.sh new file mode 100755 index 0000000..138286f --- /dev/null +++ b/control_conda.sh @@ -0,0 +1,230 @@ +#!/bin/bash + +# System: +# 1. Darwin or Linux +# Python: +# 1. version: 2.7.3+/3.1.2+ is recommended +# 2. other: +# * python should belong to current user +# * pip is required +# Node.js: +# 1. version: 4.7.0+ is recommended +# 2. other: +# * npm is required + +set -u +set -e + +cd "$(dirname "$0")" +readonly G_ROOT_DIR=`pwd` +readonly G_WEB_DIR="${G_ROOT_DIR}/web" +readonly G_API_DIR="${G_ROOT_DIR}/api" + +G_VERSION='none' +if [ -e .git ]; then + G_VERSION=`git rev-parse HEAD` +fi +G_CONDA='' + + +PS1='$' + +cutoff() { + echo "=============================================================" +} + + +help() { + echo "${0} " + exit 1 +} + +version() { + if [ ${G_VERSION}x != 'x' ]; then + cutoff + echo "local Curve version: ${G_VERSION}" + cutoff + fi +} + +check_web() { + readonly BUILD_PATH="${G_WEB_DIR}/build" + readonly BUILD_VERSION_FILE="${BUILD_PATH}/version" + BUILD_VERSION='' + if [ -e ${BUILD_VERSION_FILE} ]; then + BUILD_VERSION=`cat ${BUILD_VERSION_FILE}` + fi + readonly DEPLOY_PATH="${G_API_DIR}/curve/web" + readonly DEPLOY_VERSION_FILE="${DEPLOY_PATH}/version" + DEPLOY_VERSION='' + if [ -e ${DEPLOY_VERSION_FILE} ]; then + DEPLOY_VERSION=`cat ${DEPLOY_VERSION_FILE}` + fi + + if [ ${G_VERSION}x != 'x' -a ${G_VERSION}x == ${DEPLOY_VERSION}x ]; then + return + fi + + cutoff + echo "build web..." + if [ ${G_VERSION}x == 'x' -o ${G_VERSION}x != ${BUILD_VERSION}x ]; then + cd ${G_WEB_DIR} + npm install + npm run build + echo ${G_VERSION} > ${BUILD_VERSION_FILE} + fi + if [ -e ${DEPLOY_PATH} ]; then + rm -rf ${DEPLOY_PATH} + fi + mv ${BUILD_PATH} ${DEPLOY_PATH} + echo "web built." + cutoff +} + +check_py() { + readonly VENV_VERSION_FILE="${G_ROOT_DIR}/pylib_version" + VENV_VERSION='' + if [ -e ${VENV_VERSION_FILE} ]; then + VENV_VERSION=`cat ${VENV_VERSION_FILE}` + fi + + if [ ${G_VERSION}x != 'x' -a ${G_VERSION}x == ${VENV_VERSION}x ]; then + return + fi + + cutoff + pip install -r ${G_API_DIR}/requirements.txt + echo ${G_VERSION} > ${VENV_VERSION_FILE} + cutoff +} + +check_api() { + readonly SWAGGER_UI_DIR="${G_API_DIR}/curve/web/swagger-ui" + readonly SWAGGER_UI_VERSION_FILE="${SWAGGER_UI_DIR}/version" + SWAGGER_UI_VERSION='' + if [ -e ${SWAGGER_UI_VERSION_FILE} ]; then + SWAGGER_UI_VERSION=`cat ${SWAGGER_UI_VERSION_FILE}` + fi + + if [ ${G_VERSION}x != 'x' -a ${G_VERSION}x == ${SWAGGER_UI_VERSION}x ]; then + return + fi + + cutoff + echo "deploy api..." + cd ${G_ROOT_DIR} + pip install swagger-py-codegen==0.2.9 + swagger_py_codegen --ui --spec -s doc/web_api.yaml api -p curve + deactivate + if [ -e ${G_API_DIR}/curve/web/swagger-ui ]; then + rm -rf ${G_API_DIR}/curve/web/swagger-ui + fi + mv ${G_API_DIR}/curve/static/swagger-ui ${G_API_DIR}/curve/web/ + if [ -e ${G_API_DIR}/curve/web/static/v1 ]; then + rm -rf ${G_API_DIR}/curve/web/static/v1 + fi + mv ${G_API_DIR}/curve/static/v1 ${G_API_DIR}/curve/web/static/ + rm -rf ${G_API_DIR}/curve/static + + patch ${G_API_DIR}/curve/web/swagger-ui/index.html ${G_ROOT_DIR}/opt/swagger-ui/index.html.patch + echo ${G_VERSION} > ${SWAGGER_UI_VERSION_FILE} + echo "api deployed." + cutoff +} + +check_path() { + mkdir -p ${G_API_DIR}/log +} + +check() { + check_web + check_py + check_api + check_path +} + +start() { + if [ -e ${G_API_DIR}/uwsgi.pid ]; then + PID=`cat ${G_API_DIR}/uwsgi.pid` + if [ `ps -ef | fgrep uwsgi | fgrep ${PID} | wc -l` -gt 0 ]; then + echo "Curve is running." + return + fi + fi + check + if [ `ps -ef | fgrep uwsgi | fgrep -v 'grep' | wc -l` -gt 0 ]; then + ps -ef | fgrep uwsgi | fgrep -v 'grep' | awk '{ print $2 }' | xargs kill -9 + fi + echo "start Curve..." + cd ${G_API_DIR} + uwsgi uwsgi.ini + echo "Curve started." +} + +stop() { + echo "stop Curve..." + cd ${G_API_DIR} + [ -e uwsgi.pid ] && uwsgi --stop uwsgi.pid + echo "Curve stopped." +} + +reload() { + check + if [ -e ${G_API_DIR}/uwsgi.pid ]; then + PID=`cat ${G_API_DIR}/uwsgi.pid` + if [ `ps -ef | fgrep uwsgi | fgrep ${PID} | wc -l` -gt 0 ]; then + echo "reload Curve..." + cd ${G_API_DIR} + uwsgi --reload uwsgi.pid + echo "Curve reloaded." + return + fi + fi + echo "clean Curve..." + if [ `ps -ef | fgrep uwsgi | fgrep -v 'grep' | wc -l` -gt 0 ]; then + ps -ef | fgrep uwsgi | fgrep -v 'grep' | awk '{ print $2 }' | xargs kill -9 + fi + echo "start Curve..." + cd ${G_API_DIR} + uwsgi uwsgi.ini + echo "Curve reloaded." +} + +terminate() { + echo "terminate Curve..." + if [ `ps -ef | fgrep uwsgi | fgrep -v 'grep' | wc -l` -gt 0 ]; then + ps -ef | fgrep uwsgi | fgrep -v 'grep' | awk '{ print $2 }' | xargs kill -9 + echo "Curve terminated." + fi + echo "Curve is not running." +} + +case "${1}" in +check) + version + check + ;; +start) + version + start + ;; +stop) + stop + ;; +reload) + version + reload + ;; +terminate) + terminate + ;; +help) + help + ;; +version) + version + ;; +*) + help + ;; +esac