Skip to content

Commit

Permalink
chore(scripts): add script for deleting old users that never logged a…
Browse files Browse the repository at this point in the history
…ny data
  • Loading branch information
benji6 committed Jul 2, 2024
1 parent a6d0898 commit 08ce724
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
analytics:
@cd scripts && poetry run python3 ./analytics.py

# Deletes old users that have never logged any data
cleanup_zombie_users:
@cd scripts && poetry run python3 ./cleanup_zombie_users.py

# Makes notifications_send.zip
scripts/cloudformation/lambdas/notifications_send.zip: scripts/cloudformation/lambdas/notifications_send/*
@cd scripts/cloudformation/lambdas/notifications_send &&\
Expand Down Expand Up @@ -77,4 +81,4 @@ test: cloudformation/test
test/ci:
@cd client && npm run test-ci && echo "🍄 All tests pass! 🍄"

.PHONY: analytics cloudformation/test deploy deploy/dry-run deploy/notifications_send help init init/ci stack-policy start test test/ci
.PHONY: analytics cleanup_zombie_users cloudformation/test deploy deploy/dry-run deploy/notifications_send help init init/ci stack-policy start test test/ci
34 changes: 34 additions & 0 deletions scripts/cleanup_zombie_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import boto3
from boto3.dynamodb.conditions import Key
from datetime import datetime, timedelta, timezone

USER_POOL_ID = "us-east-1_rdB8iu5X4"

events_table = boto3.resource("dynamodb").Table("moodtracker_events")
cognito_idp_client = boto3.client("cognito-idp")

users_to_delete = [
u
for u in cognito_idp_client.get_paginator("list_users")
.paginate(
UserPoolId=USER_POOL_ID,
AttributesToGet=[],
)
.build_full_result()["Users"]
if u["UserLastModifiedDate"] < datetime.now(timezone.utc) - timedelta(365)
and not events_table.query(
KeyConditionExpression=Key("userId").eq(u["Username"]),
Limit=1,
)["Count"]
]
print("Users to delete:", users_to_delete)

if input('Type "yes" to proceed or anything else to abort: ') == "yes":
print("Deleting users...")
for user in users_to_delete:
cognito_idp_client.admin_delete_user(
UserPoolId=USER_POOL_ID, Username=user["Username"]
)
print("Deleted user ID: ", user["Username"])
else:
print("Aborted")
3 changes: 2 additions & 1 deletion scripts/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[tool.poetry]
name = "moodtracker-analytics"
name = "moodtracker-scripts"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.8"
Expand Down

0 comments on commit 08ce724

Please sign in to comment.