diff --git a/application/config/config.yaml b/application/config/config.yaml index 11021a03f66b76788737e8d0ebdb4b43d3df9412..ea90305a66f3ecdfbeaaff46e7390d9ee479ce7d 100644 --- a/application/config/config.yaml +++ b/application/config/config.yaml @@ -9,7 +9,7 @@ Database: Host: localhost Port: 5432 DB: elp - TableName: users + TableName: logs Elasticsearch: Host: 172.28.5.39 diff --git a/application/dao/__init__.py b/application/dao/__init__.py index 2e9edd7553462cf0600d98e21effc9899e117a26..2ae0f9efc42c1f6cce5af1f934f2ba69c8c210cb 100644 --- a/application/dao/__init__.py +++ b/application/dao/__init__.py @@ -10,5 +10,4 @@ # @Description : """ -from .user import UserDao from .log import LogDao diff --git a/application/dao/log.py b/application/dao/log.py index 66827f726bf39c98dcbac6fd2a7633a5cbde0b07..9d0a5e60d4b68a860928dbf724040df25b3228bf 100644 --- a/application/dao/log.py +++ b/application/dao/log.py @@ -13,10 +13,9 @@ from loguru import logger -from application.utils import ElasticsearchUtil from application.libs.helper import MySQLHelper -from application.utils import ParseUtil from application.models import Log +from application.utils import ElasticsearchUtil, ParseUtil class LogDao: diff --git a/application/dao/user.py b/application/dao/user.py deleted file mode 100644 index ed1571c60ca31c711124153ea0678e72bd3f4821..0000000000000000000000000000000000000000 --- a/application/dao/user.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -# @Version : Python 3.11.4 -# @Software : Sublime Text 4 -# @Author : StudentCWZ -# @Email : StudentCWZ@outlook.com -# @Date : 2023/10/28 23:56 -# @File : user.py -# @Description : -""" - -from application.extensions.init_sqlalchemy import db -from application.models.user import User - - -class UserDao: - @classmethod - def get_all(cls): - return User.query.all() - - @classmethod - def get_by_id(cls, user_id: int): - return User.query.get(user_id) - - @classmethod - def create(cls, username: str, password: str, email: str): - new_user = User(username=username, email=email, password=password) - db.session.add(new_user) - db.session.commit() - return new_user diff --git a/application/extensions/init_sqlalchemy.py b/application/extensions/init_sqlalchemy.py index 14be62e5cc8b996619870797f1bacd053d1b19eb..1b17461db59e1ee6a59700ae769a316d9377bebc 100644 --- a/application/extensions/init_sqlalchemy.py +++ b/application/extensions/init_sqlalchemy.py @@ -42,17 +42,6 @@ def init_database(app: Flask) -> None: with app.app_context(): db.session.execute(text(f""" CREATE TABLE IF NOT EXISTS {cfg.TableName} ( - id SERIAL NOT NULL, - username VARCHAR(80) NOT NULL, - email VARCHAR(120) NOT NULL, - password VARCHAR(255) NOT NULL, - active BOOLEAN, - created_at TIMESTAMP WITH TIME ZONE NOT NULL, - PRIMARY KEY (id, created_at) - ) PARTITION BY RANGE (created_at); - """)) - db.session.execute(text(""" - CREATE TABLE IF NOT EXISTS logs ( id SERIAL NOT NULL, date_time TIMESTAMP WITH TIME ZONE NOT NULL, uuid varchar(100) DEFAULT NULL, diff --git a/application/libs/tasks/task.py b/application/libs/tasks/task.py index 5b3913091efa1f4437fa71ba162b37db42a9bac8..8c3eac097cca83f77c9f8b0b5351e6eb5c352056 100644 --- a/application/libs/tasks/task.py +++ b/application/libs/tasks/task.py @@ -12,7 +12,6 @@ import datetime from application.extensions.init_sqlalchemy import db -from application.models import User from application.libs.helper import MySQLHelper @@ -33,9 +32,3 @@ def task(): } with MySQLHelper(**options) as helper: print(helper.execute(cfg.Sql)) - print("create user") - # user = User(username="CWzz", email="Stuu@outlook.com", password="qwe!2345", active=True, - # created_at=datetime.datetime.utcnow()) - # user.save() - - print("created user admin") diff --git a/application/models/__init__.py b/application/models/__init__.py index 661b9f5e95249611c44f78fa1aaf5864ac98a3b1..c854a9f6e842bc72d380fe1ed751ac449656d2b9 100644 --- a/application/models/__init__.py +++ b/application/models/__init__.py @@ -10,5 +10,4 @@ # @Description : """ -from .user import User from .log import Log diff --git a/application/models/log.py b/application/models/log.py index 526b131831e886ab042ded71b6aa275a72a8fc18..192b880f65d07d0a3ebfd86114e81a99ab3a35d1 100644 --- a/application/models/log.py +++ b/application/models/log.py @@ -69,19 +69,6 @@ class Log(db.Model): _dic[key] = value return _dic - def save(self): - partition_date = self.date_time.strftime('%Y_%m') - partition_name = f'{self.__tablename__}_{partition_date}' - - with db.session.begin_nested(): - db.session.execute(text(f""" - CREATE TABLE IF NOT EXISTS {partition_name} PARTITION OF {self.__tablename__} - FOR VALUES FROM ('{self.date_time.strftime('%Y-%m-01')}') TO - ('{self.date_time.strftime('%Y-%m-01')}'::date + interval '1 month'); - """)) - db.session.add(self) - db.session.commit() - @classmethod def batch_save(cls, objects): with db.session.begin_nested(): diff --git a/application/models/user.py b/application/models/user.py deleted file mode 100644 index 94d8433596646d4d01890114206fc87c5ab32cb1..0000000000000000000000000000000000000000 --- a/application/models/user.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -# @Version : Python 3.11.4 -# @Software : Sublime Text 4 -# @Author : StudentCWZ -# @Email : StudentCWZ@outlook.com -# @Date : 2023/10/28 22:12 -# @File : user.py -# @Description : -""" - -import os -from sqlalchemy import text -from sqlalchemy.ext.hybrid import hybrid_property - -from application.extensions.init_bcrypt import bcrypt -from application.extensions.init_sqlalchemy import db - - -class User(db.Model): - """Basic user model""" - __tablename__ = os.environ.get('TableName', 'users') - - id = db.Column(db.Integer, primary_key=True) - username = db.Column(db.String(80), unique=True, nullable=False) - email = db.Column(db.String(80), unique=True, nullable=False) - _password = db.Column("password", db.String(255), nullable=False) - active = db.Column(db.Boolean, default=True) - created_at = db.Column(db.DateTime, nullable=False) - - @hybrid_property - def password(self): - return self._password - - @password.setter - def password(self, password: str): - self._password = bcrypt.generate_password_hash(password).decode('utf-8') - - def check_password(self, password: str): - # 判断传过来的密码是否与数据库存的密码一致 - return bcrypt.check_password_hash(self._password, password) - - def to_dict(self) -> dict: - """object to dict""" - return { - 'id': self.id, - 'username': self.username, - 'email': self.email, - } - - def __repr__(self) -> str: - return f'' - - def save(self): - partition_date = self.created_at.strftime('%Y_%m') - partition_name = f'{self.__tablename__}_{partition_date}' - - with db.session.begin_nested(): - db.session.execute(text(f""" - CREATE TABLE IF NOT EXISTS {partition_name} PARTITION OF {self.__tablename__} - FOR VALUES FROM ('{self.created_at.strftime('%Y-%m-01')}') TO - ('{self.created_at.strftime('%Y-%m-01')}'::date + interval '1 month'); - """)) - db.session.add(self) - db.session.commit() diff --git a/application/schemas/__init__.py b/application/schemas/__init__.py index b278f76c178e8b4c7ac4022a7d31d01b1da8e411..30de9fa48588f2b01e384fd0ed9ac13e58b714e3 100644 --- a/application/schemas/__init__.py +++ b/application/schemas/__init__.py @@ -10,6 +10,4 @@ # @Description : """ -from .user import CreateUserItem -from .user import UserSchema from .request import ParseLogRequestItem diff --git a/application/schemas/user.py b/application/schemas/user.py deleted file mode 100644 index 1a13d2a55e530df6e13f0bf6ba88c1d98b3a25e6..0000000000000000000000000000000000000000 --- a/application/schemas/user.py +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -# @Version : Python 3.11.4 -# @Software : Sublime Text 4 -# @Author : StudentCWZ -# @Email : StudentCWZ@outlook.com -# @Date : 2023/10/29 00:07 -# @File : user.py -# @Description : -""" - -from pydantic import BaseModel, EmailStr, field_validator -from application.extensions.init_marshmallow import ma -from application.extensions.init_sqlalchemy import db -from application.models import User -from marshmallow import validates, ValidationError - - -class CreateUserItem(BaseModel): - username: str - password: str - email: EmailStr - - @classmethod - @field_validator("username") - def name_must_contain_only_characters(cls, value): - if not value.isalpha(): - raise ValueError("Username must contain only characters.") - return value - - -class UserSchema(ma.SQLAlchemyAutoSchema): - - id = ma.Int(dump_only=True) - username = ma.Str(required=True) - password = ma.String(load_only=True, required=True) - email = ma.Email(required=True) - - class Meta: - model = User - sqla_session = db.session - load_instance = True - exclude = ("_password",) - - @validates('username') - def validate_username(self, username): - if len(username) < 3: - raise ValidationError('Username must be at least 3 characters.') \ No newline at end of file diff --git a/application/script/cmdline.py b/application/script/cmdline.py index d80507cbd61173644d5b39d3b3a8927b47e8f444..913dca2879cc99efae86f44ff5475675d4daa6ed 100644 --- a/application/script/cmdline.py +++ b/application/script/cmdline.py @@ -17,12 +17,10 @@ from flask.cli import with_appcontext @click.command("init") @with_appcontext def init(): - """Create a new admin user""" + """Create a new log""" from application.extensions.init_sqlalchemy import db - from application.models import User - click.echo("create user") - user = User(username="StudentCWZ", email="StudentCWZ@outlook.com", password="qwe!2345", active=True) - db.session.add(user) - db.session.commit() - click.echo("created user admin") + # click.echo("create log") + # db.session.add(user) + # db.session.commit() + # click.echo("created log") diff --git a/application/services/__init__.py b/application/services/__init__.py index bf20f5564ab122572bc7eff3cbc1d2db2441e48f..ad352d9fc4d67515ee72ffa0989aeb1fa3910f74 100644 --- a/application/services/__init__.py +++ b/application/services/__init__.py @@ -10,5 +10,4 @@ # @Description : """ -from .user import UserService from .log import LogService diff --git a/application/services/user.py b/application/services/user.py deleted file mode 100644 index ce1983b63d87efbc4b619e8c0dfc6cce735b4318..0000000000000000000000000000000000000000 --- a/application/services/user.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -# @Version : Python 3.11.4 -# @Software : Sublime Text 4 -# @Author : StudentCWZ -# @Email : StudentCWZ@outlook.com -# @Date : 2023/10/29 00:03 -# @File : user.py -# @Description : -""" - -from application.dao import UserDao - - -class UserService: - - @classmethod - def get_all_users(cls): - return UserDao.get_all() - - @classmethod - def get_user_by_id(cls, user_id: int): - return UserDao.get_by_id(user_id) - - @classmethod - def create_user(cls, username: str, password: str, email: str): - return UserDao.create(username, password, email) diff --git a/application/views/__init__.py b/application/views/__init__.py index 9b5bce2799aa9f0ac26617865838eba660acc066..aa980a661e8c102d76f71f45554fcc9c4b16d480 100644 --- a/application/views/__init__.py +++ b/application/views/__init__.py @@ -12,10 +12,8 @@ from flask import Flask -from .user import register_user_views from .log import register_log_views def init_views(app: Flask) -> None: - register_user_views(app) register_log_views(app) diff --git a/application/views/user/__init__.py b/application/views/user/__init__.py deleted file mode 100644 index 17d3c96c6ecfa009d3d496e79c477a1ed6b8260f..0000000000000000000000000000000000000000 --- a/application/views/user/__init__.py +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -# @Version : Python 3.11.4 -# @Software : Sublime Text 4 -# @Author : StudentCWZ -# @Email : StudentCWZ@outlook.com -# @Date : 2023/10/29 00:18 -# @File : __init__.py.py -# @Description : -""" - -from flask import Flask - -from .user import user_api - - -def register_user_views(app: Flask): - app.register_blueprint(user_api) diff --git a/application/views/user/user.py b/application/views/user/user.py deleted file mode 100644 index b5ed835f1965c1f319a88ade56aba216f829af3e..0000000000000000000000000000000000000000 --- a/application/views/user/user.py +++ /dev/null @@ -1,69 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -# @Version : Python 3.11.4 -# @Software : Sublime Text 4 -# @Author : StudentCWZ -# @Email : StudentCWZ@outlook.com -# @Date : 2023/10/29 00:08 -# @File : user.py -# @Description : -""" - -import json -from flask import Blueprint, current_app, request, jsonify -from flask_restful import Api -from application.services import UserService -from marshmallow import ValidationError - -from application.schemas import UserSchema -from application.utils import ElasticsearchUtil -from application.libs.helper import ConfigHelper - -user_api = Blueprint('user_api', __name__) -api_users = Api(user_api) - - -@user_api.route('/users', methods=['GET']) -def get_users(): - users = UserService.get_all_users() - return jsonify([user.to_dict() for user in users]) - - -@user_api.route('/users/', methods=['GET']) -def get_user(user_id): - user = UserService.get_user_by_id(user_id) - if user is None: - return jsonify({'error': 'User not found'}), 404 - return jsonify(user.to_dict()) - - -@user_api.route('/users', methods=['POST']) -def create_user(): - user_schema = UserSchema() - try: - data = user_schema.load(request.json) - except ValidationError as e: - return jsonify(e.messages), 400 - username = data.username - password = data.password - email = data.email - user = UserService.create_user(username, password, email) - return jsonify(user_schema.dump(user)), 201 - - -@user_api.route('/tests', methods=['POST']) -def test_user(): - # data = request.get_json() - # start = data.get('start') - # end = data.get('end') - # dsl = ElasticsearchUtil.dsl(start, end) - # index = current_app.config.Elasticsearch.Index - # res = ElasticsearchUtil.search(index, dsl) - # print(len(res)) - res = { - "user_name": "libai", - "user_age": 18, - } - res_json = json.dumps(res) - return res_json, 200