From 16e2bc5ab53cd27db3514c1a6f0f8ce8bbb59819 Mon Sep 17 00:00:00 2001 From: Simon Reeves Date: Tue, 23 May 2023 17:01:22 +0100 Subject: [PATCH] tables now limit row count to AN_FARMVIEW_MAX_TABLE_ROWS --- an_farmview/client/ubl.py | 1 - an_farmview/config.py | 1 + an_farmview/crud.py | 29 +++++++++++++++++++++++++++++ an_farmview/main.py | 2 +- an_farmview/templates/home.html | 2 +- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/an_farmview/client/ubl.py b/an_farmview/client/ubl.py index a00cd1b..e55ada6 100644 --- a/an_farmview/client/ubl.py +++ b/an_farmview/client/ubl.py @@ -1,4 +1,3 @@ -import os import requests import pprint diff --git a/an_farmview/config.py b/an_farmview/config.py index 6f1c22d..d1c3728 100644 --- a/an_farmview/config.py +++ b/an_farmview/config.py @@ -8,6 +8,7 @@ class Settings(BaseSettings): snmp_community: str = '' snmp_ip: str = '' an_farmview_api_url: str = 'empty' + max_table_row_count: int = 4000 class Config: env_file = ".env" diff --git a/an_farmview/crud.py b/an_farmview/crud.py index dc67322..2cd9458 100644 --- a/an_farmview/crud.py +++ b/an_farmview/crud.py @@ -3,6 +3,7 @@ from sqlalchemy import desc from . import models, schemas +from .config import settings def get_envmonitor(db: Session, envmonitor_id: int): @@ -14,6 +15,10 @@ def get_envmonitors(db: Session, skip: int = 0, limit: int = 100): def create_envmonitor(db: Session, envmonitor: schemas.EnvMonitorCreate): + + + limit_rowcount(db=db, model=models.EnvMonitor) + db_envmonitor = models.EnvMonitor( timestamp=func.now(), temperature01=envmonitor.temperature01, @@ -30,7 +35,11 @@ def get_ubl(db: Session, skip: int=0, limit: int=100): return db.query(models.UBL).order_by(desc(models.UBL.timestamp)).offset(skip).limit(limit).all() + def create_ubl(db: Session, ubl: schemas.UBLCreate): + + limit_rowcount(db=db, model=models.UBL) + db_ubl = models.UBL( timestamp=func.now(), redshift_entitled=ubl.redshift_entitled, @@ -46,3 +55,23 @@ def create_ubl(db: Session, ubl: schemas.UBLCreate): db.commit() db.refresh(db_ubl) return db_ubl + +def limit_rowcount(db: Session, model): + + row_count = db.query(model).count() + + if row_count > settings.max_table_row_count: + to_delete = row_count - settings.max_table_row_count + + rows_to_delete = db.query(model).order_by(model.id.asc()).limit(to_delete) + + for row in rows_to_delete: + print(f'{row.id}, {row.timestamp}') + db.delete(row) + + db.commit() + print(f'Rows: {row_count} is greater than settings {settings.max_table_row_count}') + else: + print(f'Rows: {row_count} is not greater than settings {settings.max_table_row_count}') + + return row_count diff --git a/an_farmview/main.py b/an_farmview/main.py index c3cf5a9..2bcc759 100644 --- a/an_farmview/main.py +++ b/an_farmview/main.py @@ -97,8 +97,8 @@ def api_ubl_info(db: Session = Depends(get_db)): return data -def mins_info(mins, name): +def mins_info(mins, name): # cover 'null' if not isinstance(mins, int): return '' diff --git a/an_farmview/templates/home.html b/an_farmview/templates/home.html index 3f0da9d..0a36831 100644 --- a/an_farmview/templates/home.html +++ b/an_farmview/templates/home.html @@ -2,7 +2,7 @@ - an-farmview temp + an-farmview