re-org and db views

This commit is contained in:
mcinj 2022-05-22 14:20:44 -04:00
parent 9f2b2e7be2
commit 7c6493cea8
10 changed files with 160 additions and 27 deletions

2
.gitignore vendored
View File

@ -4,5 +4,5 @@ __pycache__/
.idea
config/config.ini
config/*.log*
config/sqlite.db
config/sqlite*
.DS_STORE

View File

@ -1,4 +1,4 @@
from src.bot import run
from src import run
if __name__ == '__main__':

View File

@ -6,7 +6,7 @@ from alembic import command
from alembic.config import Config
from dateutil import tz
from sqlalchemy import func
from sqlalchemy.orm import Session
from sqlalchemy.orm import Session, joinedload
from sqlalchemy_utils import database_exists
from .log import get_logger
@ -17,11 +17,7 @@ engine = sqlalchemy.create_engine(f"{os.getenv('BOT_DB_URL', 'sqlite:///./config
engine.connect()
def create_engine(db_url: str):
return engine
def run_migrations(script_location: str, db_url: str) -> None:
def run_db_migrations(script_location: str, db_url: str) -> None:
logger.debug('Running DB migrations in %r on %r', script_location, db_url)
alembic_cfg = Config()
alembic_cfg.set_main_option('script_location', script_location)
@ -30,10 +26,9 @@ def run_migrations(script_location: str, db_url: str) -> None:
if not database_exists(db_url):
logger.debug(f"'{db_url}' does not exist. Running normal migration to create db and tables." )
command.upgrade(alembic_cfg, 'head')
if database_exists(db_url):
logger.debug(f"'{db_url} exists.")
e = create_engine(db_url)
insp = sqlalchemy.inspect(e)
elif database_exists(db_url):
logger.debug(f"'{db_url}' exists.")
insp = sqlalchemy.inspect(engine)
alembic_version_table_name = 'alembic_version'
has_alembic_table = insp.has_table(alembic_version_table_name)
if has_alembic_table:
@ -49,6 +44,11 @@ def run_migrations(script_location: str, db_url: str) -> None:
class NotificationHelper:
@classmethod
def get(cls):
with Session(engine) as session:
return session.query(TableNotification).order_by(TableNotification.created_at.desc()).all()
@classmethod
def insert(cls, type_of_error, message, medium, success):
with Session(engine) as session:
@ -89,6 +89,11 @@ class NotificationHelper:
class GiveawayHelper:
@classmethod
def get(cls):
with Session(engine) as session:
return session.query(TableGiveaway).options(joinedload('steam_item')).order_by(TableGiveaway.giveaway_ended_at.desc()).all()
@classmethod
def unix_timestamp_to_utc_datetime(cls, timestamp):
return datetime.utcfromtimestamp(timestamp)

View File

@ -1,13 +1,13 @@
import os
from time import sleep
from .log import get_logger
from .config_reader import ConfigReader, ConfigException
from .enter_giveaways import SteamGiftsException
from .giveaway_thread import GiveawayThread
from .notification import Notification
from .database import run_migrations, create_engine
from .webserver_thread import WebServerThread
from src.bot.log import get_logger
from src.bot.config_reader import ConfigReader, ConfigException
from src.bot.enter_giveaways import SteamGiftsException
from src.bot.giveaway_thread import GiveawayThread
from src.bot.notification import Notification
from src.bot.database import run_db_migrations
from src.web.webserver_thread import WebServerThread
logger = get_logger(__name__)
config_file_name = f"{os.getenv('BOT_CONFIG_DIR', './config')}/config.ini"
@ -73,9 +73,9 @@ def entry():
|___/
-------------------------------------------------------------------------------------
""")
run_migrations(alembic_migration_files, db_url)
create_engine(db_url)
run_db_migrations(alembic_migration_files, db_url)
run()
if __name__ == '__main__':
entry()

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Steamgifts Bot Configuration</title>
<title>{{name}} Steamgifts Bot Configuration</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
</head>
<body>
@ -13,6 +13,8 @@
<li><a href="{{ url_for('config') }}">Config</a></li>
<li><a href="{{ url_for('log_info') }}">Info Logs</a></li>
<li><a href="{{ url_for('log_debug') }}">Debug Logs</a></li>
<li><a href="{{ url_for('db_giveaways') }}">Giveaways</a></li>
<li><a href="{{ url_for('db_notifications') }}">Notifications</a></li>
</ul>
</nav></strong>
</div>

View File

@ -0,0 +1,63 @@
<!DOCTYPE html>
<html>
<head>
<title>{{name}} Steamgifts Bot DB View</title>
<link href="{{ url_for('static', filename='css/main.css') }}" rel="stylesheet">
</head>
<body>
<header>
<div class="container">
<h1 class="logo">Steamgifts Bot</h1>
<strong>
<nav>
<ul class="menu">
<li><a href="{{ url_for('config') }}">Config</a></li>
<li><a href="{{ url_for('log_info') }}">Info Logs</a></li>
<li><a href="{{ url_for('log_debug') }}">Debug Logs</a></li>
<li><a href="{{ url_for('db_giveaways') }}">Giveaways</a></li>
<li><a href="{{ url_for('db_notifications') }}">Notifications</a></li>
</ul>
</nav>
</strong>
</div>
</header>
<div class="container">
<table>
<thead>
<tr>
<th>Giveaway ID</th>
<th>Steam ID</th>
<th>Giveaway URI</th>
<th>Name</th>
<th>Steam URL</th>
<th>User</th>
<th>Giveaway Created At</th>
<th>Giveaway Ended At</th>
<th>Cost</th>
<th>Copies</th>
<th>Contributor Level</th>
<th>Entered</th>
</tr>
</thead>
<tbody>
{% for row in content %}
<tr>
<td>{{row.giveaway_id}}</td>
<td>{{row.steam_id}}</td>
<td>{{row.giveaway_uri}}</td>
<td>{{row.steam_item.game_name}}</td>
<td>{{row.steam_item.steam_url}}</td>
<td>{{row.user}}</td>
<td>{{row.giveaway_created_at}}</td>
<td>{{row.giveaway_ended_at}}</td>
<td>{{row.cost}}</td>
<td>{{row.copies}}</td>
<td>{{row.contributor_level}}</td>
<td>{{row.entered}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Steamgifts Bot Logs</title>
<title>{{name}} Steamgifts Bot {{log_type}} Logs</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
</head>
<body>
@ -13,6 +13,8 @@
<li><a href="{{ url_for('config') }}">Config</a></li>
<li><a href="{{ url_for('log_info') }}">Info Logs</a></li>
<li><a href="{{ url_for('log_debug') }}">Debug Logs</a></li>
<li><a href="{{ url_for('db_giveaways') }}">Giveaways</a></li>
<li><a href="{{ url_for('db_notifications') }}">Notifications</a></li>
</ul>
</nav></strong>
</div>

View File

@ -0,0 +1,51 @@
<!DOCTYPE html>
<html>
<head>
<title>{{name}} Steamgifts Bot DB View</title>
<link href="{{ url_for('static', filename='css/main.css') }}" rel="stylesheet">
</head>
<body>
<header>
<div class="container">
<h1 class="logo">Steamgifts Bot</h1>
<strong>
<nav>
<ul class="menu">
<li><a href="{{ url_for('config') }}">Config</a></li>
<li><a href="{{ url_for('log_info') }}">Info Logs</a></li>
<li><a href="{{ url_for('log_debug') }}">Debug Logs</a></li>
<li><a href="{{ url_for('db_giveaways') }}">Giveaways</a></li>
<li><a href="{{ url_for('db_notifications') }}">Notifications</a></li>
</ul>
</nav>
</strong>
</div>
</header>
<div class="container">
<table>
<thead>
<tr>
<th>ID</th>
<th>Medium</th>
<th>Type</th>
<th>Success</th>
<th>Created At</th>
<th>Message</th>
</tr>
</thead>
<tbody>
{% for row in content %}
<tr>
<td>{{row.id}}</td>
<td>{{row.medium}}</td>
<td>{{row.type}}</td>
<td>{{row.success}}</td>
<td>{{row.created_at}}</td>
<td>{{row.message}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>

View File

@ -4,7 +4,8 @@ from threading import Thread
from flask_basicauth import BasicAuth
from .log import get_logger
from src.bot.database import NotificationHelper, GiveawayHelper
from src.bot.log import get_logger
logger = get_logger(__name__)
@ -15,6 +16,7 @@ class WebServerThread(threading.Thread):
Thread.__init__(self)
self.exc = None
self.config = config
self.name = config['NOTIFICATIONS'].get('notification.prefix')
self.host = config['WEB'].get('web.host')
self.port = config['WEB'].getint('web.port')
self.ssl = config['WEB'].getboolean('web.ssl')
@ -41,24 +43,32 @@ class WebServerThread(threading.Thread):
def config():
with open(f"{os.getenv('BOT_CONFIG_DIR', './config')}/config.ini", 'r') as f:
content = f.read()
return render_template('configuration.html', content=content)
return render_template('configuration.html', name=self.name, content=content)
@app.route(f"{self.app_root}log_info")
def log_info():
with open(f"{os.getenv('BOT_CONFIG_DIR', './config')}/info.log", 'r') as f:
content = f.read()
return render_template('log.html', content=content)
return render_template('log.html', name=self.name, log_type='info', content=content)
@app.route(f"{self.app_root}log_debug")
def log_debug():
with open(f"{os.getenv('BOT_CONFIG_DIR', './config')}/debug.log", 'r') as f:
content = f.read()
return render_template('log.html', content=content)
return render_template('log.html', name=self.name, log_type='debug', content=content)
@app.route(f"{self.app_root}alive")
def alive():
return 'OK'
@app.route(f"{self.app_root}notifications")
def db_notifications():
return render_template('notifications.html', content=NotificationHelper.get())
@app.route(f"{self.app_root}giveaways")
def db_giveaways():
return render_template('giveaways.html', content=GiveawayHelper.get())
if self.enabled:
logger.info("Webserver Enabled. Running")
if self.ssl: