Skip to content

Commit edbed7d

Browse files
author
[Arusey]
committed
CON-72-story(notifications): admin receives notification when device is not seen in a while
- setup queue manager for when notifications are sent - implement notifications when device is not seen in a while [Delivers CON-72]
1 parent 5d07c8e commit edbed7d

File tree

9 files changed

+95
-74
lines changed

9 files changed

+95
-74
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from datetime import datetime
2+
from api.devices.models import Devices as DevicesModel
3+
from utilities.utility import update_entity_fields
4+
from admin_notifications.helpers.create_notification import create_notification
5+
from admin_notifications.helpers.notification_templates import device_offline_notification # noqa 501
6+
import celery
7+
8+
9+
@celery.task(name='check-device-last-seen')
10+
def notify_when_device_is_offline(**kwargs):
11+
query = DevicesModel.query
12+
online_devices = query.filter(DevicesModel.activity == "online").all()
13+
for device in online_devices:
14+
device_last_seen = device.last_seen
15+
current_time = datetime.now()
16+
duration_offline = current_time - device_last_seen
17+
18+
if duration_offline.days > 1:
19+
update_entity_fields(device, activity="offline")
20+
device.save()
21+
return online_devices
22+
23+
24+
@celery.task(name='notify')
25+
def notify():
26+
query = DevicesModel.query
27+
offline_device = query.filter(DevicesModel.activity == "offline").all()
28+
for device in offline_device:
29+
if device:
30+
room_name = device.room.name
31+
room_id = device.room.id
32+
notification_payload = device_offline_notification(
33+
room_name, room_id)
34+
create_notification(title=notification_payload['title'],
35+
message=notification_payload['message'],
36+
location_id=device.room.location_id)

admin_notifications/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class AdminNotification(Base, Utility):
77
__tablename__ = 'admin_notifications'
88

9-
id = Column(Integer, primary_key=True) # noqa
9+
id = Column(Integer, primary_key=True) # noqa
1010
title = Column(String, nullable=True)
1111
message = Column(String, nullable=True)
1212
date_received = Column(String, nullable=True)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""add activity column to devices table
2+
3+
Revision ID: 79ef610dbd41
4+
Revises: a36af2be7b0c
5+
Create Date: 2019-06-28 08:05:37.542613
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
from sqlalchemy.dialects import postgresql
11+
12+
13+
14+
# revision identifiers, used by Alembic.
15+
revision = '79ef610dbd41'
16+
down_revision = 'a36af2be7b0c'
17+
branch_labels = None
18+
depends_on = None
19+
20+
21+
def upgrade():
22+
# ### commands auto generated by Alembic - please adjust! ###
23+
activitytype = postgresql.ENUM(
24+
'online', 'offline', name='activitytype')
25+
activitytype.create(op.get_bind())
26+
op.add_column('devices', sa.Column('activity', sa.Enum('online', 'offline', name='activitytype'), nullable=True))
27+
# ### end Alembic commands ###
28+
29+
30+
def downgrade():
31+
# ### commands auto generated by Alembic - please adjust! ###
32+
op.drop_column('devices', 'activity')
33+
# ### end Alembic commands ###

alembic/versions/c65718cd4ed8_add_admin_notifications_table.py renamed to alembic/versions/a36af2be7b0c_add_admin_notifications_table.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
"""add admin notifications table
22
3-
Revision ID: c65718cd4ed8
4-
Revises: a5a4841351d7
5-
Create Date: 2019-06-21 08:38:07.110402
3+
Revision ID: a36af2be7b0c
4+
Revises: 50173cb0491f
5+
Create Date: 2019-06-28 07:51:40.249610
66
77
"""
88
from alembic import op
99
import sqlalchemy as sa
1010

1111

1212
# revision identifiers, used by Alembic.
13-
revision = 'c65718cd4ed8'
14-
down_revision = 'a5a4841351d7'
13+
revision = 'a36af2be7b0c'
14+
down_revision = '50173cb0491f'
1515
branch_labels = None
1616
depends_on = None
1717

1818

