示例#1
0
def get_or_create_counter_id(name, counter_type, db_session=None):
    if not db_session:
        db_session = g.db
    name = name.lower()
    counter = get_counter(name)
    if counter:
        return counter["counter_id"]

    # we fall through here if the counter does not exist

    # Note: counter type is only inserted for the first entry and then not updated again
    row = db_session.query(Counter).filter(Counter.name == name).first()
    if not row:
        db_session.commit()
        log.error("Creating new counter called %s", name)
        try:
            row = Counter(name=name, counter_type=counter_type)
            db_session.add(row)
            db_session.commit()
        except IntegrityError as e:
            # if someone has inserted the counter in the meantime, retrieve it
            if "duplicate key" in repr(e):
                db_session.rollback()
                row = db_session.query(Counter).filter(
                    Counter.name == name).first()
            else:
                raise

        clear_counter_cache()
    counter_id = row.counter_id

    return counter_id
示例#2
0
    def get(self, player_id):
        counter_entries = g.db.query(CounterEntry) \
                              .filter(CounterEntry.player_id == player_id,
                                      CounterEntry.period == "total")
        ret = {}
        for row in counter_entries:
            counter = get_counter(row.counter_id)
            ret[counter["name"]] = row.value

        return ret
示例#3
0
    def get(self, player_id):
        """
        """
        # TODO: Playercheck
        if not get_player(player_id):
            abort(404, message="Player Not found")

        rows = g.db.query(PlayerCounter).filter(
            PlayerCounter.player_id == player_id)
        ret = []
        for row in rows:
            counter_id = row.counter_id
            counter = get_counter(counter_id)
            entry = {
                "counter_id":
                counter_id,
                "first_update":
                row.create_date,
                "last_update":
                row.modify_date,
                "num_updates":
                row.num_updates,
                "url":
                url_for("playercounters.lookup",
                        player_id=player_id,
                        counter_id=counter_id,
                        _external=True),
                "name":
                counter["name"],
                "periods": {}
            }
            for period in COUNTER_PERIODS + ["all"]:
                entry["periods"][period] = url_for("playercounters.period",
                                                   player_id=player_id,
                                                   counter_id=counter_id,
                                                   period=period,
                                                   _external=True)
            total = g.db.query(CounterEntry.value).filter(
                CounterEntry.player_id == player_id,
                CounterEntry.counter_id == counter_id,
                CounterEntry.period == "total").first()
            if total:
                entry["total"] = total.value
            else:
                entry["total"] = 0
            ret.append(entry)

        return ret
示例#4
0
    def patch(self, player_id, counter_id, context_id):
        """
        Update a single existing counter
        """
        args = request.json
        value = args["value"]
        timestamp = parser.parse(args["timestamp"])

        ok = check_and_update_player_counter(player_id, counter_id, timestamp)
        if not ok:
            return "Count has been applied before at this timestamp"

        counter = get_counter(counter_id)
        is_absolute = (counter["counter_type"] == "absolute")
        add_count(counter_id,
                  player_id,
                  timestamp,
                  value,
                  is_absolute=is_absolute,
                  context_id=context_id)
        return "OK"
示例#5
0
    def get(self, player_id, counter_id, period):
        counter = get_counter(counter_id)
        if not counter:
            abort(404)
        if period == "all":
            counter_entries = g.db.query(CounterEntry) \
                                  .filter(CounterEntry.player_id == player_id,
                                          CounterEntry.counter_id == counter_id) \
                                  .order_by(CounterEntry.period, CounterEntry.id)
            ret = collections.defaultdict(dict)
            for row in counter_entries:
                ret[row.period][row.date_time.isoformat() + "Z"] = row.value

        else:
            counter_entries = g.db.query(CounterEntry) \
                                  .filter(CounterEntry.player_id == player_id,
                                          CounterEntry.counter_id == counter_id,
                                          CounterEntry.period == period)
            ret = {}
            for row in counter_entries:
                ret[row.date_time.isoformat() + "Z"] = row.value
        return ret
