-
Notifications
You must be signed in to change notification settings - Fork 133
feat(frontend): add option to unstack downloads per version #1101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tylersayshi
wants to merge
2
commits into
jsr-io:main
Choose a base branch
from
tylersayshi:fix-download-stack
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+62
−49
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -20,7 +20,6 @@ export function DownloadChart(props: Props) { | |
|
||
const getChartOptions = ( | ||
isDarkMode: boolean, | ||
aggregationPeriod: AggregationPeriod = "weekly", | ||
) => ({ | ||
chart: { | ||
type: "area", | ||
|
@@ -57,7 +56,7 @@ export function DownloadChart(props: Props) { | |
curve: "straight", | ||
width: 1.7, | ||
}, | ||
series: getSeries(props.downloads, aggregationPeriod), | ||
series: getSeries(props.downloads, "weekly"), | ||
xaxis: { | ||
type: "datetime", | ||
tooltip: { | ||
|
@@ -137,37 +136,52 @@ export function DownloadChart(props: Props) { | |
return ( | ||
<div class="relative"> | ||
{graphRendered && ( | ||
<div className="absolute flex items-center gap-2 pt-1 text-sm pl-5 z-20"> | ||
<label htmlFor="aggregationPeriod" className="text-secondary"> | ||
Aggregation Period: | ||
</label> | ||
<select | ||
id="aggregationPeriod" | ||
onChange={(e) => { | ||
const isDarkMode = document.documentElement.classList.contains( | ||
"dark", | ||
); | ||
const newAggregationPeriod = e.currentTarget | ||
.value as AggregationPeriod; | ||
<div className="absolute flex md:flex-col md:-top-4 gap-2 pt-1 text-sm pl-5 z-20"> | ||
<div className="flex items-center gap-2"> | ||
<label htmlFor="aggregationPeriod" className="text-secondary"> | ||
Aggregation Period: | ||
</label> | ||
<select | ||
id="aggregationPeriod" | ||
onChange={(e) => { | ||
chartRef.current?.updateSeries( | ||
getSeries( | ||
props.downloads, | ||
e.currentTarget.value as AggregationPeriod, | ||
), | ||
); | ||
}} | ||
className="input-container input select w-20" | ||
> | ||
<option value="daily">Daily</option> | ||
<option value="weekly" selected> | ||
Weekly | ||
</option> | ||
<option value="monthly">Monthly</option> | ||
</select> | ||
</div> | ||
<div className="flex items-center gap-2"> | ||
<label htmlFor="displayAs" className="text-secondary"> | ||
Display As | ||
</label> | ||
<select | ||
id="displayAs" | ||
onChange={(e) => { | ||
const newDisplay = e.currentTarget.value === "stacked"; | ||
|
||
// Update chart with new options including the new aggregation period | ||
chartRef.current?.updateOptions( | ||
getChartOptions(isDarkMode, newAggregationPeriod), | ||
); | ||
|
||
chartRef.current?.updateSeries( | ||
getSeries( | ||
props.downloads, | ||
e.currentTarget.value as AggregationPeriod, | ||
), | ||
); | ||
}} | ||
className="input-container input select w-20" | ||
> | ||
<option value="daily">Daily</option> | ||
<option value="weekly" selected>Weekly</option> | ||
<option value="monthly">Monthly</option> | ||
</select> | ||
// Update chart with new options including the new stacked display | ||
chartRef.current?.updateOptions( | ||
{ chart: { stacked: newDisplay } }, | ||
); | ||
}} | ||
className="input-container input select w-24" | ||
> | ||
<option value="stacked" selected>Stacked</option> | ||
<option value="unstacked"> | ||
Unstacked | ||
</option> | ||
</select> | ||
</div> | ||
</div> | ||
)} | ||
<div className="h-[300px] md:pt-0 pt-10 text-secondary"> | ||
|
@@ -197,19 +211,17 @@ function adjustTimePeriod( | |
switch (aggregation) { | ||
case "weekly": | ||
// start of week (Sunday) in UTC | ||
out = new Date(Date.UTC( | ||
date.getUTCFullYear(), | ||
date.getUTCMonth(), | ||
date.getUTCDate() - date.getUTCDay(), | ||
)); | ||
out = new Date( | ||
Date.UTC( | ||
date.getUTCFullYear(), | ||
date.getUTCMonth(), | ||
date.getUTCDate() - date.getUTCDay(), | ||
), | ||
); | ||
Comment on lines
-200
to
+220
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why might this change? I ran |
||
break; | ||
case "monthly": | ||
// first day of month in UTC | ||
out = new Date(Date.UTC( | ||
date.getUTCFullYear(), | ||
date.getUTCMonth(), | ||
1, | ||
)); | ||
out = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1)); | ||
break; | ||
default: // daily | ||
out = date; | ||
|
@@ -227,8 +239,8 @@ export function collectX( | |
xValues.add(adjustTimePeriod(point.timeBucket, aggregationPeriod)); | ||
}); | ||
|
||
return Array.from(xValues).sort((a, b) => | ||
new Date(a).getTime() - new Date(b).getTime() | ||
return Array.from(xValues).sort( | ||
(a, b) => new Date(a).getTime() - new Date(b).getTime(), | ||
); | ||
} | ||
|
||
|
@@ -252,17 +264,18 @@ export function normalize( | |
} | ||
}); | ||
|
||
return Object.entries(normalized).map(( | ||
[key, value], | ||
) => [new Date(key).getTime(), value]); | ||
return Object.entries(normalized).map(([key, value]) => [ | ||
new Date(key).getTime(), | ||
value, | ||
]); | ||
} | ||
|
||
function getSeries( | ||
recentVersions: PackageDownloadsRecentVersion[], | ||
aggregationPeriod: AggregationPeriod, | ||
) { | ||
const dataPointsWithDownloads = recentVersions.filter((dataPoints) => | ||
dataPoints.downloads.length > 0 | ||
const dataPointsWithDownloads = recentVersions.filter( | ||
(dataPoints) => dataPoints.downloads.length > 0, | ||
); | ||
|
||
const dataPointsToDisplay = dataPointsWithDownloads.slice(0, 5); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
took the chance to simplify this logic while I was reading it and adding the other select.
only updateSeries is needed for this