class InfluxLogger: def __init__(self, url, token, org, bucket_id='sun2000'): self.url = url self.token = token self.org = org self.bucket_id = bucket_id self.client = InfluxDBClient(url=self.url, token=self.token, org=self.org) self.write_api = self.client.write_api(write_options=SYNCHRONOUS) atexit.register(self.on_exit, self.client, self.write_api) def on_exit(self, db_client, write_api): write_api.__del__() db_client.__del__() def write(self, data): try: line_protocol = data.to_line_protocol() print(f'Line: {line_protocol}') self.write_api.write(bucket=self.bucket_id, org=self.org, record=line_protocol) except Exception as err: print('Influx exception: {}'.format(err))
class InfluxDBDataWriter(CovidDataWriter): def __init__(self, url: str, token: str, org: str, bucket: str): self._org = org self._bucket = bucket self._client = InfluxDBClient(url=url, token=token) self._write_api = self._client.write_api() def write_data(self, data_point: CovidDataPoint) -> None: self._write_api.write( bucket=self._bucket, org=self._org, record=self._convert_datapoint(data_point=data_point), ) @staticmethod def _convert_datapoint(data_point: CovidDataPoint) -> Point: point = (Point(data_point.measurement.value).tag( "iso3", data_point.iso_3).tag("country", data_point.country).tag( "state_province", data_point.state_province).tag( "county", data_point.county).tag("lat", data_point.lat).tag( "long", data_point.long).field("count", data_point.value).time( data_point.timestamp)) return point def __del__(self): self._client.close()
def main(): global args, write_api parser = ArgumentParser(description=__doc__) parser.add_argument("--host", default='localhost', help='hostname of mqtt broker') parser.add_argument( "-t", action='append', default=['velogen/raw'], help='MQTT topic to subscribe to. Can be put multiple times.') parser.add_argument("-d", action='store_true', help='enable debug output') parser.add_argument("--user", default=None, help="username") parser.add_argument("--pw", default=None, help="password") args = parser.parse_args() client = Client() client.on_connect = on_connect client.on_disconnect = on_disconnect client.on_message = on_message if args.user is not None and args.pw is not None: client.username_pw_set(args.user, args.pw) client.connect(args.host) # For influxdb2 only db = InfluxDBClient(url='http://localhost:8086', token=token, debug=args.d) write_api = db.write_api(write_options=SYNCHRONOUS) while True: client.loop(timeout=1.0)
def submit_measurements_to_influx(measurements): print(" >> uploading to influx") influx_url = "https://us-east-1-1.aws.cloud2.influxdata.com/" influx_orgID, _, influx_token = netrc().authenticators("influx") influx_client = InfluxDBClient(url=influx_url, orgID=influx_orgID, token=influx_token) influx_write = influx_client.write_api(write_options=SYNCHRONOUS).write # https://www.influxdata.com/blog/getting-started-with-python-and-influxdb-v2-0/ influx_write( "burnham", "*****@*****.**", ## bucket / org [{ "measurement": "networks", "tags": { "install": "oberlin" }, "fields": measurements, "time": datetime.utcnow() }]) from influx_credentials import uc_cred uc_cred.client.write_points([{ "measurement": "networks", "tags": { "install": "oberlin" }, "fields": measurements, "time": datetime.utcnow() }])
class InfluxClient: def __init__( self, url="http://localhost:8086", token="IpLnoNkWhqmnSLO2ieeqmHejYrrokycO5Be8HRgM6UI1S_CO-Py2_opA2E1z6iCzJrv5U_gHGVHh5JMCFsgwjQ==" ): # You can generate a Token from the "Tokens Tab" in the UI @ localhost:9999 self.org = "vwa" self.bucket = "vwa" self.client = InfluxDBClient(url=url, token=token) self.write_api = self.client.write_api(write_options=SYNCHRONOUS) self.b = Batcher(500, 5, self._send) self.q = self.client.query_api() def send(self, line): self.b.send(line) def sendSequence(self, sequence): self._send(sequence) def _send(self, sequence): try: self.write_api.write(self.bucket, self.org, sequence) print("%d items sent!" % len(sequence)) except Exception as e: print("%d items not sent!" % len(sequence), e)
def write_to_influxdb(_url, _token, _org, _bucket, _host, _seconds): """ Write the response time to Influxdb :param _url: Influxdb server URL as string :param _token: Influxdb auth token as string :param _org: Influxdb organization as string :param _bucket: Influxdb bucket as string :param _host: Name of the host being as string :param _seconds: Response time in seconds as float :return: None """ point = Point("response_time").tag("host", _host).field("seconds", _seconds) try: client = InfluxDBClient(url=_url, token=_token, org=_org, verify_ssl=False) write_api = client.write_api(write_options=SYNCHRONOUS) write_api.write(bucket=_bucket, record=point) client.close() except Exception as e: print("CRITICAL - Failed to write to influxdb: %s" % e) sys.exit(2) return None
def create_batch(batch): def to_line_protocol(source): measurement = source["measurement"] time = source["time"] tagset = [] fieldset = [] for key, value in source["fields"].items(): fieldset.append("{key}={value}".format( key=key, value=str(value).replace(" ", "_"))) for key, value in source["tags"].items(): tagset.append("{key}={value}".format(key=key, value=str(value).replace( " ", "_"))) return "{measurement},{tagset} {fieldset} {time}".format( measurement=measurement, tagset=",".join(tagset), fieldset=",".join(fieldset), time=time, ).encode() client = InfluxDBClient(url=host, token=token) write_client = client.write_api(write_options=SYNCHRONOUS) print("Writing to {bucket} in {org}".format(bucket=bucket, org=org)) data = list(map(to_line_protocol, batch)) response = write_client.write(bucket, org, data) return True
def main(): config_location = 'default_config.json' if len(sys.argv) > 1: config_location = sys.argv[1] with open(config_location) as json_file: config = json.load(json_file) influx_config = config["influxDB"] influx_host = influx_config["hostname"] influx_port = influx_config["port"] influx_username = influx_config["username"] influx_password = os.environ[influx_config["passwordEnvName"]] influx_database = influx_config["database"] influx_client = InfluxDBClient( url=f'http://{influx_host}:{influx_port}', token=f'{influx_username}:{influx_password}', org='-') influx_exporter = InfluxExporter( InfluxWriteApiProxy(influx_client.write_api(write_options=SYNCHRONOUS)), influx_database) monitors_config = config["monitors"] monitors = list(map(lambda mon: NamedMonitor(mon["name"], _get_class(mon["class"])()), monitors_config)) updater = AirMonitoringApp(monitors, influx_exporter, DatetimeCurrentTimeProvider()) scheduler = TimeScheduler(updater, config["timeGranularityInSeconds"]) scheduler.start()
def init(self): """Init the connection to the InfluxDB server.""" if not self.export_enable: return None url = '{}://{}:{}'.format(self.protocol, self.host, self.port) try: client = InfluxDBClient(url=url, enable_gzip=False, org=self.org, token=self.token) except Exception as e: logger.critical("Cannot connect to InfluxDB server '%s' (%s)" % (url, e)) sys.exit(2) else: logger.info("Connected to InfluxDB server version {} ({})".format( client.health().version, client.health().message)) # Create the write client write_client = client.write_api( write_options=WriteOptions(batch_size=500, flush_interval=10000, jitter_interval=2000, retry_interval=5000, max_retries=5, max_retry_delay=30000, exponential_base=2)) return write_client
def send_data(what, value): """ this function sends the data as json, and checks if it was succesfull """ client = InfluxDBClient(url=influx_cloud_url, token=influx_cloud_token) try: point = Point("measurement").field(what, value).time(time=datetime.utcnow()) # if data is incorrect, don't send anything if point is None: return -1 """ Write data by Point structure """ print(f'Writing to InfluxDB cloud: {point.to_line_protocol()} ...') write_api = client.write_api(write_options=SYNCHRONOUS) write_api.write(bucket=bucket, org=org, record=point) print() print('success') print() print() except Exception as e: print(e) finally: client.close()
def publishing_thread(self, url, token, org, bucket): from influxdb_client import InfluxDBClient bucket = bucket client = InfluxDBClient(url=url, token=token, org=org) from influxdb_client import WriteOptions write_api = None while True: time.sleep(1) if not write_api: write_api = client.write_api( write_options=WriteOptions(batch_size=100, flush_interval=5_000, jitter_interval=2_000, retry_interval=2_000, max_retries=3, max_retry_delay=30_000, exponential_base=2)) try: buf = self.queue.copy() self.queue.clear() if len(buf) > 0: write_api.write(bucket, org, buf) time.sleep(5) except: write_api = None time.sleep(10)
def write_to_influx(gesture_name, fields): '''Write data to InfluxDB and return a Flask response''' # Attempt to load the config_file file try: config_file = toml.load('config.toml')['influxdb'] except (FileNotFoundError, KeyError): logging.error('Config file not found.') influx_protocol = "http" if config_file['tls']: influx_protocol = "https" # Initalize database collection _client = InfluxDBClient( url=f"{influx_protocol}://{config_file['host']}:{config_file['port']}", verify_ssl=config_file['verify_tls'], org=config_file['organization'], token=config_file['token']) # pylint: disable=line-too-long _write_client = _client.write_api( write_options=WriteOptions(batch_size=500, flush_interval=10_000, jitter_interval=2_000, retry_interval=5_000, max_retries=5, max_retry_delay=30_000, exponential_base=2))
def run(influxdb_client: InfluxDBClient) -> None: """ Processes queued measurements. """ # Keep batches small, only send the latest X items stored. The rest will be purged (in case of delay). selection = InfluxdbMeasurement.objects.all().order_by( '-pk')[0:settings.DSMRREADER_INFLUXDB_MAX_MEASUREMENTS_IN_QUEUE] if not selection: return logger.info('INFLUXDB: Processing %d measurement(s)', len(selection)) influxdb_settings = InfluxdbIntegrationSettings.get_solo() for current in selection: try: decoded_fields = codecs.decode(current.fields.encode(), 'base64') unpickled_fields = pickle.loads(decoded_fields) # noqa: S301 with influxdb_client.write_api( write_options=SYNCHRONOUS) as write_api: write_api.write(bucket=influxdb_settings.bucket, org=influxdb_settings.organization, record={ "measurement": current.measurement_name, "time": current.time, "fields": unpickled_fields }) except Exception as error: logger.error( 'INFLUXDB: Writing measurement(s) failed: %s, data: %s', error, current.fields) current.delete() # This purges the remainder. InfluxdbMeasurement.objects.all().delete()
class DataUpdater(Thread): def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.daemon = True self.db = None if len(INFLUX_HOST) > 0: self.db = InfluxDBClient(url=f"http://{INFLUX_HOST}:{INFLUX_PORT}", token=INFLUX_TOKEN, org=INFLUX_ORG) self.write = self.db.write_api(write_options=SYNCHRONOUS) def update_influx(self): pp = [] for date, fields in DATA.italy.to_dict('index').items(): p = Point("italia") p.time(date.value) for k, v in fields.items(): if type(v) is str: p.tag(k, v) if not np.isnan(v): p.field(k, v) pp.append(p) self.write.write("coviddi", "coviddi", pp) def run(self) -> None: global INFO global DATA while True: INFO, DATA = updates.get(block=True) if self.db is not None: self.update_influx()
def main(): parse_row.progress = 0 url = "https://github.com/influxdata/influxdb-client-python/wiki/data/stock-prices-example.csv" response = requests.get(url, stream=True) data = rx \ .from_iterable(DictReader(response.iter_lines(decode_unicode=True))) \ .pipe(ops.map(lambda row: parse_row(row))) client = InfluxDBClient(url="http://localhost:9999", token="my-token", org="my-org", debug=False) write_api = client.write_api(write_options=WriteOptions(batch_size=50_000, flush_interval=10_000)) write_api.write(bucket="my-bucket", record=data) write_api.__del__() query = ''' from(bucket:"my-bucket") |> range(start: 0, stop: now()) |> filter(fn: (r) => r._measurement == "financial-analysis") |> filter(fn: (r) => r.symbol == "AAPL") |> filter(fn: (r) => r._field == "close") |> drop(columns: ["_start", "_stop", "table", "_field","_measurement"]) ''' result = client.query_api().query_data_frame(query=query) print(result.head(100)) """ Close client """ client.__del__()
def load_data_printer_profile(): url_connection = "http://octopi.local/api/connection" headers = {"X-Api-Key": X_API_KEY, "Content-Type": "application/json"} res_connection = requests.get(url_connection, headers=headers).json() print(res_connection["current"]) current_profile = res_connection["current"].get("printerProfile", "_default") url_printerprofiles = "http://octopi.local/api/printerprofiles" res_printer_profiles = requests.get(url_printerprofiles, headers=headers).json()["profiles"] print(res_printer_profiles[current_profile]["axes"]) print(res_printer_profiles[current_profile]["extruder"]) print(res_printer_profiles[current_profile]["id"]) print(res_printer_profiles[current_profile]["model"]) print(res_printer_profiles[current_profile]["name"]) print(res_printer_profiles[current_profile]["volume"]) dbClient = InfluxDBClient(url=INFLUXDB_CLOUD_HOST, token=INFLUXDB_TOKEN) write_api = dbClient.write_api() for data_point in data_convert_printer_profiles(res_printer_profiles, current_profile): write_api.write(INFLUXDB_BUCKETID, INFLUXDB_ORGID, data_point)
def config(config): global influx2_debug global influx2_client global influx2_write_api influx2_debug = int(config.get('debug', 0)) org = config.get('org', "my-org") url = config.get('url', "http://localhost:8086") token = config.get('token', "my-token") # create connection influx2_client.close() if influx2_debug > 0: influx2_client = InfluxDBClient(url=url, token=token, org=org, debug=True) else: influx2_client = InfluxDBClient(url=url, token=token, org=org, debug=False) influx2_write_api.close() influx2_write_api = influx2_client.write_api( write_options=WriteOptions(batch_size=200, flush_interval=120_000, jitter_interval=2_000, retry_interval=5_000, max_retries=5, max_retry_delay=30_000, exponential_base=2))
def insert_to_influx(data, routing_key): client = InfluxDBClient( url="192.168.43.70:8086", token= "a6WAt3mAwy4MP2Cq1koohw4wQTiK09M8_gx9WLpzQwtrH_w5eDRyQe1q4bMYCcKn0hT0UTdV94zsBcJgInLFgw==", org="pnup") write_api = client.write_api() query_api = client.query_api() keys = routing_key.split('-') measurement = keys[0] namenode = keys[1] # client.switch_database('iot_multinode_DB') data = [{ "measurement": measurement, "tags": { "name": namenode }, "fields": { "value": data, # "namenode": namenode } }] write_api.write(bucket="iot_multinode_DB", org="pnup", record=data)
def __init__(self, config, logger): self.__config__ = config self.__logger__ = logger client = InfluxDBClient(url=self.__config__.influx_uri, token=INFLUX_TOKEN) self.__write_api__ = client.write_api(write_options=SYNCHRONOUS)
class OutputInfluxDb(Output): CONFIG = merge_dict(Output.CONFIG, { 'token': 'str: Token to authenticate with the influxdb instance', 'org': 'str: Organization to use in the influxdb instance', 'bucket': 'str: Bucket to store the check results in', 'url': 'str: Url to connect to the influxdb instance' }) """Send check results to an influxdb instance""" def __init__(self, config): super(OutputInfluxDb, self).__init__('influxdb', config) self.logger = logging.getLogger("aggregator.output.influxdb") self.token = config['token'] self.org = config['org'] self.bucket = config['bucket'] self.url = config['url'] self.client = InfluxDBClient(url=self.url, token=self.token) self.write_api = self.client.write_api(write_options=SYNCHRONOUS) def write(self, results): points = [] for result in results: point = Point(result[Check.Result.NAME]) \ .tag("host", result[Check.Result.HOST]) \ .time(result[Check.Result.TIME].isoformat()) if Check.Result.DEVICE in result: point = point.tag("device", result[Check.Result.DEVICE]) for field in result[Check.Result.FIELDS]: point = point.field(field[Check.Field.NAME], field[Check.Field.VALUE]) if Check.Field.UNIT in field: point = point.field(f"{field[Check.Field.NAME]}_unit", field[Check.Field.UNIT]) points.append(point) self.write_api.write(self.bucket, self.org, points)
class InfluxDBWriter(multiprocessing.Process): """ Writer that writes data in batches with 50_000 items. """ def __init__(self, queue): multiprocessing.Process.__init__(self) self.queue = queue self.client = InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org", debug=False) self.write_api = self.client.write_api( write_options=WriteOptions(write_type=WriteType.batching, batch_size=50_000, flush_interval=10_000)) def run(self): while True: next_task = self.queue.get() if next_task is None: # Poison pill means terminate self.terminate() self.queue.task_done() break self.write_api.write(bucket="my-bucket", record=next_task) self.queue.task_done() def terminate(self) -> None: proc_name = self.name print() print('Writer: flushing data...') self.write_api.close() self.client.close() print('Writer: closed'.format(proc_name))
def run(): # Grab info if os.path.isfile('influxdb.json'): with open('influxdb.json', "r") as f: config = json.load(f) else: raise Exception(f'Please create influxdb.json with fields: url,token,org,bucket and optionally: measurement,field') # Connect to a database try: from influxdb_client import InfluxDBClient except ImportError: print('No influxdb_client library. Install with "pip install influxdb_client"') sys.exit(0) influx = InfluxDBClient(url=config['url'], token=config['token'], org=config['org']) writer = influx.write_api() print('Connected to InfluxDB') # Get data from kraken.errors import KrakenErrorHttp from .data import KrakenData counter = 0 failed_attempts = 0 failed_attempts_wait = [2, 3, 5, 10] kraken_data = KrakenData(keys_file='keys.json', quiet=True) print('Initialized Kraken API') interval = config['interval'] if 'interval' in config else 60 print(f'Insert every {interval} seconds') while True: # Refresh try: kraken_data.loadTrades(trades_file='trades.json') kraken_data.processTrades() kraken_data.loadCurrentPrices() data = kraken_data.calculateData() failed_attempts = 0 counter += 1 except KrakenErrorHttp as e: # Get wait time before next attempt wait_minutes = failed_attempts_wait[failed_attempts if failed_attempts < len(failed_attempts_wait) else len(failed_attempts_wait)-1] failed_attempts += 1 print(f"\nKraken API server returned HTTP {e.code} error. Retrying in {wait_minutes} minutes (attempt {failed_attempts})") time.sleep(wait_minutes * 60) continue # Calculate profit profit = data['total_live_profit'] + data['total_closed_profit'] # Push to database measurement = config['measurement'] if 'measurement' in config else 'kraken' field = config['field'] if 'field' in config else 'profit' writer.write(config['bucket'], config['org'], [f"{measurement} {field}={profit}"]) # Progress if counter % 20 == 0: print('.') else: print(".", end='', flush=True) # Wait for 60 seconds time.sleep(interval)
def post_influxdb(): url = u_weather_uri mtk_weather = requests.get(url).json() logr.debug('UG Weather API Results', mtk_weather) weather = { 'stationid': mtk_weather['observations'][0]['stationID'], 'obsTimeUTC': mtk_weather['observations'][0]['obsTimeUtc'], 'localtime': mtk_weather['observations'][0]['obsTimeLocal'], 'winddir': mtk_weather['observations'][0]['winddir'], 'humidity': mtk_weather['observations'][0]['humidity'], 'temp': mtk_weather['observations'][0]['metric']['temp'], 'heatindex': mtk_weather['observations'][0]['metric']['heatIndex'], 'dewPoint': mtk_weather['observations'][0]['metric']['dewpt'], 'windchill': mtk_weather['observations'][0]['metric']['windChill'], 'windspeed': mtk_weather['observations'][0]['metric']['windSpeed'], 'windgust': mtk_weather['observations'][0]['metric']['windGust'], 'psure': mtk_weather['observations'][0]['metric']['pressure'], 'precip': mtk_weather['observations'][0]['metric']['precipRate'], 'obsTimeUTC': mtk_weather['observations'][0]['obsTimeUtc'], } # Calculate FFDI data point df = drought() fdi = ffdi(weather["temp"], weather["humidity"], df[0], weather["windspeed"]) # Setup connection to the DB client = InfluxDBClient(url='{}'.format(influx_server), token='{}'.format(influx_token), org='{}'.format(influx_org)) write_api = client.write_api(write_options=SYNCHRONOUS) query_api = client.query_api() # Form the data to ingest into InfluxDB database p = Point("weather") \ .tag("location", "Mount Kuring-Gai") \ .field("temperature", float(weather['temp'])) \ .field("Heat Index", float(weather['heatindex'])) \ .field("Dew Point", float(weather['dewPoint'])) \ .field("humidity", int(weather['humidity'])) \ .field("Wind Speed", float(weather['windspeed'])) \ .field("Wind Gust", float(weather['windgust'])) \ .field("Pressure", float(weather['psure'])) \ .field("Wind Direction", float(weather['winddir'])) \ .field("Precipitation", float(weather['precip'])) \ .field("Heat Index", float(weather['heatindex'])) \ .field("DroughtF", float(df[0])) \ .field("FFDI", float(fdi)) \ .time((weather['obsTimeUTC'])) # Write that into the InfluxDB write_api.write(record=p, bucket='{}'.format(influx_bucket), time_precision='s') return None
def writeToInflux(value): bucket = "temperature" client = InfluxDBClient(url="INFLUXHOST", token="TOKEN", org="ORG") write_api = client.write_api(write_options=SYNCHRONOUS) p = Point("sensor").field("temperature", float(value)) write_api.write(bucket=bucket, record=p)
def connect_to_influx(host, port, token, organization): global write_api print("Connecting to database") client = InfluxDBClient(url=f"http://{host}:{port}", token=token, org=organization) write_api = client.write_api(write_options=SYNCHRONOUS) print("Connected to database")
def ensure_influx(self) -> None: if not self._influx: log.debug("Initializing Influx client") self._influx_init_count += 1 client = InfluxDBClient(url=self._url, token=self._token, org=self._org) self._influx = client.write_api(write_options=SYNCHRONOUS)
class WriteApiTestMock(BaseTest): def setUp(self) -> None: httpretty.enable() httpretty.reset() conf = influxdb_client.configuration.Configuration() conf.host = "http://localhost" conf.debug = False self.influxdb_client = InfluxDBClient(url=conf.host, token="my-token") def tearDown(self) -> None: self.influxdb_client.close() httpretty.disable() def test_writes_synchronous_without_retry(self): httpretty.register_uri(httpretty.POST, uri="http://localhost/api/v2/write", status=503) self.write_client = self.influxdb_client.write_api( write_options=SYNCHRONOUS) with self.assertRaises(ApiException) as cm: self.write_client.write( "my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1 1") exception = cm.exception self.assertEqual("Service Unavailable", exception.reason) self.assertEqual(1, len(httpretty.httpretty.latest_requests)) def test_writes_asynchronous_without_retry(self): httpretty.register_uri(httpretty.POST, uri="http://localhost/api/v2/write", status=503) self.write_client = self.influxdb_client.write_api( write_options=ASYNCHRONOUS) with self.assertRaises(ApiException) as cm: self.write_client.write( "my-bucket", "my-org", "h2o_feet,location=coyote_creek water_level=1 1").get() exception = cm.exception self.assertEqual("Service Unavailable", exception.reason) self.assertEqual(1, len(httpretty.httpretty.latest_requests))
def influxexporter2(localhost, url, token, bucket, org, dict_data, source, file_logger): if not influx_modules: file_logger.error(" ********* MAJOR ERROR ********** ") file_logger.error( "One or more Influx Python .are not installed on this system. Influx export failed, exiting" ) file_logger.error(import_err) sys.exit() client = InfluxDBClient(url=url, token=token, org=org, timeout=100) file_logger.debug("Creating InfluxDB2 API client...") file_logger.debug("URL: -{}-".format(url)) file_logger.debug("Token: -{}-".format(token)) file_logger.debug("Org: -{}-".format(org)) try: write_api = client.write_api(write_options=SYNCHRONOUS) except Exception as err: file_logger.error( "Error creating InfluxDB2 API client: {}".format(err)) return False now = time_lookup() data = [] # construct data structure to send to InFlux for key, value in dict_data.items(): if key == 'time': continue data_point = { "measurement": source, "tags": { "host": localhost }, "fields": { key: value }, "time": now } data.append(data_point) # send to Influx file_logger.debug("Data structure sent to Influx:") file_logger.debug(data) try: write_api.write(bucket, org, data) file_logger.info("Data sent to InfluxDB2. (bucket: {})".format(bucket)) except Exception as err: file_logger.error("Error sending data to InfluxDB2: {}".format(err)) return False return True
class CoffeeChart: def __init__(self) -> None: self.client = InfluxDBClient(url="http://localhost:8086", token="asdf", org="olympus") self.write_api = self.client.write_api(write_options=SYNCHRONOUS) def write_measurement( self, measurement: str, field: str, value: Any, ) -> Dict[str, Any]: point = Point(measurement).tag("location", "garage").field(field, value).time( datetime.utcnow(), WritePrecision.MS, ) self.write_api.write(bucket="espresso", org="olympus", record=point) def send_row_to_influx(self, row: Dict[str, Any]) -> None: # TODO: logger print(row) row_time = int(time.time()) if row["type"] == "event": self.write_measurement( "event", "event", row["event"], ) elif row["type"] == "temp": self.write_measurement( "boiler_temp", "temperature", row["boiler_temp"], ) self.write_measurement( "grouphead_temp", "temperature", row["grouphead_temp"], ) self.write_measurement( "target_boiler_temp", "temperature", row["target_boiler_temp"], ) elif row["type"] == "brew": self.write_measurement( "brew", "weight", row["weight"], ) else: raise ValueError('Unsupported row type "{}"'.format(row["type"]))
def getPageInsights(self): token = "THPa1hMyYsiLk1SfIZ_MFloSia4QwfdtSxY--kog96rs8VoiV03ZOk-Fv17WE3qn8sTJMzewlq7BEyvBKdwzow==" org = "*****@*****.**" bucket = "insights" client = InfluxDBClient( url="https://us-west-2-1.aws.cloud2.influxdata.com", token=token) try: targetURL = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" + str( self.site) + "&key=" + self.key + "&strategy=DESKTOP" req = requests.get(targetURL, timeout=30) res = json.loads(req.text) fetchTime = int(round(time.time() * 1000)) fcp = str(res['lighthouseResult']['audits'] ['first-contentful-paint']['displayValue']) si = str(res['lighthouseResult']['audits']['speed-index'] ['displayValue']) toi = str(res['lighthouseResult']['audits']['interactive'] ['displayValue']) fmp = str(res['lighthouseResult']['audits'] ['first-meaningful-paint']['displayValue']) fci = str(res['lighthouseResult']['audits']['first-cpu-idle'] ['displayValue']) eil = str(res['lighthouseResult']['audits'] ['estimated-input-latency']['displayValue']) field_lcp = str(res['loadingExperience']['metrics'] ['LARGEST_CONTENTFUL_PAINT_MS']['percentile']) field_fcp = str(res['loadingExperience']['metrics'] ['FIRST_CONTENTFUL_PAINT_MS']['percentile']) field_fid = str(res['loadingExperience']['metrics'] ['FIRST_INPUT_DELAY_MS']['percentile']) field_cls = str(res['loadingExperience']['metrics'] ['CUMULATIVE_LAYOUT_SHIFT_SCORE']['percentile']) write_api = client.write_api(write_options=SYNCHRONOUS) point = Point("mem").tag("host", "host1").field( "URL", self.site).field("FETCH_TIME", fetchTime).field( "FCP", fcp).field("TOI", toi).field("SI", si).field( "FCI", fci).field("FMP", fmp).field("EIL", eil).field( "FIRST_CONTENTFUL_PAINT_MS", field_fcp).field( "FIRST_INPUT_DELAY_MS", field_fid).field( "LARGEST_CONTENTFUL_PAINT_MS", field_lcp).field( "CUMULATIVE_LAYOUT_SHIFT_SCORE", field_cls).time( datetime.utcnow(), WritePrecision.NS) write_api.write(bucket, org, point) except requests.exceptions.Timeout as e: print(e)