export Pacer data to InfluxDB
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.9KB

  1. #!/usr/bin/env python3
  2. from influxdb import InfluxDBClient
  3. import datetime
  4. import time
  5. import sqlite3 as lite
  6. import argparse
  7. def main(host='localhost', port=8086, user='root', password='root', dbname='demo', dbfile='demo.db'):
  8. """Instantiate the connection to the InfluxDB client."""
  9. db = InfluxDBClient(host, port, user, password, dbname)
  10. measurements = db.get_list_measurements()
  11. if measurements != []:
  12. lastentry = db.query('SELECT LAST("steps") FROM "steps"')
  13. points = lastentry.get_points('steps')
  14. lastdate = list(points)[0]['time']
  15. ts = time.mktime(datetime.datetime.strptime(lastdate, '%Y-%m-%dT%H:%M:%SZ').timetuple())
  16. else:
  17. ts=0
  18. lastdate=0
  19. print("last entry is %s, ts is %s" % (lastdate,ts))
  20. con = lite.connect(dbfile)
  21. with con:
  22. cur = con.cursor()
  23. cur.execute("SELECT recordedForDate,steps,distanceInMeters,activeTimeInSeconds,calories FROM dailyActivityLog WHERE recordedForDate > %s" %ts)
  24. while True:
  25. row = cur.fetchone()
  26. if row == None:
  27. break
  28. mytime = datetime.datetime.fromtimestamp(row[0]).strftime('%Y-%m-%dT%H:%M:%SZ')
  29. data = [
  30. {"measurement":"steps",
  31. "time":mytime,
  32. "fields": {
  33. "steps":row[1],
  34. "distanceInMeters":row[2],
  35. "activeTimeInSeconds":row[3],
  36. "calories":row[4]
  37. }
  38. }
  39. ]
  40. #print(data)
  41. db.write_points(data)
  42. db.close()
  43. def parse_args():
  44. """Parse the args from main."""
  45. parser = argparse.ArgumentParser(
  46. description='Export Pacer data to InfluxDB')
  47. parser.add_argument('--host', type=str, required=False,
  48. default='localhost',
  49. help='hostname of InfluxDB http API')
  50. parser.add_argument('--user', type=str, required=False,
  51. default='root',
  52. help='username for InfluxDB http API')
  53. parser.add_argument('--password', type=str, required=False,
  54. default='root',
  55. help='passworid for InfluxDB http API')
  56. parser.add_argument('--port', type=int, required=False, default=8086,
  57. help='port of InfluxDB http API')
  58. parser.add_argument('--dbname', type=str, required=True, default='demo',
  59. help='InfluxDB database name')
  60. parser.add_argument('--dbfile', type=str, required=True, default='demo.db',
  61. help='sqlite (pacer) database name')
  62. return parser.parse_args()
  63. if __name__ == '__main__':
  64. args = parse_args()
  65. main(host=args.host, port=args.port, user=args.user, password=args.password, dbname=args.dbname, dbfile=args.dbfile)