示例#6
0
    def get(self, player_id, counter_id):
        counter = get_counter(counter_id)
        if not counter:
            abort(404)
        player_counter = g.db.query(PlayerCounter).filter(PlayerCounter.player_id == player_id,
                                                          PlayerCounter.counter_id == counter_id) \
                                                  .first()
        if not player_counter:
            abort(404)

        ret = {
            "counter": counter,
            "player_counter": player_counter.as_dict(),
            "periods": {}
        }
        for period in COUNTER_PERIODS + ["all"]:
            ret["periods"][period] = url_for("playercounters.period",
                                             player_id=player_id,
                                             counter_id=counter_id,
                                             period=period,
                                             _external=True)

        return ret
示例#7
0
    def get(self, counter_id):
        start_time = time.time()
        args = self.get_args.parse_args()
        num = args.get("num") or NUM_RESULTS
        counter = get_counter(counter_id)
        if not counter:
            abort(404)

        filter_player_ids = []
        reverse = not not args.reverse
        if args.player_id:
            filter_player_ids = args.player_id

        query = g.db.query(CounterEntry, CorePlayer)
        query = query.filter(
            CounterEntry.counter_id == counter_id,
            CounterEntry.period == "total",
            CounterEntry.player_id == CorePlayer.player_id,
            CorePlayer.status == "active",
            CorePlayer.player_name != u"",
        )

        if filter_player_ids:
            query = query.filter(CounterEntry.player_id.in_(filter_player_ids))

        if args.player_group:
            filter_player_ids = get_playergroup_ids(args.player_group)
            query = query.filter(CounterEntry.player_id.in_(filter_player_ids))

        if reverse:
            query = query.order_by(CounterEntry.value)
        else:
            query = query.order_by(-CounterEntry.value)
        query = query.limit(num)

        rows = query.all()

        counter_totals = collections.defaultdict(list)
        counter_names = {}
        if args.include:
            all_counters = get_all_counters()
            # inline other counters for the players
            player_ids = [r[0].player_id for r in rows]
            counter_rows = g.db.query(CounterEntry.player_id,
                                      CounterEntry.counter_id,
                                      CounterEntry.value) \
                               .filter(CounterEntry.period == "total",
                                       CounterEntry.player_id.in_(player_ids),
                                       CounterEntry.counter_id.in_(args.include)) \
                               .all()
            for r in counter_rows:
                this_player_id = r[0]
                this_counter_id = r[1]
                this_value = r[2]
                # find the name of this counter. We cache this locally for performance
                try:
                    counter_name = counter_names[this_counter_id]
                except:
                    c = all_counters.get(unicode(this_counter_id), {})
                    name = c.get("name", this_counter_id)
                    counter_names[this_counter_id] = name
                    counter_name = name

                entry = {
                    "name":
                    counter_name,
                    "counter_id":
                    this_counter_id,
                    "counter_url":
                    url_for("playercounters.lookup",
                            player_id=this_player_id,
                            counter_id=this_counter_id,
                            _external=True),
                    "total":
                    this_value
                }
                counter_totals[r.player_id].append(entry)

        ret = []
        for i, row in enumerate(rows):
            player_id = row[0].player_id
            entry = {
                "name":
                counter["name"],
                "counter_id":
                counter_id,
                "player_id":
                player_id,
                "player_name":
                row[1].player_name,
                "player_url":
                url_for("players.player", player_id=player_id, _external=True),
                "counter_url":
                url_for("playercounters.lookup",
                        player_id=player_id,
                        counter_id=row[0].counter_id,
                        _external=True),
                "total":
                row[0].value,
                "position":
                i + 1,
                "include":
                counter_totals.get(player_id, {})
            }
            ret.append(entry)

        resp = api.make_response(ret, httplib.OK)
        resp.cache_control.max_age = 60
        log.info("Returning counters in %.2fsec", time.time() - start_time)

        return resp