def on_message(client, userdata, message): payload = str(message.payload.decode("utf-8")) #print("message received " ,playload) payload = json.loads(payload) #print("message topic=",message.topic) #print("message qos=",message.qos) #print("message retain flag=",message.retain) if telemetry in message.topic: if payload["topic"] not in data_topics: data_topics.add(payload["topic"]) client.subscribe(payload["topic"]) #save telemetry service = message.topic.replace(telemetry, "") p = influxdb_client.Point("telemetry").tag("test",tag)\ .tag("service_type",service)\ .field("id",payload["topic"].replace(service+"-",""))\ .field("queue_len",payload["qsize"]) write_api.write(bucket=bucket, org=org, record=p, token=token) else: for topic in topics: if topic in message.topic: del payload['pixels'] #print( message.topic," : ",payload) #save data p = influxdb_client.Point("message").tag("test",tag) \ .tag("service_type", topic) \ .field("id", message.topic.replace(topic + "-", "")) for key in payload: if key == "licence_plate": for lp in payload["licence_plate"]: lp_point = influxdb_client.Point("licese_plate").tag("test",tag) \ .tag("service_type", topic) \ .field("id", message.topic.replace(topic + "-", "")) \ .tag("frame_id",payload["frame_id"]) for key in lp: if key != "position": lp_point.field(key, lp[key]) write_api.write(bucket=bucket, org=org, record=lp_point, token=token) else: p.field(key, payload[key]) write_api.write(bucket=bucket, org=org, record=p, token=token) break
def write_influxDB(): import influxdb_client from influxdb_client.client.write_api import SYNCHRONOUS import random # bucket = "HRWEB" # org = "PM72" # token = "3sLsq9ECi2eSQEYQQjIdxZsTuV6NtFcaohVKzNeILEo5hOPGCRt0Mmgzug_8iai9fCNfbUD1s3wAYd5LAXHOjg==" ## Store the URL of your InfluxDB instance # # url="https://eu-central-1-1.aws.cloud2.influxdata.com/" client = influxdb_client.InfluxDBClient(url=URL, token=TOKEN, org=ORG) write_api = client.write_api(write_options=SYNCHRONOUS) npoints = tdicprsar['number_of_points_write'] tdicprsar.update({'hr_measurement': MEASUREMENT}) min = -5 max = 15 for item in range(tdicprsar['number_of_points_write']): hr = 70 + random.randint(min, max) p = influxdb_client.Point(tdicprsar['hr_measurement']).tag( "username", tdicprsar['username']).tag("location", tdicprsar['location']).tag( "conditions", tdicprsar['conditions']).field("hr_per_minute", hr) write_api.write(bucket=BUCKET, org=ORG, record=p)
def getTickerPriceDates2Db(ticker,bucket,org,date1,date2,url,token): #stock = yf.Ticker(ticker) #stock_history=stock.history(period="max") stock_history=yf.download(ticker,start=date1, end=date2) if stock_history.empty: return print("=============") print("ticker:",ticker) print(stock_history) num_records = len(stock_history) print("length:",num_records) for i in range(num_records): print("record:",i) dictionary_stock_history = stock_history.to_dict('records') field_string=dictionary_stock_history[i] time_string=str(stock_history.index.values[i]) time_string=time_string[:-3] #print(time_string) utc_time = datetime.strptime(time_string,"%Y-%m-%dT%H:%M:%S.%f") utc_time=int(utc_time.timestamp()) open = float(field_string['Open']) high = float(field_string['High']) low = float(field_string['Low']) close = float(field_string["Close"]) volume = float(field_string['Volume']) q = influxdb_client.Point("price").tag("ticker",ticker).field("close",close).field("high",high).field("low",low).field("open",open).field("volume",volume).time(time_string) writePriceToDb(q,bucket,org,url,token)
def __write_data(self): """ Writes into influxdb data found into internal shared priority queue """ # Check if there is volume values to write while len(self.tcp_queue) > 0: # Retrieve timestamp,values from <timestamp:[data]> timestamp, values = heapq.heappop(self.tcp_queue) # Create point with [data] and write it to bucket bucket_name p_syn = influxdb_client.Point("data_interval") for label, value in values: p_syn.field(label, value) try: # Writing point to influxdb self.write_api.write(bucket=self.bucket_name, org=self.org, record=p_syn) except Exception: self.stop_writing_thread() raise Exception( "[Graph mode] - Error while writing to influxdb instance: check your service or .ini file!" )
def getTickerPrice(ticker): stock = yf.Ticker(ticker) stock_yesterday=stock.history(period="1d") if stock_yesterday.empty: return print("=============") print("ticker:",ticker) print(stock_yesterday) dictionary_stock_yesterday = stock_yesterday.to_dict('records') field_string=dictionary_stock_yesterday[0] time_string=str(stock_yesterday.index.values[0]) time_string=time_string[:-3] print(time_string) utc_time = datetime.strptime(time_string,"%Y-%m-%dT%H:%M:%S.%f") utc_time=int(utc_time.timestamp()) open = float(field_string['Open']) high = float(field_string['High']) low = float(field_string['Low']) close = float(field_string["Close"]) volume = float(field_string['Volume']) q = influxdb_client.Point("price").tag("ticker",ticker).field("close",close).field("high",high).field("low",low).field("open",open).field("volume",volume).time(time_string) return q
def post(self, fields): point = influxdb_client.Point(self.measurement) for t in self.tags.keys(): point.tag(t, self.tags[t]) for f in fields.keys(): point.field(f, fields[f]) self.write_api.write(bucket=self.rover, org="", record=point)
def callback(data: BatteryState): dt = datetime.now() utc_time = dt.replace(tzinfo=timezone.utc) utc_timestamp = utc_time.timestamp() rospy.loginfo( rospy.get_caller_id() + " Voltaje en batería: %s en la hora: %s", str(data.voltage), str(utc_timestamp)) p = influxdb_client.Point("battery").field("voltage", data.voltage) write_api.write(bucket=bucket, record=p)
def get_cpu_usage(): """Get CPU usage per second Returns: point object from influxdb-client """ cpu_usage = cpu_percent(interval=1) return influxdb_client.Point("cpu").field("CPU Percent", cpu_usage).tag( "host_name", gma() + "")
def get_swap_memory_used(): """Get swap memory used Returns: point object from influxdb-client """ swap_used = swap_memory() return influxdb_client.Point("memory").field("Swap memory used", swap_used.percent).tag( "host_name", gma() + "")
def get_network_data(): """Get network information since system starts Returns: point object from influxdb-client """ network_data = net_io_counters() return influxdb_client.Point("network")\ .field("uploading", network_data.bytes_sent)\ .field("downloading", network_data.bytes_recv)\ .tag("host_name", gma() + "")
def post_to_influx(self, key, value, tags, time): p = influxdb_client.Point(key) for tag_key, tag_value in tags.items(): p.tag(tag_key, tag_value) print(tag_key, tag_value) p.time(time) p.field("value", value) self.write_api.write(bucket=self.influx_bucket, org=self.influx_org, record=p)
def log_user_login(user: str, bucket: str, api=write_api): try: print("send login data to InfluxDB") point = influxdb_client.Point("user_login").tag( "user", user).field("login", 1.0) api.write(bucket=bucket, org=ORG, record=point) except Exception as err: print(err) pass
def get_virtual_memory(): """Get virtual memory information Returns: point object from influxdb-client """ memory = virtual_memory() return influxdb_client.Point("memory")\ .field("total_virtual_memory", memory.total)\ .field("virtual_memory_used", memory.used)\ .field("virtual_memory_free", memory.free)\ .tag("host_name", gma() + "")
def get_battery_level(): """Get battery level Returns: point object from influxdb-client """ battery_level = sensors_battery() if battery_level is not None: return influxdb_client.Point("sensors").field( "Battery Level", battery_level.percent).tag("host_name", gma() + "")
def post_to_influx(self, key, value, tags, time): p = influxdb_client.Point(key) for tag_key, tag_value in tags.items(): p.tag(tag_key, tag_value) print(tag_key, tag_value) p.time( datetime.datetime.strptime(time, '%Y-%m-%dT%H:%M:%S.%f').utcnow(), WritePrecision.NS) p.field("value", value) self.write_api.write(bucket=self.influx_bucket, org=self.influx_org, record=p)
def update(self): self.__logger.debug('Writing data to InfluxDB') p = influxdb2.Point('BME280') \ .field('temperature', self.__sensors.bme280_temperature) \ .field('humidity', self.__sensors.bme280_humidity) \ .field('pressure', self.__sensors.bme280_pressure) if not self.__is_db_connected: self.__logger.error( 'Lost connection with database. Try to reconnect') self.__connect_to_db(self.__influx_db_client.url, self.__influx_db_client.token) self.__db_writer.write('sensors/monthly', record=[p])
def log_sections(user: str, sections: list, bucket: str, api=write_api): try: print(client.health()) for section in sections: point = influxdb_client.Point("downloads").tag( "user", user).tag("section", section).field("section", 1.0) print(f"send {section} data to InfluxDB") api.write(bucket=bucket, org=ORG, record=point) except Exception as err: print(err) pass
def updatePlantData(pPlant): recordData = influxdb_client.Point("measurements").tag( "ID", pPlant.getID()).field("Temperatur", pPlant.getTemperature()).field( "Humidity", pPlant.getHumidity()).field("Light", pPlant.getLight()).field( "WaterLevel", pPlant.getWater()).field("Time", pPlant.getTime()).field( "Name", pPlant.getName()).field("PlantFinish", pPlant.getPlantFinish()).field( "Battery", pPlant.getBatteryLevel()) write_api.write(bucket=bucketName, org=org, record=recordData)
def _log_timed_metric(measurement: str, duration_ms, **tags: str): client = _get_influxdb_client() if client: with client.write_api( point_settings=_get_point_settings()) as write_api: p = influxdb_client.Point(measurement) for k, v in tags.items(): p = p.tag(k, v) p = p.field("duration", duration_ms) p.tag("thread_id", threading.get_ident()) p.tag("run", get_run_id()) p.time(_time_ns(), write_precision="ns") org_name, bucket_name = _get_org_bucket() write_api.write(bucket_name, org_name, p)
def get_disk_usage(): """Get disks information Returns: list of points object from influxdb-client """ disks = disk_partitions() for disk in disks: space_disk_usage = disk_usage(disk.device) yield influxdb_client.Point("disks")\ .field("total", space_disk_usage.total)\ .field("used", space_disk_usage.used)\ .field("free", space_disk_usage.free)\ .field("percent", space_disk_usage.percent)\ .tag("host_name", gma() + "")\ .tag("disk_name", disk.device)
def updateInfluxDBINFLUX(locationX, locationY, intensityFire): # Create Point pointToInsert = influxdb_client.Point("CurrentFire").tag( "Sensor", "Sensor(" + str(locationX) + "," + str(locationY) + ")").field("Location X", int(locationX)).field("Location Y", int(locationY)).field( "Fire Intensity", int(intensityFire)) print("----- Insertion in InfluxDB -----") print("-> Measurement : CurrentFire") print("-> tag Name: Fire(" + str(locationX) + "," + str(locationY) + ")") print("-> Value: " + intensityFire) # Write Point write_apiInfluxDB.write(bucket=INFLUXDB_BUCKET, org=INFLUXDB_ORG, record=pointToInsert)
def insert_point(measurement, tags, fields): _queue_lock.acquire() try: if not _client: return point = influxdb_client.Point(settings.app.influxdb_prefix + measurement) for key, val in tags.items(): point.tag(key, val) for key, val in fields.items(): point.field(key, val) _queue.append(point) finally: _queue_lock.release()
def insert_sensordatarow(sensor, location, dblvalueraw, value2): """ Insert statment for sensor row data - One row :param sensor: :param location: :param dblvalue_raw: :param value2: """ # query = "INSERT INTO SensorData(sensor, location, dblvalueraw, value2) " \ # "VALUES(%s, %s, %d, %s)" # args = (sensor, location, dblvalue_raw, value2) try: # connect to influxdb and insert a point = row db_config = read_db_config() #print(db_config) client = influxdb_client.InfluxDBClient(url=db_config.get("url"), token=db_config.get("token"), org=db_config.get("org")) if client is None: print('Connection failed.') else: print('Connection established.') write_api = client.write_api(write_options=SYNCHRONOUS) # write a point or row to influxdb p = influxdb_client.Point("SendorData").tag("location", location).tag( "sensor", sensor).field("value", dblvalueraw).time(datetime.now(), WritePrecision.MS) write_api.write(bucket=bucket, org=org, record=p) except client is None: print("Connection Failed with error: ") finally: """ Close client """ client.__del__()
def createDataPoint(account, chanName, watts, timestamp, detailed): dataPoint = None if influxVersion == 2: dataPoint = influxdb_client.Point("energy_usage") \ .tag("account_name", account['name']) \ .tag("device_name", chanName) \ .tag("detailed", detailed) \ .field("usage", watts) \ .time(time=timestamp) else: dataPoint = { "measurement": "energy_usage", "tags": { "account_name": account['name'], "device_name": chanName, "detailed": detailed, }, "fields": { "usage": watts, }, "time": timestamp } return dataPoint
def message_handler(ch, method, properties, body): try: msg = message.load(body) except Exception: ch.basic_ack(method.delivery_tag) logging.warning("failed to parse message") return try: record = (influxdb_client.Point(msg.name).tag( "node", msg.meta["node"]).tag("plugin", msg.meta["plugin"]).field( "value", msg.value).time(msg.timestamp, WritePrecision.NS)) except KeyError as key: ch.basic_ack(method.delivery_tag) logging.warning("message missing meta %s", key) return writer.write(bucket=args.influxdb_bucket, org=args.influxdb_org, record=record) ch.basic_ack(method.delivery_tag) logging.debug("proccessed message %s", msg)
choices = {'Pionero':'CFIPIONERO.SN','Moneda Latinoamérica Deuda Local (Serie A)':'CFIMLDL.SN','Moneda Renta CLP':'CFIMRCLP.SN'} #print(new_table) #print(d.items()) midnight = datetime.combine(datetime.today(), time.min) yesterday=midnight-timedelta(days=1) #print("yesterday:",yesterday) #insertdate = yesterday #print(midnight) for i in range(row_marker): fund_name=new_table[0][i].strip() test_string=new_table[1][i].strip() currency=test_string[0:3] value=float((test_string[3:].replace(".","")).strip().replace(",",".")) print("Fondo:",fund_name,"; Moneda:",currency,";Valor Cuota:",value) ticker=choices.get(fund_name,0) ##DEBUG # print(new_table) ##DEBUG # exit() if ticker != 0: print("yes:",value,insertdate,currency) q = influxdb_client.Point("price").tag("ticker",ticker).tag("name",fund_name).tag("currency",currency).field("nav",value).time(insertdate) writePriceToDb(q,bucket,org,config.url,config.token) if currency == "USD" and is_number(usd_obs): value=value*usd_obs print("value clp:",value,"date:",insertdate) q = influxdb_client.Point("price").tag("ticker",ticker).tag("name",fund_name).tag("currency","CLP").field("nav",value).time(insertdate) writePriceToDb(q,bucket,org,config.url,config.token)
org = os.environ["INFLUXDB_ORGANIZATION"] token = os.environ["INFLUXDB_TOKEN"] url = os.environ["INFLUXDB_URL"] client = influxdb_client.InfluxDBClient(url=url, token=token, org=org) write_api = client.write_api(write_options=SYNCHRONOUS) # クランプ内に流れる電力を返す(交流なので負の値もありえる) def current(): # `pot.value`の取る値[0, +1]を[-0.5, +0.5]の範囲にしたあと, # 最大電圧(5.0V)をかけて電圧に変換する. voltage = (pot.value - bias) * max_voltage # オームの法則から, センサーに流れている電流を計算する. ampere = voltage / resist # CTセンサに流れている電流から, 観測対象の回路の電流を計算する. ampere_observe = ampere * rate # 電流*実効電圧で電力量を計算する. watt = ampere_observe * effective_voltage return watt while True: sum_ = 0 for i in range(num_sampling): sum_ += current()**2 watt = math.sqrt(sum_ / num_sampling) p = influxdb_client.Point("watt").tag("location", "somewhere").field("watt", watt) write_api.write(bucket=bucket, org=org, record=p)
write_api = client.write_api(write_options=SYNCHRONOUS) # Powerwall ip = os.getenv("POWERWALL_IP") if ip is None: raise ValueError("POWERWALL_IP must be set") email = os.getenv("POWERWALL_EMAIL") password = os.getenv("POWERWALL_PASSWORD") power_wall = Powerwall(ip) # Program # Identify the powerwall version power_wall.detect_and_pin_version() print("Detected and pinned version: {}".format( power_wall.get_pinned_version())) print("Current charge: {}".format(power_wall.get_charge())) print("Device Type: {}".format(power_wall.get_device_type())) # Sending Data while True: p = influxdb_client.Point("Measurement").field("Charge", power_wall.get_charge) write_api.write(bucket=bucket, org=org, record=p) sleep(1)
def send_job_influxdb(): for x in range(100): time.sleep(2) write_api = client.write_api(write_options=SYNCHRONOUS) p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature",random.uniform(1, 100)) write_api.write(bucket=bucket, org=org, record=p)
def __init__(self, influx_config_file): with open(influx_config_file, 'r') as stream: try: db_config = yaml.safe_load(stream) except yaml.YAMLError as exc: print(exc) self.client = influxdb_client.InfluxDBClient( url=db_config["url"], token=db_config["token"], org=db_config["org"] ) print(db_config) self.write_api = self.client.write_api(write_options=SYNCHRONOUS) p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature", 25.3) self.write_api.write(bucket=db_config["bucket"], org=db_config["org"], record=p) self.query_api = self.client.query_api() query = f'from(bucket:"{db_config["bucket"]}")\ |> range(start: -10m)\ |> filter(fn:(r) => r._measurement == "my_measurement")\ |> filter(fn: (r) => r.location == "Prague")\ |> filter(fn:(r) => r._field == "temperature" )' result = self.query_api.query(org=db_config["org"], query=query) results = [] for table in result: for record in table.records: results.append((record.get_field(), record.get_value())) print(results) t1 = datetime.datetime.now(tz=datetime.timezone.utc) time.sleep(1) t2 = datetime.datetime.now(tz=datetime.timezone.utc) point1 = {"measurement": "h2o_feet", "tags": {"location": "coyote_creek"}, "fields": {"water_level": 2.0}, "time": t1} point2 = {"measurement": "h2o_feet", "tags": {"location": "coyote_creek"}, "fields": {"water_level": 3.0}, "time": t2} points = [point1, point2] self.write_api.write(bucket=db_config["bucket"], org=db_config["org"], record=points) # query = f'from(bucket:"{db_config["bucket"]}")\ |> range(start: -10m)\ |> filter(fn: (r) => r._measurement == "h2o_feet")\ |> filter(fn: (r) => r.location == "coyote_creek")\ |> filter(fn: (r) => r._field == "water_level" )' result = self.query_api.query(org=db_config["org"], query=query) results = [] for table in result: for record in table.records: results.append((record.get_value(), record.get_field())) print(results)