Skip to content

Commit f73a92e

Browse files
committed
add script to automatically update project status
1 parent 18abeaa commit f73a92e

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

mapswipe_workers/mapswipe_workers/definitions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ class MessageType(Enum):
113113
FAIL = 2
114114
NOTIFICATION_90 = 3
115115
NOTIFICATION_100 = 4
116+
PROJECT_STATUS_FINISHED = 5
117+
PROJECT_STATUS_ACTIVE = 6
116118

117119

118120
class ProjectType(Enum):

mapswipe_workers/mapswipe_workers/utils/slack_helper.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ def send_slack_message(
5858
+ "and activate another one."
5959
)
6060
slack_client.chat_postMessage(channel="mapswipe_managers", text=message)
61+
elif message_type == MessageType.PROJECT_STATUS_FINISHED:
62+
message = (
63+
"### SET PROJECT STATUS TO FINISHED ###\n"
64+
+ f"Project Name: {project_name}\n"
65+
+ f"Project Id: {project_id}\n\n"
66+
+ "The status of the project has been set to 'finished' "
67+
+ "by MapSwipe's backend workers."
68+
)
69+
slack_client.chat_postMessage(channel=SLACK_CHANNEL, text=message)
70+
elif message_type == MessageType.PROJECT_STATUS_ACTIVE:
71+
message = (
72+
"### SET PROJECT STATUS TO ACTIVE ###\n"
73+
+ f"Project Name: {project_name}\n"
74+
+ f"Project Id: {project_id}\n\n"
75+
+ "The status of the project has been set to 'active' "
76+
+ "by MapSwipe's backend workers."
77+
)
78+
slack_client.chat_postMessage(channel=SLACK_CHANNEL, text=message)
6179
else:
6280
# TODO: Raise an Exception
6381
pass
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from mapswipe_workers.auth import firebaseDB
2+
from mapswipe_workers.definitions import MessageType, logger
3+
from mapswipe_workers.utils.slack_helper import send_slack_message
4+
5+
6+
def get_projects(status):
7+
"""Load 'active' projects from Firebase."""
8+
fb_db = firebaseDB()
9+
projects = (
10+
fb_db.reference("v2/projects/").order_by_child("status").equal_to(status).get()
11+
)
12+
logger.info(f"got {len(projects)} {status} projects from firebase")
13+
return projects
14+
15+
16+
def filter_projects_by_name_and_progress(projects, filter_string, progress_threshold):
17+
"""Load 'active' projects from Firebase."""
18+
selected_project_ids = []
19+
for project_id in projects.keys():
20+
name = projects[project_id]["name"]
21+
progress = projects[project_id]["progress"]
22+
if filter_string in name and progress >= progress_threshold:
23+
selected_project_ids.append(project_id)
24+
25+
logger.info(
26+
f"selected {len(selected_project_ids)} project(s) which contain(s) "
27+
f"'{filter_string}' in the project name and progress >= {progress_threshold}%."
28+
)
29+
return selected_project_ids
30+
31+
32+
def set_status_in_firebase(project_id, project_name, new_status):
33+
"""Change status of a project in Firebase."""
34+
# change status in firebase
35+
fb_db = firebaseDB()
36+
fb_db.reference(f"v2/projects/{project_id}/status").set(new_status)
37+
logger.info(f"set project status to '{new_status}' for project {project_id}")
38+
39+
# send slack message
40+
if new_status == "finished":
41+
send_slack_message(
42+
MessageType.PROJECT_STATUS_FINISHED, project_name, project_id
43+
)
44+
elif new_status == "active":
45+
send_slack_message(MessageType.PROJECT_STATUS_ACTIVE, project_name, project_id)
46+
47+
48+
if __name__ == "__main__":
49+
"""Check if project status should be updated and change value in Firebase."""
50+
filter_string = "Test"
51+
52+
active_projects = get_projects(status="active")
53+
finished_projects = filter_projects_by_name_and_progress(
54+
active_projects, filter_string, progress_threshold=1,
55+
)
56+
57+
inactive_projects = get_projects(status="inactive")
58+
new_active_projects = filter_projects_by_name_and_progress(
59+
inactive_projects, filter_string, progress_threshold=0,
60+
)[0 : len(finished_projects)]
61+
62+
if len(new_active_projects) > 0:
63+
for project_id in finished_projects:
64+
project_name = active_projects[project_id]["name"]
65+
set_status_in_firebase(project_id, project_name, new_status="finished")
66+
67+
for project_id in new_active_projects:
68+
project_name = inactive_projects[project_id]["name"]
69+
set_status_in_firebase(project_id, project_name, new_status="active")

0 commit comments

Comments
 (0)