Skip to content

Commit 7d7b3bc

Browse files
feat: add monthly downloads line graph (#216)
1 parent e416de7 commit 7d7b3bc

File tree

6 files changed

+134
-91
lines changed

6 files changed

+134
-91
lines changed

web/app/licenses.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

web/app/modules/instrumentation.server.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import { NodeSDK } from "@opentelemetry/sdk-node";
44
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
55
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
66
import { Resource } from "@opentelemetry/resources";
7-
import {
8-
ATTR_SERVICE_NAME,
9-
ATTR_SERVICE_VERSION,
10-
} from "@opentelemetry/semantic-conventions";
117
import { ENV } from "../data/env";
128
import { RemixInstrumentation } from "opentelemetry-instrumentation-remix";
139

@@ -26,8 +22,8 @@ export function initOTEL() {
2622
new RemixInstrumentation(),
2723
],
2824
resource: new Resource({
29-
[ATTR_SERVICE_NAME]: ENV.OTEL_NAME,
30-
[ATTR_SERVICE_VERSION]: ENV.npm_package_version,
25+
["service.name"]: ENV.OTEL_NAME,
26+
["service.version"]: ENV.npm_package_version,
3127
}),
3228
});
3329

web/app/modules/observability.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import { registerInstrumentations } from "@opentelemetry/instrumentation";
99
import { WinstonInstrumentation } from "@opentelemetry/instrumentation-winston";
1010
import * as winston from "winston";
1111
import { Resource } from "@opentelemetry/resources";
12-
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
1312
import { ENV } from "../data/env";
1413
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
1514

1615
const serviceName = "crane-app";
1716

1817
const resource = new Resource({
19-
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
18+
["service.name"]: serviceName,
19+
["service.version"]: ENV.npm_package_version,
2020
});
2121

2222
const otlpExporter = new OTLPLogExporter({

web/app/routes/_page.package.$packageName.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ type LoaderData = {
6060
totalMonthDownloads: number;
6161
yesterdayDownloads?: { day: string; downloads: number };
6262
peakYearlyDayDownloads?: { day: string; downloads: number };
63+
last31DaysDownloads: CranDownloadsResponse;
6364
monthlyDayDownloadsComment: string;
6465
totalYearDownloads: number;
66+
totalLast31DaysDownloads: number;
6567
totalYearlyDownloadsComment: string;
6668
indexOfTrendingItems: number;
6769
indexOfTopDownloads: number;
@@ -165,12 +167,14 @@ export const loader: LoaderFunction = async ({ params }) => {
165167
maintainer: undefined,
166168
dailyDownloads: [],
167169
yearlyDailyDownloads: [],
170+
last31DaysDownloads: [],
168171
lastRelease: "",
169172
totalMonthDownloads: 0,
170173
yesterdayDownloads: undefined,
171174
peakYearlyDayDownloads: undefined,
172175
monthlyDayDownloadsComment: "",
173176
totalYearDownloads: 0,
177+
totalLast31DaysDownloads: 0,
174178
totalYearlyDownloadsComment: "",
175179
indexOfTrendingItems: -1,
176180
indexOfTopDownloads: -1,
@@ -285,6 +289,21 @@ export const loader: LoaderFunction = async ({ params }) => {
285289
loaderData.indexOfTopDownloads = _topDownloads?.downloads.findIndex(
286290
(item) => item.package === packageName,
287291
);
292+
293+
if (loaderData.yearlyDailyDownloads.at(0)) {
294+
const yearly = loaderData.yearlyDailyDownloads.at(0)!;
295+
loaderData.last31DaysDownloads = [
296+
{
297+
...yearly,
298+
downloads: yearly.downloads.slice(-31),
299+
},
300+
];
301+
loaderData.totalLast31DaysDownloads =
302+
loaderData.last31DaysDownloads[0].downloads.reduce(
303+
(acc, curr) => acc + curr.downloads,
304+
0,
305+
);
306+
}
288307
} catch (error) {
289308
slog.error(error);
290309
throw new Response(null, {
@@ -310,11 +329,13 @@ export default function PackagePage() {
310329
authors,
311330
dailyDownloads,
312331
yearlyDailyDownloads,
332+
last31DaysDownloads,
313333
lastRelease,
314334
totalMonthDownloads,
315335
yesterdayDownloads,
316336
totalYearDownloads,
317337
totalYearlyDownloadsComment,
338+
totalLast31DaysDownloads,
318339
monthlyDayDownloadsComment,
319340
peakYearlyDayDownloads,
320341
indexOfTrendingItems,
@@ -367,6 +388,8 @@ export default function PackagePage() {
367388
totalYearDownloads={totalYearDownloads}
368389
monthlyDayDownloadsComment={monthlyDayDownloadsComment}
369390
totalYearlyDownloadsComment={totalYearlyDownloadsComment}
391+
last31DaysDownloads={last31DaysDownloads}
392+
totalLast31DaysDownloads={totalLast31DaysDownloads}
370393
/>
371394

372395
<BinariesPageContentSection
@@ -709,6 +732,8 @@ function InsightsPageContentSection(
709732
| "monthlyDayDownloadsComment"
710733
| "totalYearlyDownloadsComment"
711734
| "totalYearDownloads"
735+
| "last31DaysDownloads"
736+
| "totalLast31DaysDownloads"
712737
>,
713738
) {
714739
const {
@@ -720,6 +745,7 @@ function InsightsPageContentSection(
720745
totalYearDownloads,
721746
monthlyDayDownloadsComment,
722747
totalYearlyDownloadsComment,
748+
last31DaysDownloads,
723749
} = props;
724750

725751
const nrFormatter = new Intl.NumberFormat("en-US");
@@ -791,6 +817,29 @@ function InsightsPageContentSection(
791817
/>
792818
)}
793819
</ClientOnly>
820+
821+
<div className="space-y-4">
822+
<p>
823+
The following line graph shows the downloads per day. You can
824+
hover over the graph to see the exact number of downloads per
825+
day.
826+
</p>
827+
</div>
828+
<ClientOnly
829+
fallback={
830+
<div className="h-200 bg-gray-ui animate-pulse rounded-md" />
831+
}
832+
>
833+
{() => (
834+
<LineGraph
835+
height={200}
836+
data={last31DaysDownloads[0].downloads.map((d) => ({
837+
date: d.day,
838+
value: d.downloads,
839+
}))}
840+
/>
841+
)}
842+
</ClientOnly>
794843
</>
795844
) : null}
796845

0 commit comments

Comments
 (0)