diff --git a/manager_dashboard/manager_dashboard/js/project-management.js b/manager_dashboard/manager_dashboard/js/project-management.js index d4c7537aa..67279fde5 100644 --- a/manager_dashboard/manager_dashboard/js/project-management.js +++ b/manager_dashboard/manager_dashboard/js/project-management.js @@ -94,7 +94,7 @@ function updateIsFeatured(projectId, newStatus) { } function updateTableView() { - status_array = ["new", "active", "inactive", "finished"] + status_array = ["active", "inactive", "finished", "archived"] for (var i = 0; i < status_array.length; i++) { status = status_array[i] diff --git a/mapswipe_workers/mapswipe_workers/mapswipe_workers.py b/mapswipe_workers/mapswipe_workers/mapswipe_workers.py index 2152cc649..ee5898e61 100644 --- a/mapswipe_workers/mapswipe_workers/mapswipe_workers.py +++ b/mapswipe_workers/mapswipe_workers/mapswipe_workers.py @@ -91,14 +91,14 @@ def _run_create_projects(): project.calc_required_results() # Save project and its groups and tasks to Firebase and Postgres. project.save_project() + send_slack_message("success", project_name, project.projectId) + logger.info("Success: Project Creation ({0})".format(project_name)) except CustomError: ref = fb_db.reference(f"v2/projectDrafts/{project_draft_id}") ref.set({}) send_slack_message("fail", project_name, project.projectId) logger.exception("Failed: Project Creation ({0}))".format(project_name)) sentry.capture_exception() - send_slack_message("success", project_name, project.projectId) - logger.info("Success: Project Creation ({0})".format(project_name)) continue diff --git a/mapswipe_workers/scripts/archive_old_projects.py b/mapswipe_workers/scripts/archive_old_projects.py new file mode 100644 index 000000000..5c2d9ae99 --- /dev/null +++ b/mapswipe_workers/scripts/archive_old_projects.py @@ -0,0 +1,91 @@ +from mapswipe_workers import auth +from mapswipe_workers.definitions import logger + + +def get_old_projects(): + """ + Get all projects from Firebase which have been created + before we switched to v2. + """ + fb_db = auth.firebaseDB() + ref = fb_db.reference("projects") + projects = ref.get() + logger.info("got old projects from firebase") + return projects + + +def move_project_data_to_v2(project_id): + """ + Copy project information from old path to v2/projects in Firebase. + Add status=archived attribute. + Use Firebase transaction function for this. + """ + + # Firebase transaction function + def transfer(current_data): + # we need to add these attributes + # since they are expected for version 2 + current_data["status"] = "archived" + current_data["projectType"] = 1 + current_data["projectId"] = str(project_id) + current_data["progress"] = current_data.get("progress", 0) + current_data["name"] = current_data.get("name", "unknown") + fb_db.reference("v2/projects/{0}".format(project_id)).set(current_data) + return dict() + + fb_db = auth.firebaseDB() + projects_ref = fb_db.reference(f"projects/{project_id}") + try: + projects_ref.transaction(transfer) + logger.info(f"{project_id}: Transfered project to v2 and delete in old path") + return True + except fb_db.TransactionAbortedError: + logger.exception( + f"{project_id}: Firebase transaction" + f"for transferring project failed to commit" + ) + return False + + +def delete_old_groups(project_id): + """ + Delete old groups for a project + """ + fb_db = auth.firebaseDB() + fb_db.reference("groups/{0}".format(project_id)).set({}) + logger.info(f"deleted groups for: {project_id}") + + +def delete_other_old_data(): + """ + Delete old imports, results, announcements in Firebase + """ + fb_db = auth.firebaseDB() + fb_db.reference("imports").set({}) + fb_db.reference("results").set({}) + fb_db.reference("announcements").set({}) + logger.info(f"deleted old results, imports, announcements") + + +def archive_old_projects(): + """ + Run workflow to archive old projects. + First get all old projects. + Move project data to v2/projects in Firebase and + set status=archived. + Then delete all groups for a project. + Finally, delete old results, imports and announcements. + We don't touch the old user data in this workflow. + """ + + projects = get_old_projects() + for project_id in projects.keys(): + if move_project_data_to_v2(project_id): + delete_old_groups(project_id) + else: + logger.info(f"didn't delete project and groups for project: {project_id}") + + delete_other_old_data() + + +archive_old_projects()