Skip to content

Commit 008b70b

Browse files
committed
send project progress notifications #330
1 parent 3fb5b5e commit 008b70b

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

mapswipe_workers/mapswipe_workers/mapswipe_workers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
from mapswipe_workers.project_types.footprint.footprint_project import FootprintProject
2828
from mapswipe_workers.utils import user_management
2929
from mapswipe_workers.utils.create_directories import create_directories
30-
from mapswipe_workers.utils.slack_helper import send_slack_message
30+
from mapswipe_workers.utils.slack_helper import (
31+
send_slack_message,
32+
send_progress_notification,
33+
)
3134

3235

3336
class PythonLiteralOption(click.Option):
@@ -101,6 +104,7 @@ def run_firebase_to_postgres() -> list:
101104
update_data.update_user_data()
102105
update_data.update_project_data()
103106
project_ids = transfer_results.transfer_results()
107+
[send_progress_notification(project_id) for project_id in project_ids]
104108
return project_ids
105109

106110

mapswipe_workers/mapswipe_workers/utils/slack_helper.py

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
"""Initialize slack client with values provided by the config file"""
2-
3-
import json
4-
51
import slack
6-
72
from mapswipe_workers.definitions import logger
3+
from mapswipe_workers import auth
84
from mapswipe_workers.config import SLACK_CHANNEL, SLACK_TOKEN
95

106

117
def send_slack_message(message_type: str, project_name: str, project_id: str = None):
12-
8+
"""Initialize slack client with values provided in environment."""
139
if SLACK_TOKEN is None or SLACK_CHANNEL is None:
1410
logger.info(
1511
"No configuration for Slack was found. "
@@ -27,14 +23,58 @@ def send_slack_message(message_type: str, project_name: str, project_id: str = N
2723
+ "Make sure to activate the project using the manager dashboard.\n"
2824
+ "Happy Swiping. :)"
2925
)
26+
slack_client.chat_postMessage(channel=SLACK_CHANNEL, text=message)
3027
elif message_type == "fail":
3128
message = (
3229
"### PROJECT CREATION FAILED ###\n"
3330
+ "Project Name: {0}\n".format(project_name)
3431
+ "Project draft is deleted."
3532
)
33+
slack_client.chat_postMessage(channel=SLACK_CHANNEL, text=message)
34+
elif message_type == "notification_90":
35+
message = (
36+
"### ALMOST THERE! PROJECT REACHED 90% ###\n"
37+
+ "Project Name: {0}\n".format(project_name)
38+
+ "Project Id: {0}\n\n".format(project_id)
39+
+ "Get your next projects ready."
40+
)
41+
slack_client.chat_postMessage(channel="mapswipe_managers", text=message)
42+
elif message_type == "notification_100":
43+
message = (
44+
"### GREAT! PROJECT REACHED 100% ###\n"
45+
+ "Project Name: {0}\n".format(project_name)
46+
+ "Project Id: {0}\n\n".format(project_id)
47+
+ "You can set this project to 'finished' "
48+
+ "and activate another one."
49+
)
50+
slack_client.chat_postMessage(channel="mapswipe_managers", text=message)
3651
else:
3752
# TODO: Raise an Exception
3853
pass
3954

40-
slack_client.chat_postMessage(channel=SLACK_CHANNEL, text=message)
55+
56+
def send_progress_notification(project_id: int):
57+
"""Send progress notification to project managers in Slack."""
58+
fb_db = auth.firebaseDB()
59+
progress = fb_db.reference(f"v2/projects/{project_id}/progress").get()
60+
project_name = fb_db.reference(f"v2/projects/{project_id}/name").get()
61+
notification_90_sent = fb_db.reference(
62+
f"v2/projects/{project_id}/notification_90_sent"
63+
).get()
64+
notification_100_sent = fb_db.reference(
65+
f"v2/projects/{project_id}/notification_100_sent"
66+
).get()
67+
logger.info(
68+
f"{project_id} - progress: {progress},"
69+
f"notifications: {notification_90_sent} {notification_100_sent}"
70+
)
71+
72+
if progress >= 90 and not notification_90_sent:
73+
# send notification and set value in firebase
74+
fb_db.reference(f"v2/projects/{project_id}/notification_90_sent").set(True)
75+
send_slack_message("notification_90", project_name, project_id)
76+
77+
if progress >= 100 and not notification_100_sent:
78+
# send notification and set value in firebase
79+
fb_db.reference(f"v2/projects/{project_id}/notification_100_sent").set(True)
80+
send_slack_message("notification_100", project_name, project_id)

0 commit comments

Comments
 (0)