Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximilianAnzinger committed Nov 29, 2024
1 parent 7b58fe6 commit 26fa319
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 83 deletions.
32 changes: 16 additions & 16 deletions atlas-metrics/app/api/data/dashboard/route.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import {NextRequest, NextResponse} from "next/server";
import { NextRequest, NextResponse } from "next/server";
import {
ErrorResponse,
SearchParamDateOrUndefined,
SearchParamStringOrUndefined
ErrorResponse,
SearchParamDateOrUndefined,
SearchParamStringOrUndefined,
} from "@lib/api-utils";
import applicationContainer from "@server/applicationContainer";

export async function GET(request: NextRequest) {
try {
const service = SearchParamStringOrUndefined(request.nextUrl.searchParams, "service");
const from = SearchParamDateOrUndefined(request.nextUrl.searchParams, "from");
const to = SearchParamDateOrUndefined(request.nextUrl.searchParams, "to");
try {
const service = SearchParamStringOrUndefined(request.nextUrl.searchParams, "service");
const from = SearchParamDateOrUndefined(request.nextUrl.searchParams, "from");
const to = SearchParamDateOrUndefined(request.nextUrl.searchParams, "to");
try {
const metricsController = applicationContainer.getMetricsController();
const metrics = await metricsController.GetMetricsForDashboard(service, from, to);
return NextResponse.json(metrics, { status: 200 });
} catch (error) {
return ErrorResponse(error, 500);
}
const metricsController = applicationContainer.getMetricsController();
const metrics = await metricsController.GetMetricsForDashboard(service, from, to);
return NextResponse.json(metrics, { status: 200 });
} catch (error) {
return ErrorResponse(error, 400);
return ErrorResponse(error, 500);
}
}
} catch (error) {
return ErrorResponse(error, 400);
}
}
18 changes: 0 additions & 18 deletions atlas-metrics/app/api/data/route.ts

This file was deleted.

2 changes: 1 addition & 1 deletion atlas-metrics/app/components/custom/DateRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function DateRangePicker({ className }: React.HTMLAttributes<HTMLDivEleme
<PopoverTrigger asChild>
<Button
id="date"
variant={"outline"}
variant="outline"
className={cn(
"w-[300px] justify-start text-left font-normal",
!date && "text-muted-foreground",
Expand Down
13 changes: 6 additions & 7 deletions atlas-metrics/app/dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { EndpointActivity } from "@components/custom/EndpointActivity";
import { PieChartDonut } from "@components/custom/PieChartDonut";
import { Menu } from "@components/custom/Menu";
import {useEffect, useState} from "react";
import {DashboardDataDAO, DashboardDataSchema} from "@server/domain/dao/dashboardData";
import { useEffect, useState } from "react";
import { DashboardDataDAO, DashboardDataSchema } from "@server/domain/dao/dashboardData";

export default function Dashboard() {

const [metrics, setMetrics] = useState<DashboardDataDAO|undefined>(undefined);
const [metrics, setMetrics] = useState<DashboardDataDAO | undefined>(undefined);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);

Expand All @@ -30,9 +29,9 @@ export default function Dashboard() {
fetchMetrics().finally(() => setLoading(false));
}, []);

if (loading) {
return <div>Loading...</div>;
}
if (loading) {
return <div>Loading...</div>;
}

if (error) {
return <p>Error: {error}</p>;
Expand Down
10 changes: 6 additions & 4 deletions atlas-metrics/app/server/domain/dao/ChartDataItem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {z} from "zod";
import { z } from "zod";

export interface ChartDataItemDAO {
label: string;
Expand All @@ -15,6 +15,8 @@ export interface TimeSeriesChartDataItemDAO {
[key: string]: number | string;
}

export const TimeSeriesChartDataItemSchema = z.object({
date: z.string(),
}).catchall(z.union([z.number(), z.string()]));
export const TimeSeriesChartDataItemSchema = z
.object({
date: z.string(),
})
.catchall(z.union([z.number(), z.string()]));
2 changes: 1 addition & 1 deletion atlas-metrics/app/server/domain/dao/RequestTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ export function mapRequestTypeToCategory(requestType: RequestType): RequestCateg
case RequestType.CONNECT:
return RequestCategory.CONNECT;
}
}
}
12 changes: 6 additions & 6 deletions atlas-metrics/app/server/domain/dao/dashboardData.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
EndpointActivityDashboardDAO,
EndpointActivityDashboardSchema
EndpointActivityDashboardDAO,
EndpointActivityDashboardSchema,
} from "@server/domain/dao/endpointActivityDashboard";
import {z} from "zod";
import { z } from "zod";

