Skip to content

Addition of live stats api endpoint #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Petap0w
Copy link

@Petap0w Petap0w commented Mar 28, 2025

Adds an api endpoint to get count of active pokemon, active with IV, active 100iv and active shiny.

Call with /api/pokemon/livestats and response like {"pokemon_active":18853,"pokemon_active_iv":13077,"pokemon_active_100iv":5,"pokemon_active_shiny":56}

@jfberry
Copy link
Collaborator

jfberry commented Mar 28, 2025

I like the idea of this, but it’s kind of expensive because it is going to count every single pokemon in a loop when you call it.

another (almost free) approach would be to keep a running total, which could do this per area (since that is already calculated for every new Pokemon). I guess double counting might be a challenge on Pokemon re-encounter though

@Petap0w
Copy link
Author

Petap0w commented Mar 28, 2025

At the moment the only way to get this info is to use db write of Pokémon table which is way more expensive than calling this endpoint even with hundreds of thousands of Pokémon.
I’ll have a look at what you suggest but my zero knowledge of Golang is slowing me down a bit 🙃

@jfberry
Copy link
Collaborator

jfberry commented Mar 28, 2025

Agreed (or call the scan with a world range and some filters which is equivalent to this but even more expensive)

@jfberry
Copy link
Collaborator

jfberry commented Mar 28, 2025

If the live count is straight forward (we do know it’s a re encounter because of other stats data) and removing a pokemon gets a callback also… so it might be ok… then it could be worth it

@Petap0w
Copy link
Author

Petap0w commented Mar 28, 2025

Just for context, with 2 million active pokemon that takes 250ms:
apiLiveStats - 2133307 pokemon_active, 1594005 pokemon_active_iv, 450 pokemon_active_100iv, 6062 pokemon_active_shiny, total time 254.594317ms

@jfberry
Copy link
Collaborator

jfberry commented Mar 28, 2025

During which time there will be contention to all other activities

@jfberry
Copy link
Collaborator

jfberry commented Mar 28, 2025

Ah, actually the problem is that you are using the wrong cache.
PokemonLookupCache is intended for fast lookup for scanning api; the cache of all pokemon is pokemonCache -- this is the one that contains the full record. You would not needed to have added expire timestamp or shiny if you were iterating pokemonCache

@Petap0w
Copy link
Author

Petap0w commented Mar 30, 2025

Follow up and testing on cache entries I added some debug log lines to get PokemonCache and PokemonLookupCache counts for total, no timer and expired Pokémon and I confirm expired are not cleaned properly (25% of my cache is expired…)
Expiration is tested with int64(valueOrMinus1(pokemon.ExpireTimestamp)) < now && int64(valueOrMinus1(pokemon.ExpireTimestamp)) > -1

apiLiveStats - PokemonCache : 4134016 pokemon_cached, 204 pokemon_no_timer, 1171489 pokemon_expired, 2960306 pokemon_active, 2238920 pokemon_active_iv, 732 pokemon_active_100iv, 8455 pokemon_active_shiny, total time 6.100892071s
apiLiveStats - PokemonLookupCache : 4153473 pokemon_lookup_cached, 0 pokemon_lookup_no_timer, 1166184 pokemon_lookup_expired, 2985274 pokemon_lookup_active, 2257956 pokemon_lookup_active_iv, 736 pokemon_lookup_active_100iv, total time 6.100938236s

@jfberry
Copy link
Collaborator

jfberry commented Mar 30, 2025

When did you run this? The expiry looks like it will run the first time after one hour of operation and then continuously afterwards — can you repeat after a while? There may be a way of kickstarting the first run (the library is built to wait an hour when there are no entries to expire it seems)

@Petap0w
Copy link
Author

Petap0w commented Mar 30, 2025

Ran after 8 hours roughly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants