-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmonitor_basic.ts
More file actions
62 lines (53 loc) · 1.54 KB
/
monitor_basic.ts
File metadata and controls
62 lines (53 loc) · 1.54 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
import { ScrapeGraphAI } from "scrapegraph-js";
const sgai = ScrapeGraphAI();
const res = await sgai.monitor.create({
url: "https://time.is/",
name: "Time Monitor",
interval: "*/10 * * * *",
formats: [
{
type: "json",
prompt: "Extract the current time",
schema: {
type: "object",
properties: {
time: { type: "string" },
},
required: ["time"],
},
},
],
});
if (res.status !== "success" || !res.data) {
throw new Error(`Failed to create monitor: ${res.error}`);
}
const { cronId: monitorId, interval } = res.data;
console.log(`Monitor created: ${monitorId}`);
console.log(`Interval: ${interval}`);
console.log("\nPolling for activity (Ctrl+C to stop)...\n");
function cleanup() {
console.log("\nStopping monitor...");
sgai.monitor.delete(monitorId).then(() => {
console.log("Monitor deleted");
process.exit(0);
});
}
process.on("SIGINT", cleanup);
const seenIds = new Set<string>();
while (true) {
const activity = await sgai.monitor.activity(monitorId);
if (activity.status === "success" && activity.data) {
for (const tick of activity.data.ticks) {
if (seenIds.has(tick.id)) continue;
seenIds.add(tick.id);
const changes = tick.changed ? "CHANGED" : "no change";
console.log(`[${tick.createdAt}] ${tick.status} - ${changes} (${tick.elapsedMs}ms)`);
if (tick.diffs && Object.keys(tick.diffs).length > 0) {
console.log(" Diffs:", JSON.stringify(tick.diffs, null, 2));
} else if (tick.changed) {
console.log(" (no diffs data)");
}
}
}
await new Promise((r) => setTimeout(r, 30000));
}