export interface DashboardDataDAO {
endpointActivity: EndpointActivityDashboardDAO;
endpointActivity: EndpointActivityDashboardDAO;
}

export const DashboardDataSchema = z.object({
endpointActivity: EndpointActivityDashboardSchema,
});
endpointActivity: EndpointActivityDashboardSchema,
});
6 changes: 3 additions & 3 deletions atlas-metrics/app/server/domain/dao/endpointActivityBucket.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {z} from "zod";
import {RequestType} from "@server/domain/dao/RequestTypes";
import {z_stringOrDateToDate} from "@lib/zod-utils";
import { z } from "zod";
import { RequestType } from "@server/domain/dao/RequestTypes";
import { z_stringOrDateToDate } from "@lib/zod-utils";

export type EndpointActivityBucketDAO = {
endpoint: string;
Expand Down
23 changes: 14 additions & 9 deletions atlas-metrics/app/server/domain/dao/endpointActivityDashboard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import {z} from "zod";
import {TimeSeriesChartDataItemDAO, TimeSeriesChartDataItemSchema, ChartDataItemDAO, ChartDataItemSchema} from "@server/domain/dao/ChartDataItem";
import { z } from "zod";
import {
TimeSeriesChartDataItemDAO,
TimeSeriesChartDataItemSchema,
ChartDataItemDAO,
ChartDataItemSchema,
} from "@server/domain/dao/ChartDataItem";

export interface EndpointActivityDashboardDAO {
timeSeries: TimeSeriesChartDataItemDAO[];
byType: ChartDataItemDAO[];
byCategory: ChartDataItemDAO[];
timeSeries: TimeSeriesChartDataItemDAO[];
byType: ChartDataItemDAO[];
byCategory: ChartDataItemDAO[];
}

export const EndpointActivityDashboardSchema = z.object({
timeSeries: TimeSeriesChartDataItemSchema.array(),
byType: ChartDataItemSchema.array(),
byCategory: ChartDataItemSchema.array(),
});
timeSeries: TimeSeriesChartDataItemSchema.array(),
byType: ChartDataItemSchema.array(),
byCategory: ChartDataItemSchema.array(),
});
11 changes: 5 additions & 6 deletions atlas-metrics/app/server/domain/service/metrics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { IMetricsRepository } from "@server/domain/repository/metrics.repository
import { EndpointActivityBucketDAO } from "@server/domain/dao/endpointActivityBucket";
import { RequestType } from "@server/domain/dao/RequestTypes";
import { EndpointActivityFullDAO } from "@server/domain/dao/endpointActivity";
import {TimeSeriesChartDataItemDAO} from "@server/domain/dao/ChartDataItem";

