Skip to content

Commit 4e6154b

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 d686ac1 commit 4e6154b

27 files changed

Lines changed: 199 additions & 33 deletions

.circleci/config.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ gcloud_setup: &gcloud_setup
3636
run:
3737
name: setup gcloud
3838
command: |
39-
# install
39+
# install
4040
sudo curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz > /tmp/google-cloud-sdk.tar.gz
41-
sudo mkdir -p /usr/local/gcloud
41+
sudo mkdir -p /usr/local/gcloud
4242
sudo tar -C /usr/local/gcloud -xvf /tmp/google-cloud-sdk.tar.gz
4343
sudo /usr/local/gcloud/google-cloud-sdk/install.sh --quiet
4444
echo PATH=$PATH:/usr/local/gcloud/google-cloud-sdk/bin >> ~/.bashrc
@@ -190,8 +190,8 @@ jobs:
190190
command: |
191191
./cc-test-reporter before-build
192192
. venv/bin/activate
193-
coverage combine parallel-coverage/
194-
coverage xml
193+
coverage combine parallel-coverage/
194+
coverage xml -i
195195
coverage report
196196
./cc-test-reporter format-coverage -o ./.coverage -t coverage.py
197197
./cc-test-reporter upload-coverage -i .coverage
@@ -304,13 +304,13 @@ jobs:
304304
command: |
305305
if [ "$CIRCLE_BRANCH" == master ] || [ "$CIRCLE_BRANCH" == develop ]; then
306306
touch google-service-key.json
307-
echo $GOOGLE_CREDENTIALS_STAGING | base64 --decode >> google-service-key.json
307+
echo $GOOGLE_CREDENTIALS_STAGING | base64 --decode >> google-service-key.json
308308
gcloud auth activate-service-account --key-file google-service-key.json
309309
gcloud --quiet config set project ${GOOGLE_PROJECT_ID_STAGING}
310310
gcloud --quiet config set compute/zone ${GOOGLE_COMPUTE_ZONE}
311311
else
312312
touch google-service-key.json
313-
echo $GOOGLE_CREDENTIALS_SANDBOX | base64 --decode >> google-service-key.json
313+
echo $GOOGLE_CREDENTIALS_SANDBOX | base64 --decode >> google-service-key.json
314314
gcloud auth activate-service-account --key-file google-service-key.json
315315
gcloud --quiet config set project ${GOOGLE_PROJECT_ID_SANDBOX}
316316
gcloud --quiet config set compute/zone ${GOOGLE_COMPUTE_ZONE}

.codeclimate.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ version: "2"
22
exclude_patterns:
33
- "helpers/auth/authentication.py"
44
- "helpers/calendar/events.py"
5-
- "alembic/"
5+
- "**/alembic/"
6+
- "**/*__init__.py"
7+
- "**/tests/"

.coveragerc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,3 @@ omit =
1313

1414
[html]
1515
directory=html_coverage_report
16-
17-

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ html_coverage_report/
4242
.tox/
4343
.coverage
4444
.coverage.*
45-
.coveragerc
46-
setup.cfg
4745
.cache
4846
nosetests.xml
4947
coverage.xml

admin_notifications/__init__.py

Whitespace-only changes.

admin_notifications/helpers/__init__.py

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from admin_notifications.models import AdminNotification
2+
from api.location.models import Location
3+
from datetime import datetime
4+
5+
6+
def update_notification(notification_id):
7+
notification = AdminNotification.query.filter_by(id=notification_id).first()
8+
notification.date_received = datetime.now()
9+
notification.save()
10+
11+
12+
def create_notification(title, message, location_id):
13+
"""
14+
Create notifications in the database and emit them to the client
15+
"""
16+
from manage import socketio
17+
location = Location.query.filter_by(id=location_id).first()
18+
location_name = location.name
19+
notification = AdminNotification(
20+
title=title,
21+
message=message,
22+
location_id=location_id,
23+
status="unread"
24+
)
25+
notification.save()
26+
new_notification = {"title": title, "message": message}
27+
return socketio.emit(
28+
f"notifications-{location_name}",
29+
{'notification': new_notification},
30+
broadcast=True,
31+
callback=update_notification(notification.id)
32+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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():
11+
"""Asynchronous method that checks whether a device's last seen is greater\
12+
than 24hours, turns them to offline and subsequently notify's
13+
"""
14+
query = DevicesModel.query
15+
online_devices = query.filter(DevicesModel.activity == "online").all()
16+
for device in online_devices:
17+
device_last_seen = device.last_seen
18+
current_time = datetime.now()
19+
duration_offline = current_time - device_last_seen
20+
21+
if duration_offline.days > 1:
22+
update_entity_fields(device, activity="offline")
23+
device.save()
24+
25+
room_name = device.room.name
26+
room_id = device.room.id
27+
notification_payload = device_offline_notification(
28+
room_name, room_id)
29+
create_notification(title=notification_payload['title'],
30+
message=notification_payload['message'],
31+
location_id=device.room.location_id)
32+
33+
return online_devices
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
def device_offline_notification(room_name, room_id):
3+
"""Notification message when device has been offline for a while"""
4+
return {
5+
"title": "Device is offline",
6+
"message": f"A device in {room_name} roomid:{room_id} is offline."
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from datetime import timedelta
2+
"""Celery beat schedule that checks a device's last seen every 24 hours"""
3+
beat_schedule = {
4+
'run-check-device-last-seen-hourly': {
5+
'task': 'check-device-last-seen',
6+
'schedule': timedelta(hours=24)
7+
}
8+
}

0 commit comments

Comments
 (0)