22
import six.moves.urllib.parse as urlparse
24
24
from oslo.config import cfg
25
25
from sqlalchemy import Column, Integer, String, Table, ForeignKey, \
26
Index, UniqueConstraint, BigInteger
27
from sqlalchemy import Float, Boolean, Text
26
Index, UniqueConstraint, BigInteger, join
27
from sqlalchemy import Float, Boolean, Text, DateTime
28
28
from sqlalchemy.dialects.mysql import DECIMAL
29
29
from sqlalchemy.ext.declarative import declarative_base
30
30
from sqlalchemy.orm import backref
31
from sqlalchemy.orm import column_property
31
32
from sqlalchemy.orm import relationship
32
from sqlalchemy.types import TypeDecorator, DATETIME
33
from sqlalchemy.types import TypeDecorator
34
35
from ceilometer.openstack.common import timeutils
35
36
from ceilometer.storage import models as api_models
130
131
Index('idx_su', sourceassoc.c['source_id'], sourceassoc.c['user_id']),
131
132
Index('idx_sp', sourceassoc.c['source_id'], sourceassoc.c['project_id']),
132
133
Index('idx_sr', sourceassoc.c['source_id'], sourceassoc.c['resource_id']),
133
Index('idx_sm', sourceassoc.c['source_id'], sourceassoc.c['meter_id']),
134
Index('idx_ss', sourceassoc.c['source_id'], sourceassoc.c['sample_id']),
134
135
Index('ix_sourceassoc_source_id', sourceassoc.c['source_id'])
135
UniqueConstraint(sourceassoc.c['meter_id'], sourceassoc.c['user_id'],
136
name='uniq_sourceassoc0meter_id0user_id')
136
UniqueConstraint(sourceassoc.c['sample_id'], sourceassoc.c['user_id'],
137
name='uniq_sourceassoc0sample_id0user_id')
139
140
class Source(Base):
184
185
__table_args__ = (
185
186
Index('ix_meta_float_key', 'meta_key'),
187
id = Column(Integer, ForeignKey('meter.id'), primary_key=True)
188
id = Column(Integer, ForeignKey('sample.id'), primary_key=True)
188
189
meta_key = Column(String(255), primary_key=True)
189
value = Column(Float, default=False)
190
value = Column(Float(53), default=False)
192
193
class Meter(Base):
194
"""Meter definition data."""
196
__tablename__ = 'meter'
198
UniqueConstraint('name', 'type', 'unit', name='def_unique'),
199
Index('ix_meter_name', 'name')
201
id = Column(Integer, primary_key=True)
202
name = Column(String(255), nullable=False)
203
type = Column(String(255))
204
unit = Column(String(255))
193
208
"""Metering data."""
195
__tablename__ = 'meter'
210
__tablename__ = 'sample'
196
211
__table_args__ = (
197
Index('ix_meter_timestamp', 'timestamp'),
198
Index('ix_meter_user_id', 'user_id'),
199
Index('ix_meter_project_id', 'project_id'),
200
Index('idx_meter_rid_cname', 'resource_id', 'counter_name'),
212
Index('ix_sample_timestamp', 'timestamp'),
213
Index('ix_sample_user_id', 'user_id'),
214
Index('ix_sample_project_id', 'project_id'),
202
216
id = Column(Integer, primary_key=True)
203
counter_name = Column(String(255))
217
meter_id = Column(Integer, ForeignKey('meter.id'))
204
218
user_id = Column(String(255), ForeignKey('user.id'))
205
219
project_id = Column(String(255), ForeignKey('project.id'))
206
220
resource_id = Column(String(255), ForeignKey('resource.id'))
207
221
resource_metadata = Column(JSONEncodedDict())
208
counter_type = Column(String(255))
209
counter_unit = Column(String(255))
210
counter_volume = Column(Float(53))
222
volume = Column(Float(53))
211
223
timestamp = Column(PreciseTimestamp(), default=timeutils.utcnow)
224
recorded_at = Column(PreciseTimestamp(), default=timeutils.utcnow)
212
225
message_signature = Column(String(1000))
213
226
message_id = Column(String(1000))
214
227
sources = relationship("Source", secondary=lambda: sourceassoc)
215
meta_text = relationship("MetaText", backref="meter",
228
meta_text = relationship("MetaText", backref="sample",
216
229
cascade="all, delete-orphan")
217
meta_float = relationship("MetaFloat", backref="meter",
230
meta_float = relationship("MetaFloat", backref="sample",
218
231
cascade="all, delete-orphan")
219
meta_int = relationship("MetaBigInt", backref="meter",
232
meta_int = relationship("MetaBigInt", backref="sample",
220
233
cascade="all, delete-orphan")
221
meta_bool = relationship("MetaBool", backref="meter",
234
meta_bool = relationship("MetaBool", backref="sample",
222
235
cascade="all, delete-orphan")
238
class MeterSample(Base):
239
"""Helper model as many of the filters work against Sample data
240
joined with Meter data.
242
meter = Meter.__table__
243
sample = Sample.__table__
244
__table__ = join(meter, sample)
246
id = column_property(sample.c.id)
247
meter_id = column_property(meter.c.id, sample.c.meter_id)
248
counter_name = column_property(meter.c.name)
249
counter_type = column_property(meter.c.type)
250
counter_unit = column_property(meter.c.unit)
251
counter_volume = column_property(sample.c.volume)
252
sources = relationship("Source", secondary=lambda: sourceassoc)
225
255
class User(Base):
226
256
__tablename__ = 'user'
227
257
id = Column(String(255), primary_key=True)
228
258
sources = relationship("Source", secondary=lambda: sourceassoc)
229
259
resources = relationship("Resource", backref='user')
230
meters = relationship("Meter", backref='user')
260
samples = relationship("Sample", backref='user')
233
263
class Project(Base):