Skip to content

Commit 805a6fb

Browse files
authored
python(feat): Make ingestion client key optional (#226)
1 parent 10f789a commit 805a6fb

21 files changed

Lines changed: 1061 additions & 41 deletions

File tree

python/examples/ingestion_with_csv/multi_csv/main.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def parse_csv(
4646

4747

4848
def load_telemetry_config(
49-
csv_paths: List[Path], asset_name: str, ingestion_client_key: str
49+
csv_paths: List[Path],
50+
asset_name: str,
5051
) -> Tuple[TelemetryConfig, List[Dict]]:
5152
channels = []
5253
data: List[Dict] = [] # Each channel will have its own dictionary: {timestamp: value}
@@ -72,7 +73,6 @@ def load_telemetry_config(
7273

7374
telemetry_config = TelemetryConfig(
7475
asset_name=asset_name,
75-
ingestion_client_key=ingestion_client_key,
7676
flows=[FlowConfig(name="data", channels=channels)],
7777
)
7878
return telemetry_config, data
@@ -94,12 +94,9 @@ def load_telemetry_config(
9494
asset_name = os.getenv("ASSET_NAME")
9595
assert asset_name, "expected 'ASSET_NAME' environment variable to be set"
9696

97-
ingestion_client_key = os.getenv("INGESTION_CLIENT_KEY")
98-
assert ingestion_client_key, "expected 'INGESTION_CLIENT_KEY' environment variable to be set"
99-
10097
csv_data = [Path("channel_a.csv"), Path("channel_b.csv"), Path("channel_c.csv")]
10198

102-
telemetry_config, data = load_telemetry_config(csv_data, asset_name, ingestion_client_key)
99+
telemetry_config, data = load_telemetry_config(csv_data, asset_name)
103100
flows = parse_csv(data, telemetry_config)
104101

105102
sift_channel_config = SiftChannelConfig(
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
SIFT_API_URI=""
22
SIFT_API_KEY=""
3-
ASSET_NAME=""
4-
INGESTION_CLIENT_KEY=""
3+
ASSET_NAME=""

python/examples/ingestion_with_csv/single_csv/main.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def parse_csv(
4242

4343

4444
def load_telemetry_config(
45-
path_to_csv: Path, asset_name: str, ingestion_client_key: str
45+
path_to_csv: Path,
46+
asset_name: str,
4647
) -> TelemetryConfig:
4748
channels = []
4849

@@ -61,7 +62,6 @@ def load_telemetry_config(
6162

6263
return TelemetryConfig(
6364
asset_name=asset_name,
64-
ingestion_client_key=ingestion_client_key,
6565
flows=[FlowConfig(name="data", channels=channels)],
6666
)
6767

@@ -82,12 +82,9 @@ def load_telemetry_config(
8282
asset_name = os.getenv("ASSET_NAME")
8383
assert asset_name, "expected 'ASSET_NAME' environment variable to be set"
8484

85-
ingestion_client_key = os.getenv("INGESTION_CLIENT_KEY")
86-
assert ingestion_client_key, "expected 'INGESTION_CLIENT_KEY' environment variable to be set"
87-
8885
sample_data_csv = Path("sample_data.csv")
8986

90-
telemetry_config = load_telemetry_config(sample_data_csv, asset_name, ingestion_client_key)
87+
telemetry_config = load_telemetry_config(sample_data_csv, asset_name)
9188
flows = parse_csv(sample_data_csv, telemetry_config)
9289

9390
sift_channel_config = SiftChannelConfig(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Retrieve the BASE_URI from the Sift team
2+
# Be sure to exclude "https://" from the BASE_URI
3+
#
4+
# BASE URIs and further details can be found here:
5+
# https://docs.siftstack.com/docs/api/authentication
6+
BASE_URI=""
7+
8+
SIFT_API_KEY=""
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import logging
2+
import os
3+
from datetime import datetime, timezone
4+
5+
from dotenv import load_dotenv
6+
from sift_py.grpc.transport import SiftChannelConfig, use_sift_channel
7+
from sift_py.ingestion.service import IngestionService
8+
from simulator import Simulator
9+
from telemetry_config import get_new_flow_config, nostromos_lv_426
10+
11+
if __name__ == "__main__":
12+
"""
13+
Example of telemetering data for the asset of name 'NostromoLV426' with various channels
14+
and rules. Also shows how to dynamically add new flows to your configuration at run time.
15+
The simulator will be sending data for various flows at various frequencies.
16+
"""
17+
logging.basicConfig(level=logging.DEBUG)
18+
logger = logging.getLogger(__name__)
19+
20+
load_dotenv()
21+
22+
apikey = os.getenv("SIFT_API_KEY")
23+
24+
if apikey is None:
25+
raise Exception("Missing 'SIFT_API_KEY' environment variable.")
26+
27+
base_uri = os.getenv("BASE_URI")
28+
29+
if base_uri is None:
30+
raise Exception("Missing 'BASE_URI' environment variable.")
31+
32+
# Load your telemetry config
33+
telemetry_config = nostromos_lv_426()
34+
35+
# Create a gRPC transport channel configured specifically for the Sift API
36+
sift_channel_config = SiftChannelConfig(uri=base_uri, apikey=apikey)
37+
38+
with use_sift_channel(sift_channel_config) as channel:
39+
# Create ingestion service using the telemetry config we loaded in
40+
ingestion_service = IngestionService(
41+
channel,
42+
telemetry_config,
43+
end_stream_on_error=True, # End stream if errors occur API-side.
44+
)
45+
46+
# Create an optional run as part of this ingestion
47+
current_ts = datetime.now(timezone.utc)
48+
run_name = f"[{telemetry_config.asset_name}].{current_ts.timestamp()}"
49+
ingestion_service.attach_run(channel, run_name, "Run simulation")
50+
51+
# Dynamically add a new flow. This is done before starting the simulator
52+
# for this example, but can be done at any time after creating the IngestionService.
53+
new_flow = get_new_flow_config()
54+
if new_flow.name in ingestion_service.flow_configs_by_name:
55+
logger.info("New flow already exists, not adding.")
56+
else:
57+
logger.info("New flow does not exists, adding.")
58+
ingestion_service.try_create_flow(new_flow)
59+
60+
# Create our simulator
61+
simulator = Simulator(ingestion_service)
62+
63+
# Run it
64+
simulator.run()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python-dotenv
2+
sift-stack-py
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace: velocity
2+
3+
rules:
4+
- name: vehicle_stuck
5+
description: Triggers if the vehicle velocity is not 0 for 5s after entering accelerating state
6+
expression: $1 == "Accelerating" && persistence($2 == 0, 5)
7+
type: review
8+
9+
- name: vehicle_not_stopped
10+
description: Triggers if the vehicle velocity does not remain 0 while stopped
11+
expression: $1 == "Stopped" && $2 > 0
12+
type: review
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace: voltage
2+
3+
rules:
4+
- name: overvoltage
5+
description: Checks for overvoltage while accelerating
6+
expression: $1 == "Accelerating" && $2 > 80
7+
type: review
8+
9+
- name: undervoltage
10+
description: Checks for undervoltage while accelerating
11+
expression: $1 == "Accelerating" && $2 < 40
12+
type: review

0 commit comments

Comments
 (0)