Skip to content

Commit

Permalink
Merge branch 'release/0.7.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
simonreeves committed May 23, 2023
2 parents b34d431 + 16e2bc5 commit 5e87a99
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
1 change: 0 additions & 1 deletion an_farmview/client/ubl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import requests
import pprint

Expand Down
1 change: 1 addition & 0 deletions an_farmview/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 29 additions & 0 deletions an_farmview/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sqlalchemy import desc

from . import models, schemas
from .config import settings


def get_envmonitor(db: Session, envmonitor_id: int):
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
2 changes: 1 addition & 1 deletion an_farmview/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ''
Expand Down
2 changes: 1 addition & 1 deletion an_farmview/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<head>
<link rel="stylesheet" href="{{ url_for('static', path='/css/style.css') }}">
<title>an-farmview temp</title>
<title>an-farmview</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
</head>
Expand Down

0 comments on commit 5e87a99

Please sign in to comment.