Skip to content

feat: show user limit on active users chart #10101

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

Merged
merged 7 commits into from
Oct 9, 2023
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
1 change: 1 addition & 0 deletions site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"canvas": "2.11.0",
"chart.js": "4.4.0",
"chartjs-adapter-date-fns": "3.0.0",
"chartjs-plugin-annotation": "3.0.1",
"chroma-js": "2.4.2",
"color-convert": "2.0.1",
"cron-parser": "4.9.0",
Expand Down
11 changes: 11 additions & 0 deletions site/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions site/src/components/ActiveUserChart/ActiveUserChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import dayjs from "dayjs";
import { FC } from "react";
import { Line } from "react-chartjs-2";
import annotationPlugin from "chartjs-plugin-annotation";

ChartJS.register(
CategoryScale,
Expand All @@ -35,16 +36,21 @@ ChartJS.register(
Title,
Tooltip,
Legend,
annotationPlugin,
);

const USER_LIMIT_DISPLAY_THRESHOLD = 60;

export interface ActiveUserChartProps {
data: { date: string; amount: number }[];
interval: "day" | "week";
userLimit: number | undefined;
}

export const ActiveUserChart: FC<ActiveUserChartProps> = ({
data,
interval,
userLimit,
}) => {
const theme: Theme = useTheme();

Expand All @@ -57,6 +63,24 @@ export const ActiveUserChart: FC<ActiveUserChartProps> = ({
const options: ChartOptions<"line"> = {
responsive: true,
plugins: {
annotation: {
annotations: [
{
type: "line",
scaleID: "y",
display: shouldDisplayUserLimit(userLimit, chartData),
value: userLimit,
borderColor: theme.palette.secondary.contrastText,
borderWidth: 5,
label: {
content: "User limit",
color: theme.palette.primary.contrastText,
display: true,
font: { weight: "normal" },
},
},
],
},
legend: {
display: false,
},
Expand Down Expand Up @@ -127,3 +151,15 @@ export const ActiveUsersTitle = () => {
</Box>
);
};

function shouldDisplayUserLimit(
userLimit: number | undefined,
activeUsers: number[],
): boolean {
if (!userLimit || activeUsers.length === 0) {
return false;
}
return (
Math.max(...activeUsers) >= (userLimit * USER_LIMIT_DISPLAY_THRESHOLD) / 100
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { pageTitle } from "utils/page";
import { GeneralSettingsPageView } from "./GeneralSettingsPageView";
import { useQuery } from "react-query";
import { deploymentDAUs } from "api/queries/deployment";
import { entitlements } from "api/queries/entitlements";

const GeneralSettingsPage: FC = () => {
const { deploymentValues } = useDeploySettings();
const deploymentDAUsQuery = useQuery(deploymentDAUs());
const entitlementsQuery = useQuery(entitlements());

return (
<>
Expand All @@ -19,6 +21,7 @@ const GeneralSettingsPage: FC = () => {
deploymentOptions={deploymentValues.options}
deploymentDAUs={deploymentDAUsQuery.data}
deploymentDAUsError={deploymentDAUsQuery.error}
entitlements={entitlementsQuery.data}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Meta, StoryObj } from "@storybook/react";
import { mockApiError, MockDeploymentDAUResponse } from "testHelpers/entities";
import {
mockApiError,
MockDeploymentDAUResponse,
MockEntitlementsWithUserLimit,
} from "testHelpers/entities";
import { GeneralSettingsPageView } from "./GeneralSettingsPageView";

const meta: Meta<typeof GeneralSettingsPageView> = {
Expand Down Expand Up @@ -44,6 +48,13 @@ type Story = StoryObj<typeof GeneralSettingsPageView>;

export const Page: Story = {};

export const WithUserLimit: Story = {
args: {
deploymentDAUs: MockDeploymentDAUResponse,
entitlements: MockEntitlementsWithUserLimit,
},
};

export const NoDAUs: Story = {
args: {
deploymentDAUs: undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Box from "@mui/material/Box";
import { ClibaseOption, DAUsResponse } from "api/typesGenerated";
import { ClibaseOption, DAUsResponse, Entitlements } from "api/typesGenerated";
import { ErrorAlert } from "components/Alert/ErrorAlert";
import {
ActiveUserChart,
Expand All @@ -16,11 +16,13 @@ export type GeneralSettingsPageViewProps = {
deploymentOptions: ClibaseOption[];
deploymentDAUs?: DAUsResponse;
deploymentDAUsError: unknown;
entitlements: Entitlements | undefined;
};
export const GeneralSettingsPageView = ({
deploymentOptions,
deploymentDAUs,
deploymentDAUsError,
entitlements,
}: GeneralSettingsPageViewProps): JSX.Element => {
return (
<>
Expand All @@ -36,7 +38,15 @@ export const GeneralSettingsPageView = ({
{deploymentDAUs && (
<Box height={200} sx={{ mb: 3 }}>
<ChartSection title={<ActiveUsersTitle />}>
<ActiveUserChart data={deploymentDAUs.entries} interval="day" />
<ActiveUserChart
data={deploymentDAUs.entries}
interval="day"
userLimit={
entitlements?.features.user_limit.enabled
? entitlements?.features.user_limit.limit
: undefined
}
/>
</ChartSection>
</Box>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Meta, StoryObj } from "@storybook/react";
import { TemplateInsightsPageView } from "./TemplateInsightsPage";
import { MockEntitlementsWithUserLimit } from "testHelpers/entities";

const meta: Meta<typeof TemplateInsightsPageView> = {
title: "pages/TemplateInsightsPageView",
Expand Down Expand Up @@ -515,7 +516,7 @@ export const Loaded: Story = {
end_time: "2023-07-25T00:00:00Z",
template_ids: ["0d286645-29aa-4eaf-9b52-cc5d2740c90b"],
interval: "day",
active_users: 11,
active_users: 16,
},
],
},
Expand Down Expand Up @@ -861,3 +862,11 @@ export const Loaded: Story = {
},
},
};

export const LoadedWithUserLimit: Story = {
...Loaded,
args: {
...Loaded.args,
entitlements: MockEntitlementsWithUserLimit,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Helmet } from "react-helmet-async";
import { getTemplatePageTitle } from "../utils";
import { Loader } from "components/Loader/Loader";
import {
Entitlements,
Template,
TemplateAppUsage,
TemplateInsightsResponse,
Expand Down Expand Up @@ -48,6 +49,7 @@ import {
insightsUserLatency,
} from "api/queries/insights";
import { useSearchParams } from "react-router-dom";
import { entitlements } from "api/queries/entitlements";

const DEFAULT_NUMBER_OF_WEEKS = numberOfWeeksOptions[0];

Expand Down Expand Up @@ -75,6 +77,7 @@ export default function TemplateInsightsPage() {
const { data: templateInsights } = useQuery(insightsTemplate(insightsFilter));
const { data: userLatency } = useQuery(insightsUserLatency(commonFilters));
const { data: userActivity } = useQuery(insightsUserActivity(commonFilters));
const { data: entitlementsQuery } = useQuery(entitlements());

return (
<>
Expand Down Expand Up @@ -106,6 +109,7 @@ export default function TemplateInsightsPage() {
userLatency={userLatency}
userActivity={userActivity}
interval={interval}
entitlements={entitlementsQuery}
/>
</>
);
Expand Down Expand Up @@ -146,12 +150,14 @@ export const TemplateInsightsPageView = ({
templateInsights,
userLatency,
userActivity,
entitlements,
controls,
interval,
}: {
templateInsights: TemplateInsightsResponse | undefined;
userLatency: UserLatencyInsightsResponse | undefined;
userActivity: UserActivityInsightsResponse | undefined;
entitlements: Entitlements | undefined;
controls: ReactNode;
interval: InsightsInterval;
}) => {
Expand All @@ -178,6 +184,11 @@ export const TemplateInsightsPageView = ({
<ActiveUsersPanel
sx={{ gridColumn: "span 2" }}
interval={interval}
userLimit={
entitlements?.features.user_limit.enabled
? entitlements?.features.user_limit.limit
: undefined
}
data={templateInsights?.interval_reports}
/>
<UsersLatencyPanel data={userLatency} />
Expand All @@ -198,10 +209,12 @@ export const TemplateInsightsPageView = ({
const ActiveUsersPanel = ({
data,
interval,
userLimit,
...panelProps
}: PanelProps & {
data: TemplateInsightsResponse["interval_reports"] | undefined;
interval: InsightsInterval;
userLimit: number | undefined;
}) => {
return (
<Panel {...panelProps}>
Expand All @@ -216,6 +229,7 @@ const ActiveUsersPanel = ({
{data && data.length > 0 && (
<ActiveUserChart
interval={interval}
userLimit={userLimit}
data={data.map((d) => ({
amount: d.active_users,
date: d.start_time,
Expand Down
22 changes: 19 additions & 3 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export const MockTemplateDAUResponse: TypesGen.DAUsResponse = {
export const MockDeploymentDAUResponse: TypesGen.DAUsResponse = {
tz_hour_offset: 0,
entries: [
{ date: "2022-08-27T00:00:00Z", amount: 1 },
{ date: "2022-08-29T00:00:00Z", amount: 2 },
{ date: "2022-08-30T00:00:00Z", amount: 1 },
{ date: "2022-08-27T00:00:00Z", amount: 10 },
{ date: "2022-08-29T00:00:00Z", amount: 22 },
{ date: "2022-08-30T00:00:00Z", amount: 14 },
],
};
export const MockSessionToken: TypesGen.LoginWithPasswordResponse = {
Expand Down Expand Up @@ -1925,6 +1925,22 @@ export const MockEntitlementsWithScheduling: TypesGen.Entitlements = {
}),
};

export const MockEntitlementsWithUserLimit: TypesGen.Entitlements = {
errors: [],
warnings: [],
has_license: true,
require_telemetry: false,
trial: false,
refreshed_at: "2022-05-20T16:45:57.122Z",
features: withDefaultFeatures({
user_limit: {
enabled: true,
entitlement: "entitled",
limit: 25,
},
}),
};

export const MockExperiments: TypesGen.Experiment[] = ["moons"];

export const MockAuditLog: TypesGen.AuditLog = {
Expand Down