Skip to content

Commit d00efeb

Browse files
refactor: match monitor examples to JS SDK style
- Add seen_ids deduplication - Cleanup in signal handler directly - Show "(no diffs data)" when changed but no diffs Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3ad0eee commit d00efeb

4 files changed

Lines changed: 42 additions & 18 deletions

File tree

examples/monitor/monitor_basic.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,29 @@
3333
print(f"Interval: {res.data.interval}")
3434
print("\nPolling for activity (Ctrl+C to stop)...\n")
3535

36-
running = True
37-
3836
def cleanup(_sig, _frame):
39-
global running
40-
running = False
37+
print("\nStopping monitor...")
38+
sgai.monitor.delete(monitor_id)
39+
print("Monitor deleted")
40+
exit(0)
4141

4242
signal.signal(signal.SIGINT, cleanup)
4343

44-
while running:
44+
seen_ids = set()
45+
46+
while True:
4547
activity = sgai.monitor.activity(monitor_id)
4648
if activity.status == "success" and activity.data:
4749
for tick in activity.data.ticks:
50+
if tick.id in seen_ids:
51+
continue
52+
seen_ids.add(tick.id)
53+
4854
changes = "CHANGED" if tick.changed else "no change"
4955
print(f"[{tick.created_at}] {tick.status} - {changes} ({tick.elapsed_ms}ms)")
5056
diffs = tick.diffs.model_dump(exclude_none=True)
5157
if diffs:
5258
print(f" Diffs: {json.dumps(diffs, indent=2)}")
59+
elif tick.changed:
60+
print(" (no diffs data)")
5361
time.sleep(30)
54-
55-
print("\nStopping monitor...")
56-
sgai.monitor.delete(monitor_id)
57-
print("Monitor deleted")

examples/monitor/monitor_basic_async.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,24 @@ async def main():
3232
print(f"Interval: {res.data.interval}")
3333
print("\nPolling for activity (Ctrl+C to stop)...\n")
3434

35+
seen_ids = set()
36+
3537
try:
3638
while True:
3739
activity = await sgai.monitor.activity(monitor_id)
3840
if activity.status == "success" and activity.data:
3941
for tick in activity.data.ticks:
42+
if tick.id in seen_ids:
43+
continue
44+
seen_ids.add(tick.id)
45+
4046
changes = "CHANGED" if tick.changed else "no change"
4147
print(f"[{tick.created_at}] {tick.status} - {changes} ({tick.elapsed_ms}ms)")
4248
diffs = tick.diffs.model_dump(exclude_none=True)
4349
if diffs:
4450
print(f" Diffs: {json.dumps(diffs, indent=2)}")
51+
elif tick.changed:
52+
print(" (no diffs data)")
4553
await asyncio.sleep(30)
4654
except asyncio.CancelledError:
4755
pass

examples/monitor/monitor_with_webhook.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,29 @@
3535
print("Webhook configured")
3636
print("\nPolling for activity (Ctrl+C to stop)...\n")
3737

38-
running = True
39-
4038
def cleanup(_sig, _frame):
41-
global running
42-
running = False
39+
print("\nStopping monitor...")
40+
sgai.monitor.delete(monitor_id)
41+
print("Monitor deleted")
42+
exit(0)
4343

4444
signal.signal(signal.SIGINT, cleanup)
4545

46-
while running:
46+
seen_ids = set()
47+
48+
while True:
4749
activity = sgai.monitor.activity(monitor_id)
4850
if activity.status == "success" and activity.data:
4951
for tick in activity.data.ticks:
52+
if tick.id in seen_ids:
53+
continue
54+
seen_ids.add(tick.id)
55+
5056
changes = "CHANGED" if tick.changed else "no change"
5157
print(f"[{tick.created_at}] {tick.status} - {changes} ({tick.elapsed_ms}ms)")
5258
diffs = tick.diffs.model_dump(exclude_none=True)
5359
if diffs:
5460
print(f" Diffs: {json.dumps(diffs, indent=2)}")
61+
elif tick.changed:
62+
print(" (no diffs data)")
5563
time.sleep(30)
56-
57-
print("\nStopping monitor...")
58-
sgai.monitor.delete(monitor_id)
59-
print("Monitor deleted")

examples/monitor/monitor_with_webhook_async.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,24 @@ async def main():
3434
print("Webhook configured")
3535
print("\nPolling for activity (Ctrl+C to stop)...\n")
3636

37+
seen_ids = set()
38+
3739
try:
3840
while True:
3941
activity = await sgai.monitor.activity(monitor_id)
4042
if activity.status == "success" and activity.data:
4143
for tick in activity.data.ticks:
44+
if tick.id in seen_ids:
45+
continue
46+
seen_ids.add(tick.id)
47+
4248
changes = "CHANGED" if tick.changed else "no change"
4349
print(f"[{tick.created_at}] {tick.status} - {changes} ({tick.elapsed_ms}ms)")
4450
diffs = tick.diffs.model_dump(exclude_none=True)
4551
if diffs:
4652
print(f" Diffs: {json.dumps(diffs, indent=2)}")
53+
elif tick.changed:
54+
print(" (no diffs data)")
4755
await asyncio.sleep(30)
4856
except asyncio.CancelledError:
4957
pass

0 commit comments

Comments
 (0)