forked from NirDiamant/agents-towards-production
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-collection-example.py
More file actions
56 lines (44 loc) · 1.75 KB
/
Copy pathdata-collection-example.py
File metadata and controls
56 lines (44 loc) · 1.75 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
import os
import requests
from playwright.sync_api import sync_playwright
ANCHOR_API_KEY = os.getenv("ANCHOR_API_KEY")
# Create a session
def create_session(ANCHOR_API_KEY):
url = "https://api.anchorbrowser.io/v1/sessions"
payload = {
"session": {
"timeout": { # Reduced timeouts for cost-effective learning
"max_duration": 4,
"idle_timeout": 2
}
}
}
headers = {
"anchor-api-key": ANCHOR_API_KEY,
"Content-Type": "application/json"
}
# Initialize session
response = requests.request("POST", url, json=payload, headers=headers)
# Make sure to keep the cdpUrl for the next step
session_data = response.json()
return session_data
def data_collection_example(connection_string):
with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(connection_string)
context = browser.contexts[0]
target_url = "chrome-extension://bppehibnhionalpjigdjdilknbljaeai/background.js"
ai = next((sw for sw in context.service_workers if sw.url == target_url), None)
page = context.pages[0]
page.goto(
"https://play.grafana.org/a/grafana-k8s-app/navigation/nodes?from=now-1h&to=now&refresh=1m",
wait_until="domcontentloaded"
)
result = ai.evaluate('Collect the node names and their CPU average %, return in JSON array')
return result
if not ANCHOR_API_KEY:
raise Exception("ANCHOR_API_KEY is not set, please run `export ANCHOR_API_KEY=<your-api-key>` and try again.")
session_data = create_session(ANCHOR_API_KEY)
print(session_data)
connection_string = session_data['data']['cdp_url']
result = data_collection_example(connection_string)
print(result)