Esempio n. 1
0
 def test_decimal_to_datetime(self):
     expected = 1356093296.12
     dexpected = decimal.Decimal(str(expected))  # Python 2.6 wants str()
     expected_datetime = datetime.datetime.utcfromtimestamp(expected)
     actual_datetime = utils.decimal_to_dt(dexpected)
     # Python 3 have rounding issue on this, so use float
     self.assertAlmostEqual(utils.dt_to_decimal(expected_datetime),
                            utils.dt_to_decimal(actual_datetime),
                            places=5)
Esempio n. 2
0
 def test_decimal_to_datetime(self):
     expected = 1356093296.12
     dexpected = decimal.Decimal(str(expected))  # Python 2.6 wants str()
     expected_datetime = datetime.datetime.utcfromtimestamp(expected)
     actual_datetime = utils.decimal_to_dt(dexpected)
     # Python 3 have rounding issue on this, so use float
     self.assertAlmostEqual(utils.dt_to_decimal(expected_datetime),
                            utils.dt_to_decimal(actual_datetime),
                            places=5)
Esempio n. 3
0
 def get_value(self):
     if self.t_type == api_models.Trait.INT_TYPE:
         return self.t_int
     if self.t_type == api_models.Trait.FLOAT_TYPE:
         return self.t_float
     if self.t_type == api_models.Trait.DATETIME_TYPE:
         return utils.decimal_to_dt(self.t_datetime)
     if self.t_type == api_models.Trait.TEXT_TYPE:
         return self.t_string
     return None
Esempio n. 4
0
 def get_value(self):
     if self.t_type == api_models.Trait.INT_TYPE:
         return self.t_int
     if self.t_type == api_models.Trait.FLOAT_TYPE:
         return self.t_float
     if self.t_type == api_models.Trait.DATETIME_TYPE:
         return utils.decimal_to_dt(self.t_datetime)
     if self.t_type == api_models.Trait.TEXT_TYPE:
         return self.t_string
     return None
Esempio n. 5
0
    def get_events(self, event_filter):
        """Return an iterable of model.Event objects.

        :param event_filter: EventFilter instance
        """

        start = utils.dt_to_decimal(event_filter.start)
        end = utils.dt_to_decimal(event_filter.end)
        session = sqlalchemy_session.get_session()
        with session.begin():
            sub_query = session.query(Event.id)\
                .join(Trait, Trait.event_id == Event.id)\
                .filter(Event.generated >= start, Event.generated <= end)

            if event_filter.event_name:
                event_name = self._get_unique(session, event_filter.event_name)
                sub_query = sub_query.filter(Event.unique_name == event_name)

            if event_filter.traits:
                for key, value in event_filter.traits.iteritems():
                    if key == 'key':
                        key = self._get_unique(session, value)
                        sub_query = sub_query.filter(Trait.name == key)
                    elif key == 't_string':
                        sub_query = sub_query.filter(Trait.t_string == value)
                    elif key == 't_int':
                        sub_query = sub_query.filter(Trait.t_int == value)
                    elif key == 't_datetime':
                        dt = utils.dt_to_decimal(value)
                        sub_query = sub_query.filter(Trait.t_datetime == dt)
                    elif key == 't_float':
                        sub_query = sub_query.filter(Trait.t_datetime == value)

            sub_query = sub_query.subquery()

            all_data = session.query(Trait)\
                .join(sub_query, Trait.event_id == sub_query.c.id)

            # Now convert the sqlalchemy objects back into Models ...
            event_models_dict = {}
            for trait in all_data.all():
                event = event_models_dict.get(trait.event_id)
                if not event:
                    generated = utils.decimal_to_dt(trait.event.generated)
                    event = api_models.Event(trait.event.message_id,
                                             trait.event.unique_name.key,
                                             generated, [])
                    event_models_dict[trait.event_id] = event
                value = trait.get_value()
                trait_model = api_models.Trait(trait.name.key, trait.t_type,
                                               value)
                event.append_trait(trait_model)

        event_models = event_models_dict.values()
        return sorted(event_models, key=operator.attrgetter('generated'))
Esempio n. 6
0
    def get_events(self, event_filter):
        """Return an iterable of model.Event objects.

        :param event_filter: EventFilter instance
        """

        start = utils.dt_to_decimal(event_filter.start)
        end = utils.dt_to_decimal(event_filter.end)
        session = sqlalchemy_session.get_session()
        with session.begin():
            sub_query = session.query(Event.id)\
                .join(Trait, Trait.event_id == Event.id)\
                .filter(Event.generated >= start, Event.generated <= end)

            if event_filter.event_name:
                event_name = self._get_unique(session, event_filter.event_name)
                sub_query = sub_query.filter(Event.unique_name == event_name)

            if event_filter.traits:
                for key, value in event_filter.traits.iteritems():
                    if key == 'key':
                        key = self._get_unique(session, value)
                        sub_query = sub_query.filter(Trait.name == key)
                    elif key == 't_string':
                        sub_query = sub_query.filter(Trait.t_string == value)
                    elif key == 't_int':
                        sub_query = sub_query.filter(Trait.t_int == value)
                    elif key == 't_datetime':
                        dt = utils.dt_to_decimal(value)
                        sub_query = sub_query.filter(Trait.t_datetime == dt)
                    elif key == 't_float':
                        sub_query = sub_query.filter(Trait.t_datetime == value)

            sub_query = sub_query.subquery()

            all_data = session.query(Trait)\
                .join(sub_query, Trait.event_id == sub_query.c.id)

            # Now convert the sqlalchemy objects back into Models ...
            event_models_dict = {}
            for trait in all_data.all():
                event = event_models_dict.get(trait.event_id)
                if not event:
                    generated = utils.decimal_to_dt(trait.event.generated)
                    event = api_models.Event(trait.event.unique_name.key,
                                             generated, [])
                    event_models_dict[trait.event_id] = event
                value = trait.get_value()
                trait_model = api_models.Trait(trait.name.key, trait.t_type,
                                               value)
                event.append_trait(trait_model)

        event_models = event_models_dict.values()
        return sorted(event_models, key=operator.attrgetter('generated'))
