|
1 | 1 | import { createContext, useContext, useState, useCallback, useEffect, useMemo } from 'react' |
2 | | -import { fetchOrg, fetchRepos, fetchContributors, fetchIssues, fetchRateLimit } from '../services/github' |
| 2 | +import { fetchOrg, fetchRepos, fetchContributors, fetchIssues, fetchRateLimit, fetchPulls } from '../services/github' |
3 | 3 | import { buildAnalyticalModel, getTopRepositories } from '../services/analytics' |
4 | 4 |
|
5 | 5 | const Ctx = createContext(null) |
@@ -29,12 +29,14 @@ export function AppProvider({ children }) { |
29 | 29 | const [orgs, setOrgs] = useState([]) |
30 | 30 | const [model, setModel] = useState(null) |
31 | 31 | const [issuesData, setIssuesData] = useState({}) |
| 32 | + const [pullsData, setPullsData] = useState({}) |
32 | 33 | const [rateLimit, setRateLimit] = useState(getStoredRateLimit) |
33 | 34 | const [loading, setLoading] = useState(false) |
34 | 35 | const [loadMsg, setLoadMsg] = useState('') |
35 | 36 | const [govLoading, setGovLoading] = useState(false) |
36 | 37 | const [error, setError] = useState('') |
37 | 38 | const [totalRepo, setTotalRepo] = useState(0) |
| 39 | + const [advanceAnalyticsLoading, setAdvanceAnalyticsLoading] = useState(false); |
38 | 40 |
|
39 | 41 | useEffect(() => { |
40 | 42 | const handler = e => { |
@@ -151,6 +153,24 @@ export function AppProvider({ children }) { |
151 | 153 | setGovLoading(false) |
152 | 154 | }, [model, pat, govLoading]) |
153 | 155 |
|
| 156 | + // Advanced analytics — parallel batches of 5 (Section 3.2.5) |
| 157 | + const runAdvanceAnalytics = useCallback(async () => { |
| 158 | + if (!model || advanceAnalyticsLoading) return |
| 159 | + setAdvanceAnalyticsLoading(true) |
| 160 | + const map = {} |
| 161 | + const repos = model.totalRepos; |
| 162 | + |
| 163 | + // Batches of 5 using Promise.allSettled |
| 164 | + for (let i = 0; i < repos.length; i += 5) { |
| 165 | + const batch = repos.slice(i, i + 5) |
| 166 | + await Promise.allSettled(batch.map(async repo => { |
| 167 | + map[`${repo.orgLogin}/${repo.name}`] = await fetchPulls(repo.orgLogin, repo.name, pat) |
| 168 | + })) |
| 169 | + } |
| 170 | + setPullsData(map) |
| 171 | + setAdvanceAnalyticsLoading(false) |
| 172 | + }, [model, pat, advanceAnalyticsLoading]) |
| 173 | + |
154 | 174 | const STALE_DAYS = 90 |
155 | 175 |
|
156 | 176 | const staleRepoStats = useMemo(() => { |
@@ -187,9 +207,9 @@ export function AppProvider({ children }) { |
187 | 207 |
|
188 | 208 | return ( |
189 | 209 | <Ctx.Provider value={{ |
190 | | - pat, savePat, orgs, model, issuesData, |
| 210 | + pat, savePat, orgs, model, issuesData, pullsData, |
191 | 211 | rateLimit, loading, loadMsg, govLoading, error, totalRepo, |
192 | | - explore, runAudit, setError, refreshRateLimit, staleRepoStats, |
| 212 | + explore, runAudit, runAdvanceAnalytics, setError, refreshRateLimit, staleRepoStats, advanceAnalyticsLoading |
193 | 213 | }}> |
194 | 214 | {children} |
195 | 215 | </Ctx.Provider> |
|
0 commit comments