-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(website): show organism statistics on landing page
- Loading branch information
1 parent
5cf57c5
commit d2e8465
Showing
5 changed files
with
133 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
import type { OrganismStatistics } from './getOrganismStatistics'; | ||
import { routes } from '../../routes/routes'; | ||
interface Props { | ||
key: string; | ||
image: string | undefined; | ||
displayName: string; | ||
organismStatistics: OrganismStatistics; | ||
numberDaysAgoStatistics: number; | ||
} | ||
const { key, image, displayName, organismStatistics, numberDaysAgoStatistics } = Astro.props; | ||
--- | ||
|
||
<a | ||
href={routes.organismStartPage(key)} | ||
class='block rounded border border-gray-300 p-4 m-2 w-64 text-center hover:bg-gray-100' | ||
> | ||
{image !== undefined && <img src={image} class='h-32 mx-auto mb-4' alt={displayName} />} | ||
<h3 class='font-semibold text-gray-700'>{displayName}</h3> | ||
<p class='text-gray-700 text-sm'> | ||
{organismStatistics.totalSequences} sequences<br /> | ||
(+{organismStatistics.recentSequences} in last {numberDaysAgoStatistics} days)<br /> | ||
{organismStatistics.lastUpdatedAt && <>Last updated {organismStatistics.lastUpdatedAt.toRelative()}</>} | ||
</p> | ||
</a> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { DateTime, FixedOffsetZone } from 'luxon'; | ||
|
||
import { LapisClient } from '../../services/lapisClient.ts'; | ||
import { RELEASED_AT_FIELD } from '../../settings.ts'; | ||
|
||
export type OrganismStatistics = { | ||
totalSequences: number; | ||
recentSequences: number; | ||
lastUpdatedAt: DateTime | undefined; | ||
}; | ||
type OrganismStatisticsMap = Map<string, OrganismStatistics>; | ||
|
||
export const getOrganismStatisticsMap = async ( | ||
organismNames: string[], | ||
numberDaysAgo: number, | ||
): Promise<OrganismStatisticsMap> => { | ||
const statistics = await Promise.all( | ||
organismNames.map((organism) => getOrganismStatistics(organism, numberDaysAgo)), | ||
); | ||
const result = new Map<string, OrganismStatistics>(); | ||
for (let i = 0; i < organismNames.length; i++) { | ||
result.set(organismNames[i], statistics[i]); | ||
} | ||
return result; | ||
}; | ||
|
||
const getOrganismStatistics = async (organism: string, numberDaysAgo: number): Promise<OrganismStatistics> => { | ||
const [{ total, lastUpdatedAt }, recent] = await Promise.all([ | ||
getTotalAndLastUpdatedAt(organism), | ||
getRecent(organism, numberDaysAgo), | ||
]); | ||
return { | ||
totalSequences: total, | ||
recentSequences: recent, | ||
lastUpdatedAt, | ||
}; | ||
}; | ||
|
||
const getTotalAndLastUpdatedAt = async ( | ||
organism: string, | ||
): Promise<{ total: number; lastUpdatedAt: DateTime | undefined }> => { | ||
const client = LapisClient.createForOrganism(organism); | ||
return ( | ||
await client.call('aggregated', { | ||
version: 1, | ||
}) | ||
) | ||
.map((x) => ({ | ||
total: x.data[0].count, | ||
lastUpdatedAt: DateTime.fromSeconds(Number.parseInt(x.info.dataVersion, 10), { | ||
zone: FixedOffsetZone.utcInstance, | ||
}), | ||
})) | ||
.unwrapOr({ | ||
total: 0, | ||
lastUpdatedAt: undefined, | ||
}); | ||
}; | ||
|
||
const getRecent = async (organism: string, numberDaysAgo: number): Promise<number> => { | ||
const recentTimestamp = Math.floor(Date.now() / 1000 - numberDaysAgo * 24 * 60 * 60); | ||
const client = LapisClient.createForOrganism(organism); | ||
return ( | ||
await client.call('aggregated', { | ||
[`${RELEASED_AT_FIELD}From`]: recentTimestamp, | ||
version: 1, | ||
}) | ||
) | ||
.map((x) => x.data[0].count) | ||
.unwrapOr(0); | ||
}; |
This file contains 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
This file contains 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
This file contains 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