Upload 2 files
Browse files- deepboreai_sdk/cli.py +30 -0
- deepboreai_sdk/sdk.py +37 -0
deepboreai_sdk/cli.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import typer
|
| 2 |
+
import json
|
| 3 |
+
from deepboreai_sdk.sdk import DeepBoreAI
|
| 4 |
+
|
| 5 |
+
app = typer.Typer()
|
| 6 |
+
client = DeepBoreAI()
|
| 7 |
+
|
| 8 |
+
@app.command()
|
| 9 |
+
def ingest(file: str):
|
| 10 |
+
with open(file, 'r') as f:
|
| 11 |
+
data = json.load(f)
|
| 12 |
+
result = client.post_telemetry(data)
|
| 13 |
+
typer.echo(result)
|
| 14 |
+
|
| 15 |
+
@app.command()
|
| 16 |
+
def export(out: str = 'drilling_data.csv'):
|
| 17 |
+
path = client.export_csv(out)
|
| 18 |
+
typer.echo(f"Exported to {path}")
|
| 19 |
+
|
| 20 |
+
@app.command()
|
| 21 |
+
def stream():
|
| 22 |
+
def callback(data):
|
| 23 |
+
typer.echo(f"Live Alert: {data}")
|
| 24 |
+
typer.echo("Listening to live data stream...")
|
| 25 |
+
client.watch_live(callback)
|
| 26 |
+
input("Press Enter to stop...
|
| 27 |
+
")
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
app()
|
deepboreai_sdk/sdk.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import websocket
|
| 3 |
+
import threading
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
class DeepBoreAI:
|
| 7 |
+
def __init__(self, host='http://localhost:8000'):
|
| 8 |
+
self.host = host
|
| 9 |
+
|
| 10 |
+
def post_telemetry(self, data: dict):
|
| 11 |
+
url = f"{self.host}/ingest"
|
| 12 |
+
response = requests.post(url, json=data)
|
| 13 |
+
return response.json()
|
| 14 |
+
|
| 15 |
+
def get_recent_alerts(self):
|
| 16 |
+
url = f"{self.host}/history"
|
| 17 |
+
response = requests.get(url)
|
| 18 |
+
return response.json()
|
| 19 |
+
|
| 20 |
+
def export_csv(self, out_path='drilling_data.csv'):
|
| 21 |
+
url = f"{self.host}/export"
|
| 22 |
+
r = requests.get(url)
|
| 23 |
+
with open(out_path, 'wb') as f:
|
| 24 |
+
f.write(r.content)
|
| 25 |
+
return out_path
|
| 26 |
+
|
| 27 |
+
def watch_live(self, callback):
|
| 28 |
+
def on_message(ws, message):
|
| 29 |
+
data = json.loads(message)
|
| 30 |
+
callback(data)
|
| 31 |
+
|
| 32 |
+
ws = websocket.WebSocketApp(f"{self.host.replace('http', 'ws')}/ws",
|
| 33 |
+
on_message=on_message)
|
| 34 |
+
thread = threading.Thread(target=ws.run_forever)
|
| 35 |
+
thread.daemon = True
|
| 36 |
+
thread.start()
|
| 37 |
+
return ws
|