1919
def upgrade():
2020
# ### commands auto generated by Alembic - please adjust! ###
21+
op.execute('DROP TYPE statustype;')
2122
op.create_table('admin_notifications',
2223
sa.Column('id', sa.Integer(), nullable=False),
2324
sa.Column('title', sa.String(), nullable=True),

api/devices/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Devices(Base, Utility):
1717
location = Column(String, nullable=False)
1818
room_id = Column(Integer, ForeignKey('rooms.id', ondelete="CASCADE"))
1919
room = relationship('Room')
20-
activity = Column(Enum(ActivityType), default="active")
20+
activity = Column(Enum(ActivityType), default="online")
2121

2222
def __init__(self, **kwargs):
2323
validate_empty_fields(**kwargs)

app.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from flask_graphql import GraphQLView
44
from flask_cors import CORS
55
from flask_json import FlaskJSON
6-
from flask_socketio import SocketIO
76

87
from flask_mail import Mail
98
from config import config
@@ -20,7 +19,6 @@ def create_app(config_name):
2019
app = Flask(__name__)
2120
CORS(app)
2221
FlaskJSON(app)
23-
SocketIO(app)
2422
app.config.from_object(config[config_name])
2523
config[config_name].init_app(app)
2624
mail.init_app(app)

cworker.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99

1010
app.config.update(
11-
CELERY_BROKER_URL='redis://localhost:6379',
12-
CELERY_RESULT_BACKEND='redis://localhost:6379',
11+
CELERY_BROKER_URL=os.getenv('CELERY_BROKER_URL'),
12+
CELERY_RESULT_BACKEND=os.getenv('CELERY_RESULT_BACKEND'),
1313
CELERY_ACCEPT_CONTENT=['pickle'],
1414
CELERYBEAT_SCHEDULE=beat_schedule
1515
)
@@ -34,5 +34,6 @@ def __call__(self, *args, **kwargs):
3434

3535

3636
celery = make_celery(app)
37+
3738
celery_scheduler = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
38-
celery_scheduler.conf.update(app.config)
39+
celery_scheduler.conf.enable_utc = False

requirements.txt

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,39 @@
11
alembic==0.9.8
2-
amqp==1.4.9
3-
aniso8601==3.0.2
4-
anyjson==0.3.3
5-
astroid==2.2.5
6-
attrs==19.1.0
7-
autopep8==1.4.4
8-
billiard==3.3.0.23
9-
blinker==1.4
102
bugsnag==3.4.2
3+
blinker==1.4
114
celery==3.1.17
12-
certifi==2019.3.9
13-
chardet==3.0.4
14-
Click==7.0
155
coverage==4.5.1
16-
coveralls==1.8.0
17-
decorator==4.4.0
18-
docopt==0.6.2
19-
flake8==3.5.0
206
Flask==0.12.2
217
Flask-Cors==3.0.4
22-
Flask-GraphQL==1.4.1
238
Flask-JSON==0.3.2
24-
Flask-Mail==0.9.1
259
Flask-Script==2.0.6
26-
Flask-SocketIO==4.1.0
27-
Flask-Testing==0.7.1
10+
Flask-GraphQL==1.4.1
11+
Flask-Mail==0.9.1
2812
google-api-python-client==1.6.7
29-
graphene==2.1
3013
graphene-sqlalchemy==2.0.0
31-
graphql-core==2.2
32-
graphql-relay==0.4.5
33-
httplib2==0.13.0
34-
idna==2.8
14+
graphene==2.1
3515
imgkit==1.0.1
36-
iso8601==0.1.12
37-
isort==4.3.20
38-
itsdangerous==1.1.0
39-
Jinja2==2.10.1
40-
kombu==3.0.37
41-
lazy-object-proxy==1.4.1
42-
Mako==1.0.12
4316
MarkupSafe==1.0
44-
mccabe==0.6.1
4517
more-itertools==4.1.0
46-
nose==1.3.7
4718
numpy==1.15.2
48-
oauth2client==4.1.3
4919
opencv-python==3.4.3.18
5020
pandas==0.23.4
51-
pdfkit==0.6.1
5221
Pillow==5.3.0
53-
pluggy==0.6.0
54-
promise==2.2.1
22+
pdfkit==0.6.1
5523
psycopg2-binary==2.7.4
5624
py==1.5.3
57-
pyasn1==0.4.5
58-
pyasn1-modules==0.2.5
59-
pycodestyle==2.3.1
60-
pyflakes==1.6.0
61-
PyJWT==1.6.4
62-
pylint==2.3.1
6325
pytest==3.5.0
6426
python-dateutil==2.7.0
65-
python-editor==1.0.3
66-
python-engineio==3.8.1
67-
python-socketio==4.1.0
68-
pytz==2019.1
27+
PyJWT==1.6.4
6928
redis==2.10.3
70-
requests==2.22.0
71-
rsa==4.0
72-
Rx==1.6.1
73-
singledispatch==3.4.0.3
74-
six==1.12.0
29+
nose==1.3.7
30+
python-editor==1.0.3
7531
SQLAlchemy==1.2.6
76-
SQLAlchemy-Utils==0.33.2
7732
tox==3.0.0
78-
typed-ast==1.4.0
79-
typing==3.6.4
80-
uritemplate==3.0.0
81-
urllib3==1.25.3
82-
validators==0.12.4
33+
SQLAlchemy-Utils==0.33.2
8334
virtualenv==15.2.0
84-
WebOb==1.8.5
85-
Werkzeug==0.15.4
86-
wrapt==1.11.1
35+
Flask-Testing==0.7.1
36+
typing==3.6.4
37+
flake8==3.5.0
38+
coveralls
39+
validators==0.12.4

utilities/utility.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ class QuestionType(enum.Enum):
8989
missingitem = "missing_items"
9090

9191

92-
9392
class StatusType(enum.Enum):
9493
read = "read"
9594
unread = "unread"

0 commit comments

Comments
 (0)