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

Add statistics #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
298 changes: 298 additions & 0 deletions src/actions/statistics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const openStatisticsModal = async (
client,
trigger_id: string,
isGardener: boolean,
stats,
) => {
var empty = undefined;

return await client.views.open({
trigger_id,
view: {
title: {
type: "plain_text",
text: "Gardening Statistics",
emoji: true,
},
type: "modal",
close: {
type: "plain_text",
text: "Close",
emoji: true,
},
blocks: [
{
type: "divider",
},
{
type: "section",
fields: [
...(isGardener
? [
{
type: "mrkdwn",
text: "*:seedling: Your statistics*",
},
]
: []),
{
type: "mrkdwn",
text: "*:hackclub: Global statistics*",
},
],
},
{
type: "divider",
},
{
type: "section",
fields: [
...(isGardener
? [
{
type: "mrkdwn",
text: `*${stats.user.allTime}* users welcomed`,
},
]
: []),
{
type: "mrkdwn",
text: `*${stats.global.allTime}* users welcomed`,
},
],
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: ":earth_americas: All Time",
},
],
},
{
type: "section",
fields: [
...(isGardener
? [
{
type: "mrkdwn",
text: `*${stats.user.monthly}* users welcomed`,
},
]
: []),
{
type: "mrkdwn",
text: `*${stats.global.monthly}* users welcomed`,
},
],
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: ":calendar: This month (October)",
},
],
},
{
type: "section",
fields: [
...(isGardener
? [
{
type: "mrkdwn",
text: `*${stats.user.lastSevenDays}* users welcomed`,
},
]
: []),
{
type: "mrkdwn",
text: `*${stats.global.lastSevenDays}* users welcomed`,
},
],
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: ":hourglass: Last 7 days",
},
],
},
{
type: "section",
fields: [
...(isGardener
? [
{
type: "mrkdwn",
text: `*${stats.user.weekToDate}* users welcomed`,
},
]
: []),
{
type: "mrkdwn",
text: `*${stats.global.weekToDate}* users welcomed`,
},
],
},
{
type: "context",
elements: [
{
type: "mrkdwn",
text: ":clock4: Week to date (Monday -> Wednesday)",
},
],
},
],
},
});
};

const getStatistics = async (id: string, isGardener: boolean) => {
const today = new Date();
today.setHours(0, 0, 0, 0);
today.setDate(today.getDate() + 1);

const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);

const firstOfMonth = new Date(today);
firstOfMonth.setDate(1);

const firstOfWeek = new Date(today);
firstOfWeek.setDate(firstOfWeek.getDate() - ((firstOfWeek.getDay() + 6) % 7));

const weekAgo = new Date(today);
weekAgo.setDate(weekAgo.getDate() - 7);

const globalStats = await getWelcomeData(
today,
yesterday,
firstOfMonth,
firstOfWeek,
weekAgo,
);
var userStats = {};

if (isGardener) {
userStats = {
daily: globalStats.daily.filter((event) => event.welcomerId === id)
.length,
allTime: globalStats.allTime.filter((event) => event.welcomerId === id)
.length,
monthly: globalStats.monthly.filter((event) => event.welcomerId === id)
.length,
weekToDate: globalStats.weekToDate.filter(
(event) => event.welcomerId === id,
).length,
lastSevenDays: globalStats.lastSevenDays.filter(
(event) => event.welcomerId === id,
).length,
};
}

return {
global: {
allTime: globalStats.allTime.length,
monthly: globalStats.monthly.length,
weekToDate: globalStats.weekToDate.length,
lastSevenDays: globalStats.lastSevenDays.length,
},
user: userStats,
};
};

const getWelcomeData = async (
today: Date,
yesterday: Date,
firstOfMonth: Date,
firstOfWeek: Date,
weekAgo: Date,
) => {
var startTime = performance.now();

const total = await prisma.welcomeEvent.findMany({
where: { status: "completed" },
});

const dailyWelcomed = await prisma.welcomeEvent.findMany({
where: {
completedAt: {
gte: yesterday,
lt: today,
},
status: "completed",
},
});
const monthlyWelcomed = await prisma.welcomeEvent.findMany({
where: {
completedAt: {
gte: firstOfMonth,
lt: today,
},
status: "completed",
},
});
const weekToDateWelcomed = await prisma.welcomeEvent.findMany({
where: {
completedAt: {
gte: firstOfWeek,
lt: today,
},
status: "completed",
},
});
const lastSevenDaysWelcomed = await prisma.welcomeEvent.findMany({
where: {
completedAt: {
gte: weekAgo,
lt: today,
},
status: "completed",
},
});

var endTime = performance.now();
console.log(`Call to doSomething took ${endTime - startTime} milliseconds`);

return {
daily: dailyWelcomed,
allTime: total,
monthly: monthlyWelcomed,
weekToDate: weekToDateWelcomed,
lastSevenDays: lastSevenDaysWelcomed,
};
};

export const handleStatistics = async ({ ack, body, client, action }) => {
try {
await ack();
} catch (error) {
console.error("Error acknowledging request:", error);
return;
}

const users = (
await prisma.user.findMany({
select: { slack: true },
})
).map((user) => user.slack);

const stats = await getStatistics(body.user.id, users.includes(body.user.id));

await openStatisticsModal(
client,
body.trigger_id,
users.includes(body.user.id),
stats,
);
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
handleWelcomerActions,
} from "./actions/welcomerManagment";
import { submissionWelcome } from "./views/submissionWelcome";
import { handleStatistics } from "./actions/statistics";

const createLogger = (): ConsoleLogger => {
const logger = new ConsoleLogger();
Expand Down Expand Up @@ -94,6 +95,7 @@ app.action("lemmewelcomethem", handleLemmeWelcomeThem);
app.action("edit_welcome_template", handleEditTemplate);
app.action("add_welcomer", handleAddWelcomer);
app.action("welcomer_actions", handleWelcomerActions);
app.action("view_statistics", handleStatistics);
app.view("add_welcomer_modal", handleAddWelcomerSubmission);
app.view("edit_prompt", handleEditPromptSubmission);

Expand Down