mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
feat: admin monthly active users metric (#1724)
This commit is contained in:
@ -0,0 +1,73 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { DateTime } from 'luxon';
|
||||||
|
import type { TooltipProps } from 'recharts';
|
||||||
|
import { Bar, BarChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts';
|
||||||
|
import type { NameType, ValueType } from 'recharts/types/component/DefaultTooltipContent';
|
||||||
|
|
||||||
|
import type { GetMonthlyActiveUsersResult } from '@documenso/lib/server-only/admin/get-users-stats';
|
||||||
|
|
||||||
|
export type MonthlyActiveUsersChartProps = {
|
||||||
|
className?: string;
|
||||||
|
title: string;
|
||||||
|
cummulative?: boolean;
|
||||||
|
data: GetMonthlyActiveUsersResult;
|
||||||
|
};
|
||||||
|
|
||||||
|
const CustomTooltip = ({ active, payload, label }: TooltipProps<ValueType, NameType>) => {
|
||||||
|
if (active && payload && payload.length) {
|
||||||
|
return (
|
||||||
|
<div className="z-100 w-60 space-y-1 rounded-md border border-solid bg-white p-2 px-3">
|
||||||
|
<p>{label}</p>
|
||||||
|
<p className="text-documenso">
|
||||||
|
{payload[0].name === 'cume_count' ? 'Cumulative MAU' : 'Monthly Active Users'}:{' '}
|
||||||
|
<span className="text-black">{Number(payload[0].value).toLocaleString('en-US')}</span>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const MonthlyActiveUsersChart = ({
|
||||||
|
className,
|
||||||
|
data,
|
||||||
|
title,
|
||||||
|
cummulative = false,
|
||||||
|
}: MonthlyActiveUsersChartProps) => {
|
||||||
|
const formattedData = [...data].reverse().map(({ month, count, cume_count }) => {
|
||||||
|
return {
|
||||||
|
month: DateTime.fromFormat(month, 'yyyy-MM').toFormat('MMM yyyy'),
|
||||||
|
count: Number(count),
|
||||||
|
cume_count: Number(cume_count),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={className}>
|
||||||
|
<div className="border-border flex flex-1 flex-col justify-center rounded-2xl border p-6 pl-2">
|
||||||
|
<div className="mb-6 flex px-4">
|
||||||
|
<h3 className="text-lg font-semibold">{title}</h3>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<ResponsiveContainer width="100%" height={400}>
|
||||||
|
<BarChart data={formattedData}>
|
||||||
|
<XAxis dataKey="month" />
|
||||||
|
<YAxis />
|
||||||
|
|
||||||
|
<Tooltip content={<CustomTooltip />} cursor={{ fill: 'hsl(var(--primary) / 10%)' }} />
|
||||||
|
|
||||||
|
<Bar
|
||||||
|
dataKey={cummulative ? 'cume_count' : 'count'}
|
||||||
|
fill="hsl(var(--primary))"
|
||||||
|
radius={[4, 4, 0, 0]}
|
||||||
|
maxBarSize={60}
|
||||||
|
label={cummulative ? 'Cumulative MAU' : 'Monthly Active Users'}
|
||||||
|
/>
|
||||||
|
</BarChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
@ -18,12 +18,14 @@ import {
|
|||||||
import { getDocumentStats } from '@documenso/lib/server-only/admin/get-documents-stats';
|
import { getDocumentStats } from '@documenso/lib/server-only/admin/get-documents-stats';
|
||||||
import { getRecipientsStats } from '@documenso/lib/server-only/admin/get-recipients-stats';
|
import { getRecipientsStats } from '@documenso/lib/server-only/admin/get-recipients-stats';
|
||||||
import {
|
import {
|
||||||
|
getMonthlyActiveUsers,
|
||||||
getOrganisationsWithSubscriptionsCount,
|
getOrganisationsWithSubscriptionsCount,
|
||||||
getUserWithSignedDocumentMonthlyGrowth,
|
getUserWithSignedDocumentMonthlyGrowth,
|
||||||
getUsersCount,
|
getUsersCount,
|
||||||
} from '@documenso/lib/server-only/admin/get-users-stats';
|
} from '@documenso/lib/server-only/admin/get-users-stats';
|
||||||
import { getSignerConversionMonthly } from '@documenso/lib/server-only/user/get-signer-conversion';
|
import { getSignerConversionMonthly } from '@documenso/lib/server-only/user/get-signer-conversion';
|
||||||
|
|
||||||
|
import { MonthlyActiveUsersChart } from '~/components/general/admin-monthly-active-user-charts';
|
||||||
import { AdminStatsSignerConversionChart } from '~/components/general/admin-stats-signer-conversion-chart';
|
import { AdminStatsSignerConversionChart } from '~/components/general/admin-stats-signer-conversion-chart';
|
||||||
import { AdminStatsUsersWithDocumentsChart } from '~/components/general/admin-stats-users-with-documents';
|
import { AdminStatsUsersWithDocumentsChart } from '~/components/general/admin-stats-users-with-documents';
|
||||||
import { CardMetric } from '~/components/general/metric-card';
|
import { CardMetric } from '~/components/general/metric-card';
|
||||||
@ -39,6 +41,7 @@ export async function loader() {
|
|||||||
recipientStats,
|
recipientStats,
|
||||||
signerConversionMonthly,
|
signerConversionMonthly,
|
||||||
monthlyUsersWithDocuments,
|
monthlyUsersWithDocuments,
|
||||||
|
monthlyActiveUsers,
|
||||||
] = await Promise.all([
|
] = await Promise.all([
|
||||||
getUsersCount(),
|
getUsersCount(),
|
||||||
getOrganisationsWithSubscriptionsCount(),
|
getOrganisationsWithSubscriptionsCount(),
|
||||||
@ -46,6 +49,7 @@ export async function loader() {
|
|||||||
getRecipientsStats(),
|
getRecipientsStats(),
|
||||||
getSignerConversionMonthly(),
|
getSignerConversionMonthly(),
|
||||||
getUserWithSignedDocumentMonthlyGrowth(),
|
getUserWithSignedDocumentMonthlyGrowth(),
|
||||||
|
getMonthlyActiveUsers(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -55,6 +59,7 @@ export async function loader() {
|
|||||||
recipientStats,
|
recipientStats,
|
||||||
signerConversionMonthly,
|
signerConversionMonthly,
|
||||||
monthlyUsersWithDocuments,
|
monthlyUsersWithDocuments,
|
||||||
|
monthlyActiveUsers,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,6 +73,7 @@ export default function AdminStatsPage({ loaderData }: Route.ComponentProps) {
|
|||||||
recipientStats,
|
recipientStats,
|
||||||
signerConversionMonthly,
|
signerConversionMonthly,
|
||||||
monthlyUsersWithDocuments,
|
monthlyUsersWithDocuments,
|
||||||
|
monthlyActiveUsers,
|
||||||
} = loaderData;
|
} = loaderData;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -144,6 +150,14 @@ export default function AdminStatsPage({ loaderData }: Route.ComponentProps) {
|
|||||||
<Trans>Charts</Trans>
|
<Trans>Charts</Trans>
|
||||||
</h3>
|
</h3>
|
||||||
<div className="mt-5 grid grid-cols-2 gap-8">
|
<div className="mt-5 grid grid-cols-2 gap-8">
|
||||||
|
<MonthlyActiveUsersChart title={_(msg`MAU (signed in)`)} data={monthlyActiveUsers} />
|
||||||
|
|
||||||
|
<MonthlyActiveUsersChart
|
||||||
|
title={_(msg`Cumulative MAU (signed in)`)}
|
||||||
|
data={monthlyActiveUsers}
|
||||||
|
cummulative
|
||||||
|
/>
|
||||||
|
|
||||||
<AdminStatsUsersWithDocumentsChart
|
<AdminStatsUsersWithDocumentsChart
|
||||||
data={monthlyUsersWithDocuments}
|
data={monthlyUsersWithDocuments}
|
||||||
title={_(msg`MAU (created document)`)}
|
title={_(msg`MAU (created document)`)}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { SubscriptionStatus } from '@prisma/client';
|
|
||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
|
|
||||||
import { prisma } from '@documenso/prisma';
|
import { kyselyPrisma, prisma, sql } from '@documenso/prisma';
|
||||||
|
import { SubscriptionStatus, UserSecurityAuditLogType } from '@documenso/prisma/client';
|
||||||
|
|
||||||
export const getUsersCount = async () => {
|
export const getUsersCount = async () => {
|
||||||
return await prisma.user.count();
|
return await prisma.user.count();
|
||||||
@ -49,3 +49,37 @@ export const getUserWithSignedDocumentMonthlyGrowth = async () => {
|
|||||||
signed_count: Number(row.signed_count),
|
signed_count: Number(row.signed_count),
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type GetMonthlyActiveUsersResult = Array<{
|
||||||
|
month: string;
|
||||||
|
count: number;
|
||||||
|
cume_count: number;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
export const getMonthlyActiveUsers = async () => {
|
||||||
|
const qb = kyselyPrisma.$kysely
|
||||||
|
.selectFrom('UserSecurityAuditLog')
|
||||||
|
.select(({ fn }) => [
|
||||||
|
fn<Date>('DATE_TRUNC', [sql.lit('MONTH'), 'UserSecurityAuditLog.createdAt']).as('month'),
|
||||||
|
fn.count('userId').distinct().as('count'),
|
||||||
|
fn
|
||||||
|
.sum(fn.count('userId').distinct())
|
||||||
|
.over((ob) =>
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/consistent-type-assertions
|
||||||
|
ob.orderBy(fn('DATE_TRUNC', [sql.lit('MONTH'), 'UserSecurityAuditLog.createdAt']) as any),
|
||||||
|
)
|
||||||
|
.as('cume_count'),
|
||||||
|
])
|
||||||
|
.where(sql`type = ${UserSecurityAuditLogType.SIGN_IN}::"UserSecurityAuditLogType"`)
|
||||||
|
.groupBy(({ fn }) => fn('DATE_TRUNC', [sql.lit('MONTH'), 'UserSecurityAuditLog.createdAt']))
|
||||||
|
.orderBy('month', 'desc')
|
||||||
|
.limit(12);
|
||||||
|
|
||||||
|
const result = await qb.execute();
|
||||||
|
|
||||||
|
return result.map((row) => ({
|
||||||
|
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
|
||||||
|
count: Number(row.count),
|
||||||
|
cume_count: Number(row.cume_count),
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|||||||
@ -1,10 +1,15 @@
|
|||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { Trans } from '@lingui/react/macro';
|
import { Trans, useLingui } from '@lingui/react/macro';
|
||||||
import { useLingui } from '@lingui/react/macro';
|
import {
|
||||||
import { DocumentVisibility, TeamMemberRole } from '@prisma/client';
|
DocumentStatus,
|
||||||
import { DocumentStatus, type Field, type Recipient, SendStatus } from '@prisma/client';
|
DocumentVisibility,
|
||||||
|
type Field,
|
||||||
|
type Recipient,
|
||||||
|
SendStatus,
|
||||||
|
TeamMemberRole,
|
||||||
|
} from '@prisma/client';
|
||||||
import { InfoIcon } from 'lucide-react';
|
import { InfoIcon } from 'lucide-react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { match } from 'ts-pattern';
|
import { match } from 'ts-pattern';
|
||||||
|
|||||||
Reference in New Issue
Block a user