You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.8 KiB
87 lines
2.8 KiB
# -*- coding: utf-8 -*-
|
|
'''
|
|
设置每天 00:00:00 新建一个日志记录
|
|
'''
|
|
import os
|
|
import sys
|
|
|
|
sys.path.append(os.path.join(os.path.abspath(__file__).split('auto')[0] + 'auto'))
|
|
|
|
import time
|
|
from datetime import datetime
|
|
import pymongo
|
|
from base.base_load_config import load_config, get_base_path
|
|
|
|
config_json = load_config()
|
|
base_project = get_base_path()
|
|
|
|
PROJECT_NAME = config_json.get('PROJECT_NAME')
|
|
DB_USER = config_json.get('DB_USER')
|
|
DB_PASSWORD = config_json.get('DB_PASSWORD')
|
|
DB_IP = config_json.get('DB_IP')
|
|
DB_PORT = config_json.get('DB_PORT')
|
|
MONGO_LINK = f'mongodb://{DB_USER}:{DB_PASSWORD}@{DB_IP}:{DB_PORT}/'
|
|
MAIL_HOST = config_json.get('MAIL_HOST')
|
|
MAIL_USER = config_json.get('MAIL_USER')
|
|
MAIL_PASS = config_json.get('MAIL_PASS')
|
|
MAIL_SENDER = config_json.get('MAIL_SENDER')
|
|
MAIL_RECEIVERS = config_json.get('MAIL_RECEIVERS')
|
|
|
|
now_day = time.strftime('%Y-%m-%d', time.localtime())
|
|
rss_base_url = 'http://home.erhe.link:20002/xmlfile/'
|
|
|
|
|
|
class LogsHandle(object):
|
|
def __init__(self):
|
|
self.now_day = time.strftime('%Y-%m-%d', time.localtime())
|
|
db = 'logs'
|
|
collection = 'logs_' + self.now_day
|
|
self.mongo = MongoHandle(db=db, collection=collection, del_db=False, del_collection=False, auto_remove=0)
|
|
|
|
def logs_generate(self):
|
|
data_to_insert = {
|
|
"title": "logs",
|
|
"context": 'generate message logs',
|
|
"state": "create",
|
|
"create_time": int(time.time()),
|
|
"create_datetime": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
}
|
|
|
|
self.mongo.collection.insert_one(data_to_insert)
|
|
|
|
|
|
class MongoHandle(object):
|
|
def __init__(self, db, collection, del_db=False, del_collection=False, auto_remove=0):
|
|
self.client = pymongo.MongoClient(MONGO_LINK)
|
|
self.db = db
|
|
self.collection = collection
|
|
|
|
if del_db and db:
|
|
# 检查数据库是否存在
|
|
if db in self.client.list_database_names():
|
|
# 删除数据库
|
|
self.client.drop_database(db)
|
|
self.db = self.client[db]
|
|
|
|
if del_collection and self.collection:
|
|
# 检查集合是否存在
|
|
if self.collection in self.db.list_collection_names():
|
|
# 删除集合
|
|
self.db.drop_collection(collection)
|
|
self.collection = self.db[collection]
|
|
|
|
if auto_remove:
|
|
self.auto_remove_data(auto_remove)
|
|
|
|
def write_data(self, data):
|
|
self.collection.insert_one(data)
|
|
|
|
def auto_remove_data(self, day):
|
|
for data in self.collection.find({'create_time': {'$lt': int(time.time()) - day * 24 * 60 * 60}}):
|
|
self.collection.delete_one({'_id': data['_id']})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
print("新建当天日志记录...")
|
|
LogsHandle().logs_generate()
|
|
print("当天日志记录已创建...")
|
|
|