Skip to content

Instantly share code, notes, and snippets.

@filipecosta90
Last active March 23, 2019 13:05
Show Gist options
  • Save filipecosta90/8c274f45aed77e38b6147ee3e798cea3 to your computer and use it in GitHub Desktop.
Save filipecosta90/8c274f45aed77e38b6147ee3e798cea3 to your computer and use it in GitHub Desktop.
redis-porto :: redistimeseries-airquality :: dataloader snippet
# snippet from https://github.com/redis-porto/redistimeseries-airquality/blob/master/dataloader.py
#(...)
#(...)
# redis setup
redis_obj = redis.Redis(host=args.host, port=args.port, password=args.password)
temperature_key = "ts:temperature"
carbon_monoxide_key = "ts:carbon_monoxide"
relative_humidity_key = "ts:relative_humidity"
with open(args.csv, newline="") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=args.csv_delimiter)
next(csv_reader, None) # skip the headers
for row in tqdm(csv_reader):
(
result,
unix_ts,
carbon_monoxide,
temperature_c,
relative_humidity,
) = parse_dataset_row(row)
if result is True:
try:
if temperature_c is not None:
redis_obj.execute_command(
"ts.add", temperature_key, unix_ts, temperature_c
)
logging.info(
"ts.add {0} {1} {2}".format(
temperature_key, unix_ts, temperature_c
)
)
if carbon_monoxide is not None:
redis_obj.execute_command(
"ts.add", carbon_monoxide_key, unix_ts, carbon_monoxide
)
logging.info(
"ts.add {0} {1} {2}".format(
carbon_monoxide_key, unix_ts, carbon_monoxide
)
)
if relative_humidity is not None:
redis_obj.execute_command(
"ts.add", relative_humidity_key, unix_ts, relative_humidity
)
logging.info(
"ts.add {0} {1} {2}".format(
relative_humidity_key, unix_ts, relative_humidity
)
)
except redis.RedisError as err:
logging.error(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment