Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
DashboardLeaf
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ProjectLeaf
DashboardLeaf
Commits
59df2919
Commit
59df2919
authored
4 years ago
by
Robert Goldmann
Browse files
Options
Downloads
Patches
Plain Diff
#45 - only send notification once per outdated value:
- reset if time since last value is under threshold again
parent
a31100d2
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/logic/tile/tiles/SensorLineChartTile.py
+9
-0
9 additions, 0 deletions
src/logic/tile/tiles/SensorLineChartTile.py
test/logic/tile/tiles/TestSensorLineChartTile.py
+51
-10
51 additions, 10 deletions
test/logic/tile/tiles/TestSensorLineChartTile.py
with
60 additions
and
10 deletions
src/logic/tile/tiles/SensorLineChartTile.py
+
9
−
0
View file @
59df2919
...
...
@@ -58,6 +58,7 @@ class SensorLineChartTile(Tile):
def
__init__
(
self
,
uniqueName
:
str
,
settings
:
Dict
,
intervalInSeconds
:
int
):
super
().
__init__
(
uniqueName
,
settings
,
intervalInSeconds
)
self
.
_notificationSent
=
False
def
fetch
(
self
,
pageName
:
str
)
->
Dict
:
storageLeafService
=
ServiceManager
.
get_instance
().
get_service_by_type_name
(
'
StorageLeafService
'
)
...
...
@@ -225,6 +226,13 @@ class SensorLineChartTile(Tile):
if
not
warningSettings
[
'
enableNotificationViaPushbullet
'
]:
return
if
not
timeSinceLastValue
:
self
.
_notificationSent
=
False
return
if
self
.
_notificationSent
:
return
token
=
warningSettings
[
'
pushbulletToken
'
]
sensorName
=
sensorInfo
[
'
name
'
]
...
...
@@ -236,6 +244,7 @@ class SensorLineChartTile(Tile):
f
'
(type:
{
sensorType
}
, device:
{
deviceName
}
)
'
Helpers
.
send_notification_via_pushbullet
(
token
,
title
,
description
)
self
.
_notificationSent
=
True
def
__format_date
(
self
,
dateTime
:
str
):
parsedDateTime
=
datetime
.
strptime
(
dateTime
,
self
.
DATE_FORMAT
)
...
...
This diff is collapsed.
Click to expand it.
test/logic/tile/tiles/TestSensorLineChartTile.py
+
51
−
10
View file @
59df2919
import
datetime
from
typing
import
Dict
from
unittest
import
mock
from
unittest.mock
import
MagicMock
...
...
@@ -208,7 +209,7 @@ class TestGetTimeSinceLastValue:
class
TestSendNotification
:
def
__get_warning_settings
(
self
,
enable
:
bool
,
enableNotification
:
bool
):
def
__get_warning_settings
(
self
,
enable
:
bool
,
enableNotification
:
bool
)
->
Dict
:
return
{
'
enable
'
:
enable
,
'
limitInSeconds
'
:
10
,
...
...
@@ -216,6 +217,17 @@ class TestSendNotification:
'
pushbulletToken
'
:
'
myToken
'
}
def
__get_sensor_info
(
self
)
->
Dict
[
str
,
str
]:
return
{
'
name
'
:
'
mySensor
'
,
'
type
'
:
'
temperature
'
}
def
__get_device_info
(
self
)
->
Dict
[
str
,
str
]:
return
{
'
name
'
:
'
myDevice
'
}
@mock.patch
(
'
logic.tile.tiles.SensorLineChartTile.Helpers
'
)
def
test_notification_disabled_should_do_nothing
(
self
,
helpersMock
):
tile
=
SensorLineChartTile
(
'
mySensorTile
'
,
example_settings
(
False
),
10
)
...
...
@@ -225,22 +237,51 @@ class TestSendNotification:
tile
.
_send_notification
(
warningSettings
,
{},
{},
'
1 hour ago
'
)
helpersMock
.
send_notification_via_pushbullet
.
assert_not_called
()
@mock.patch
(
'
logic.tile.tiles.SensorLineChartTile.Helpers
'
)
def
test_notification_enabled_no_outdated_value_should_do_nothing
(
self
,
helpersMock
):
tile
=
SensorLineChartTile
(
'
mySensorTile
'
,
example_settings
(
False
),
10
)
warningSettings
=
self
.
__get_warning_settings
(
True
,
True
)
tile
.
_send_notification
(
warningSettings
,
{},
{},
''
)
helpersMock
.
send_notification_via_pushbullet
.
assert_not_called
()
@mock.patch
(
'
logic.Helpers.requests
'
)
def
test_send_notification_should_call_pushbullet_api
(
self
,
requestsMock
):
tile
=
SensorLineChartTile
(
'
mySensorTile
'
,
example_settings
(
False
),
10
)
warningSettings
=
self
.
__get_warning_settings
(
True
,
True
)
sensorInfo
=
{
'
name
'
:
'
mySensor
'
,
'
type
'
:
'
temperature
'
}
requestsMock
.
post
.
return_value
.
status_code
=
200
deviceInfo
=
{
'
name
'
:
'
myDevice
'
}
tile
.
_send_notification
(
warningSettings
,
self
.
__get_sensor_info
(),
self
.
__get_device_info
(),
'
1 hour ago
'
)
requestsMock
.
post
.
assert_called_once_with
(
Helpers
.
PUSHBULLET_PUSH_URL
,
data
=
mock
.
ANY
,
headers
=
mock
.
ANY
)
@mock.patch
(
'
logic.Helpers.requests
'
)
def
test_already_sent_should_skip_sending
(
self
,
requestsMock
):
tile
=
SensorLineChartTile
(
'
mySensorTile
'
,
example_settings
(
False
),
10
)
warningSettings
=
self
.
__get_warning_settings
(
True
,
True
)
requestsMock
.
post
.
return_value
.
status_code
=
200
tile
.
_send_notification
(
warningSettings
,
sensorInfo
,
deviceInfo
,
'
1 hour ago
'
)
requestsMock
.
post
.
assert_called_once_with
(
Helpers
.
PUSHBULLET_PUSH_URL
,
data
=
mock
.
ANY
,
headers
=
mock
.
ANY
)
tile
.
_send_notification
(
warningSettings
,
self
.
__get_sensor_info
(),
self
.
__get_device_info
(),
'
1 hour ago
'
)
tile
.
_send_notification
(
warningSettings
,
self
.
__get_sensor_info
(),
self
.
__get_device_info
(),
'
1 hour ago
'
)
requestsMock
.
post
.
assert_called_once
()
@mock.patch
(
'
logic.Helpers.requests
'
)
def
test_already_sent_new_value_arrives_and_gets_outdated_should_call_pushbullet_api
(
self
,
requestsMock
):
tile
=
SensorLineChartTile
(
'
mySensorTile
'
,
example_settings
(
False
),
10
)
warningSettings
=
self
.
__get_warning_settings
(
True
,
True
)
requestsMock
.
post
.
return_value
.
status_code
=
200
tile
.
_send_notification
(
warningSettings
,
self
.
__get_sensor_info
(),
self
.
__get_device_info
(),
'
1 hour ago
'
)
# a new valid value arrives
tile
.
_send_notification
(
warningSettings
,
self
.
__get_sensor_info
(),
self
.
__get_device_info
(),
''
)
# value is outdated again
tile
.
_send_notification
(
warningSettings
,
self
.
__get_sensor_info
(),
self
.
__get_device_info
(),
'
1 hour ago
'
)
assert
requestsMock
.
post
.
call_count
==
2
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment