'''
Author: your name
Date: 2021-04-14 14:12:18
LastEditTime: 2021-06-29 19:58:57
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \evm-store\backend\model\monitor.py
'''
#!/usr/bin/env python
# -*- coding: utf_8 -*-

from app.setting import config
from datetime import datetime
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

engine = create_engine('sqlite:///{}?check_same_thread=False'.format(config.get("DATABASE")), echo=True)

Base = declarative_base()

def get_current_datetime():
    return datetime.now().strftime("%Y-%m-%d %H:%M:%S")

class Watch(Base):
    __tablename__ = 'monitor_watch'
    id = Column(Integer, primary_key=True, autoincrement=True)
    imei = Column(String)
    create_at = Column(String, default=get_current_datetime)

class Request(Base):
    __tablename__ = 'monitor_request'
    id = Column(Integer, primary_key=True, autoincrement=True)
    watch = Column(Integer) # 手表ID
    host = Column(String)
    path = Column(String)
    protocol = Column(String)
    create_at = Column(String, default=get_current_datetime)

class System(Base):
    __tablename__ = 'monitor_system'

    id = Column(Integer, primary_key=True, autoincrement=True)
    watch = Column(Integer) # 手表ID
    free_size = Column(Integer) # 单位：字节
    timestamp = Column(String(50), default=get_current_datetime)

class Lvgl(Base):
    __tablename__ = 'monitor_lvgl'

    id = Column(Integer, primary_key=True, autoincrement=True)
    watch = Column(Integer) # 手表ID
    total_size = Column(Integer) # 单位：字节
    free_cnt = Column(Integer)
    free_size = Column(Integer)
    free_biggest_size = Column(Integer)
    used_cnt = Column(Integer)
    used_pct = Column(Integer)
    frag_pct = Column(Integer)
    timestamp = Column(String(50), default=get_current_datetime)

class Evm(Base):
    __tablename__ = 'monitor_evm'

    id = Column(Integer, primary_key=True, autoincrement=True)
    watch = Column(Integer) # 手表ID
    # total_size = Column(Integer) # 单位：字节
    # free_size = Column(Integer)
    # gc_usage = Column(Integer)
    heap_map_size = Column(Integer)
    heap_total_size = Column(Integer)
    heap_used_size = Column(Integer)
    stack_total_size = Column(Integer)
    stack_used_size = Column(Integer)
    timestamp = Column(String(50), default=get_current_datetime)

class Image(Base):
    __tablename__ = 'monitor_image'

    id = Column(Integer, primary_key=True, autoincrement=True)
    watch = Column(Integer) # 手表ID
    uri = Column(String(50))
    length = Column(Integer)
    png_uncompressed_size = Column(Integer)
    png_total_count = Column(Integer)
    png_file_size = Column(Integer)
    timestamp = Column(String(50), default=get_current_datetime)

Base.metadata.create_all(engine, checkfirst=True)

# engine是2.2中创建的连接
Session = sessionmaker(bind=engine)
 
# 创建Session类实例
session = Session()
