Read X post metrics from the timeline that already returned them - #300
Closed
paulocastellano wants to merge 1 commit into
Closed
Read X post metrics from the timeline that already returned them#300paulocastellano wants to merge 1 commit into
paulocastellano wants to merge 1 commit into
Conversation
Analytics fetched the account's timeline for post ids, then turned around and looked the same ids up again through GET /2/tweets purely to read the public_metrics the first request could have returned. The timeline call asked for start_time, end_time and max_results — never tweet.fields. Both endpoints bill per Post returned, so the second pass claimed the same resources a second time, took a second round-trip, and spent a second slice of the same rate limit. For an account with 250 posts in range that is 6 requests where 3 will do. The saving is in round-trips and rate limit rather than dollars: X deduplicates a resource within a 24-hour UTC window, so the second read of an id already read that day is not charged again. But the docs call that a soft guarantee that "may result in resources not being deduplicated" — this stops leaning on it for 250 resources per analytics load. Behaviour is unchanged: same totals, same 5-page ceiling, same empty result when the account posted nothing in range. The page cap is now a named constant, since it bounds what one load can cost as much as how long it takes. Adds the first tests for XAnalytics::getMetrics, covering the totals, the pagination, and that the metrics arrive on the timeline request.
Contributor
Author
|
Folded into #299 — this belonged in the same PR, not a separate one. The commit is now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #299, independent of it — branched from
mainand mergeable on its own.The problem
XAnalytics::getMetricsfetched the account's timeline for post ids, then looked the same ids up again throughGET /2/tweets, purely to read thepublic_metricsthe first request could have returned.The timeline call asked for
start_time,end_timeandmax_results— nevertweet.fields:...and then, a few lines later:
Both endpoints bill per Post returned. The second pass claimed the same resources again, took a second round-trip, and spent a second slice of the same rate limit.
Measured
One analytics load for an account with 250 posts in range:
mainWhat this actually saves — and what it doesn't
Not necessarily dollars. X deduplicates a resource within a 24-hour UTC window, so the second read of an id already read that day isn't charged again. In the common case the redundant pass was probably free.
What it does buy:
Worth stating plainly rather than claiming a saving that dedup was likely already absorbing.
Behaviour
Unchanged. Same totals, same 5-page ceiling, same empty result when the account posted nothing in the range. The page cap is now a named constant (
MAX_TIMELINE_PAGES) — it bounds what a single load can cost as much as how long it takes, which the bare5didn't say.The two helpers collapse into one paginated pass that sums metrics as pages arrive. The accumulator also drops the
foreach (... as &$total)reference in favour of indexing by key.Tests
First tests for
XAnalytics::getMetrics— there were none:public_metricsFull suite: 3750 passed, 1 skipped. Pint clean.
Still open
The larger analytics lever isn't this: it's that a load can pull up to 500 posts (5 pages × 100) at $0.005 each, and
since/untilcome straight off the request without validation (AnalyticsController.php:72), capped at 100 days inside the service. For a heavy poster on a long range that's $2.50 per UTC day, per account. Reading fewer posts would change what the numbers mean, so it's a product decision rather than a cleanup.