export interface IMetricsService {
/**
Expand Down Expand Up @@ -105,11 +104,11 @@ export class MetricsServiceImpl implements IMetricsService {
service,
version,
endpoint,
type,
from,
to,
type,
from,
to,
);
const groupedData = new Map<string, { type: RequestType, date: Date; count: number }>();
const groupedData = new Map<string, { type: RequestType; date: Date; count: number }>();

endpointActivity.forEach(({ endpoint, type, date }) => {
const normalizedDate = new Date(date);
Expand All @@ -118,7 +117,7 @@ export class MetricsServiceImpl implements IMetricsService {
const key = `${endpoint}_${type}_${normalizedDate.toISOString()}`;

if (!groupedData.has(key)) {
groupedData.set(key, {type: type, date: normalizedDate, count: 0 });
groupedData.set(key, { type: type, date: normalizedDate, count: 0 });
}
groupedData.get(key)!.count += 1;
});
Expand Down
2 changes: 1 addition & 1 deletion atlas-metrics/app/server/infrastructure/influx/influx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export class InfluxClient {
public async collectRows(fluxQuery: string) {
try {
return await this.client.getQueryApi(this.org).collectRows(fluxQuery);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (error) {
console.log(error);
throw new Error("Failed to collect rows");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { IMetricsService } from "@server/domain/service/metrics.service";
import {DashboardDataDAO} from "@server/domain/dao/dashboardData";
import {EndpointActivityBucketDAO} from "@server/domain/dao/endpointActivityBucket";
import {TimeSeriesChartDataItemDAO, ChartDataItemDAO} from "@server/domain/dao/ChartDataItem";
import {mapRequestTypeToCategory} from "@server/domain/dao/RequestTypes";
import { DashboardDataDAO } from "@server/domain/dao/dashboardData";
import { EndpointActivityBucketDAO } from "@server/domain/dao/endpointActivityBucket";
import { TimeSeriesChartDataItemDAO, ChartDataItemDAO } from "@server/domain/dao/ChartDataItem";
import { mapRequestTypeToCategory } from "@server/domain/dao/RequestTypes";

export interface IMetricsController {

/**
* Get metrics for the dashboard.
*
Expand All @@ -19,9 +18,19 @@ export interface IMetricsController {
export class MetricsControllerImpl implements IMetricsController {
constructor(private readonly metricsService: IMetricsService) {}

async GetMetricsForDashboard(service?: string, from?: Date, to?: Date) : Promise<DashboardDataDAO> {

const endpointActivityByDay = await this.metricsService.getEndpointActivityByDay(service, undefined, undefined, undefined, from, to);
async GetMetricsForDashboard(
service?: string,
from?: Date,
to?: Date,
): Promise<DashboardDataDAO> {
const endpointActivityByDay = await this.metricsService.getEndpointActivityByDay(
service,
undefined,
undefined,
undefined,
from,
to,
);

const timeSeries = this.transformEndpointActivityToChartData(endpointActivityByDay);
const byType = this.transformEndpointActivityToTypeChartData(endpointActivityByDay);
Expand All @@ -36,7 +45,9 @@ export class MetricsControllerImpl implements IMetricsController {
};
}

private transformEndpointActivityToChartData(buckets: EndpointActivityBucketDAO[]): TimeSeriesChartDataItemDAO[] {
private transformEndpointActivityToChartData(
buckets: EndpointActivityBucketDAO[],
): TimeSeriesChartDataItemDAO[] {
const groupedByDate = new Map<string, TimeSeriesChartDataItemDAO>();

buckets.forEach(({ endpoint, date, count }) => {
Expand All @@ -57,7 +68,9 @@ export class MetricsControllerImpl implements IMetricsController {
return Array.from(groupedByDate.values());
}

private transformEndpointActivityToTypeChartData(buckets: EndpointActivityBucketDAO[]): ChartDataItemDAO[] {
private transformEndpointActivityToTypeChartData(
buckets: EndpointActivityBucketDAO[],
): ChartDataItemDAO[] {
const groupedByType = new Map<string, number>();

buckets.forEach(({ type, count }) => {
Expand All @@ -72,7 +85,9 @@ export class MetricsControllerImpl implements IMetricsController {
return Array.from(groupedByType.entries()).map(([label, value]) => ({ label, value }));
}

private transformEndpointActivityToCategoryChartData(buckets: EndpointActivityBucketDAO[]): ChartDataItemDAO[] {
private transformEndpointActivityToCategoryChartData(
buckets: EndpointActivityBucketDAO[],
): ChartDataItemDAO[] {
const groupedByCategory = new Map<string, number>();

buckets.forEach(({ type, count }) => {
Expand Down

0 comments on commit 26fa319

Please sign in to comment.