forked from ydf0509/distributed_framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
3,123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
# 以下为配置,请按需修改。 | ||
|
||
# MONGO_CONNECT_URL = f'mongodb://yourname:[email protected]:27017/admin' | ||
MONGO_CONNECT_URL = f'mongodb://192.168.6.131:27017/' | ||
# | ||
# RABBITMQ_USER = 'rabbitmq_user' | ||
# RABBITMQ_PASS = 'rabbitmq_pass' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Author : ydf | ||
# @Time : 2019/9/18 0018 14:46 | ||
import datetime | ||
import json | ||
|
||
from flask import render_template, Flask, request, url_for, jsonify, flash, redirect | ||
from flask_bootstrap import Bootstrap | ||
from flask_wtf import FlaskForm | ||
from wtforms import StringField, PasswordField, BooleanField, SubmitField | ||
from wtforms.validators import DataRequired, Length | ||
from flask_login import login_user, logout_user, login_required, LoginManager, UserMixin | ||
|
||
from function_scheduling_distributed_framework import nb_print | ||
from function_result_web.functions import get_cols, query_result, get_speed, Statistic | ||
|
||
app = Flask(__name__) | ||
app.secret_key = 'mtfy54321' | ||
app.config['JSON_AS_ASCII'] = False | ||
bootstrap = Bootstrap(app) | ||
login_manager = LoginManager() | ||
login_manager.login_view = 'login' | ||
login_manager.login_message_category = 'info' | ||
login_manager.login_message = 'Access denied.' | ||
login_manager.init_app(app) | ||
|
||
class User(UserMixin): | ||
pass | ||
|
||
users = [ | ||
{'id':'Tom', 'user_name': 'Tom', 'password': '111111'}, | ||
{'id':'Michael', 'user_name': 'Michael', 'password': '123456'}, | ||
{'id': 'user', 'user_name': 'user', 'password': 'mtfy123'} | ||
] | ||
|
||
def query_user(user_name): | ||
for user in users: | ||
if user_name == user['user_name']: | ||
return user | ||
|
||
@login_manager.user_loader | ||
def load_user(user_id): | ||
if query_user(user_id) is not None: | ||
curr_user = User() | ||
curr_user.id = user_id | ||
return curr_user | ||
|
||
class LoginForm(FlaskForm): | ||
user_name = StringField(u'用户名', validators=[DataRequired(), Length(3, 64)]) | ||
password = PasswordField(u'密码', validators=[DataRequired(), Length(3, 64)]) | ||
remember_me = BooleanField(u'记住我') | ||
|
||
|
||
@app.route('/login', methods=['GET', 'POST']) | ||
def login(): | ||
form = LoginForm() | ||
if request.method == 'POST': | ||
|
||
nb_print(form.validate()) | ||
nb_print(form.password.data) | ||
nb_print(form.user_name.data) | ||
nb_print(form.user_name.errors) | ||
nb_print(form.password.errors) | ||
if form.validate_on_submit(): | ||
user = query_user(form.user_name.data) | ||
if user is not None and request.form['password'] == user['password']: | ||
curr_user = User() | ||
curr_user.id = form.user_name.data | ||
|
||
# 通过Flask-Login的login_user方法登录用户 | ||
nb_print(form.remember_me.data) | ||
login_user(curr_user,remember=form.remember_me.data,duration=datetime.timedelta(days=7)) | ||
|
||
return redirect(url_for('index')) | ||
|
||
flash('用户名或密码错误',category='error') | ||
|
||
# if form.user_name.data == 'user' and form.password.data == 'mtfy123': | ||
# login_user(form.user_name.data, form.remember_me.data) | ||
# return redirect(url_for('index')) | ||
# else: | ||
# flash('账号或密码错误',category='error') | ||
# return render_template('login4.html', form=form) | ||
|
||
return render_template('login.html', form=form) | ||
|
||
@app.route("/logout") | ||
@login_required | ||
def logout(): | ||
logout_user() | ||
return redirect(url_for('login')) | ||
|
||
@app.route('/') | ||
@login_required | ||
def index(): | ||
return render_template('index.html') | ||
|
||
@app.route('/query_cols') | ||
@login_required | ||
def query_cols_view(): | ||
nb_print(request.args) | ||
return jsonify(get_cols(request.args.get('col_name_search'))) | ||
|
||
|
||
@app.route('/query_result') | ||
@login_required | ||
def query_result_view(): | ||
nb_print(request.values.to_dict()) | ||
return jsonify(query_result(**request.values.to_dict())) | ||
|
||
|
||
@app.route('/speed_stats') | ||
@login_required | ||
def speed_stats(): | ||
return jsonify(get_speed(**request.values.to_dict())) | ||
|
||
@app.route('/speed_statistic_for_echarts') | ||
@login_required | ||
def speed_statistic_for_echarts(): | ||
stat = Statistic(request.args.get('col_name')) | ||
stat.build_result() | ||
return jsonify(stat.result) | ||
|
||
|
||
if __name__ == '__main__': | ||
app.jinja_env.auto_reload = True | ||
with app.test_request_context(): | ||
print(url_for('query_cols_view')) | ||
|
||
app.run(debug=True, threaded=True,host='0.0.0.0',port=27018) | ||
#gunicorn -w 9 -k gevent --bind 0.0.0.0:27018 function_result_web.app:app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Author : ydf | ||
# @Time : 2019/9/19 0019 9:48 | ||
import datetime | ||
import json | ||
from pprint import pprint | ||
import time | ||
from flask import jsonify | ||
|
||
from function_scheduling_distributed_framework import nb_print | ||
from function_scheduling_distributed_framework.utils import time_util, decorators, LoggerMixin | ||
from function_scheduling_distributed_framework.utils.mongo_util import MongoMixin | ||
# from test_frame.my_patch_frame_config import do_patch_frame_config | ||
# | ||
# do_patch_frame_config() | ||
|
||
db = MongoMixin().mongo_db_task_status | ||
|
||
|
||
def get_cols(col_name_search: str): | ||
if not col_name_search: | ||
collection_name_list = db.collection_names() | ||
else: | ||
collection_name_list = [collection_name for collection_name in db.collection_names() if col_name_search in collection_name] | ||
return [{'collection_name': collection_name, 'count': db.get_collection(collection_name).find().count()} for collection_name in collection_name_list] | ||
# for collection_name in collection_list: | ||
# if col_name_search in collection_name: | ||
# print (collection,db[collection].find().count()) | ||
|
||
|
||
def query_result(col_name, start_time, end_time, is_success, function_params: str, page, ): | ||
condition = { | ||
'insert_time': {'$gt': time_util.DatetimeConverter(start_time).datetime_obj, | ||
'$lt': time_util.DatetimeConverter(end_time).datetime_obj}, | ||
} | ||
# condition = { | ||
# 'insert_time_str': {'$gt': start_time, | ||
# '$lt': end_time}, | ||
# } | ||
if is_success in ('2', 2, True): | ||
condition.update({"success": True}) | ||
elif is_success in ('3', 3, False): | ||
condition.update({"success": False}) | ||
if function_params.strip(): | ||
condition.update({'params_str': {'$regex': function_params.strip()}}) | ||
nb_print(col_name) | ||
nb_print(condition) | ||
# results = list(db.get_collection(col_name).find(condition, ).sort([('insert_time', -1)]).skip(int(page) * 100).limit(100)) | ||
with decorators.TimerContextManager(): | ||
results = list(db.get_collection(col_name).find(condition, {'insert_time': 0, 'utime': 0}).skip(int(page) * 100).limit(100)) | ||
# nb_print(result) | ||
return results | ||
|
||
|
||
def get_speed(col_name, start_time, end_time): | ||
condition = { | ||
'insert_time': {'$gt': time_util.DatetimeConverter(start_time).datetime_obj, | ||
'$lt': time_util.DatetimeConverter(end_time).datetime_obj}, | ||
} | ||
# condition = { | ||
# 'insert_time_str': {'$gt': time_util.DatetimeConverter(time.time() - 60).datetime_str}, | ||
# } | ||
nb_print(condition) | ||
with decorators.TimerContextManager(): | ||
success_num = db.get_collection(col_name).count({**{'success': True}, **condition}) | ||
fail_num = db.get_collection(col_name).count({**{'success': False}, **condition}) | ||
qps = (success_num + fail_num) / (time_util.DatetimeConverter(end_time).timestamp - time_util.DatetimeConverter(start_time).timestamp) | ||
return {'success_num': success_num, 'fail_num': fail_num, 'qps': round(qps, 1)} | ||
|
||
|
||
class Statistic(LoggerMixin): | ||
def __init__(self, col_name): | ||
self.col = db.get_collection(col_name) | ||
self.result = {'recent_10_days': {'time_arr': [], 'count_arr': []}, | ||
'recent_24_hours': {'time_arr': [], 'count_arr': []}, | ||
'recent_60_minutes': {'time_arr': [], 'count_arr': []}, | ||
'recent_60_seconds': {'time_arr': [], 'count_arr': []}} | ||
|
||
def statistic_by_period(self, t_start: str, t_end: str): | ||
return self.col.count({'insert_time': {'$gt': time_util.DatetimeConverter(t_start).datetime_obj, | ||
'$lt': time_util.DatetimeConverter(t_end).datetime_obj}}) | ||
|
||
def build_result(self): | ||
with decorators.TimerContextManager(): | ||
for i in range(10): | ||
t1 = datetime.datetime.now() + datetime.timedelta(days=-(9 - i)) | ||
t2 = datetime.datetime.now() + datetime.timedelta(days=-(8 - i)) | ||
self.result['recent_10_days']['time_arr'].append(time_util.DatetimeConverter(t1).date_str) | ||
count = self.statistic_by_period(time_util.DatetimeConverter(t1).date_str + ' 00:00:00', | ||
time_util.DatetimeConverter(t2).date_str + ' 00:00:00') | ||
self.result['recent_10_days']['count_arr'].append(count) | ||
|
||
for i in range(0, 24): | ||
t1 = datetime.datetime.now() + datetime.timedelta(hours=-(23 - i)) | ||
t2 = datetime.datetime.now() + datetime.timedelta(hours=-(22 - i)) | ||
self.result['recent_24_hours']['time_arr'].append(t1.strftime('%Y-%m-%d %H:00:00')) | ||
# hour1_str = f'0{i}' if i < 10 else i | ||
count = self.statistic_by_period(t1.strftime('%Y-%m-%d %H:00:00'), | ||
t2.strftime('%Y-%m-%d %H:00:00')) | ||
self.result['recent_24_hours']['count_arr'].append(count) | ||
|
||
for i in range(0, 60): | ||
t1 = datetime.datetime.now() + datetime.timedelta(minutes=-(59 - i)) | ||
t2 = datetime.datetime.now() + datetime.timedelta(minutes=-(58 - i)) | ||
self.result['recent_60_minutes']['time_arr'].append(t1.strftime('%Y-%m-%d %H:%M:00')) | ||
count = self.statistic_by_period(t1.strftime('%Y-%m-%d %H:%M:00'), | ||
t2.strftime('%Y-%m-%d %H:%M:00')) | ||
self.result['recent_60_minutes']['count_arr'].append(count) | ||
|
||
for i in range(0, 60): | ||
t1 = datetime.datetime.now() + datetime.timedelta(seconds=-(59 - i)) | ||
t2 = datetime.datetime.now() + datetime.timedelta(seconds=-(58 - i)) | ||
self.result['recent_60_seconds']['time_arr'].append(t1.strftime('%Y-%m-%d %H:%M:%S')) | ||
count = self.statistic_by_period(t1.strftime('%Y-%m-%d %H:%M:%S'), | ||
t2.strftime('%Y-%m-%d %H:%M:%S')) | ||
self.result['recent_60_seconds']['count_arr'].append(count) | ||
|
||
|
||
if __name__ == '__main__': | ||
print(get_cols('4')) | ||
# pprint(query_result('queue_test54_task_status', '2019-09-15 00:00:00', '2019-09-25 00:00:00', True, '999', 0)) | ||
# print(json.dumps(query_result(**{'col_name': 'queue_test56', 'start_time': '2019-09-18 16:03:29', 'end_time': '2019-09-21 16:03:29', 'is_success': '1', 'function_params': '', 'page': '0'}))[:1000]) | ||
# nb_print(get_speed_last_minute('queue_test54')) | ||
|
||
# nb_print(get_speed('queue_test56', '2019-09-18 16:03:29', '2019-09-23 16:03:29')) | ||
stat = Statistic('queue_test56') | ||
stat.build_result() | ||
nb_print(stat.result) | ||
|
||
|
Oops, something went wrong.