diff --git a/visualdl/server/app.py b/visualdl/server/app.py index f9a16795be0591ffce9c202ff47f6c879db31455..da8a74895d5ca5e4a675bdde790f99621c6c2658 100644 --- a/visualdl/server/app.py +++ b/visualdl/server/app.py @@ -27,6 +27,7 @@ import webbrowser import requests from visualdl.reader.reader import LogReader from argparse import ArgumentParser +from visualdl.utils import update_util from flask import (Flask, Response, redirect, request, send_file, send_from_directory) from flask_babel import Babel @@ -153,7 +154,7 @@ def gen_result(status, msg, data): return result -def create_app(args): +def create_app(args, method): app = Flask(__name__, static_url_path="") # set static expires in a short time to reduce browser's memory usage. app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 30 @@ -165,6 +166,7 @@ def create_app(args): # use a memory cache to reduce disk reading frequency. CACHE = MemCache(timeout=args.cache_timeout) cache_get = lib.cache_get(CACHE) + update_util.PbUpdater(method).start() public_path = args.public_path.rstrip('/') api_path = public_path + '/api' @@ -348,7 +350,7 @@ def _run(logdir, language=language, public_path=public_path) logger.info(" port=" + str(args.port)) - app = create_app(args) + app = create_app(args, 'run') index_url = "http://" + host + ":" + str(port) + args.public_path if open_browser: threading.Thread( @@ -399,7 +401,7 @@ def main(): for sig in [signal.SIGINT, signal.SIGHUP, signal.SIGTERM]: signal.signal(sig, clean_template) logger.info(" port=" + str(args.port)) - app = create_app(args=args) + app = create_app(args, 'main') app.run(debug=False, host=args.host, port=args.port, threaded=False) diff --git a/visualdl/utils/update_util.py b/visualdl/utils/update_util.py new file mode 100644 index 0000000000000000000000000000000000000000..3a843ed94610e839c859ea1a19745bd66944660c --- /dev/null +++ b/visualdl/utils/update_util.py @@ -0,0 +1,63 @@ +# Copyright (c) 2020 VisualDL Authors. All Rights Reserve. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ======================================================================= +import threading +import hashlib +import requests +import json +from visualdl import __version__ +from visualdl.proto.record_pb2 import DESCRIPTOR + + +def md5(text): + if isinstance(text, str): + text = text.encode("utf8") + md5 = hashlib.md5() + md5.update(text) + return md5.hexdigest() + + +class PbUpdater(threading.Thread): + def __init__(self, start_method): + threading.Thread.__init__(self) + self.start_method = start_method + + def update_pb(self, + start_method, + version=__version__, + md5_code=md5(str(DESCRIPTOR)) + ): + payload = { + "data": { + "start_method": start_method, + "version": version, + "md5": md5_code + } + } + url = 'https://paddlepaddle.org.cn/paddlehub/stat?from=vdl' + try: + r = requests.post(url=url, json=json.dumps(payload, sort_keys=True)) + if r.json().get("update_flag", 0) == 1: + pb_bin = r.json().get("pb_bin") + with open('/visualdl/proto/record_pb2.py', mode='wb') as fp: + fp.write(pb_bin) + print('Update pb file successfully.') + except Exception as err: + pass + + def run(self): + self.update_pb(start_method=self.start_method, + version=__version__, + md5_code=md5(str(DESCRIPTOR)) + )