def test_connection_option_from_conf_file(self): self.client.close() self.client = InfluxDBClient.from_config_file(self._path_to_config(), self.debug) self.assertEqual("http://localhost:9999", self.client.url) self._check_connection_settings()
def main(): if len(sys.argv) != 3: usage() start_time, end_time = sys.argv[1:] client = InfluxDBClient.from_config_file('influxdb.ini') query_api = client.query_api() write_api = client.write_api(write_options=ASYNCHRONOUS) query = f"""import "strings" from (bucket: "gnss") |> range(start: {start_time}, stop: {end_time}) |> filter(fn: (r) => r._measurement == "azel" and strings.containsAny(v: r.svn, chars: "GE")) |> pivot(rowKey: ["_time"], columnKey: ["svn", "_field"], valueColumn: "_value")""" print('Querying InfluxDB for azels...') records = query_api.query_stream(query) for record in records: print('Computing DOPs for', record.get_time()) for constellation in ['E', 'G', 'EG']: dops = compute_constellation_dops(record, constellation) dop_record = Point('dop').time(record.get_time()).tag('constellation', constellation).\ tag('ship', record['ship']).\ field('gdop', dops[0]).\ field('pdop', dops[1]).\ field('hdop', dops[2]).\ field('vdop', dops[3]) write_api.write(bucket='gnss', record=dop_record)
def main(): if len(sys.argv) != 4: usage() sp3_path, start_time, end_time = sys.argv[1:] print('Loading SP3 files...') for sp3 in pathlib.Path(sp3_path).glob('*.sp3'): load_sp3(str(sp3)) client = InfluxDBClient.from_config_file('influxdb.ini') query_api = client.query_api() write_api = client.write_api(write_options = ASYNCHRONOUS) query = f"""from (bucket:"hesperides") |> range(start: {start_time}, stop: {end_time}) |> aggregateWindow(every: 1m, fn: first) |> filter(fn: (r) => r._measurement == "position" and r.ship == "hesperides" and (r._field == "lat" or r._field == "lon")) |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")""" print('Querying InfluxDB for positions...') records = query_api.query_stream(query) for record in records: print('Computing azels for', record.get_time()) for satellite in satellites: azel = compute_azel(satellite, record) azel_record = Point('azel').time(record.get_time()).tag('svn', satellite).tag('ship', 'hesperides').field('az', azel[0]).field('el', azel[1]) write_api.write(bucket = 'gnss', record = azel_record)
def __init__(self): config = configparser.ConfigParser() config_file = os.path.join(os.path.dirname(__file__), 'config.ini') config.read(config_file) client = InfluxDBClient.from_config_file(config_file) self.query_api = client.query_api() self.bucket = config['ha']['influx_bucket'] self.actions = Actions()
def __init__(self, config: ConfigParser, name): super().__init__( logger.Logger(module_name=f"influx data adapter '{name}'"), config) self.name = name self.bucket = config[name]["bucket"] self.start = config[name].get("start", "-30d") self.limit = config[name].get("limit", "100") self.drops = json.loads(config[name].get( "drops", '["pm1", "pm2.5", "pm4.0", "result", "table"]')) self.client = InfluxDBClient.from_config_file( self.config["influx"]["config"], debug=False)
def upload_influx_db(con: Connection, table: str, start_time: datetime = None): from influxdb_client import InfluxDBClient from influxdb_client.client.write_api import SYNCHRONOUS client = InfluxDBClient.from_config_file(default_config()['influx']['config']) write_api = client.write_api(write_options=SYNCHRONOUS) cols = set(con.execute(f'SELECT * FROM {table} LIMIT 1').keys()) places = set(p.split('.', 1)[0] for p in cols) places_pm10 = set(p for p in places if f'{p}.PM10' in cols) places_pm2_5 = set(p for p in places if f'{p}.PM2.5' in cols) places_temperature = set(p for p in places if f'{p}.Temperature' in cols) places_rainfall = set(p for p in places if f'{p}.Rainfall' in cols) places_humidity = set(p for p in places if f'{p}.Humidity' in cols) places_pressure = set(p for p in places if f'{p}.Pressure' in cols) places = sorted(set(chain(places_pm10, places_pm2_5, places_temperature, places_rainfall))) q = " || '\n' || ".join( f"""'pollution,place={p} ' || SUBSTR(""" + (f"""IFNULL(',pm10=' || "{p}.PM10", '') || """ if p in places_pm10 else '') + (f"""IFNULL(',pm2.5=' || "{p}.PM2.5", '') || """ if p in places_pm2_5 else '') + f"""'', 2) || STRFTIME(' %s000000000', date) || '\n' || """ + f"""'weather,place={p} ' || SUBSTR(""" + (f"""IFNULL(',temperature=' || "{p}.Temperature", '') || """ if p in places_temperature else '') + (f"""IFNULL(',rainfall=' || "{p}.Rainfall", '') || """ if p in places_rainfall else '') + (f"""IFNULL(',pressure=' || "{p}.Pressure", '') || """ if p in places_pressure else '') + (f"""IFNULL(',humidity=' || "{p}.Humidity", '') || """ if p in places_humidity else '') + f"""'', 2) || STRFTIME(' %s000000000', date)""" for p in places ) query = f"""SELECT {q} AS line_data FROM {table}""" if start_time is not None: query += f" WHERE date >= '{start_time.isoformat()}'" res = con.execute(query) sequence = [l for row in res for l in row['line_data'].split('\n') if ' ' not in l] print('SQL query:', query) # print('\n'.join(sequence)) write_api.write(INFLUXDB_HIST_DATA_BUCKET, records=sequence) print('Done!')
def main(): if len(sys.argv) != 2: usage() data_file = sys.argv[1] client = InfluxDBClient.from_config_file('influxdb.ini') write_api = client.write_api() data = rx.from_iterable(regs(data_file)) \ .pipe(ops.map(point)) write_api.write(bucket='les5', record=data, write_precision=WritePrecision.S) write_api.__del__() client.__del__()
def test_init_from_file_ssl_ca_cert_default(self): self.client = InfluxDBClient.from_config_file( f'{os.path.dirname(__file__)}/config.ini') self.assertIsNone(self.client.api_client.configuration.ssl_ca_cert)
from influxdb_client import InfluxDBClient influxClient = InfluxDBClient.from_config_file("../config/influxdb.ini") bucket = "boni.garcia's Bucket" kind = "sine-wave" myquery = f'from(bucket: "{bucket}") |> range(start: -1d) |> filter(fn: (r) => r._measurement == "{kind}")' tables = influxClient.query_api().query(myquery) for table in tables: print(table) for row in table.records: print(row.values)
def __init__(self, configuration, configuration_file): requests.packages.urllib3.disable_warnings() self._configuration = configuration self._client = InfluxDBClient.from_config_file(configuration_file) self._write_api = self._client.write_api(write_options=SYNCHRONOUS) logging.info("Connecting to InfluxDB: " + self._configuration['url'])
def test_init_from_file_ssl_default(self): self.client = InfluxDBClient.from_config_file( f'{os.path.dirname(__file__)}/config.ini') self.assertTrue(self.client.api_client.configuration.verify_ssl)
def __enter__(self): self.client = InfluxDBClient.from_config_file( self.config["influx"]["config"], debug=self.config[self.name]["debug"].lower() in ['true', 't', 'yes']) return self
#!/usr/bin/env python3 from influxdb_client import InfluxDBClient bucket = "timecube" client = InfluxDBClient.from_config_file("config.ini") query = f'from(bucket: "{bucket}") |> range(start: -1h)' tables = client.query_api().query(query, org=client.org) print(client.org) for table in tables: print(table) for row in table.records: print(row.values) print("---")
config.read('/home/pi/app/config.ini') NC05 = "nc0p5" PM1 = "pm1p0" NC1 = "nc1p0" PM25 = "pm2p5" NC25 = "nc2p5" PM4 = "pm4p0" NC4 = "nc4p0" PM10 = "pm10p0" NC10 = "nc10p0" TYP = "typical" # You can generate a Token from the "Tokens Tab" in the UI bucket = "actual_weather_data" client = InfluxDBClient.from_config_file("/home/pi/app/config.ini") sps = SPS30(1) if sps.read_article_code() == sps.ARTICLE_CODE_ERROR: raise Exception("ARTICLE CODE CRC ERROR!") else: print("ARTICLE CODE: " + str(sps.read_article_code())) if sps.read_device_serial() == sps.SERIAL_NUMBER_ERROR: raise Exception("SERIAL NUMBER CRC ERROR!") else: print("DEVICE SERIAL: " + str(sps.read_device_serial())) sps.set_auto_cleaning_interval(604800) # default 604800, set 0 to disable auto-cleaning
def test_init_from_file_ssl_ca_cert(self): self.client = InfluxDBClient.from_config_file( f'{os.path.dirname(__file__)}/config-ssl-ca-cert.ini') self.assertEqual("/path/to/my/cert", self.client.api_client.configuration.ssl_ca_cert)
def test_init_from_toml_file(self): self.client = InfluxDBClient.from_config_file( f'{os.path.dirname(__file__)}/config.toml') self.assertConfig()
def test_init_from_file_proxy(self): self.client = InfluxDBClient.from_config_file( f'{os.path.dirname(__file__)}/config-enabled-proxy.ini') self.assertConfig() self.assertEqual("http://proxy.domain.org:8080", self.client.api_client.configuration.proxy)
def test_init_from_file_ssl(self): self.client = InfluxDBClient.from_config_file( f'{os.path.dirname(__file__)}/config-disabled-ssl.ini') self.assertFalse(self.client.api_client.configuration.verify_ssl)
import os import zmq from influxdb_client import InfluxDBClient, Point from influxdb_client.client.write_api import SYNCHRONOUS context = zmq.Context() sock_address = os.environ.get('SOCK_ADDRESS', 'localhost:5555') socket = context.socket(zmq.SUB) socket.connect(f'tcp://{sock_address}') socket.setsockopt(zmq.SUBSCRIBE, b'timecube') bucket = 'timecube' client = InfluxDBClient.from_config_file('config.ini') write_api = client.write_api(write_options=SYNCHRONOUS) query_api = client.query_api() while True: payload = socket.recv_string() topic, identifier, side = payload.split() print(f'topic={topic}, identifier={identifier}, side={side}') p = Point('measurement').tag('identifier', identifier).field('side', side) try: write_api.write(bucket=bucket, record=p) except ConnectionError as e: print(e)
def test_default_tags_from_conf_file(self): self.client.close() self.client = InfluxDBClient.from_config_file(self._path_to_config(), self.debug) self._write_point()
purpleair_sensors = settings.PURPLEAIR_SENSORS.split(",") points = [] # Get weather try: points += openweathermap.get_points(settings.POSTCODE) except Exception as error: print("fail to get weather data: %s", error) # Get Air Quality Index try: points += airnow.get_points(settings.POSTCODE) except Exception as error: print("fail to get Air Quality data: %s", error) # Get Air Quality Index from purple air try: for sensor in purpleair_sensors: points += purpleair.get_points(sensor) except Exception as error: print("fail to get Purple Air data: %s", error) client = InfluxDBClient.from_config_file(settings.INFLUX_CONFIG_PATH) write_api = client.write_api(write_options=SYNCHRONOUS) write_api.write(settings.INFLUX_BUCKET, settings.INFLUX_ORG, points) print(f"wrote {len(points)} points to database") print() print(points)