From 0284eb2fa8484d70a2f5546a538f12dd6fd61d0e Mon Sep 17 00:00:00 2001 From: Raphael Maenle Date: Fri, 17 Jul 2020 10:39:16 +0200 Subject: [PATCH] adds cleanup functionality for old devices - device now last field 'last connection' which gets updated at every server connection from the respective device - manual cleanup script in database deletes all devices which have never been updated, or have a timestamp older than 30 days --- database/__init__.py | 14 ++++++++++ database/migrations/versions/121eab3835ee_.py | 28 +++++++++++++++++++ database/models.py | 1 + server/routes.py | 2 ++ 4 files changed, 45 insertions(+) create mode 100644 database/__init__.py create mode 100644 database/migrations/versions/121eab3835ee_.py diff --git a/database/__init__.py b/database/__init__.py new file mode 100644 index 0000000..e0e1a9e --- /dev/null +++ b/database/__init__.py @@ -0,0 +1,14 @@ +from server import db +import time +from database.models import Device + +def cleanDevices(): + allDevs = db.session.query(Device) + devices = allDevs.filter(Device.lastConnection <= int(round(time.time())) - 60*60*24*30).all() + devices += allDevs.filter(Device.lastConnection == None) + for device in devices: + print(device.deviceName) + db.session.delete(device) + + db.session.commit() + diff --git a/database/migrations/versions/121eab3835ee_.py b/database/migrations/versions/121eab3835ee_.py new file mode 100644 index 0000000..84d5414 --- /dev/null +++ b/database/migrations/versions/121eab3835ee_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: 121eab3835ee +Revises: e5ef5e4a807b +Create Date: 2020-07-17 08:03:36.947158 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '121eab3835ee' +down_revision = 'e5ef5e4a807b' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('device', sa.Column('lastConnection', sa.BigInteger(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('device', 'lastConnection') + # ### end Alembic commands ### diff --git a/database/models.py b/database/models.py index 316cae5..6dc7ac5 100644 --- a/database/models.py +++ b/database/models.py @@ -61,6 +61,7 @@ class Device(db.Model): user_id = db.Column(db.Integer, db.ForeignKey('user.id')) deviceName = db.Column(db.String(64), unique=True) connection = db.Column(db.Boolean) + lastConnection = db.Column(db.BigInteger) class Calendar(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) diff --git a/server/routes.py b/server/routes.py index 8308df7..e25741e 100644 --- a/server/routes.py +++ b/server/routes.py @@ -1,6 +1,7 @@ # Python standard libraries import json import os +import time # Third-party libraries import flask @@ -204,6 +205,7 @@ def downloader(device): if request_device.user_id == None: return jsonify(kind="unregistered") + request_device.lastConnection=int(round(time.time())) request_device.connection=True db.session.commit() request_user = db.session.query(User).filter(User.id==request_device.user_id).first()