Skip to content

Commit a454487

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 1c0e9ff commit a454487

File tree

14 files changed

+58
-65
lines changed

14 files changed

+58
-65
lines changed

.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
@@ -301,13 +301,13 @@ jobs:
301301
command: |
302302
if [ "$CIRCLE_BRANCH" == master ] || [ "$CIRCLE_BRANCH" == develop ]; then
303303
touch google-service-key.json
304-
echo $GOOGLE_CREDENTIALS_STAGING | base64 --decode >> google-service-key.json
304+
echo $GOOGLE_CREDENTIALS_STAGING | base64 --decode >> google-service-key.json
305305
gcloud auth activate-service-account --key-file google-service-key.json
306306
gcloud --quiet config set project ${GOOGLE_PROJECT_ID_STAGING}
307307
gcloud --quiet config set compute/zone ${GOOGLE_COMPUTE_ZONE}
308308
else
309309
touch google-service-key.json
310-
echo $GOOGLE_CREDENTIALS_SANDBOX | base64 --decode >> google-service-key.json
310+
echo $GOOGLE_CREDENTIALS_SANDBOX | base64 --decode >> google-service-key.json
311311
gcloud auth activate-service-account --key-file google-service-key.json
312312
gcloud --quiet config set project ${GOOGLE_PROJECT_ID_SANDBOX}
313313
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/"
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
from flask_socketio import send, emit
21
from admin_notifications.models import AdminNotification
3-
from manage import socketio
4-
import celery
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()
510

611

7-
@celery.task(name="create-notification")
812
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
919
notification = AdminNotification(
1020
title=title,
1121
message=message,
@@ -14,4 +24,9 @@ def create_notification(title, message, location_id):
1424
)
1525
notification.save()
1626
new_notification = {"title": title, "message": message}
17-
return socketio.emit('notification', {'notification': new_notification}, broadcast=True) # noqa 501
27+
return socketio.emit(
28+
f"notifications-{location_name}",
29+
{'notification': new_notification},
30+
broadcast=True,
31+
callback=update_notification(notification.id)
32+
)

admin_notifications/helpers/device_last_seen.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
@celery.task(name='check-device-last-seen')
10-
def notify_when_device_is_offline(**kwargs):
10+
def notify_when_device_is_offline():
1111
query = DevicesModel.query
1212
online_devices = query.filter(DevicesModel.activity == "online").all()
1313
for device in online_devices:
@@ -18,19 +18,13 @@ def notify_when_device_is_offline(**kwargs):
1818
if duration_offline.days > 1:
1919
update_entity_fields(device, activity="offline")
2020
device.save()
21-
return online_devices
22-
2321

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:
3022
room_name = device.room.name
3123
room_id = device.room.id
3224
notification_payload = device_offline_notification(
3325
room_name, room_id)
3426
create_notification(title=notification_payload['title'],
3527
message=notification_payload['message'],
3628
location_id=device.room.location_id)
29+
30+
return online_devices
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
def event_auto_cancelled_notification(event_name, room_name):
1+
def device_offline_notification(room_name, room_id):
22
return {
3-
"title": "Event Auto cancelled.",
4-
"message": f"An event {event_name} in {room_name} \
5-
has been auto cancelled."
6-
}
3+
"title": "Device is offline",
4+
"message": f"A device in {room_name} roomid:{room_id} is offline."}

alembic/versions/1b53553ddacf_add_activity_column_to_devices_table.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

api/devices/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def mutate(self, info, **kwargs):
4141
room_location = location_join_room().filter(
4242
RoomModel.id == kwargs['room_id'],
4343
RoomModel.state == 'active'
44-
).first()
44+
).first()
4545
if not room_location:
4646
raise GraphQLError("Room not found")
4747
user = get_user_from_db()

cworker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
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
)
1616

1717

1818
def make_celery(app):
19-
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'], include=['admin_notifications.helpers.device_last_seen', 'admin_notifications.helpers.create_notification'], # noqa 501
19+
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'], include=['admin_notifications.helpers.device_last_seen', 'admin_notifications.helpers.create_notification'], # noqa 501
2020
backend=app.config['CELERY_BROKER_URL'])
2121

2222
celery.conf.update(app.config)

docker/dev/start_redis.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
cd /app
66
export $(cat .env | xargs)
77
celery worker -A cworker.celery --loglevel=info
8+
celery -A cworker.celery beat -l info

docker/prod/start_redis.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
cd /app
33
export $(cat .env | xargs)
44
celery worker -A cworker.celery --loglevel=info
5+
celery -A cworker.celery beat -l info

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Flask-JSON==0.3.2
99
Flask-Script==2.0.6
1010
Flask-GraphQL==1.4.1
1111
Flask-Mail==0.9.1
12+
Flask-SocketIO==4.1.0
1213
google-api-python-client==1.6.7
1314
graphene-sqlalchemy==2.0.0
1415
graphene==2.1

tests/test_admin_notification/__init__.py

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from tests.base import BaseTestCase
2+
from fixtures.token.token_fixture import ADMIN_TOKEN
3+
from fixtures.devices.devices_fixtures import devices_query
4+
from admin_notifications.helpers.device_last_seen import (
5+
notify_when_device_is_offline)
6+
7+
8+
class TestDeviceOffline(BaseTestCase):
9+
def test_when_device_is_offline(self):
10+
"""
11+
Testing for device creation
12+
"""
13+
headers = {"Authorization": "Bearer" + " " + ADMIN_TOKEN}
14+
self.app_test.post(devices_query, headers=headers)
15+
res = notify_when_device_is_offline()
16+
assert res[0].activity.value == 'offline'

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)