Skip to content

Commit

Permalink
Add weekly signups in admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
dispatchrabbi committed Oct 12, 2024
1 parent b0daf84 commit 30fd8dd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
24 changes: 24 additions & 0 deletions server/api/admin/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,27 @@ GROUP BY "weekNumber"

return res.status(200).send(success(activeUsersByWeek));
}));

statsRouter.get('/weekly-signups',
requireAdminUser,
h(async (req: RequestWithUser, res: ApiResponse<WeeklyStat[]>) =>
{
const results: { weekNumber: string; signups: number }[] = await dbClient.$queryRaw`
SELECT
EXTRACT(ISOYEAR FROM "createdAt") || '-' || lpad(EXTRACT(WEEK FROM "createdAt")::text, 2, '0') AS "weekNumber",
COUNT(DISTINCT "agentId") AS "signups"
FROM public."AuditEvent"
WHERE "eventType" = 'user:signup'
GROUP BY "weekNumber"
;
`;

const now = new Date();
const signupsByWeek = results.map(({ weekNumber, signups }) => {
const weekStart = format(parse(weekNumber, 'R-II', now), 'y-MM-dd');

return { weekStart, count: Number(signups) };
});

return res.status(200).send(success(signupsByWeek));
}));
46 changes: 28 additions & 18 deletions src/components/pages/admin/AdminStatsPage.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { getWeeklyActiveUsers, WeeklyStat } from 'src/lib/api/admin/stats.ts'
import { getWeeklyActiveUsers, getWeeklySignups, WeeklyStat } from 'src/lib/api/admin/stats.ts'
import AdminLayout from 'src/layouts/AdminLayout.vue';
import SectionTitle from 'src/components/layout/SectionTitle.vue';
Expand All @@ -15,31 +15,39 @@ const breadcrumbs: MenuItem[] = [
];
const weeklyActiveUsers = ref<WeeklyStat[]>([]);
const isLoading = ref<boolean>(false);
const errorMessage = ref<string | null>(null);
const loadWeeklyActiveUsers = async function() {
isLoading.value = true;
errorMessage.value = null;
try {
weeklyActiveUsers.value = await getWeeklyActiveUsers();
} catch(err) {
errorMessage.value = err.message;
} finally {
isLoading.value = false;
}
} catch(err) { }
}
const data = computed(() => {
const weeklySignups = ref<WeeklyStat[]>([]);
const loadWeeklySignups = async function() {
try {
weeklySignups.value = await getWeeklySignups();
} catch(err) { }
}
const activeUsersData = computed(() => {
return weeklyActiveUsers.value.map(({ weekStart, count }) => ({
series: 'Weekly Active Users',
date: weekStart,
value: count,
}));
});
onMounted(() => loadWeeklyActiveUsers());
const signupsData = computed(() => {
return weeklySignups.value.map(({ weekStart, count }) => ({
series: 'Weekly Signups',
date: weekStart,
value: count,
}));
});
onMounted(() => {
loadWeeklyActiveUsers();
loadWeeklySignups();
});
</script>

Expand All @@ -48,13 +56,15 @@ onMounted(() => loadWeeklyActiveUsers());
:breadcrumbs="breadcrumbs"
>
<SectionTitle title="Weekly Active Users" />
<div class="w-full">
<div class="max-w-screen-lg">
<PlotWavyLineChart
:data="data"
:data="activeUsersData"
/>
</div>
<div v-if="data.length === 0">
No stats found. Are you sure this thing is on?
<div class="max-w-screen-lg">
<PlotWavyLineChart
:data="signupsData"
/>
</div>
</AdminLayout>
</template>
Expand Down
4 changes: 4 additions & 0 deletions src/lib/api/admin/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ const ENDPOINT = '/api/admin/stats';
export async function getWeeklyActiveUsers() {
return callApiV1<WeeklyStat[]>(ENDPOINT + '/weekly-active-users', 'GET');
}

export async function getWeeklySignups() {
return callApiV1<WeeklyStat[]>(ENDPOINT + '/weekly-signups', 'GET');
}

0 comments on commit 30fd8dd

Please sign in to comment.