Skip to content

Commit

Permalink
feat: show archived repo status, start and forks count
Browse files Browse the repository at this point in the history
  • Loading branch information
dmijatovic committed Nov 12, 2024
1 parent 5442ca6 commit 477c9b7
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 37 deletions.
2 changes: 1 addition & 1 deletion frontend/components/category/CategoriesWithHeadlines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const CategoriesWithHeadlines = ({categories}: CategoriesWithHeadlinesPro
const category = node.getValue()
const children = node.children()

return <React.Fragment key={category.short_name}>
return <React.Fragment key={category.id}>
<SidebarHeadline iconName={category.properties.icon} title={category.name} />
<div className='ml-4'>
<CategoryTreeLevel items={children} />
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/charts/d3LineChart/drawLineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type LineChartConfig = {
const margin = {
// minimal margins to host first/last year label 'overflow'
left: 12, right: 12,
top: 4, bottom: 16
top: 4, bottom: 24
}

function findMax(data:LineData[]) {
Expand Down
5 changes: 4 additions & 1 deletion frontend/components/charts/d3LineChart/formatData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-FileCopyrightText: 2022 Dusan Mijatovic (dv4all)
// SPDX-FileCopyrightText: 2022 dv4all
// SPDX-FileCopyrightText: 2024 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2024 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -37,10 +39,11 @@ export function formatUnixDateData(data: Data) {
}

export function prepareDataForSoftwarePage(data: Data) {
console.log('prepareDataForSoftwarePage...',data)
// format unix time in seconds to ms for js
const {lineData,lastUpdateInMs} = formatUnixDateData(data)
// calculate total number of commits
const totalCountY = lineData.reduce((acc: any, point) => {
const totalCountY:number = lineData.reduce((acc: any, point) => {
return acc+=point.y
}, 0)
// extract last commit date
Expand Down
105 changes: 91 additions & 14 deletions frontend/components/software/CommitsChart.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2023 - 2024 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2023 - 2024 Netherlands eScience Center
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (dv4all) (dv4all)
// SPDX-FileCopyrightText: 2023 Netherlands eScience Center
// SPDX-FileCopyrightText: 2023 dv4all
//
// SPDX-License-Identifier: Apache-2.0

import {render, screen} from '@testing-library/react'

import {WithAppContext} from '~/utils/jest/WithAppContext'
import CommitsChart, {CommitsChartProps} from './CommitsChart'
import CommitsChart, {CommitsChartProps,ArchivedRepo,StarCount,ForkCount} from './CommitsChart'

// MOCK useResizeObserver
jest.mock('~/components/charts/d3LineChart/useResizeObserver')
Expand All @@ -21,20 +21,18 @@ jest.mock('~/components/charts/d3LineChart/NoDataAvailableChart', () => ({
default: jest.fn(({text}) =><div data-testid="no-data-chart-message">{text}</div>)
}))

// const mockPrepareDataForSoftwarePage = jest.fn(props => ({
// lineData: [],
// totalCountY:0,
// lastCommitDate: null
// }))
// jest.mock('~/components/charts/d3LineChart/formatData', () => ({
// prepareDataForSoftwarePage: jest.fn(props=>mockPrepareDataForSoftwarePage(props))
// }))
type MutableProps = {
-readonly [key in keyof CommitsChartProps]: CommitsChartProps[key]
}

const mockProps:CommitsChartProps = {
const mockProps:MutableProps = {
repository_url: null,
commit_history: undefined,
commit_history_scraped_at: undefined,
className: undefined
className: undefined,
archived: null,
star_count: null,
fork_count: null
}

it('renders "missing repository_url" message', () => {
Expand Down Expand Up @@ -70,7 +68,7 @@ it('renders "did not scrape repo yet" message', () => {
// screen.debug()
})

it('renders "reposotory is empty" message', () => {
it('renders "repository is empty" message', () => {
mockProps.repository_url = 'https://some.repo.url.com'
mockProps.commit_history_scraped_at = new Date().toISOString()
mockProps.commit_history = {}
Expand Down Expand Up @@ -105,3 +103,82 @@ it('renders "we cannot read the commit history" message', () => {
expect(message).toHaveTextContent(expectMessage)
// screen.debug()
})

it('renders "archived repository" message', () => {
// render
render(
<WithAppContext>
<ArchivedRepo archived={true} />
</WithAppContext>
)

screen.getByTestId('archived-repository')
})

it('renders "0 stars" message', () => {
// render
render(
<WithAppContext>
<StarCount star_count={0} />
</WithAppContext>
)

screen.getByText('0 stars')
})

it('does NOT render the stars message on null', async() => {
// render
render(
<WithAppContext>
<StarCount star_count={null} />
</WithAppContext>
)
const stars = screen.queryByTestId('star-count')
expect(stars).not.toBeInTheDocument()
// screen.debug(stars)
})

it('does NOT render the stars message on undefined', async() => {
// render
render(
<WithAppContext>
<StarCount star_count={undefined} />
</WithAppContext>
)
const stars = screen.queryByTestId('star-count')
expect(stars).not.toBeInTheDocument()
})

it('renders "0 forks" message', () => {
// render
render(
<WithAppContext>
<ForkCount fork_count={0} />
</WithAppContext>
)

screen.getByText('0 forks')
})

it('does NOT render forks message on null', async() => {
// render
render(
<WithAppContext>
<ForkCount fork_count={null} />
</WithAppContext>
)
const forks = screen.queryByTestId('fork-count')
expect(forks).not.toBeInTheDocument()
})

it('does NOT render forks message on undefined', async() => {
// render
render(
<WithAppContext>
<ForkCount fork_count={undefined} />
</WithAppContext>
)
const stars = screen.queryByTestId('fork-count')
expect(stars).not.toBeInTheDocument()
})

59 changes: 53 additions & 6 deletions frontend/components/software/CommitsChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// SPDX-FileCopyrightText: 2022 Christian Meeßen (GFZ) <[email protected]>
// SPDX-FileCopyrightText: 2022 Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (dv4all) (dv4all)
// SPDX-FileCopyrightText: 2024 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2024 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -12,14 +14,58 @@ import {prepareDataForSoftwarePage} from '~/components/charts/d3LineChart/format
import NoDataAvailableChart from '~/components/charts/d3LineChart/NoDataAvailableChart'
import SingleLineChart from '~/components/charts/d3LineChart/SingleLineChart'

export type CommitsChartProps = {
export type CommitsChartProps = Readonly<{
repository_url: string | null,
archived: boolean | null,
star_count: number | null,
fork_count: number | null,
commit_history?: CommitHistory
commit_history_scraped_at?: string
className?: string
}>

export function ArchivedRepo({archived}:Readonly<{archived:boolean|null}>){
if (!archived) return null
return (
<span data-testid="archived-repository">
<b className="text-warning uppercase">archived repository</b>
</span>
)
}

export function StarCount({star_count}:Readonly<{star_count:number|null}>){
if (star_count===null || star_count===undefined) return null
return (
<span data-testid="star-count">
{star_count===1 ? `${star_count} star`:`${star_count} stars`}
</span>
)
}

export function ForkCount({fork_count}:Readonly<{fork_count:number|null}>){
if (fork_count===null || fork_count===undefined) return null
return (
<span data-testid="fork-count">
{fork_count===1 ? `${fork_count} fork` : `${fork_count} forks`}
</span>
)
}

export function Commits({commits,lastCommitDate}:Readonly<{commits:number|null,lastCommitDate?:Date}>){
return (
<>
<span><b>{commits===1 ? `${commits} commit` : `${commits} commits`}</b></span>
<span>Last commit&nbsp;<b>&#x2248;&nbsp;{
getTimeAgoSince(new Date(), lastCommitDate?.toISOString() ?? null)
}</b></span>
</>
)
}

export default function CommitsChart({repository_url, commit_history, commit_history_scraped_at, className}: CommitsChartProps) {
export default function CommitsChart({
repository_url, commit_history, commit_history_scraped_at,
archived, star_count, fork_count, className
}: CommitsChartProps) {
// if there is commit_history
if (commit_history && Object.keys(commit_history).length > 0) {
// format commits data for chart and calculate other stats
Expand All @@ -28,10 +74,11 @@ export default function CommitsChart({repository_url, commit_history, commit_his
return (
<div className={`flex-1 w-full ${className ?? ''}`}>
<SingleLineChart data={lineData} />
<div className="software_commitsStat pt-4" id="commitsStat">
<b>{totalCountY} commits</b> | Last commit <b>&#x2248; {
getTimeAgoSince(new Date(), lastCommitDate?.toISOString() ?? null)
}</b>
<div className="flex pt-4 px-2 [&>*]:border-l [&>*]:border-base-700 [&>*]:px-2 [&>*]:text-center" id="commitsStat">
<ArchivedRepo archived={archived} />
<Commits commits={totalCountY} lastCommitDate={lastCommitDate}/>
<StarCount star_count={star_count} />
<ForkCount fork_count={fork_count} />
</div>
</div>
)
Expand Down
23 changes: 12 additions & 11 deletions frontend/components/software/GetStartedSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@
// SPDX-FileCopyrightText: 2021 - 2022 dv4all
// SPDX-FileCopyrightText: 2022 Christian Meeßen (GFZ) <[email protected]>
// SPDX-FileCopyrightText: 2022 Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences
// SPDX-FileCopyrightText: 2024 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2024 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0

import LinkIcon from '@mui/icons-material/Link'
import CommitsChart from './CommitsChart'
import {CommitHistory} from '../../types/SoftwareTypes'
import {RepositoryInfo} from '../../types/SoftwareTypes'

type GetStartedSectionProps = {
get_started_url: string | null,
repository_url: string | null,
commit_history: CommitHistory,
commit_history_scraped_at: string
repositoryInfo: RepositoryInfo
}

export default function GetStartedSection(props:GetStartedSectionProps) {
const {repository_url, get_started_url, commit_history, commit_history_scraped_at} = props

export default function GetStartedSection({get_started_url,repositoryInfo}:GetStartedSectionProps) {
// if no get_started_url and repository_url we do not render this section
if (!get_started_url && !repository_url) return null
if (!get_started_url && !repositoryInfo?.url) return null

function renderGetStartedUrl() {
if (get_started_url) {
Expand All @@ -45,9 +43,12 @@ export default function GetStartedSection(props:GetStartedSectionProps) {
return (
<CommitsChart
className={classes}
repository_url={repository_url}
commit_history={commit_history}
commit_history_scraped_at={commit_history_scraped_at}
repository_url={repositoryInfo?.url}
commit_history={repositoryInfo?.commit_history}
commit_history_scraped_at={repositoryInfo?.commit_history_scraped_at}
archived={repositoryInfo?.archived}
star_count={repositoryInfo?.star_count}
fork_count={repositoryInfo?.fork_count}
/>
)
}
Expand Down
4 changes: 1 addition & 3 deletions frontend/pages/software/[slug]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ export default function SoftwareIndexPage(props:SoftwareIndexData) {
/>
<GetStartedSection
get_started_url={software.get_started_url}
repository_url={repositoryInfo?.url}
commit_history={repositoryInfo?.commit_history}
commit_history_scraped_at={repositoryInfo?.commit_history_scraped_at}
repositoryInfo={repositoryInfo}
/>
<CitationSection
releases={releases}
Expand Down
5 changes: 5 additions & 0 deletions frontend/types/SoftwareTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ export type RepositoryInfo = {
commit_history: CommitHistory,
commit_history_scraped_at: string,
code_platform: CodePlatform
archived: boolean | null
fork_count: number | null
star_count: number | null
open_issue_count: number | null
contributor_count: number | null
}

/**
Expand Down

0 comments on commit 477c9b7

Please sign in to comment.