Esempio n. 7
0
 def test_decimal_to_datetime(self):
     expected = 1356093296.12
     dexpected = decimal.Decimal(str(expected))  # Python 2.6 wants str()
     expected_datetime = datetime.datetime.utcfromtimestamp(expected)
     actual_datetime = utils.decimal_to_dt(dexpected)
     self.assertEqual(actual_datetime, expected_datetime)
Esempio n. 8
0
 def process_result_value(value, dialect):
     if value is None:
         return value
     elif dialect.name == 'mysql':
         return utils.decimal_to_dt(value)
     return value
Esempio n. 9
0
 def process_result_value(self, value, dialect):
     if value is None:
         return value
     elif dialect.name == 'mysql':
         return utils.decimal_to_dt(value)
     return value
Esempio n. 10
0
 def test_decimal_to_dt_with_none_parameter(self):
     self.assertIsNone(utils.decimal_to_dt(None))
Esempio n. 11
0
 def test_decimal_to_dt_with_none_parameter(self):
     self.assertEqual(utils.decimal_to_dt(None), None)
Esempio n. 12
0
 def test_decimal_to_datetime(self):
     expected = 1356093296.12
     dexpected = decimal.Decimal(str(expected))  # Python 2.6 wants str()
     expected_datetime = datetime.datetime.utcfromtimestamp(expected)
     actual_datetime = utils.decimal_to_dt(dexpected)
     self.assertEqual(actual_datetime, expected_datetime)
Esempio n. 13
0
 def test_decimal_to_dt_with_none_parameter(self):
     self.assertIsNone(utils.decimal_to_dt(None))
Esempio n. 14
0
 def test_decimal_to_dt_with_none_parameter(self):
     self.assertEqual(utils.decimal_to_dt(None), None)
Esempio n. 15
0
    def get_events(self, event_filter):
        """Return an iterable of model.Event objects. The event model objects
        have their Trait model objects available -- filtered by any traits
        in the event_filter.

        :param event_filter: EventFilter instance
        """

        start = utils.dt_to_decimal(event_filter.start)
        end = utils.dt_to_decimal(event_filter.end)
        session = sqlalchemy_session.get_session()
        with session.begin():
            sub_query = session.query(models.Event.id)\
                .join(models.EventType,
                      models.Event.event_type_id == models.EventType.id)\
                .join(models.Trait,
                      models.Trait.event_id == models.Event.id)\
                .filter(models.Event.generated >= start,
                        models.Event.generated <= end)

            if event_filter.event_type:
                event_type = event_filter.event_type
                sub_query = sub_query\
                    .filter(models.EventType.desc == event_type)

            event_models_dict = {}
            if event_filter.traits:
                for key, value in event_filter.traits.iteritems():
                    if key == 'key':
                        key = self._get_unique(session, value)
                        sub_query = sub_query.filter(models.Trait.name == key)
                    elif key == 't_string':
                        sub_query = sub_query.filter(
                            models.Trait.t_string == value)
                    elif key == 't_int':
                        sub_query = sub_query.filter(
                            models.Trait.t_int == value)
                    elif key == 't_datetime':
                        dt = utils.dt_to_decimal(value)
                        sub_query = sub_query.filter(
                            models.Trait.t_datetime == dt)
                    elif key == 't_float':
                        sub_query = sub_query.filter(
                            models.Trait.t_datetime == value)
            else:
                # Pre-populate event_models_dict to cover Events without traits
                events = session.query(models.Event)\
                    .filter(models.Event.generated >= start)\
                    .filter(models.Event.generated <= end)
                if event_filter.event_type:
                    events = events\
                        .join(models.EventType,
                              models.EventType.id ==
                              models.Event.event_type_id)\
                        .filter(models.EventType.desc ==
                                event_filter.event_type)
                for db_event in events.all():
                    generated = utils.decimal_to_dt(db_event.generated)
                    api_event = api_models.Event(db_event.message_id,
                                                 db_event.event_type.desc,
                                                 generated, [])
                    event_models_dict[db_event.id] = api_event

            sub_query = sub_query.subquery()

            all_data = session.query(models.Trait)\
                .join(sub_query, models.Trait.event_id == sub_query.c.id)

            # Now convert the sqlalchemy objects back into Models ...
            for trait in all_data.all():
                event = event_models_dict.get(trait.event_id)
                if not event:
                    generated = utils.decimal_to_dt(trait.event.generated)
                    event = api_models.Event(trait.event.message_id,
                                             trait.event.event_type.desc,
                                             generated, [])
                    event_models_dict[trait.event_id] = event
                value = trait.get_value()
                trait_model = api_models.Trait(trait.name.key, trait.t_type,
                                               value)
                event.append_trait(trait_model)

        event_models = event_models_dict.values()
        return sorted(event_models, key=operator.attrgetter('generated'))