Added MySQL Support for DB options

This commit is contained in:
Anton Priestley 2022-08-14 16:45:49 +01:00
parent 6e2e297aeb
commit 119fa01101
4 changed files with 62 additions and 16 deletions

View File

@ -34,7 +34,8 @@ RUN pip3 wheel --wheel-dir=/wheels \
matplotlib \ matplotlib \
click \ click \
setproctitle \ setproctitle \
peewee peewee \
pymysql
FROM scratch FROM scratch

View File

@ -13,6 +13,8 @@ import yaml
from peewee_migrate import Router from peewee_migrate import Router
from playhouse.sqlite_ext import SqliteExtDatabase from playhouse.sqlite_ext import SqliteExtDatabase
from playhouse.sqliteq import SqliteQueueDatabase from playhouse.sqliteq import SqliteQueueDatabase
from peewee import MySQLDatabase
from playhouse.shortcuts import ReconnectMixin
from pydantic import ValidationError from pydantic import ValidationError
from frigate.config import DetectorTypeEnum, FrigateConfig from frigate.config import DetectorTypeEnum, FrigateConfig
@ -33,6 +35,8 @@ from frigate.watchdog import FrigateWatchdog
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class ReconnectMySQLDatabase(ReconnectMixin, MySQLDatabase):
pass
class FrigateApp: class FrigateApp:
def __init__(self): def __init__(self):
@ -116,6 +120,7 @@ class FrigateApp:
self.recordings_info_queue = mp.Queue() self.recordings_info_queue = mp.Queue()
def init_database(self): def init_database(self):
if self.config.database.type == "sqlite":
# Migrate DB location # Migrate DB location
old_db_path = os.path.join(CLIPS_DIR, "frigate.db") old_db_path = os.path.join(CLIPS_DIR, "frigate.db")
if not os.path.isfile(self.config.database.path) and os.path.isfile( if not os.path.isfile(self.config.database.path) and os.path.isfile(
@ -136,6 +141,21 @@ class FrigateApp:
self.db = SqliteQueueDatabase(self.config.database.path) self.db = SqliteQueueDatabase(self.config.database.path)
models = [Event, Recordings] models = [Event, Recordings]
self.db.bind(models) self.db.bind(models)
elif self.config.database.type == "mysql":
self.db = ReconnectMySQLDatabase(self.config.database.database,
host=self.config.database.host,
port=self.config.database.port,
user=self.config.database.user,
passwd=self.config.database.password
)
models = [Event, Recordings]
self.db.bind(models)
Event.create_table()
Recordings.create_table()
models = [Event, Recordings]
self.db.bind(models)
def init_stats(self): def init_stats(self):
self.stats_tracking = stats_init(self.camera_metrics, self.detectors) self.stats_tracking = stats_init(self.camera_metrics, self.detectors)

View File

@ -644,12 +644,36 @@ class CameraConfig(FrigateBaseModel):
return [part for part in cmd if part != ""] return [part for part in cmd if part != ""]
class DatabaseTypeEnum(str, Enum):
sqlite = "sqlite"
mysql = "mysql"
class DatabaseConfig(FrigateBaseModel): class DatabaseConfig(FrigateBaseModel):
type: DatabaseTypeEnum = Field(
default=DatabaseTypeEnum.sqlite, title="Database connection method, sqlite, mysql"
)
# Only used for sqlite
path: str = Field( path: str = Field(
default=os.path.join(BASE_DIR, "frigate.db"), title="Database path." default=os.path.join(BASE_DIR, "frigate.db"), title="Database path."
) )
# Only used for mysql
database: str = Field(
default="frigate", title="Database name."
)
host: str = Field(
default="localhost", title="Database host."
)
user: str = Field(
default="frigate", title="Database user."
)
password: str = Field(
default="frigate", title="Database password."
)
port: int = Field(
default=3306, title="Database password."
)
class ModelConfig(FrigateBaseModel): class ModelConfig(FrigateBaseModel):
path: Optional[str] = Field(title="Custom Object detection model path.") path: Optional[str] = Field(title="Custom Object detection model path.")

View File

@ -1,6 +1,7 @@
from numpy import unique from numpy import unique
from peewee import * from peewee import *
from playhouse.sqlite_ext import * from playhouse.sqlite_ext import *
from playhouse.mysql_ext import *
class Event(Model): class Event(Model):
@ -8,7 +9,7 @@ class Event(Model):
label = CharField(index=True, max_length=20) label = CharField(index=True, max_length=20)
camera = CharField(index=True, max_length=20) camera = CharField(index=True, max_length=20)
start_time = DateTimeField() start_time = DateTimeField()
end_time = DateTimeField() end_time = DateTimeField(null=True)
top_score = FloatField() top_score = FloatField()
false_positive = BooleanField() false_positive = BooleanField()
zones = JSONField() zones = JSONField()