-
Notifications
You must be signed in to change notification settings - Fork 32
CON-72 admin turn device online mutation #454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Arusey
wants to merge
1
commit into
develop
Choose a base branch
from
story/CON-72-add-turn-online-mutation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,3 @@ omit = | |
|
||
[html] | ||
directory=html_coverage_report | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from admin_notifications.models import AdminNotification | ||
from api.location.models import Location | ||
from datetime import datetime | ||
|
||
|
||
def update_notification(notification_id): | ||
notification = AdminNotification.query.filter_by(id=notification_id).first() | ||
notification.date_received = datetime.now() | ||
notification.save() | ||
|
||
|
||
def create_notification(title, message, location_id): | ||
""" | ||
Create notifications in the database and emit them to the client | ||
""" | ||
from manage import socketio | ||
location = Location.query.filter_by(id=location_id).first() | ||
location_name = location.name | ||
notification = AdminNotification( | ||
title=title, | ||
message=message, | ||
location_id=location_id, | ||
status="unread" | ||
) | ||
notification.save() | ||
new_notification = {"title": title, "message": message} | ||
return socketio.emit( | ||
f"notifications-{location_name}", | ||
{'notification': new_notification}, | ||
broadcast=True, | ||
callback=update_notification(notification.id) | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from datetime import datetime | ||
from api.devices.models import Devices as DevicesModel | ||
from utilities.utility import update_entity_fields | ||
from admin_notifications.helpers.create_notification import create_notification | ||
from admin_notifications.helpers.notification_templates import device_offline_notification # noqa 501 | ||
import celery | ||
|
||
|
||
@celery.task(name='check-device-last-seen') | ||
def notify_when_device_is_offline(): | ||
"""Asynchronous method that checks whether a device's last seen is greater\ | ||
than 24hours, turns them to offline and subsequently notify's | ||
""" | ||
query = DevicesModel.query | ||
online_devices = query.filter(DevicesModel.activity == "online").all() | ||
for device in online_devices: | ||
device_last_seen = device.last_seen | ||
current_time = datetime.now() | ||
duration_offline = current_time - device_last_seen | ||
|
||
if duration_offline.days > 1: | ||
update_entity_fields(device, activity="offline") | ||
device.save() | ||
|
||
room_name = device.room.name | ||
room_id = device.room.id | ||
notification_payload = device_offline_notification( | ||
room_name, room_id) | ||
create_notification(title=notification_payload['title'], | ||
message=notification_payload['message'], | ||
location_id=device.room.location_id) | ||
|
||
return online_devices |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
def device_offline_notification(room_name, room_id): | ||
"""Notification message when device has been offline for a while""" | ||
return { | ||
"title": "Device is offline", | ||
"message": f"A device in {room_name} roomid:{room_id} is offline." | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from datetime import timedelta | ||
"""Celery beat schedule that checks a device's last seen every 24 hours""" | ||
beat_schedule = { | ||
'run-check-device-last-seen-hourly': { | ||
'task': 'check-device-last-seen', | ||
'schedule': timedelta(hours=24) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from sqlalchemy import (Column, String, Enum, Integer, ForeignKey) | ||
from helpers.database import Base | ||
from utilities.utility import Utility, StatusType | ||
|
||
|
||
class AdminNotification(Base, Utility): | ||
__tablename__ = 'admin_notifications' | ||
|
||
id = Column(Integer, primary_key=True) # noqa | ||
title = Column(String, nullable=True) | ||
message = Column(String, nullable=True) | ||
date_received = Column(String, nullable=True) | ||
date_read = Column(String, nullable=True) | ||
status = Column(Enum(StatusType), default="unread") | ||
location_id = Column( | ||
Integer, | ||
ForeignKey('locations.id', ondelete="CASCADE"), | ||
nullable=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from flask_socketio import send | ||
from admin_notifications.models import AdminNotification | ||
|
||
|
||
def serialize_message(notification): | ||
return { | ||
"title": notification.title, | ||
"message": notification.message, | ||
} | ||
|
||
|
||
def send_notifications(): | ||
query = AdminNotification.query | ||
notifications = query.filter_by(status="unread").all() | ||
notifications = [serialize_message(notification) | ||
for notification in notifications] | ||
return send(notifications, broadcast=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
alembic/versions/79ef610dbd41_add_activity_column_to_devices_table.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""add activity column to devices table | ||
|
||
Revision ID: 79ef610dbd41 | ||
Revises: a36af2be7b0c | ||
Create Date: 2019-06-28 08:05:37.542613 | ||
|
||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '79ef610dbd41' | ||
down_revision = 'af8e4f84b552' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
activitytype = postgresql.ENUM( | ||
'online', 'offline', name='activitytype') | ||
activitytype.create(op.get_bind()) | ||
op.add_column('devices', sa.Column('activity', sa.Enum( | ||
'online', 'offline', name='activitytype'), nullable=True)) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column('devices', 'activity') | ||
# ### end Alembic commands ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
35 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#!/bin/bash | ||
cd /app | ||
export $(cat .env | xargs) | ||
celery worker -A cworker.celery --loglevel=info | ||
celery worker -A cworker.celery --loglevel=info & | ||
celery -A cworker.celery beat -l info |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Arusey, you could make the message a bit more specific. When the device is online and the user tries to turn the device online, the message
Device not found
is a bit confusing. You can make the message to beDevice not found or already turned online
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noted