Skip to content

Commit ca0515b

Browse files
authored
perf: Minor extract coverage util perf updates (#3869)
1 parent a41eb25 commit ca0515b

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/services/pathContents/useFileWithMainCoverage.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ import {
1111
} from './constants'
1212
import { extractCoverageFromResponse } from './utils'
1313

14-
// There's only 1 hook per page table as of Feb 22, 2023. This is a limitation to tailor a table to a particular page. Each "page" should have an independent set of hooks for directory and file entries. Every usePrefetchFile<page name> hook needs to this hook's query and queryKey
15-
// There should be a coverageForFile + prefetch hook set per page. This function, due to how it's written, acts as 1 hook all pages use, and each file implements it's prefetch function accordingly. This is something that needs to be changed, likely in a subsequent PR, as we're getting rid of the individual line file type layout anyway and doing diff line only
14+
/* There's only 1 hook per page table as of Feb 22, 2023. This is a limitation to
15+
tailor a table to a particular page. Each "page" should have an independent set of
16+
hooks for directory and file entries. Every usePrefetchFile<page name> hook needs to
17+
this hook's query and queryKey. There should be a coverageForFile + prefetch hook set per page.
18+
This function, due to how it's written, acts as 1 hook all pages use, and each file implements
19+
it's prefetch function accordingly. This is something that needs to be changed, likely in a
20+
subsequent PR, as we're getting rid of the individual line file type layout anyway and
21+
doing diff line only */
1622

1723
interface UseFileWithMainCoverageArgs {
1824
provider: string

src/services/pathContents/utils.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
import keyBy from 'lodash/keyBy'
2-
import mapValues from 'lodash/mapValues'
3-
41
import { PathContentsRepositorySchema } from './constants'
52

63
export function extractCoverageFromResponse(
74
repository: PathContentsRepositorySchema | undefined | null
85
) {
96
if (!repository) return null
10-
const commit = repository?.commit
11-
const branch = repository?.branch?.head
12-
const coverageSource = (commit || branch)?.coverageAnalytics
7+
8+
const commit = repository.commit
9+
const branch = repository.branch?.head
10+
const coverageSource = commit?.coverageAnalytics || branch?.coverageAnalytics
1311
const coverageFile = coverageSource?.coverageFile
12+
1413
if (!coverageFile) return null
15-
const lineWithCoverage = keyBy(coverageFile?.coverage, 'line')
16-
const fileCoverage = mapValues(lineWithCoverage, 'coverage')
14+
15+
const fileCoverage = Object.fromEntries(
16+
coverageFile.coverage?.map((item) => [item?.line, item?.coverage]) || []
17+
)
1718
const coverageTotal = coverageFile?.totals?.percentCovered
1819
const hashedPath = coverageFile?.hashedPath
1920

2021
return {
2122
content: coverageFile?.content,
2223
coverage: fileCoverage,
23-
totals:
24-
typeof coverageTotal !== 'number' || isNaN(coverageTotal)
25-
? 0
26-
: coverageTotal,
24+
totals: coverageTotal && !Number.isNaN(coverageTotal) ? coverageTotal : 0,
2725
flagNames: coverageSource?.flagNames ?? [],
2826
componentNames: coverageSource?.components?.map(({ name }) => name) ?? [],
2927
...(hashedPath && { hashedPath }),

0 commit comments

Comments
 (0)