#!/opt/cron_script/.venv/bin/python

import sqlalchemy
from sqlalchemy import create_engine,Column,Integer,Boolean,String,DateTime
from sqlalchemy.orm import declarative_base, sessionmaker
from datetime import datetime
from dataclasses import dataclass

engine = create_engine("mariadb+mariadbconnector://session-user:session-user@database1/session?charset=utf8mb4", 
                       isolation_level="SERIALIZABLE")

Base = declarative_base()

@dataclass
class MySession(Base):
    __tablename__ = 'session'

    id: int
    node: str
    last_change: int
    script_running: int

    id = Column(Integer, primary_key=True)
    node = Column(String(20))
    last_change = Column(DateTime, server_default='last_change')
    script_running = Column(Boolean)


Base.metadata.create_all(engine)

Session = sessionmaker()
Session.configure(bind=engine)
session = Session()

def selectLast():
    mySession = session.query(MySession).order_by(MySession.id.desc()).first()
    print(int(mySession.__dict__.get('last_change').timestamp()))
    #print(mySession.get('last_change'))

selectLast()
#import time
#import pymysql
#
#
#connection = engine.connect()
#
#connection.close()
#with engine.connect() as conn:
#    #result = conn.execution_options(stream_results=True).execute(text("select * from session"))
#    #result = conn.execute("select * from session;")
#    result = conn.execute("select * from session;")
#    print(result)
#
##while True:
##    print("Working")
##    time.sleep(4)
#
#print("Working")
