forked from openbufo/influxdb-odata-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
influxdbmeta.py
163 lines (130 loc) · 5.49 KB
/
influxdbmeta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from itertools import chain
from influxdb import InfluxDBClient
xml_head = """<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.microsoft.com/ado/2007/06/edmx ">
<edmx:DataServices m:DataServiceVersion="2.0">
<Schema Namespace="InfluxDBSchema" xmlns="http://schemas.microsoft.com/ado/2006/04/edm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.microsoft.com/ado/2006/04/edm ">"""
xml_foot = """
</Schema>
</edmx:DataServices>
</edmx:Edmx>"""
influx_type_to_edm_type = {
'float': 'Edm.Double', # influxdb stores floats in a float64 format
'integer': 'Edm.Int64', # influxdb stores integers as 64-bit signed
'string': 'Edm.String'
}
def get_edm_type(influx_type):
if influx_type is None:
return 'Edm.String'
else:
return influx_type_to_edm_type[influx_type]
def mangle_measurement_name(m_name):
"""corresponds to unmangle_measurement_name in influxdbds.py"""
m_name = m_name.replace(' ', '_sp_')
m_name = m_name.replace('-', '_dsh_')
return m_name
def mangle_db_name(db_name):
"""corresponds to unmangle_db_name in influxdbds.py"""
db_name = db_name.strip('_') # edmx names cannot begin with '_'
db_name = db_name.replace('-', '_dsh_')
return db_name
def db_name__measurement_name(db_name, m_name):
return '{}__{}'.format(
mangle_db_name(db_name),
mangle_measurement_name(m_name)
)
def mangle_field_name(field_name):
return field_name.replace(' ', '_sp_').replace('.','_dot_')
class InfluxDB(object):
def __init__(self, config):
try:
dsn = config['dsn']
self.client = InfluxDBClient.from_dsn(dsn)
except Exception as e:
print("Failed to connect to InfluxDB")
print(str(e))
raise ConnectionError()
try:
databases = config['databases']
if databases is not None:
try:
databases = databases.split(',')
except:
databases = list()
else:
databases = list()
except:
raise ValueError()
self.selected_databases = databases
def fields(self, db_name):
"""returns a tuple of dicts where each dict has attributes (name, type, edm_type)"""
fields_rs = self.client.query('SHOW FIELD KEYS', database=db_name)
tags_rs = self.client.query('SHOW TAG KEYS', database=db_name)
# expand and deduplicate
fields = set(tuple(f.items()) for f in chain(*chain(fields_rs, tags_rs)))
fields = (dict(
name=mangle_field_name(f[0][1]),
type='string' if len(f)==1 else f[1][1],
edm_type=get_edm_type('string' if len(f)==1 else f[1][1])
) for f in fields)
return tuple(fields)
@property
def measurements(self):
measurements = []
for db in self.databases:
if db[u'name'] not in self.selected_databases:
continue
q = 'SHOW MEASUREMENTS'
rs = self.client.query(q, database=db[u'name'])
def m_dict(m):
d = dict(m)
d['db_name'] = db['name']
d['mangled_db'] = mangle_db_name(db['name'])
d['mangled_measurement'] = mangle_measurement_name(m['name'])
d['mangled_path'] = db_name__measurement_name(db['name'], m['name'])
d['fields'] = self.fields(db['name'])
return d
measurements.extend(m_dict(m) for m in rs.get_points())
return measurements
@property
def databases(self):
rs = self.client.get_list_database()
return iter(rs)
def gen_entity_set_xml(m):
return '<EntitySet Name="{}" EntityType="InfluxDBSchema.{}"/>'.format(m['mangled_path'], m['mangled_path'])
def generate_properties_xml(m):
return '\n'.join(
'<Property Name="{}" Type="{}" Nullable="true" />'.format(f['name'], f['edm_type']) for f in m['fields']
)
def generate_key_xml(m):
"""influxdb has no concept of a key, so we use the time value (NOT gauranteed to be unique)"""
return '<Key><PropertyRef Name="timestamp" /></Key><Property Name="timestamp" Type="Edm.DateTime" Precision="6" Nullable="false" />'
def gen_entity_type_xml(m):
return '<EntityType Name="{}">{}\n{}</EntityType>'.format(
m['mangled_path'],
generate_key_xml(m),
generate_properties_xml(m))
def entity_sets_and_types(db):
"""generate xml entries for entity sets (containers) and entity types (with properties)"""
entity_sets = []
entity_types = []
for m in db.measurements:
entity_sets.append(gen_entity_set_xml(m))
entity_types.append(gen_entity_type_xml(m))
return entity_sets, entity_types
def generate_metadata(connection):
"""connect to influxdb, read the structure, and return an edmx xml file string"""
i = InfluxDB(connection)
entity_sets, entity_types = entity_sets_and_types(i)
output = """{}
<EntityContainer Name="InfluxDB" m:IsDefaultEntityContainer="true">
{}
</EntityContainer>
{}
{}""".format(xml_head, '\n'.join(entity_sets), '\n'.join(entity_types), xml_foot)
return output