Skip to content

Commit

Permalink
Refactor code for consistency by standardizing property names and imp…
Browse files Browse the repository at this point in the history
…roving formatting across various components
  • Loading branch information
simlarsen committed Nov 11, 2024
1 parent 86e6bca commit d9d2b61
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 70 deletions.
11 changes: 5 additions & 6 deletions Common/Server/API/BaseAnalyticsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,14 @@ export default class BaseAnalyticsAPI<
) as any;
}

let groupBy: GroupBy<AnalyticsDataModel> | null = req.body["groupBy"] || null;
let groupBy: GroupBy<AnalyticsDataModel> | null =
req.body["groupBy"] || null;

if(groupBy && Object.keys(groupBy).length > 0) {
groupBy = JSONFunctions.deserialize(
groupBy as JSONObject,
) as any;
if (groupBy && Object.keys(groupBy).length > 0) {
groupBy = JSONFunctions.deserialize(groupBy as JSONObject) as any;
}

if(groupBy && Object.keys(groupBy).length === 0) {
if (groupBy && Object.keys(groupBy).length === 0) {
groupBy = null;
}

Expand Down
16 changes: 8 additions & 8 deletions Common/Server/Services/AnalyticsDatabaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ import ModelEventType from "../../Types/Realtime/ModelEventType";
export default class AnalyticsDatabaseService<
TBaseModel extends AnalyticsBaseModel,
> extends BaseService {
public modelType!: { new(): TBaseModel };
public modelType!: { new (): TBaseModel };
public database!: ClickhouseDatabase;
public model!: TBaseModel;
public databaseClient!: ClickhouseClient;
public statementGenerator!: StatementGenerator<TBaseModel>;

public constructor(data: {
modelType: { new(): TBaseModel };
modelType: { new (): TBaseModel };
database?: ClickhouseDatabase | undefined;
}) {
super();
Expand Down Expand Up @@ -240,8 +240,6 @@ export default class AnalyticsDatabaseService<
columns: Array<string>;
} = this.toAggregateStatement(aggregateBy);

debugger;

const dbResult: ExecResult<Stream> = await this.execute(
findStatement.statement,
);
Expand All @@ -261,22 +259,24 @@ export default class AnalyticsDatabaseService<

// convert date column from string to date.

const groupByColumnName: keyof TBaseModel | undefined = aggregateBy.groupBy && Object.keys(aggregateBy.groupBy).length > 0 ? Object.keys(aggregateBy.groupBy)[0] as keyof TBaseModel : undefined;
const groupByColumnName: keyof TBaseModel | undefined =
aggregateBy.groupBy && Object.keys(aggregateBy.groupBy).length > 0
? (Object.keys(aggregateBy.groupBy)[0] as keyof TBaseModel)
: undefined;

for (const item of items) {
if (
!(item as JSONObject)[
aggregateBy.aggregationTimestampColumnName as string
aggregateBy.aggregationTimestampColumnName as string
]
) {
continue;
}


const aggregatedModel: AggregatedModel = {
timestamp: OneUptimeDate.fromString(
(item as JSONObject)[
aggregateBy.aggregationTimestampColumnName as string
aggregateBy.aggregationTimestampColumnName as string
] as string,
),
value: (item as JSONObject)[
Expand Down
9 changes: 5 additions & 4 deletions Common/Server/Utils/AnalyticsDatabase/StatementGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,17 +548,18 @@ export default class StatementGenerator<TBaseModel extends AnalyticsBaseModel> {
`${aggregationMethod}(${aggregateBy.aggregateColumnName.toString()}) as ${aggregateBy.aggregateColumnName.toString()}, date_trunc('${aggregationInterval.toLowerCase()}', toStartOfInterval(${aggregateBy.aggregationTimestampColumnName.toString()}, INTERVAL 1 ${aggregationInterval.toLowerCase()})) as ${aggregateBy.aggregationTimestampColumnName.toString()}`,
);


const columns: Array<string> = [
aggregateBy.aggregateColumnName.toString(),
aggregateBy.aggregationTimestampColumnName.toString(),
];

if(aggregateBy.groupBy && Object.keys(aggregateBy.groupBy).length > 0) {
const groupByStatement: Statement = this.toGroupByStatement(aggregateBy.groupBy);
if (aggregateBy.groupBy && Object.keys(aggregateBy.groupBy).length > 0) {
const groupByStatement: Statement = this.toGroupByStatement(
aggregateBy.groupBy,
);
selectStatement.append(SQL`, `).append(groupByStatement);

// add to columns.
// add to columns.
for (const key in aggregateBy.groupBy) {
columns.push(key);
}
Expand Down
7 changes: 3 additions & 4 deletions Common/Types/Metrics/MetricsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ export default interface MetricsQuery {
aggegationType: MetricsAggregationType;
aggregateBy: Dictionary<boolean>;


// This is used for example for probes.
// To display US probe and EU probe in chart for example.
// This is used for example for probes.
// To display US probe and EU probe in chart for example.
// In this case groupByAttribute is "probeId"
// and attributeValueToLegendMap is { "xx-xx-xx-xx": "US Probe", "yy-yyy-yyy-yy-yy": "EU Probe" }

groupByAttribute?: string | undefined;
attributeValueToLegendMap?: Dictionary<string>;
}
2 changes: 1 addition & 1 deletion Common/UI/Components/Charts/ChartGroup/ChartGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const ChartGroup: FunctionComponent<ComponentProps> = (
{chart.description}
</p>
)}
<LineChart key={index} {...chart.props} syncId={syncId} />
<LineChart key={index} {...chart.props} syncid={syncId} />
</div>
);
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ interface LineChartProps extends React.HTMLAttributes<HTMLDivElement> {
legendPosition?: "left" | "center" | "right";
tooltipCallback?: (tooltipCallbackContent: TooltipProps) => void;
customTooltip?: React.ComponentType<TooltipProps>;
syncId?: string | undefined;
syncid?: string | undefined;
}

const LineChart: React.ForwardRefExoticComponent<
Expand Down Expand Up @@ -692,7 +692,7 @@ const LineChart: React.ForwardRefExoticComponent<
<ResponsiveContainer>
<RechartsLineChart
data={data}
syncId={props.syncId?.toString() || ""}
syncId={props.syncid?.toString() || ""}
onClick={
hasOnValueChange && (activeLegend || activeDot)
? () => {
Expand Down
4 changes: 2 additions & 2 deletions Common/UI/Components/Charts/Line/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface ComponentProps {
}

export interface LineInternalProps extends ComponentProps {
syncId: string;
syncid: string;
}

const LineChartElement: FunctionComponent<LineInternalProps> = (
Expand Down Expand Up @@ -64,7 +64,7 @@ const LineChartElement: FunctionComponent<LineInternalProps> = (
showTooltip={true}
connectNulls={true}
curve={props.curve}
syncId={props.sync ? props.syncId : undefined}
syncid={props.sync ? props.syncid : undefined}
yAxisWidth={60}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion Dashboard/src/Components/Metrics/MetricQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Metric from "Common/Models/AnalyticsModels/Metric";

export interface MetricQueryData {
filterData: FilterData<MetricsQuery>;
groupBy?: GroupBy<Metric> | undefined;
groupBy?: GroupBy<Metric> | undefined;
}

export interface ComponentProps {
Expand Down
2 changes: 1 addition & 1 deletion Dashboard/src/Components/Metrics/MetricQueryConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Button, {
import MetricNameAndUnit from "./Types/MetricNameAndUnit";
import AggregatedModel from "Common/Types/BaseDatabase/AggregatedModel";

export interface ChartSeries {
export interface ChartSeries {
title: string;
}

Expand Down
63 changes: 32 additions & 31 deletions Dashboard/src/Components/Metrics/MetricView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import React, {
useEffect,
useState,
} from "react";
import MetricQueryConfig, { ChartSeries, MetricQueryConfigData } from "./MetricQueryConfig";
import MetricQueryConfig, {
ChartSeries,
MetricQueryConfigData,
} from "./MetricQueryConfig";
import MetricGraphConfig, {
MetricFormulaConfigData,
} from "./MetricFormulaConfig";
Expand Down Expand Up @@ -199,12 +202,9 @@ const MetricView: FunctionComponent<ComponentProps> = (
xAxisAggregationType = XAxisAggregateType.Average;
}

let chartSeries: Array<SeriesPoint> = [

];
const chartSeries: Array<SeriesPoint> = [];

if (queryConfig.getSeries) {

for (const item of metricResults[index]!.data) {
const series: ChartSeries = queryConfig.getSeries(item);
const seriesName: string = series.title;
Expand All @@ -213,42 +213,43 @@ const MetricView: FunctionComponent<ComponentProps> = (

// if it does not exist then create a new series and add the data to it

const existingSeries: SeriesPoint | undefined = chartSeries.find((s: SeriesPoint) => {
return s.seriesName === seriesName;
});
const existingSeries: SeriesPoint | undefined = chartSeries.find(
(s: SeriesPoint) => {
return s.seriesName === seriesName;
},
);

if (existingSeries) {
existingSeries.data.push({
x: OneUptimeDate.fromString(item.timestamp),
y: item.value
y: item.value,
});
} else {
const newSeries: SeriesPoint = {
seriesName: seriesName,
data: [{
x: OneUptimeDate.fromString(item.timestamp),
y: item.value
}]
data: [
{
x: OneUptimeDate.fromString(item.timestamp),
y: item.value,
},
],
};

chartSeries.push(newSeries);
}

}
} else {
chartSeries.push({
seriesName:
queryConfig.metricAliasData.title ||
queryConfig.metricQueryData.filterData.metricName?.toString() ||
"",
data: metricResults[index]!.data.map(
(result: AggregatedModel) => {
return {
x: OneUptimeDate.fromString(result.timestamp),
y: result.value,
};
},
),
data: metricResults[index]!.data.map((result: AggregatedModel) => {
return {
x: OneUptimeDate.fromString(result.timestamp),
y: result.value,
};
}),
});
}

Expand Down Expand Up @@ -365,14 +366,14 @@ const MetricView: FunctionComponent<ComponentProps> = (
const metricAttributesResponse:
| HTTPResponse<JSONObject>
| HTTPErrorResponse = await API.post(
URL.fromString(APP_API_URL.toString()).addRoute(
"/telemetry/metrics/get-attributes",
),
{},
{
...ModelAPI.getCommonHeaders(),
},
);
URL.fromString(APP_API_URL.toString()).addRoute(
"/telemetry/metrics/get-attributes",
),
{},
{
...ModelAPI.getCommonHeaders(),
},
);

if (metricAttributesResponse instanceof HTTPErrorResponse) {
throw metricAttributesResponse;
Expand Down Expand Up @@ -421,7 +422,7 @@ const MetricView: FunctionComponent<ComponentProps> = (
OneUptimeDate.getCurrentDate(),
limit: LIMIT_PER_PROJECT,
skip: 0,
groupBy: queryConfig.metricQueryData.groupBy
groupBy: queryConfig.metricQueryData.groupBy,
},
});

Expand Down
Loading

0 comments on commit d9d2b61

Please sign in to comment.