Skip to content
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

feat: connection count usage #9294

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
type ChartDatasetType,
getChartLabel,
toChartData as newToChartData,
toConnectionChartData,
} from './chart-functions';
import { periodsRecord, selectablePeriods } from './selectable-periods';

Expand Down Expand Up @@ -182,6 +183,7 @@ const useTrafficStats = (
includedTraffic: number,
chartDataSelection: ChartDataSelection,
) => {
const connectionCountEnabled = useUiFlag('connectionCount');
const { result } = useTrafficSearch(
chartDataSelection.grouping,
toDateRange(chartDataSelection, currentDate),
Expand All @@ -198,7 +200,9 @@ const useTrafficStats = (
}
const traffic = result.data;

const chartData = newToChartData(traffic);
const chartData = connectionCountEnabled
? toConnectionChartData(traffic)
: newToChartData(traffic);
const usageTotal = calculateTotalUsage(traffic);
const overageCost = calculateOverageCost(
usageTotal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
addMonths,
differenceInCalendarDays,
differenceInCalendarMonths,
getDaysInMonth,
parseISO,
} from 'date-fns';
import { formatDay, formatMonth } from './dates';
import type { ChartDataSelection } from './chart-data-selection';
Expand Down Expand Up @@ -40,6 +42,46 @@ export const toChartData = (
return { datasets, labels };
};

export const toConnectionChartData = (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a copy of existing method with minor modifications

traffic: TrafficUsageDataSegmentedCombinedSchema,
): { datasets: ChartDatasetType[]; labels: string[] } => {
const { newRecord, labels } = getLabelsAndRecords(traffic);
const datasets = traffic.apiData
.sort(
(item1, item2) =>
endpointsInfo[item1.apiPath].order -
endpointsInfo[item2.apiPath].order,
)
.map((item) => {
const record = newRecord();
for (const dataPoint of Object.values(item.dataPoints)) {
const date = parseISO(dataPoint.period);
const requestCount = dataPoint.trafficTypes[0].count;

if (traffic.grouping === 'monthly') {
// 1 connections = 7200 * days in month requests per day
const daysInMonth = getDaysInMonth(date);
record[dataPoint.period] =
requestCount / (daysInMonth * 7200);
} else {
// 1 connection = 7200 requests per day
record[dataPoint.period] = requestCount / 7200;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

daily request quota for one connection is 7200

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next PR may change the 7200 to a variant to make it configurable

}
}

const epInfo = endpointsInfo[item.apiPath];

return {
label: epInfo.label,
data: Object.values(record),
backgroundColor: epInfo.color,
hoverBackgroundColor: epInfo.color,
};
});

return { datasets, labels };
};

const getLabelsAndRecords = (
traffic: TrafficUsageDataSegmentedCombinedSchema,
) => {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/interfaces/uiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export type UiFlags = {
frontendHeaderRedesign?: boolean;
dataUsageMultiMonthView?: boolean;
uiGlobalFontSize?: boolean;
connectionCount?: boolean;
};

export interface IVersionInfo {
Expand Down
7 changes: 6 additions & 1 deletion src/lib/types/experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export type IFlagKey =
| 'uniqueSdkTracking'
| 'frontendHeaderRedesign'
| 'dataUsageMultiMonthView'
| 'uiGlobalFontSize';
| 'uiGlobalFontSize'
| 'connectionCount';

export type IFlags = Partial<{ [key in IFlagKey]: boolean | Variant }>;

Expand Down Expand Up @@ -314,6 +315,10 @@ const flags: IFlags = {
process.env.EXPERIMENTAL_UI_GLOBAL_FONT_SIZE_NAME,
false,
),
connectionCount: parseEnvVarBoolean(
process.env.EXPERIMENTAL_CONNECTION_COUNT,
false,
),
};

export const defaultExperimentalOptions: IExperimentalOptions = {
Expand Down
Loading