API Data point sorting not consistent?

I’m trying to use the API to find the most recently added data point on a goal, and it looks like the data points are not sorted the same depending on whether the points in question were added via the API or via the web UI. This seems wrong to me and I am wondering if anyone else can reproduce this?

2 Likes

The most direct way to get the most recent datapoint is hitting the goals endpoint with include_datapoints=true and skinny=true. The response will include last_datapoint in the json goal object. In this case the datapoints are sorted by the api_updated_at field, which should be getting updated whether you add via api or website, but there could be a bug there.

If you include_datapoints && datapoints_count for the goals endpoint, we also use api_updated_at for sorting the datapoints.

If you include diff_since && include_datapoints we filter on api_updated_at > diff_since, but we don’t sort the datapoints before returning them.

It might happen that if you’re sorting by the “timestamp” field, and have two different datapoints entered on the same day, one via the api, and one in the add-data box, that the sort order could differ from the entry order – but that is when sorting by timestamp. I’ve checked the code in the api goals endpoint and datapoints endpoints and we’re sorting by updated_at and by id respectively. The reason that the timestamp order might differ from entry order is because when you give us an exact timestamp (as in the api, or using a ^ for date), we use that for the timestamp and calculate the daystamp based on it. But if you give us a date, (as in the data-entry box, or sending a datapoint like 4 1 "birthday cake") then we use the date given for the daystamp, but we assign an arbitrary timestamp for the timestamp field.

3 Likes

I am assuming you mean updated_at? The API docs mention updated_at but not api_updated_at.

Ah, this is the problem I think. I think this isn’t clear from the API doc. And it seems like it sometimes works so I thought my code was correct after testing it.

Since I am already pulling down everything with diff_since it feels silly to hit the API again just to get last_datapoint. So I’ll try sorting on updated_at and see if that works. :slight_smile:

3 Likes