Skip to content

Commit 438c03e

Browse files
committed
Add filter for non-teams
1 parent b6e4f05 commit 438c03e

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

scripts/collect_coder_analytics.py

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818

1919

2020
try:
21-
from google.cloud import storage
21+
from google.cloud import firestore, storage
2222
except ImportError:
23-
print(
24-
"Error: google-cloud-storage not installed. Run: pip install google-cloud-storage"
25-
)
23+
print("Error: google-cloud-storage or google-cloud-firestore not installed.")
24+
print("Run: pip install google-cloud-storage google-cloud-firestore")
2625
sys.exit(1)
2726

2827

@@ -45,12 +44,59 @@ def run_command(cmd: list[str]) -> Any:
4544
sys.exit(1)
4645

4746

48-
def fetch_workspaces() -> list[dict[str, Any]]:
49-
"""Fetch all workspaces using Coder CLI."""
47+
def get_team_mappings() -> dict[str, str]:
48+
"""Get team mappings from Firestore.
49+
50+
Returns a dict mapping github_handle (lowercase) -> team_name.
51+
"""
52+
print("Fetching team mappings from Firestore...")
53+
54+
project_id = "coderd"
55+
database_id = "onboarding"
56+
57+
db = firestore.Client(project=project_id, database=database_id)
58+
59+
mappings = {}
60+
participants = db.collection("participants").stream()
61+
62+
for doc in participants:
63+
data = doc.to_dict()
64+
if data:
65+
github_handle = doc.id.lower()
66+
team_name = data.get("team_name", "Unassigned")
67+
mappings[github_handle] = team_name
68+
69+
print(f"✓ Loaded {len(mappings)} participant team mappings")
70+
return mappings
71+
72+
73+
def fetch_workspaces(team_mappings: dict[str, str]) -> list[dict[str, Any]]:
74+
"""Fetch all workspaces using Coder CLI and filter out excluded teams."""
5075
print("Fetching workspaces from Coder...")
5176
workspaces = run_command(["coder", "list", "-a", "-o", "json"])
52-
print(f"✓ Fetched {len(workspaces)} workspaces")
53-
return workspaces
77+
78+
# Teams to exclude from analytics
79+
excluded_teams = ["facilitators", "Unassigned"]
80+
81+
original_count = len(workspaces)
82+
83+
# Filter out workspaces owned by users in excluded teams
84+
filtered_workspaces = []
85+
for ws in workspaces:
86+
owner_name = ws.get("owner_name", "").lower()
87+
team_name = team_mappings.get(owner_name, "Unassigned")
88+
89+
if team_name not in excluded_teams:
90+
filtered_workspaces.append(ws)
91+
92+
filtered_count = original_count - len(filtered_workspaces)
93+
if filtered_count > 0:
94+
print(
95+
f"✓ Filtered out {filtered_count} workspaces from excluded teams: {', '.join(excluded_teams)}"
96+
)
97+
98+
print(f"✓ Fetched {len(filtered_workspaces)} workspaces")
99+
return filtered_workspaces
54100

55101

56102
def fetch_templates() -> list[dict[str, Any]]:
@@ -156,8 +202,11 @@ def main() -> None:
156202
bucket_name = "coder-analytics-snapshots"
157203
save_local = "--local" in sys.argv
158204

159-
# Fetch data
160-
workspaces = fetch_workspaces()
205+
# Fetch team mappings first
206+
team_mappings = get_team_mappings()
207+
208+
# Fetch data (with filtering)
209+
workspaces = fetch_workspaces(team_mappings)
161210
templates = fetch_templates()
162211

163212
# Create snapshot

0 commit comments

Comments
 (0)