pokeapi icon indicating copy to clipboard operation
pokeapi copied to clipboard

Mechanism to find any changes made to the API data

Open vaikan opened this issue 10 years ago • 28 comments

Since timestamp is not used in the current V2 API, I would like to ask if there is any other method to detect changes to the API data. I have been storing the data in the browser using LocalStorage and would love to update the data when any changes occurs in the API.

vaikan avatar Jun 03 '16 13:06 vaikan

@vaikan I believe timestamps are just missing and need to be added.

phalt avatar Jun 03 '16 13:06 phalt

See the discussion in #140

sargunv avatar Jun 03 '16 13:06 sargunv

@phalt Is this something that's going to be added? I'm caching my results, but I check for updates every few days by redownloading and checking for differences (and performing waaaay more API calls than I need to be).

snotyak avatar Jun 20 '16 14:06 snotyak

The problem is that each resource is generated from a bunch of different things. Maybe just a global timestamp reporting when the database was last changed is sufficient?

sargunv avatar Jun 20 '16 14:06 sargunv

@sargunster That would be much better than nothing.

snotyak avatar Jun 20 '16 14:06 snotyak

@sargunster adding a global timestamp should do the trick.

vaikan avatar Jun 20 '16 15:06 vaikan

@phalt @zaneadix Thoughts on a global timestamp? Should be easy to implement, just set it when the DB build script runs. IMO it's not perfect but can get the job done for the common use cases.

sargunv avatar Jun 20 '16 15:06 sargunv

I believe this fix would do what we want: https://github.com/PokeAPI/pokeapi/issues/140

The updated_time would be the time the db is updated.

phalt avatar Jun 21 '16 08:06 phalt

Maybe this is beyond the scope of this particular issue, but it would be very useful to know which resources should be updated between timestamps. For example, I wouldn't want to reload all the pokemon-species if only contest-effects changed.

zberk avatar Jun 23 '16 03:06 zberk

@zberk can you explain more what you're looking for?

Every resource would have an individual timestamp.

phalt avatar Jun 23 '16 09:06 phalt

@phalt I think he's saying to have that global timestamp, but in addition to that have a list of resource types that have been changed with that "update". It sounds like a great idea at first, but makes less sense because once that timestamp is changed, the previously changed resources will be overridden by the new ones, so if you're more than 1 update behind, you won't get everything. The way to make that work would be to maybe have a changelog object in the db that has a date modified, a list of resources that have been changed, and some insignificant version number. I think that sounds like a better solution than the global timestamp and is likely a lot easier to implement than adding timestamps to every resource.

snotyak avatar Jun 23 '16 11:06 snotyak

Given the data set rarely sees significant changes, I think this is a bit over kill for the effort of creating it in order to provide this functionality. We make 3-4 bug fixes to data a year, as well.

phalt avatar Jun 23 '16 13:06 phalt

For me, the primary use case for timestamps is to check if I need to redownload the data. If the timestamp is attached to the object, then I have to make a request for each object anyway to check timestamps.

I think a single global timestamp would work. Alternatively, there could be a single timestamp per endpoint, but I'm not quite sure how to implement that since each endpoint pulls data from multiple tables.

They way I see this working:

  • When either the code or the database changes, the timestamp is saved somehow
  • The user can request this timestamp via something like /api/v2/updated and redownload all data when this is updated

This lacks details on which resources were updated, but I think it's sufficient since updates are infrequent anyway.

sargunv avatar Jun 23 '16 13:06 sargunv

Yup, but you shouldn't be redownloading all the data at once - what percentage of that data is actually used by a given user? If I cache the entire API once but only 1% is used, then it's inefficient to store it.

If we timestamp individual resources and compare the timestamp when I look it up again and it's older than X many days, I can make a request to check and see if it's new, if it is, store the new data.

phalt avatar Jun 23 '16 15:06 phalt

Since the date set rarely sees changes, I think a single global timestamp would suffice. However, don't you think there will be many frequent changes once S/M come out?

If you did go the per-resource route, it would be nice to have a separate endpoint /api/v2/updated?limit=x&lastUpdated=someDateTime that works just like any other resource list and would return a list of resources that have been modified/created after the provided time.

snotyak avatar Jun 27 '16 17:06 snotyak

Could we not just include lastUpdated in the resource lists? /api/v2/pokemon/ would return:

{ "count": 811, "previous": null, "results": [ { "url": "http://pokeapi.co/api/v2/pokemon/1/", "name": "bulbasaur", "updated": 1467060675 }, { "url": "http://pokeapi.co/api/v2/pokemon/2/", "name": "ivysaur", "updated": 1467060675 }, { "url": "http://pokeapi.co/api/v2/pokemon/3/", "name": "venusaur", "updated": 1467060675 } ...

sdcinglis avatar Jun 27 '16 20:06 sdcinglis

What's the best way to implement this in the code? Does Django provide something to get the last updated time of a table? Or will we have to maintain the updated time ourselves?

sargunv avatar Jun 27 '16 20:06 sargunv

I don't know about django but if the database is sql based there's usually the ability to add a column that defaults to the current timestamp on insert/update.

sdcinglis avatar Jun 27 '16 21:06 sdcinglis

Ah, you're right, I forgot about that. The database uses PostgreSQL. Just have to edit the build script to add that column in each table.

On Mon, Jun 27, 2016, 5:27 PM sdcinglis [email protected] wrote:

I don't know about django but if the database is sql based there's usually the ability to add a column that is the current timestamp on insert/update.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/PokeAPI/pokeapi/issues/205#issuecomment-228881017, or mute the thread https://github.com/notifications/unsubscribe/ABQlpVjxafPAxFAq3-Oq3SpxOXzW6Y_7ks5qQEBegaJpZM4Itgki .

sargunv avatar Jun 27 '16 21:06 sargunv

How is new data usually applied to the DB? Do you loop through all of the data from the veekun csvs and do an INSERT INTO ... ON DUPLICATE KEY UPDATE query? If so that may need to be reworked to only update the rows when something has definitely changed otherwise the update field may be changed erroneously.

sdcinglis avatar Jun 27 '16 21:06 sdcinglis