<s> import calendar <EOL> from datetime import datetime <EOL> from decimal import Decimal <EOL> from enum import IntEnum <EOL> import timex <EOL> from sqlalchemy import and_ <EOL> from sqlalchemy import Column <EOL> from sqlalchemy . dialects . mysql import DECIMAL <EOL> from sqlalchemy . ext . associationproxy import association_proxy <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> from sqlalchemy . ext . hybrid import hybrid_property <EOL> from sqlalchemy import Float <EOL> from sqlalchemy import ForeignKey <EOL> from sqlalchemy import Index <EOL> from sqlalchemy import Integer <EOL> from sqlalchemy . orm import backref <EOL> from sqlalchemy . orm . collections import attribute_mapped_collection <EOL> from sqlalchemy . orm import composite <EOL> from sqlalchemy . orm . interfaces import PropComparator <EOL> from sqlalchemy . orm import relationship <EOL> from sqlalchemy import String <EOL> from sqlalchemy import Table <EOL> from sqlalchemy . types import TypeDecorator , DATETIME <EOL> class Datatype ( IntEnum ) : <EOL> none = <NUM_LIT:0> <EOL> string = <NUM_LIT:1> <EOL> int = <NUM_LIT:2> <EOL> float = <NUM_LIT:3> <EOL> datetime = <NUM_LIT:4> <EOL> timerange = <NUM_LIT:5> <EOL> class StreamState ( IntEnum ) : <EOL> active = <NUM_LIT:1> <EOL> firing = <NUM_LIT:2> <EOL> expiring = <NUM_LIT:3> <EOL> error = <NUM_LIT:4> <EOL> expire_error = <NUM_LIT:5> <EOL> completed = <NUM_LIT:6> <EOL> retry_fire = <NUM_LIT:7> <EOL> retry_expire = <NUM_LIT:8> <EOL> class DBException ( Exception ) : <EOL> pass <EOL> class InvalidTraitType ( DBException ) : <EOL> pass <EOL> def dt_to_decimal ( dt ) : <EOL> t_sec = calendar . timegm ( dt . utctimetuple ( ) ) + ( dt . microsecond / <NUM_LIT> ) <EOL> return Decimal ( "<STR_LIT>" % t_sec ) <EOL> def decimal_to_dt ( decimal_timestamp ) : <EOL> return datetime . utcfromtimestamp ( float ( decimal_timestamp ) ) <EOL> class PreciseTimestamp ( TypeDecorator ) : <EOL> """<STR_LIT>""" <EOL> impl = DATETIME <EOL> def load_dialect_impl ( self , dialect ) : <EOL> if dialect . name == '<STR_LIT>' : <EOL> return dialect . type_descriptor ( DECIMAL ( precision = <NUM_LIT:20> , <EOL> scale = <NUM_LIT:6> , <EOL> asdecimal = True ) ) <EOL> return dialect . type_descriptor ( DATETIME ( ) ) <EOL> def process_bind_param ( self , value , dialect ) : <EOL> if value is None : <EOL> return value <EOL> elif dialect . name == '<STR_LIT>' : <EOL> return dt_to_decimal ( value ) <EOL> return value <EOL> def process_result_value ( self , value , dialect ) : <EOL> if value is None : <EOL> return value <EOL> elif dialect . name == '<STR_LIT>' : <EOL> return decimal_to_dt ( value ) <EOL> return value <EOL> class DBTimeRange ( object ) : <EOL> def __init__ ( self , begin , end ) : <EOL> self . begin = begin <EOL> self . end = end <EOL> def __composite_values__ ( self ) : <EOL> return self . begin , self . end <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . begin , self . end ) <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , DBTimeRange ) and other . begin == self . begin and other . end == self . end <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> class ProxiedDictMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __len__ ( self ) : <EOL> return len ( self . _proxied ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _proxied ) <EOL> def __getitem__ ( self , name ) : <EOL> return self . _proxied [ name ] <EOL> def __contains__ ( self , name ) : <EOL> return name in self . _proxied <EOL> def __setitem__ ( self , name , value ) : <EOL> self . _proxied [ name ] = value <EOL> def __delitem__ ( self , name ) : <EOL> del self . _proxied [ name ] <EOL> class PolymorphicVerticalProperty ( object ) : <EOL> """<STR_LIT>""" <EOL> ATTRIBUTE_MAP = { Datatype . none : None } <EOL> PY_TYPE_MAP = { unicode : Datatype . string , <EOL> int : Datatype . int , <EOL> long : Datatype . int , <EOL> float : Datatype . float , <EOL> datetime : Datatype . datetime , <EOL> DBTimeRange : Datatype . timerange } <EOL> def __init__ ( self , name , value = None ) : <EOL> self . name = name <EOL> self . value = value <EOL> @ classmethod <EOL> def get_type_value ( cls , value ) : <EOL> if value is None : <EOL> return Datatype . none , None <EOL> if isinstance ( value , str ) : <EOL> value = value . decode ( '<STR_LIT:utf8>' , '<STR_LIT:ignore>' ) <EOL> if isinstance ( value , timex . Timestamp ) : <EOL> value = value . timestamp <EOL> if isinstance ( value , timex . TimeRange ) : <EOL> value = DBTimeRange ( value . begin , value . end ) <EOL> if type ( value ) in cls . PY_TYPE_MAP : <EOL> return cls . PY_TYPE_MAP [ type ( value ) ] , value <EOL> return None , value <EOL> @ hybrid_property <EOL> def value ( self ) : <EOL> if self . type not in self . ATTRIBUTE_MAP : <EOL> raise InvalidTraitType ( <EOL> "<STR_LIT>" % ( self . name , self . type ) ) <EOL> attribute = self . ATTRIBUTE_MAP [ self . type ] <EOL> if attribute is None : <EOL> return None <EOL> if self . type == Datatype . timerange : <EOL> val = getattr ( self , attribute ) <EOL> return timex . TimeRange ( val . begin , val . end ) <EOL> else : <EOL> return getattr ( self , attribute ) <EOL> @ value . setter <EOL> def value ( self , value ) : <EOL> datatype , value = self . get_type_value ( value ) <EOL> if datatype not in self . ATTRIBUTE_MAP : <EOL> raise InvalidTraitType ( <EOL> "<STR_LIT>" % ( self . name , datatype ) ) <EOL> attribute = self . ATTRIBUTE_MAP [ datatype ] <EOL> self . type = int ( datatype ) <EOL> if attribute is not None : <EOL> setattr ( self , attribute , value ) <EOL> @ value . deleter <EOL> def value ( self ) : <EOL> self . _set_value ( None ) <EOL> @ value . comparator <EOL> class value ( PropComparator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cls ) : <EOL> self . cls = cls <EOL> def __eq__ ( self , other ) : <EOL> dtype , value = self . cls . get_type_value ( other ) <EOL> if dtype is None : <EOL> dtype = Datatype . string <EOL> if dtype == Datatype . none : <EOL> return self . cls . type == int ( Datatype . none ) <EOL> attr = getattr ( self . cls , self . cls . ATTRIBUTE_MAP [ dtype ] ) <EOL> return and_ ( attr == value , self . cls . type == int ( dtype ) ) <EOL> def __ne__ ( self , other ) : <EOL> dtype , value = self . cls . get_type_value ( other ) <EOL> if dtype is None : <EOL> dtype = Datatype . string <EOL> if dtype == Datatype . none : <EOL> return self . cls . type != int ( Datatype . none ) <EOL> attr = getattr ( self . cls , self . cls . ATTRIBUTE_MAP [ dtype ] ) <EOL> return and_ ( attr != value , self . cls . type == int ( dtype ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . name , self . value ) <EOL> Base = declarative_base ( ) <EOL> class Trait ( PolymorphicVerticalProperty , Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> __table_args__ = ( <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> event_id = Column ( Integer , ForeignKey ( '<STR_LIT>' ) , primary_key = True ) <EOL> name = Column ( String ( <NUM_LIT:100> ) , primary_key = True ) <EOL> type = Column ( Integer ) <EOL> ATTRIBUTE_MAP = { Datatype . none : None , <EOL> Datatype . string : '<STR_LIT>' , <EOL> Datatype . int : '<STR_LIT>' , <EOL> Datatype . float : '<STR_LIT>' , <EOL> Datatype . datetime : '<STR_LIT>' , } <EOL> t_string = Column ( String ( <NUM_LIT:255> ) , nullable = True , default = None ) <EOL> t_float = Column ( Float , nullable = True , default = None ) <EOL> t_int = Column ( Integer , nullable = True , default = None ) <EOL> t_datetime = Column ( PreciseTimestamp ( ) , <EOL> nullable = True , default = None ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , <EOL> self . type , <EOL> self . t_string , <EOL> self . t_float , <EOL> self . t_int , <EOL> self . t_datetime , <EOL> self . event_id ) <EOL> class EventType ( Base ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> desc = Column ( String ( <NUM_LIT:255> ) , unique = True ) <EOL> def __init__ ( self , event_type ) : <EOL> self . desc = event_type <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . desc <EOL> class Event ( ProxiedDictMixin , Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> __table_args__ = ( <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> id = Column ( Integer , primary_key = True ) <EOL> message_id = Column ( String ( <NUM_LIT:50> ) , unique = True ) <EOL> generated = Column ( PreciseTimestamp ( ) ) <EOL> event_type_id = Column ( Integer , ForeignKey ( '<STR_LIT>' ) ) <EOL> event_type = relationship ( "<STR_LIT>" , backref = backref ( '<STR_LIT>' ) ) <EOL> traits = relationship ( "<STR_LIT>" , <EOL> collection_class = attribute_mapped_collection ( '<STR_LIT:name>' ) ) <EOL> _proxied = association_proxy ( "<STR_LIT>" , "<STR_LIT:value>" , <EOL> creator = lambda name , value : Trait ( <EOL> name = name , <EOL> value = value ) ) <EOL> @ property <EOL> def event_type_string ( self ) : <EOL> return self . event_type . desc <EOL> @ property <EOL> def as_dict ( self ) : <EOL> d = dict ( self . _proxied ) <EOL> d [ '<STR_LIT>' ] = self . message_id <EOL> d [ '<STR_LIT>' ] = self . event_type_string <EOL> d [ '<STR_LIT>' ] = self . generated <EOL> return d <EOL> def __init__ ( self , message_id , event_type , generated ) : <EOL> self . message_id = message_id <EOL> self . event_type = event_type <EOL> self . generated = generated <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . id , <EOL> self . message_id , <EOL> self . event_type , <EOL> self . generated ) <EOL> stream_event_table = Table ( <EOL> '<STR_LIT>' , Base . metadata , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) , primary_key = True ) , <EOL> Column ( '<STR_LIT>' , Integer , ForeignKey ( '<STR_LIT>' ) , primary_key = True ) <EOL> ) <EOL> class Stream ( ProxiedDictMixin , Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> __table_args__ = ( <EOL> Index ( '<STR_LIT>' , '<STR_LIT:name>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT:state>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> id = Column ( Integer , primary_key = True ) <EOL> first_event = Column ( PreciseTimestamp ( ) , nullable = False ) <EOL> last_event = Column ( PreciseTimestamp ( ) , nullable = False ) <EOL> expire_timestamp = Column ( PreciseTimestamp ( ) ) <EOL> fire_timestamp = Column ( PreciseTimestamp ( ) ) <EOL> name = Column ( String ( <NUM_LIT:255> ) , nullable = False ) <EOL> state = Column ( Integer , default = StreamState . active , nullable = False ) <EOL> state_serial_no = Column ( Integer , default = <NUM_LIT:0> , nullable = False ) <EOL> distinguished_by = relationship ( <EOL> "<STR_LIT>" , <EOL> cascade = "<STR_LIT>" , <EOL> collection_class = attribute_mapped_collection ( <EOL> '<STR_LIT:name>' ) ) <EOL> _proxied = association_proxy ( <EOL> "<STR_LIT>" , "<STR_LIT:value>" , <EOL> creator = lambda name , value : DistinguishingTrait ( <EOL> name = name , value = value ) ) <EOL> events = relationship ( Event , secondary = stream_event_table , <EOL> order_by = Event . generated ) <EOL> @ property <EOL> def distinguished_by_dict ( self ) : <EOL> return dict ( self . _proxied ) <EOL> @ property <EOL> def as_dict ( self ) : <EOL> return { '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT:id>' : self . id , <EOL> '<STR_LIT:state>' : StreamState ( self . state ) . name , <EOL> '<STR_LIT>' : self . first_event , <EOL> '<STR_LIT>' : self . last_event , <EOL> '<STR_LIT>' : self . fire_timestamp , <EOL> '<STR_LIT>' : self . expire_timestamp , <EOL> '<STR_LIT>' : self . distinguished_by_dict } <EOL> def __init__ ( self , name , first_event , last_event = None , <EOL> expire_timestamp = None , <EOL> fire_timestamp = None , state = None , state_serial_no = None ) : <EOL> self . name = name <EOL> self . first_event = first_event <EOL> if last_event is None : <EOL> last_event = first_event <EOL> self . last_event = last_event <EOL> self . expire_timestamp = expire_timestamp <EOL> self . fire_timestamp = fire_timestamp <EOL> if state is None : <EOL> state = StreamState . active <EOL> self . state = int ( state ) <EOL> if state_serial_no is None : <EOL> state_serial_no = <NUM_LIT:0> <EOL> self . state_serial_no = state_serial_no <EOL> class DistinguishingTrait ( PolymorphicVerticalProperty , Base ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> __table_args__ = ( <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Index ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> stream_id = Column ( Integer , ForeignKey ( '<STR_LIT>' ) , primary_key = True ) <EOL> name = Column ( String ( <NUM_LIT:100> ) , primary_key = True ) <EOL> type = Column ( Integer ) <EOL> ATTRIBUTE_MAP = { <EOL> Datatype . none : None , <EOL> Datatype . string : '<STR_LIT>' , <EOL> Datatype . int : '<STR_LIT>' , <EOL> Datatype . float : '<STR_LIT>' , <EOL> Datatype . datetime : '<STR_LIT>' , <EOL> Datatype . timerange : '<STR_LIT>' , <EOL> } <EOL> dt_string = Column ( String ( <NUM_LIT:255> ) , nullable = True , default = None ) <EOL> dt_float = Column ( Float , nullable = True , default = None ) <EOL> dt_int = Column ( Integer , nullable = True , default = None ) <EOL> dt_datetime = Column ( PreciseTimestamp ( ) , <EOL> nullable = True , default = None ) <EOL> dt_timerange_begin = Column ( <EOL> PreciseTimestamp ( ) , nullable = True , default = None ) <EOL> dt_timerange_end = Column ( PreciseTimestamp ( ) , nullable = True , default = None ) <EOL> dt_timerange = composite ( DBTimeRange , dt_timerange_begin , dt_timerange_end ) <EOL> @ property <EOL> def as_dict ( self ) : <EOL> return { self . name : self . value } <EOL> def __repr__ ( self ) : <EOL> return ( "<STR_LIT>" <EOL> % ( self . name , <EOL> self . type , <EOL> self . dt_string , <EOL> self . dt_float , <EOL> self . dt_int , <EOL> self . dt_datetime , <EOL> self . dt_timerange_begin , <EOL> self . dt_timerange_end , <EOL> self . stream_id ) ) </s>
