Skip to content

Commit 9d1e05a

Browse files
committed
Add schema/table config
Introduce INFLUX_SCHEMA and INFLUX_TABLE and extend connect_influxdb3 to accept schema/table so IOx schema.table naming can be configured. Add quote_table utility to centralize safe quoting of schema/table references and update discovery and fetcher to use it when building FROM clauses. Adjust scanner default table logic to use the new schema/table config while preserving existing quoting behavior for legacy table inputs. Small docstring and import/refactor changes included. Handle empty INFLUX_TABLE and fallback defaults
1 parent ef8c5c2 commit 9d1e05a

5 files changed

Lines changed: 58 additions & 9 deletions

File tree

src/slicks/config.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@
1212
INFLUX_ORG: str = os.getenv("INFLUX_ORG", "Docs")
1313
INFLUX_DB: str = os.getenv("INFLUX_DB", "WFR25")
1414

15+
# Schema and Table Configuration (defaulting to iox.INFLUX_DB pattern)
16+
INFLUX_SCHEMA: str = os.getenv("INFLUX_SCHEMA", "iox")
17+
# If env var is not set or empty, default to INFLUX_DB
18+
INFLUX_TABLE: str = os.getenv("INFLUX_TABLE") or INFLUX_DB
19+
1520
def connect_influxdb3(
1621
url: Optional[str] = None,
1722
token: Optional[str] = None,
1823
org: Optional[str] = None,
1924
db: Optional[str] = None,
25+
schema: Optional[str] = None,
26+
table: Optional[str] = None,
2027
) -> None:
2128
"""
2229
Update the global configuration settings for InfluxDB connection.
@@ -28,20 +35,34 @@ def connect_influxdb3(
2835
token: Your InfluxDB API token
2936
org: Organization name (optional for InfluxDB 3.x)
3037
db: Database/bucket name (e.g., "WFR25")
38+
schema: IOx schema name (default: "iox")
39+
table: IOx table name (default: same as db)
3140
3241
Example:
3342
>>> import slicks
3443
>>> slicks.connect_influxdb3(
3544
... url="https://us-east-1-1.aws.cloud2.influxdata.com",
3645
... token="your-api-token",
37-
... db="WFR25"
46+
... db="WFR25",
47+
... table="my_custom_table"
3848
... )
3949
"""
40-
global INFLUX_URL, INFLUX_TOKEN, INFLUX_ORG, INFLUX_DB
50+
global INFLUX_URL, INFLUX_TOKEN, INFLUX_ORG, INFLUX_DB, INFLUX_SCHEMA, INFLUX_TABLE
4151
if url: INFLUX_URL = url
4252
if token: INFLUX_TOKEN = token
4353
if org: INFLUX_ORG = org
44-
if db: INFLUX_DB = db
54+
55+
# If DB is updated, we might need to update the default table name if it wasn't overridden
56+
if db:
57+
INFLUX_DB = db
58+
# Only update table if it wasn't explicitly set to something else in this call
59+
# and if it currently matches the OLD db name or is unset
60+
if not table and (not INFLUX_TABLE or INFLUX_TABLE == db):
61+
INFLUX_TABLE = db
62+
63+
if schema: INFLUX_SCHEMA = schema
64+
if table: INFLUX_TABLE = table
65+
4566

4667
# Default Sensor Registry
4768
# In an open-source context, this serves as an "Example Configuration"

src/slicks/discovery.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from tqdm.auto import tqdm
1616

1717
from . import config
18-
from .query_utils import adaptive_query, run_chunks_parallel, PermanentQueryError
18+
from .query_utils import adaptive_query, run_chunks_parallel, PermanentQueryError, quote_table
1919

2020

2121
def discover_sensors(
@@ -52,9 +52,14 @@ def _make_client() -> InfluxDBClient3:
5252
def _query_distinct(
5353
client: InfluxDBClient3, t0: datetime, t1: datetime,
5454
) -> List[str]:
55+
# Ensure safe defaults if config vars are missing or empty
56+
schema = config.INFLUX_SCHEMA or "iox"
57+
table = config.INFLUX_TABLE or config.INFLUX_DB
58+
table_ref = quote_table(schema, table)
59+
5560
sql = f"""
5661
SELECT DISTINCT "signalName"
57-
FROM "iox"."{config.INFLUX_DB}"
62+
FROM {table_ref}
5863
WHERE time >= '{t0.isoformat()}Z'
5964
AND time < '{t1.isoformat()}Z'
6065
"""

src/slicks/fetcher.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import pandas as pd
44
from influxdb_client_3 import InfluxDBClient3
55
from . import config
6+
from .query_utils import quote_table
67
from .movement_detector import filter_data_in_movement
78

9+
810
def get_influx_client(url=None, token=None, org=None, db=None):
911
"""
1012
Returns an InfluxDB Client.
@@ -18,12 +20,14 @@ def get_influx_client(url=None, token=None, org=None, db=None):
1820
database=db or config.INFLUX_DB
1921
)
2022

23+
2124
def list_target_sensors():
2225
"""
2326
Returns the list of DEFAULT sensors configured in config.py.
2427
"""
2528
return config.SIGNALS
2629

30+
2731
def fetch_telemetry(start_time, end_time, signals=None, client=None, filter_movement=True, resample="1s"):
2832
"""
2933
Fetch telemetry data for specified signals within a time range.
@@ -54,12 +58,18 @@ def fetch_telemetry(start_time, end_time, signals=None, client=None, filter_move
5458

5559
# Construct query
5660
signal_list = "', '".join(signals)
61+
62+
# Ensure safe defaults if config vars are missing or empty
63+
schema = config.INFLUX_SCHEMA or "iox"
64+
table = config.INFLUX_TABLE or config.INFLUX_DB
65+
table_ref = quote_table(schema, table)
66+
5767
query = f"""
5868
SELECT
5969
time,
6070
"signalName",
6171
"sensorReading"
62-
FROM "iox"."{config.INFLUX_DB}"
72+
FROM {table_ref}
6373
WHERE
6474
"signalName" IN ('{signal_list}')
6575
AND time >= '{start_time.isoformat()}Z'

src/slicks/query_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
)
3636

3737

38+
def quote_table(schema: str, table: str) -> str:
39+
"""Quote table name for SQL, handling schema.table format."""
40+
# If the table already contains a dot (and isn't just the schema), assume it might differ?
41+
# Actually, InfluxDB 3 usually expects "schema"."table"
42+
return f'"{schema}"."{table}"'
43+
44+
3845
class PermanentQueryError(Exception):
3946
"""An error that will not resolve by splitting the time range."""
4047

src/slicks/scanner.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,16 @@ def scan_data_availability(
366366
# Setup timezone
367367
tz = ZoneInfo(timezone)
368368

369-
# Default table
370-
if table is None:
371-
table = f"iox.{config.INFLUX_DB}"
369+
# Default table if not provided
370+
if not table:
371+
schema = config.INFLUX_SCHEMA or "iox"
372+
table_name = config.INFLUX_TABLE or config.INFLUX_DB
373+
table = f"{schema}.{table_name}"
372374

375+
# We still use _quote_table here because `table` might be passed in as "schema.table"
376+
# or just "table" (legacy). But to be consistent with other modules, we should
377+
# probably try to use the configured schema if the passed table doesn't have one?
378+
# For now, let's keep the existing logic but respect the global config default above.
373379
table_ref = _quote_table(table)
374380

375381
# Determine bin settings

0 commit comments

Comments
 (0)