@@ -100,6 +100,8 @@ function buildDateWindows(): {
100100
101101interface RateLimit {
102102 cost : number
103+ remaining : number
104+ resetAt : string
103105}
104106
105107interface IssueCount {
@@ -164,7 +166,7 @@ const SUMMARY_QUERY = `
164166 $searchIssuesOpened: String!, $searchIssuesClosed: String!,
165167 $searchIssuesOpened6m: String!, $searchIssuesOpenNow: String!
166168 ) {
167- rateLimit { cost }
169+ rateLimit { cost remaining resetAt }
168170 repository(owner: $owner, name: $name) {
169171 defaultBranchRef {
170172 target {
@@ -188,7 +190,7 @@ const SUMMARY_QUERY = `
188190
189191const PR_PAGE_QUERY = `
190192 query($owner: String!, $name: String!, $cursor: String) {
191- rateLimit { cost }
193+ rateLimit { cost remaining resetAt }
192194 repository(owner: $owner, name: $name) {
193195 pullRequests(first: ${ PAGE_SIZE } , orderBy: { field: CREATED_AT, direction: DESC }, after: $cursor) {
194196 pageInfo { hasNextPage endCursor }
@@ -210,10 +212,16 @@ async function pagePrs(
210212 token : string ,
211213 timeoutMs : number ,
212214 windowStart : Date ,
213- ) : Promise < { nodes : PrNode [ ] ; rateLimitCost : number ; httpRequestCount : number } > {
215+ ) : Promise < {
216+ nodes : PrNode [ ]
217+ rateLimitCost : number
218+ httpRequestCount : number
219+ rateLimit : RateLimit | null
220+ } > {
214221 const nodes : PrNode [ ] = [ ]
215222 let rateLimitCost = 0
216223 let httpRequestCount = 0
224+ let rateLimit : RateLimit | null = null
217225 let cursor : string | undefined
218226
219227 for ( ; ; ) {
@@ -223,6 +231,7 @@ async function pagePrs(
223231 const data = await graphqlRequest < PrPageQueryResult > ( PR_PAGE_QUERY , variables , token , timeoutMs )
224232 rateLimitCost += data . rateLimit . cost
225233 httpRequestCount ++
234+ rateLimit = data . rateLimit
226235
227236 const connection = data . repository . pullRequests
228237 let reachedWindowBoundary = false
@@ -239,12 +248,12 @@ async function pagePrs(
239248 cursor = connection . pageInfo . endCursor
240249 }
241250
242- return { nodes, rateLimitCost, httpRequestCount }
251+ return { nodes, rateLimitCost, httpRequestCount, rateLimit }
243252}
244253
245254const ISSUE_PAGE_QUERY = `
246255 query($owner: String!, $name: String!, $cursor: String) {
247- rateLimit { cost }
256+ rateLimit { cost remaining resetAt }
248257 repository(owner: $owner, name: $name) {
249258 issues(first: ${ PAGE_SIZE } , orderBy: { field: CREATED_AT, direction: DESC }, after: $cursor) {
250259 pageInfo { hasNextPage endCursor }
@@ -265,10 +274,16 @@ async function pageIssues(
265274 token : string ,
266275 timeoutMs : number ,
267276 windowStart : Date ,
268- ) : Promise < { nodes : IssueNode [ ] ; rateLimitCost : number ; httpRequestCount : number } > {
277+ ) : Promise < {
278+ nodes : IssueNode [ ]
279+ rateLimitCost : number
280+ httpRequestCount : number
281+ rateLimit : RateLimit | null
282+ } > {
269283 const nodes : IssueNode [ ] = [ ]
270284 let rateLimitCost = 0
271285 let httpRequestCount = 0
286+ let rateLimit : RateLimit | null = null
272287 let cursor : string | undefined
273288
274289 for ( ; ; ) {
@@ -283,6 +298,7 @@ async function pageIssues(
283298 )
284299 rateLimitCost += data . rateLimit . cost
285300 httpRequestCount ++
301+ rateLimit = data . rateLimit
286302
287303 const connection = data . repository . issues
288304 let reachedWindowBoundary = false
@@ -299,7 +315,7 @@ async function pageIssues(
299315 cursor = connection . pageInfo . endCursor
300316 }
301317
302- return { nodes, rateLimitCost, httpRequestCount }
318+ return { nodes, rateLimitCost, httpRequestCount, rateLimit }
303319}
304320
305321export async function fetchActivitySnapshot (
@@ -363,6 +379,11 @@ export async function fetchActivitySnapshot(
363379 const prMedians = computePrMedians ( prResult . nodes )
364380 const issueMedians = computeIssueMedians ( issueResult . nodes )
365381
382+ // Lowest remaining across all requests — remaining only decreases within a reset window
383+ const lastRateLimit = [ summaryData . rateLimit , prResult . rateLimit , issueResult . rateLimit ]
384+ . filter ( ( rl ) : rl is RateLimit => rl !== null )
385+ . reduce ( ( min , rl ) => ( rl . remaining < min . remaining ? rl : min ) )
386+
366387 return {
367388 repoId,
368389 snapshotAt : new Date ( ) . toISOString ( ) ,
@@ -385,5 +406,7 @@ export async function fetchActivitySnapshot(
385406 issueMedianTimeToFirstResponseHours : issueMedians . medianTimeToFirstResponseHours ,
386407 httpRequestCount : totalHttpRequests ,
387408 rateLimitCost : totalRateLimitCost ,
409+ rateLimitRemaining : lastRateLimit . remaining ,
410+ rateLimitResetAt : lastRateLimit . resetAt ,
388411 }
389412}
0 commit comments