Esempio n. 1
0
    def update_key(self, session, stat_key, day):
        # determine the value from the day before
        query = (session.query(Stat)
                        .filter((Stat.key == stat_key),
                                (Stat.time < day))
                        .order_by(Stat.time.desc()))
        before = query.first()
        old_value = 0
        if before:
            old_value = before.value

        # get the value from redis for the day in question
        stat_counter = StatCounter(stat_key, day)
        value = stat_counter.get(self.task.redis_client)

        # insert or update a new stat value
        query = (session.query(Stat)
                        .filter((Stat.key == stat_key),
                                (Stat.time == day)))
        stat = query.first()
        if stat is not None:
            stat.value += value
        else:
            stmt = Stat.__table__.insert(
                mysql_on_duplicate='value = value + %s' % value
            ).values(key=stat_key, time=day, value=old_value + value)
            session.execute(stmt)

        # queue the redis value to be decreased
        stat_counter.decr(self.pipe, value)
Esempio n. 2
0
    def update_key(self, session, pipe, stat_key, day):
        # Get value for the given day from Redis.
        stat_counter = StatCounter(stat_key, day)
        value = stat_counter.get(self.task.redis_client)

        # Get value for the given day from the database.
        stat = (session.query(Stat)
                       .filter((Stat.key == stat_key),
                               (Stat.time == day))).first()
        if stat is not None:
            # If the day already has an entry, update it.
            if value:
                stat.value += value
                stat_counter.decr(pipe, value)
        else:
            # Get the most recent value for the stat from the database.
            before = (session.query(Stat)
                             .filter((Stat.key == stat_key),
                                     (Stat.time < day))
                             .order_by(Stat.time.desc())
                             .limit(1)).first()
            old_value = 0
            if before:
                old_value = before.value

            # Insert a new stat value.
            stmt = Stat.__table__.insert(
                mysql_on_duplicate='value = value + %s' % value
            ).values(key=stat_key, time=day, value=old_value + value)
            session.execute(stmt)
            stat_counter.decr(pipe, value)
Esempio n. 3
0
    def update_key(self, session, pipe, stat_key, day):
        # Get value for the given day from Redis.
        stat_counter = StatCounter(stat_key, day)
        value = stat_counter.get(self.task.redis_client)

        # Get value for the given day from the database.
        columns = Stat.__table__.c
        stat = session.execute(
            select([columns.value]).where(columns.key == stat_key).where(
                columns.time == day)).fetchone()

        if stat is not None:
            # If the day already has an entry, update it.
            if value:
                session.execute(Stat.__table__.update().where(
                    columns.key == stat_key).where(columns.time == day).values(
                        value=value + columns.value))
                stat_counter.decr(pipe, value)
        else:
            # Get the most recent value for the stat from the database.
            before = session.execute(
                select([columns.value]).where(columns.key == stat_key).where(
                    columns.time < day).order_by(
                        columns.time.desc()).limit(1)).fetchone()

            old_value = before.value if before else 0

            # Insert a new stat value.
            stmt = Stat.__table__.insert(
                mysql_on_duplicate='value = value + %s' % value).values(
                    key=stat_key, time=day, value=old_value + value)
            session.execute(stmt)
            stat_counter.decr(pipe, value)
Esempio n. 4
0
    def update_key(self, session, pipe, stat_key, day):
        # determine the value from the day before
        query = (session.query(Stat).filter(
            (Stat.key == stat_key),
            (Stat.time < day)).order_by(Stat.time.desc()))
        before = query.first()
        old_value = 0
        if before:
            old_value = before.value

        # get the value from redis for the day in question
        stat_counter = StatCounter(stat_key, day)
        value = stat_counter.get(self.task.redis_client)

        # insert or update a new stat value
        query = (session.query(Stat).filter((Stat.key == stat_key),
                                            (Stat.time == day)))
        stat = query.first()
        if stat is not None:
            stat.value += value
        else:
            stmt = Stat.__table__.insert(
                mysql_on_duplicate='value = value + %s' % value).values(
                    key=stat_key, time=day, value=old_value + value)
            session.execute(stmt)

        # queue the redis value to be decreased
        stat_counter.decr(pipe, value)
Esempio n. 5
0
    def update_key(self, session, pipe, stat_key, day):
        # Get value for the given day from Redis.
        stat_counter = StatCounter(stat_key, day)
        value = stat_counter.get(self.task.redis_client)

        # Get value for the given day from the database.
        columns = Stat.__table__.c
        stat = session.execute(
            select([columns.value])
            .where(columns.key == stat_key)
            .where(columns.time == day)
        ).fetchone()

        if stat is not None:
            # If the day already has an entry, update it.
            if value:
                session.execute(
                    Stat.__table__.update()
                    .where(columns.key == stat_key)
                    .where(columns.time == day)
                    .values(value=value + columns.value)
                )
                stat_counter.decr(pipe, value)
        else:
            # Get the most recent value for the stat from the database.
            before = session.execute(
                select([columns.value])
                .where(columns.key == stat_key)
                .where(columns.time < day)
                .order_by(columns.time.desc())
                .limit(1)
            ).fetchone()

            old_value = before.value if before else 0

            # Insert a new stat value.
            stmt = Stat.__table__.insert(
                mysql_on_duplicate='value = value + %s' % value
            ).values(key=stat_key, time=day, value=old_value + value)
            session.execute(stmt)
            stat_counter.decr(pipe, value)
Esempio n. 6
0
    def update_key(self, stat_key, day):
        # determine the value from the day before
        query = (self.session.query(Stat)
                             .filter(Stat.key == stat_key)
                             .filter(Stat.time < day)
                             .order_by(Stat.time.desc()))
        before = query.first()
        old_value = 0
        if before:
            old_value = before.value

        # get the value from redis for the day in question
        stat_counter = StatCounter(stat_key, day)
        value = stat_counter.get(self.redis_client)

        # insert or update a new stat value
        hashkey = Stat.to_hashkey(key=stat_key, time=day)
        Stat.incr(self.session, hashkey, value, old=old_value)

        # queue the redis value to be decreased
        stat_counter.decr(self.pipe, value)