def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with db.session_scope() as session:
        rs = session.query(db.Fort) \
              .all()

        for g in rs:
            session.query(db.Fort)\
               .filter(db.Fort.id == g.id)\
               .with_for_update() \
               .update({'park' : sa.null(), 'parkid' : sa.null()})
        session.commit()

    op.drop_constraint('forts_fk_parkid', 'forts', type_='foreignkey')
    op.drop_index(op.f('ix_parks_instance'), table_name='parks')
    op.drop_index(op.f('ix_forts_parkid'), table_name='forts')
    op.drop_index(op.f('ix_parks_internalid'), table_name='parks')
    op.alter_column('forts',
                    'parkid',
                    existing_type=sa.Integer(),
                    type_=db.HUGE_TYPE,
                    existing_nullable=True)
    op.drop_table('parks')
    op.create_table(
        'parks',
        sa.Column('id', db.HUGE_TYPE, nullable=True, unique=True),
        sa.Column('name', sa.String(length=200), nullable=True),
        sa.Column('coords', db.LONG_TEXT, nullable=True),
        sa.Column('updated', sa.Integer(), nullable=True),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_index('ix_park', 'parks', ['id'])
    op.create_index('ix_forts_parkid', 'forts', ['parkid'])
    op.create_foreign_key('forts_fk_parkid', 'forts', 'parks', ['parkid'],
                          ['id'])
Exemplo n.º 2
0
def get_pokestop_markers():
    with session_scope() as session:
        pokestops = get_pokestops(session)
        return [{
            'id': pokestop.id,
            'external_id': pokestop.external_id,
            'lat': pokestop.lat,
            'lon': pokestop.lon,
            'name': pokestop.name,
            'url': pokestop.url,
            'updated': pokestop.updated,
            'quest_id': pokestop.quest_id,
            'pokestop_id': pokestop.pokestop_id,
            'quest_type': pokestop.quest_type,
            'quest_type_raw': pokestop.quest_type_raw,
            'item_type': pokestop.item_type,
            'item_amount': pokestop.item_amount,
            'item_id': pokestop.item_id,
            'pokemon_id': pokestop.pokemon_id,
            'quest_reward_type': pokestop.quest_reward_type,
            'quest_reward_type_raw': pokestop.quest_reward_type_raw,
            'quest_target': pokestop.quest_target,
            'quest_task': pokestop.quest_task,
            'quest_condition': pokestop.quest_condition,
            'timestamp': pokestop.timestamp
        } for pokestop in pokestops]
Exemplo n.º 3
0
def get_raid_markers(names=POKEMON, moves=MOVES):
    with session_scope() as session:
        markers = []
        raids = session.query(Raid) \
            .filter(Raid.time_end > time())
        for raid in raids:
            fort = session.query(Fort) \
                .filter(Fort.id == raid.fort_id) \
                .scalar()
            fortsighting = session.query(FortSighting) \
                .filter(FortSighting.fort_id == fort.id) \
                .order_by(FortSighting.last_modified.desc()) \
                .first()
            markers.append({
                'id': 'raid-' + str(raid.id),
                'level': raid.level,
                'team': fortsighting.team,
                'pokemon_id': raid.pokemon_id,
                'pokemon_name': names[raid.pokemon_id],
                'move1': moves[raid.move_1],
                'move2': moves[raid.move_2],
                'lat': fort.lat,
                'lon': fort.lon,
                'time_spawn': raid.time_spawn,
                'time_battle': raid.time_battle,
                'time_end': raid.time_end
                })

        return markers
Exemplo n.º 4
0
def get_weather():
    with session_scope() as session:
        weathers = session.query(Weather)
        markers = []
        for weather in weathers:
            cell = s2sphere.Cell(
                s2sphere.CellId(weather.s2_cell_id).parent(10))
            center = s2sphere.LatLng.from_point(cell.get_center())
            converted_s2_cell_id = s2sphere.CellId.from_lat_lng(
                s2sphere.LatLng.from_degrees(center.lat().degrees,
                                             center.lng().degrees)).parent(10)
            markers.append({
                'id':
                'weather-' + str(weather.id),
                'coords': [(get_vertex(cell, v)) for v in range(0, 4)],
                'center': (center.lat().degrees, center.lng().degrees),
                'condition':
                weather.condition,
                'alert_severity':
                weather.alert_severity,
                'warn':
                weather.warn,
                'day':
                weather.day,
                's2_cell_id':
                converted_s2_cell_id.id()
            })
        return markers
Exemplo n.º 5
0
def report_single(pokemon_id,
                  area_name=conf.AREA_NAME,
                  key=conf.GOOGLE_MAPS_KEY if conf.REPORT_MAPS else None):
    with db.session_scope() as session:
        session_stats = db.get_session_stats(session)
        js_data = {
            'charts_data': {
                'hours': db.get_spawns_per_hour(session, pokemon_id),
            },
            'map_center': center,
            'zoom': 13,
        }
        return render_template(
            'report_single.html',
            current_date=datetime.now(),
            area_name=area_name,
            area_size=area,
            pokemon_id=pokemon_id,
            pokemon_name=POKEMON[pokemon_id],
            total_spawn_count=db.get_total_spawns_count(session, pokemon_id),
            session_start=session_stats['start'],
            session_end=session_stats['end'],
            session_length_hours=int(session_stats['length_hours']),
            google_maps_key=key,
            js_data=js_data,
        )
Exemplo n.º 6
0
def report_main(area_name=conf.AREA_NAME,
                names=POKEMON,
                key=get_google_maps_key() if conf.REPORT_MAPS else None):
    with db.session_scope() as session:
        counts = db.get_sightings_per_pokemon(session)

        count = sum(counts.values())
        counts_tuple = tuple(counts.items())
        nonexistent = [(x, names[x]) for x in range(1, 387) if x not in counts]
        del counts

        top_pokemon = list(counts_tuple[-30:])
        top_pokemon.reverse()
        bottom_pokemon = counts_tuple[:30]
        rare_pokemon = [r for r in counts_tuple if r[0] in conf.RARE_IDS]
        if rare_pokemon:
            rare_sightings = db.get_all_sightings(
                session, [r[0] for r in rare_pokemon]
            )
        else:
            rare_sightings = []
        js_data = {
            'charts_data': {
                'punchcard': db.get_punch_card(session),
                'top30': [(names[r[0]], r[1]) for r in top_pokemon],
                'bottom30': [
                    (names[r[0]], r[1]) for r in bottom_pokemon
                ],
                'rare': [
                    (names[r[0]], r[1]) for r in rare_pokemon
                ],
            },
            'maps_data': {
                'rare': [sighting_to_report_marker(s) for s in rare_sightings],
            },
            'map_center': center,
            'zoom': 13,
        }
    icons = {
        'top30': [(r[0], names[r[0]]) for r in top_pokemon],
        'bottom30': [(r[0], names[r[0]]) for r in bottom_pokemon],
        'rare': [(r[0], names[r[0]]) for r in rare_pokemon],
        'nonexistent': nonexistent
    }
    session_stats = db.get_session_stats(session)
    return render_template(
        'report.html',
        current_date=datetime.now(),
        area_name=area_name,
        area_size=area,
        total_spawn_count=count,
        spawns_per_hour=count // session_stats['length_hours'],
        session_start=session_stats['start'],
        session_end=session_stats['end'],
        session_length_hours=session_stats['length_hours'],
        js_data=js_data,
        icons=icons,
        google_maps_key=key,
    )
Exemplo n.º 7
0
def get_pokemarkers(after_id=0):
    with session_scope() as session:
        pokemons = session.query(Sighting) \
            .filter(Sighting.expire_timestamp > time(),
                    Sighting.id > after_id)
        if conf.MAP_FILTER_IDS:
            pokemons = pokemons.filter(~Sighting.pokemon_id.in_(conf.MAP_FILTER_IDS))
        return tuple(map(sighting_to_marker, pokemons))
Exemplo n.º 8
0
def get_pokestop_markers():
    with session_scope() as session:
        pokestops = session.query(Pokestop)
        return [{
            'external_id': pokestop.external_id,
            'lat': pokestop.lat,
            'lon': pokestop.lon
        } for pokestop in pokestops]
Exemplo n.º 9
0
def get_pokestop_markers():
    with session_scope() as session:
        pokestops = session.query(Pokestop)
        return [{
            'external_id': pokestop.external_id,
            'lat': pokestop.lat,
            'lon': pokestop.lon
        } for pokestop in pokestops]
Exemplo n.º 10
0
def get_pokemarkers(after_id=0):
    with session_scope() as session:
        pokemons = session.query(Sighting) \
            .filter(Sighting.expire_timestamp > time(),
                    Sighting.id > after_id)
        if conf.MAP_FILTER_IDS:
            pokemons = pokemons.filter(~Sighting.pokemon_id.in_(conf.MAP_FILTER_IDS))
        return tuple(map(sighting_to_marker, pokemons))
Exemplo n.º 11
0
def get_pokestop_markers():
    with session_scope() as session:
        pokestops = session.query(Pokestop)
        return [{
            'name': pokestop.name,
            'url': pokestop.url,
            'lat': pokestop.lat,
            'lon': pokestop.lon
        } for pokestop in pokestops]
Exemplo n.º 12
0
def get_quest_filter_options():
    with session_scope() as session:
        quests = get_grouped_quest_task(session)
        return [{
            'quest_task': quest.quest_task,
            'quest_reward_type_raw': quest.quest_reward_type_raw,
            'pokemon_id': quest.pokemon_id,
            'item_id': quest.item_id,
            'count': quest.count
        } for quest in quests]
Exemplo n.º 13
0
def get_spawnpoint_markers():
    with session_scope() as session:
        spawns = session.query(Spawnpoint)
        return [{
            'spawn_id': spawn.spawn_id,
            'despawn_time': spawn.despawn_time,
            'lat': spawn.lat,
            'lon': spawn.lon,
            'duration': spawn.duration
        } for spawn in spawns]
Exemplo n.º 14
0
def get_spawnpoint_markers():
    with session_scope() as session:
        spawns = session.query(Spawnpoint)
        return [{
            'spawn_id': spawn.spawn_id,
            'despawn_time': spawn.despawn_time,
            'lat': spawn.lat,
            'lon': spawn.lon,
            'duration': spawn.duration
        } for spawn in spawns]
Exemplo n.º 15
0
def get_gym_markers(names=POKEMON):
    with session_scope() as session:
        forts = get_forts(session)
    return [{
            'id': 'fort-' + str(fort['fort_id']),
            'sighting_id': fort['id'],
            'prestige': fort['prestige'],
            'pokemon_id': fort['guard_pokemon_id'],
            'pokemon_name': names[fort['guard_pokemon_id']],
            'team': fort['team'],
            'lat': fort['lat'],
            'lon': fort['lon']
    } for fort in forts]
Exemplo n.º 16
0
def get_gym_markers(names=POKEMON):
    with session_scope() as session:
        forts = get_forts(session)
    return [{
        'id': 'fort-' + str(fort['fort_id']),
        'sighting_id': fort['id'],
        'prestige': fort['prestige'],
        'pokemon_id': fort['guard_pokemon_id'],
        'pokemon_name': names[fort['guard_pokemon_id']],
        'team': fort['team'],
        'lat': fort['lat'],
        'lon': fort['lon']
    } for fort in forts]
Exemplo n.º 17
0
def get_gym_markers(names=POKEMON):
    with session_scope() as session:
        forts = get_forts(session)
    return [{
        'id': 'fort-' + str(fort['fort_id']),
        'sighting_id': fort['id'],
        'pokemon_id': fort['guard_pokemon_id'],
        'pokemon_name': names[fort['guard_pokemon_id']],
        'team': fort['team'],
        'in_battle': fort['in_battle'],
        'slots_available': fort['slots_available'],
        'time_ocuppied': fort['time_ocuppied'],
        'lat': fort['lat'],
        'lon': fort['lon']
    } for fort in forts]
Exemplo n.º 18
0
def get_darkstop_markers():
    with session_scope() as session:
        darkstops = get_darkstops(session)
        return [{
            'id': darkstop.id,
            'external_id': darkstop.external_id,
            'lat': darkstop.lat,
            'lon': darkstop.lon,
            'name': darkstop.name,
            'url': darkstop.url,
            'updated': darkstop.updated,
            'incident_start': darkstop.incident_start,
            'incident_expiration': darkstop.incident_expiration,
            'incident_grunt_type': darkstop.incident_grunt_type,
            'last_modified': darkstop.last_modified,
            'last_updated': darkstop.last_updated,
            'lure_expiration': darkstop.lure_expiration,
            'lure_start': darkstop.lure_start
        } for darkstop in darkstops]
Exemplo n.º 19
0
def get_gym_markers(names=POKEMON):
    with session_scope() as session:
        forts = get_forts(session)
    return [{
        'id': 'fort-' + str(fort['fort_id']),
        'fort_id': fort['fort_id'],
        'sighting_id': fort['id'],
        'gym_name': fort['name'],
        'image_url': fort['url'],
        'external_id': fort['external_id'],
        'pokemon_id': fort['guard_pokemon_id'],
        'pokemon_name': names[fort['guard_pokemon_id']],
        'slots_available': fort['slots_available'],
        'time_occupied': fort['time_occupied'],
        'last_modified': fort['last_modified'],
        'team': fort['team'],
        'lat': fort['lat'],
        'lon': fort['lon'],
        'sponsor': fort['sponsor']
    } for fort in forts]
Exemplo n.º 20
0
def get_pokestop_markers():
    with session_scope() as session:
        pokestops = session.query(Pokestop)
        return [{
            'external_id':
            pokestop.external_id,
            'lat':
            pokestop.lat,
            'lon':
            pokestop.lon,
            'name':
            pokestop.name,
            'url':
            pokestop.url,
            'desc':
            pokestop.desc,
            'lure_expiration':
            pokestop.lure_start +
            conf.LURE_DURATION if pokestop.lure_start else 0,
        } for pokestop in pokestops]
Exemplo n.º 21
0
def get_raid_markers(names=POKEMON):
    with session_scope() as session:
        markers = []
        raids = session.query(Raid) \
            .filter(Raid.time_end > time())
        for raid in raids:
            fort_id = raid.fort_id
            fort = session.query(Fort) \
                .filter(Fort.id == fort_id) \
                .scalar()
            markers.append({
                'id': 'raid-' + str(raid.id),
                'level': raid.level,
                'pokemon_id': raid.pokemon_id,
                'pokemon_name': names[raid.pokemon_id],
                'lat': fort.lat,
                'lon': fort.lon,
                'time_spawn': raid.time_spawn,
                'time_battle': raid.time_battle,
                'time_end': raid.time_end
            })

        return markers
Exemplo n.º 22
0
def get_raid_markers(names=POKEMON, moves=MOVES):
    with session_scope() as session:
        markers = []
        raids = session.query(Raid) \
            .filter(Raid.time_end > time())
        for raid in raids:
            fort = session.query(Fort) \
                .filter(Fort.id == raid.fort_id) \
                .scalar()
            fortsighting = session.query(FortSighting) \
                .filter(FortSighting.fort_id == fort.id) \
                .order_by(FortSighting.last_modified.desc()) \
                .first()
            markers.append({
                'id': 'raid-' + str(raid.id),
                'raid_id': raid.id,
                'fort_id': fortsighting.fort_id,
                'gym_name': fort.name,
                'image_url': fort.url,
                'external_id': fort.external_id,
                'gym_team': fortsighting.team,
                'raid_battle': raid.time_battle,
                'raid_spawn': raid.time_spawn,
                'raid_end': raid.time_end,
                'raid_level': raid.level,
                'raid_pokemon_id': raid.pokemon_id,
                'raid_pokemon_name': names[raid.pokemon_id],
                'raid_pokemon_cp': raid.cp,
                'raid_pokemon_form': raid.form,
                'raid_evolution': raid.evolution,
                'raid_pokemon_move_1': moves[raid.move_1],
                'raid_pokemon_move_2': moves[raid.move_2],
                'lat': fort.lat,
                'lon': fort.lon
                })

        return markers
Exemplo n.º 23
0
def get_stats():
    cache_valid = (
        CACHE['data'] and
        CACHE['generated_at'] > datetime.now() - timedelta(minutes=15)
    )
    if cache_valid:
        return CACHE['data']
    with db.session_scope() as session:
        forts = db.get_forts(session)
    count = {t.value: 0 for t in db.Team}
    strongest = {t.value: None for t in db.Team}
    guardians = {t.value: {} for t in db.Team}
    top_guardians = {t.value: None for t in db.Team}
    prestige = {t.value: 0 for t in db.Team}
    percentages = {}
    prestige_percent = {}
    total_prestige = 0
    last_date = 0
    pokemon_names = POKEMON
    for fort in forts:
        if fort['last_modified'] > last_date:
            last_date = fort['last_modified']
        team = fort['team']
        count[team] += 1
        if team != 0:
            # Strongest gym
            existing = strongest[team]
            should_replace = (
                existing is not None and
                fort['prestige'] > existing[0] or
                existing is None
            )
            pokemon_id = fort['guard_pokemon_id']
            if should_replace:
                strongest[team] = (
                    fort['prestige'],
                    pokemon_id,
                    pokemon_names[pokemon_id],
                )
            # Guardians
            guardian_value = guardians[team].get(pokemon_id, 0)
            guardians[team][pokemon_id] = guardian_value + 1
            # Prestige
            prestige[team] += fort['prestige']
    total_prestige = sum(prestige.values())
    for team in db.Team:
        percentages[team.value] = (
            count.get(team.value) / len(forts) * 100
        )
        prestige_percent[team.value] = (
            prestige.get(team.value) / total_prestige * 100
        )
        if guardians[team.value]:
            pokemon_id = sorted(
                guardians[team.value],
                key=guardians[team.value].__getitem__,
                reverse=True
            )[0]
            top_guardians[team.value] = pokemon_names[pokemon_id]
    CACHE['generated_at'] = datetime.now()
    CACHE['data'] = {
        'order': sorted(count, key=count.__getitem__, reverse=True),
        'count': count,
        'total_count': len(forts),
        'strongest': strongest,
        'prestige': prestige,
        'prestige_percent': prestige_percent,
        'percentages': percentages,
        'last_date': last_date,
        'top_guardians': top_guardians,
        'generated_at': CACHE['generated_at'],
    }
    return CACHE['data']
Exemplo n.º 24
0
def get_stats():
    cache_valid = (
        CACHE['data']
        and CACHE['generated_at'] > datetime.now() - timedelta(minutes=15))
    if cache_valid:
        return CACHE['data']
    with db.session_scope() as session:
        forts = db.get_forts(session)
    count = {t.value: 0 for t in db.Team}
    strongest = {t.value: None for t in db.Team}
    guardians = {t.value: {} for t in db.Team}
    top_guardians = {t.value: None for t in db.Team}
    prestige = {t.value: 0 for t in db.Team}
    percentages = {}
    prestige_percent = {}
    total_prestige = 0
    last_date = 0
    pokemon_names = POKEMON
    for fort in forts:
        if fort['last_modified'] > last_date:
            last_date = fort['last_modified']
        team = fort['team']
        count[team] += 1
        if team != 0:
            # Strongest gym
            existing = strongest[team]
            should_replace = (existing is not None
                              and fort['prestige'] > existing[0]
                              or existing is None)
            pokemon_id = fort['guard_pokemon_id']
            if should_replace:
                strongest[team] = (
                    fort['prestige'],
                    pokemon_id,
                    pokemon_names[pokemon_id],
                )
            # Guardians
            guardian_value = guardians[team].get(pokemon_id, 0)
            guardians[team][pokemon_id] = guardian_value + 1
            # Prestige
            prestige[team] += fort['prestige']
    total_prestige = sum(prestige.values())
    for team in db.Team:
        percentages[team.value] = (count.get(team.value) / len(forts) * 100)
        prestige_percent[team.value] = (prestige.get(team.value) /
                                        total_prestige * 100)
        if guardians[team.value]:
            pokemon_id = sorted(guardians[team.value],
                                key=guardians[team.value].__getitem__,
                                reverse=True)[0]
            top_guardians[team.value] = pokemon_names[pokemon_id]
    CACHE['generated_at'] = datetime.now()
    CACHE['data'] = {
        'order': sorted(count, key=count.__getitem__, reverse=True),
        'count': count,
        'total_count': len(forts),
        'strongest': strongest,
        'prestige': prestige,
        'prestige_percent': prestige_percent,
        'percentages': percentages,
        'last_date': last_date,
        'top_guardians': top_guardians,
        'generated_at': CACHE['generated_at'],
    }
    return CACHE['data']
Exemplo n.º 25
0
def get_raids_markers(names=POKEMON, moves=MOVES):
    with session_scope() as session:
        raids = get_raids_info(session)
    return tuple(map(raid_to_marker, raids))
Exemplo n.º 26
0
def report_heatmap():
    pokemon_id = request.args.get('id')
    with db.session_scope() as session:
        return dumps(db.get_all_spawn_coords(session, pokemon_id=pokemon_id))
Exemplo n.º 27
0
def get_spawns_at_point():
    with db.session_scope() as session:
        spawns = db.get_sightings_per_spawn(session)
    return dict(spawns)