Skip to content

Commit

Permalink
feat($startup): record current user and command line when startup
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Sep 3, 2022
1 parent af5260b commit 8ed5022
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
12 changes: 11 additions & 1 deletion python_boilerplate/repository/model/base/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@

_db_path: str = f"{get_data_dir()}{os.path.sep}{get_module_name()}.db"
database: SqliteDatabase = SqliteDatabase(_db_path)
logger.warning(f"SQLite database created. Path: {_db_path}, {database}")
logger.warning(f"SQLite database created. Path: [{_db_path}], {database}")


class BaseModel(Model):
"""
Base model for persistence.
Models and Fields https://docs.peewee-orm.com/en/latest/peewee/models.html#model-inheritance
Field types table https://docs.peewee-orm.com/en/latest/peewee/models.html#field-types-table
"""

class Meta:
"""
Model configuration is kept namespaced in a special class called Meta. This convention is borrowed from Django.
Meta configuration is passed on to subclasses, so our project’s models will all subclass BaseModel.
There are many different attributes you can configure using Model.Meta.
Model options and table metadata https://docs.peewee-orm.com/en/latest/peewee/models.html#model-options-and-table-metadata
Primary Keys, Composite Keys and other Tricks https://docs.peewee-orm.com/en/latest/peewee/models.html#primary-keys-composite-keys-and-other-tricks
"""

database = database
legacy_table_names = False
6 changes: 4 additions & 2 deletions python_boilerplate/repository/model/startup_log.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from datetime import datetime

from peewee import DateTimeField
from peewee import CharField, DateTimeField, TextField

from python_boilerplate.repository.model.base.model import BaseModel, database


class StartupLog(BaseModel):
"""
DetectedFace model
Startup Log
"""

current_user = CharField(max_length=50, null=False)
command_line = TextField(null=False)
startup_time = DateTimeField(default=datetime.now)
created_time = DateTimeField(default=datetime.now)
modified_time = DateTimeField(default=datetime.now)
Expand Down
22 changes: 21 additions & 1 deletion python_boilerplate/repository/startup_log_repository.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import platform
import sys

import arrow
from loguru import logger

from python_boilerplate.repository.model.startup_log import StartupLog
Expand All @@ -9,6 +13,22 @@ def save() -> StartupLog:
Save a new startup log.
:return: a StartupLog object
"""
startup_log: StartupLog = StartupLog()
startup_log: StartupLog = StartupLog(
current_user=platform.node(), command_line=" ".join(sys.argv)
)
startup_log.save()
retain_startup_log()
return startup_log


def retain_startup_log() -> int:
a_week_ago = arrow.now().shift(days=-7).format("YYYY-MM-DD")
affected_rows: int = (
StartupLog.delete().where(StartupLog.startup_time < a_week_ago).execute()
)
# the affected_rows is always 1 no matter how many rows were deleted
logger.debug(
f"The program retains recent 7 days of startup log. "
f"Deleted {affected_rows} records that are before {a_week_ago}"
)
return affected_rows

0 comments on commit 8ed5022

Please sign in to comment.