-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage_exclusions.py
More file actions
107 lines (82 loc) · 3.72 KB
/
manage_exclusions.py
File metadata and controls
107 lines (82 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import argparse
import csv
import sys
from datetime import datetime, timezone
from google.cloud import firestore
from config import logger
from firestore_client import get_firestore_client
def _get_doc_ref(db: firestore.Client, eval_function_name: str):
return db.collection("excluded-submissions").document(eval_function_name)
def _handle_add(db: firestore.Client, args: argparse.Namespace) -> None:
ids_to_add = set(args.ids or [])
if args.from_csv:
try:
total_rows = 0
with open(args.from_csv, newline="") as f:
for row in csv.DictReader(f):
total_rows += 1
sid = (row.get("submission_id") or "").strip()
if sid:
ids_to_add.add(sid)
print(f" {args.from_csv}: {len(ids_to_add)} valid ID(s) from {total_rows} row(s).")
except FileNotFoundError:
logger.error(f"CSV file not found: {args.from_csv}")
sys.exit(1)
if not ids_to_add:
print("No IDs provided. Use --ids or --from_csv.")
sys.exit(1)
ids_list = list(ids_to_add)
doc_ref = _get_doc_ref(db, args.eval_function_name)
doc_ref.set(
{"ids": firestore.ArrayUnion(ids_list), "updated_at": datetime.now(timezone.utc)},
merge=True,
)
print(f"Added {len(ids_list)} ID(s) to excluded-submissions/{args.eval_function_name}.")
def _handle_remove(db: firestore.Client, args: argparse.Namespace) -> None:
ids_to_remove = list(set(args.ids))
doc_ref = _get_doc_ref(db, args.eval_function_name)
doc_ref.set(
{"ids": firestore.ArrayRemove(ids_to_remove), "updated_at": datetime.now(timezone.utc)},
merge=True,
)
print(f"Removed {len(ids_to_remove)} ID(s) from excluded-submissions/{args.eval_function_name}.")
def _handle_list(db: firestore.Client, args: argparse.Namespace) -> None:
snapshot = _get_doc_ref(db, args.eval_function_name).get()
if not snapshot.exists:
print(f"No exclusions configured for {args.eval_function_name}.")
return
ids = snapshot.get("ids") or []
updated_at = snapshot.get("updated_at")
print(f"{len(ids)} excluded submission ID(s) for {args.eval_function_name}:")
for sid in ids:
print(f" {sid}")
if updated_at:
print(f"Last updated: {updated_at}")
def main() -> None:
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass
parser = argparse.ArgumentParser(description="Manage excluded submission IDs in Firestore.")
parser.add_argument("--eval_function_name", required=True, help="Evaluation function name")
subparsers = parser.add_subparsers(dest="subcommand", required=True)
add_parser = subparsers.add_parser("add", help="Add submission IDs to the exclusion list")
add_parser.add_argument("--ids", nargs="*", default=[], metavar="UUID")
add_parser.add_argument("--from_csv", default=None, metavar="PATH",
help="CSV file with a 'submission_id' column")
remove_parser = subparsers.add_parser("remove", help="Remove submission IDs from the exclusion list")
remove_parser.add_argument("--ids", nargs="+", required=True, metavar="UUID")
subparsers.add_parser("list", help="List currently excluded submission IDs")
args = parser.parse_args()
if args.subcommand == "add" and not args.ids and not args.from_csv:
parser.error("add requires at least one of --ids or --from_csv")
db, _ = get_firestore_client()
if args.subcommand == "add":
_handle_add(db, args)
elif args.subcommand == "remove":
_handle_remove(db, args)
elif args.subcommand == "list":
_handle_list(db, args)
if __name__ == "__main__":
main()