Motr  M0
influx_test_data.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2020 Seagate Technology LLC and/or its Affiliates
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # For any questions about this software or licensing,
17 # please email opensource@seagate.com or cortx-questions@seagate.com.
18 #
19 
20 import time
21 import argparse
22 import datetime
23 from influxdb import InfluxDBClient
24 
25 USER = ''
26 PASSWORD = ''
27 DBNAME = 'm0play'
28 
29 class InfluxDB:
30  def __init__(self, user, password, dbname, host, port, bucket_size):
31  self.client = InfluxDBClient(host, port, user, password, dbname)
32  self.bucket = []
33  self.bucket_size = bucket_size
34 
35  def createDB(self):
36  self.client.create_database(self.dbname)
37 
38  def openDB(self):
39  self.client.switch_database(self.dbname)
40 
41  def closeDB(self):
42  client.write_points(self.bucket)
43 
44  def writePoint(self, point):
45  self.bucket.append(point)
46  if len(self.bucket) < bucket_size:
47  client.write_points(self.bucket)
48 
49 
50 def main(host='localhost', port=8086):
51  now = datetime.datetime.today()
52 
53  point = {
54  "measurement": 'meas0',
55  "time": now.isoformat(timespec='milliseconds')
56  "fields": {
57  "id": 11100111
58  },
59  "tags": {
60  "type": 'XXX00YYY'
61  }
62  }
63 
64  db = InfluxDB(USER, PASSWORD, DBNAME, host, port, 10000)
65  db.createDB()
66  db.openDB()
67  db.writePoint(point)
68  db.closeDB()
69 
70 
71 def parse_args():
72  """Parse the args."""
73  parser = argparse.ArgumentParser(
74  description='example code to play with InfluxDB')
75  parser.add_argument('--host', type=str, required=False,
76  default='localhost',
77  help='hostname influxdb http API')
78  parser.add_argument('--port', type=int, required=False, default=8086,
79  help='port influxdb http API')
80  return parser.parse_args()
81 
82 
83 if __name__ == '__main__':
84  args = parse_args()
85  main(host=args.host, port=args.port)
def writePoint(self, point)
def __init__(self, user, password, dbname, host, port, bucket_size)
def main(host='localhost', port=8086)
Definition: main.py:1