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: add logsnag logging #24

Merged
merged 8 commits into from
Apr 29, 2024
100 changes: 100 additions & 0 deletions auth.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import Slack from "@auth/core/providers/slack";
import { defineConfig } from "auth-astro";
import { db, like, and, User, Organization } from "astro:db";

import { LogSnag } from "logsnag";

const logsnag = new LogSnag({
token: "f269dd8e8ec57f9a73737e76c5e0024a",
project: "magicsnap",
});

export default defineConfig({
providers: [
Slack({
Expand Down Expand Up @@ -49,6 +56,41 @@ export default defineConfig({
role: "admin",
});

await logsnag.track({
channel: "signups",
event: "signup",
user_id: profile["https://slack.com/user_id"],
description: "User signed up as an admin",
icon: "🚀",
tags: {
team: profile["https://slack.com/team_id"],
role: "admin",
},
});

await logsnag.track({
channel: "actions",
event: "joined_team",
user_id: profile["https://slack.com/user_id"],
description: "User joined a team",
icon: "🤝",
tags: {
team: profile["https://slack.com/team_id"],
role: "admin",
},
});

await logsnag.identify({
user_id: profile["https://slack.com/user_id"],
properties: {
email: profile.email,
name: profile.name,
image: profile.picture,
team: profile["https://slack.com/team_id"],
role: "admin",
},
});

role[0] = { role: "admin" };
} else {
await db.insert(User).values({
Expand All @@ -60,10 +102,68 @@ export default defineConfig({
role: "user",
});

await logsnag.track({
channel: "signups",
event: "signup",
user_id: profile["https://slack.com/user_id"],
description: "User signed up as a user",
icon: "🚀",
tags: {
team: profile["https://slack.com/team_id"],
role: "user",
},
});

await logsnag.track({
channel: "actions",
event: "joined_team",
user_id: profile["https://slack.com/user_id"],
description: "User joined a team",
icon: "🤝",
tags: {
team: profile["https://slack.com/team_id"],
role: "user",
},
});

await logsnag.identify({
user_id: profile["https://slack.com/user_id"],
properties: {
email: profile.email,
name: profile.name,
image: profile.picture,
team: profile["https://slack.com/team_id"],
role: "user",
},
});

role[0] = { role: "user" };
}
} else {
role[0] = { role: "guest" };

await logsnag.track({
channel: "signups",
event: "signup",
user_id: profile["https://slack.com/user_id"],
description: "User signed up as a guest",
icon: "🚀",
tags: {
team: profile["https://slack.com/team_id"],
role: "guest",
},
});

await logsnag.identify({
user_id: profile["https://slack.com/user_id"],
properties: {
email: profile.email,
name: profile.name,
image: profile.picture,
team: profile["https://slack.com/team_id"],
role: "guest",
},
});
}
}

Expand Down
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"astro": "^4.5.10",
"auth-astro": "^4.1.1",
"jwt-decode": "^4.0.0",
"logsnag": "^1.0.0",
"magick.css": "^1.0.5",
"normalize.css": "^8.0.1",
"prettier": "^3.2.5",
Expand Down
13 changes: 13 additions & 0 deletions src/pages/api/remind.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { APIRoute } from "astro"
import { db, User, Organization, Event } from "astro:db";
import { LogSnag } from "logsnag";

const logsnag = new LogSnag({
token: "f269dd8e8ec57f9a73737e76c5e0024a",
project: "magicsnap",
});

export const POST: APIRoute = async ({ request }) => {
// get authorization header
Expand All @@ -20,6 +26,13 @@ export const POST: APIRoute = async ({ request }) => {
return diffHours < 24 && diffHours > 0
})

await logsnag.track({
channel: "api",
event: "reminder-sent",
description: `Sent reminder to ${users.length} users in ${organizations.length} different organizations about ${eventsHappeningToday.length} events happening today`,
icon: "📬",
});

return new Response(JSON.stringify({
ok: true, eventsHappeningToday: eventsHappeningToday, users: users, organizations: organizations
}), { status: 200 })
Expand Down
28 changes: 28 additions & 0 deletions src/pages/dashboard.astro
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ if (!session) {
});
}

import { LogSnag } from "logsnag";

const logsnag = new LogSnag({
token: "f269dd8e8ec57f9a73737e76c5e0024a",
project: "magicsnap",
});

import { db, Event, User } from "astro:db";
import { like } from "astro:db";

Expand Down Expand Up @@ -102,6 +109,13 @@ if (Astro.request.method === "POST") {
statusNotGoing: statusNotGoing.join(","),
})
.where(like(Event.id, eventID));

await logsnag.track({
channel: "actions",
event: "event_status_change",
icon: "📅",
user_id: session.user.id,
});
}
} else if (data.get("delete") != null) {
if (session.user.role != "admin") {
Expand All @@ -110,6 +124,13 @@ if (Astro.request.method === "POST") {
const eventID = data.get("eventID") as string;

await db.delete(Event).where(like(Event.id, eventID));

await logsnag.track({
channel: "actions",
event: "event_delete",
icon: "📅",
user_id: session.user.id,
});
}
else if (data.get("newEvent") != null) {
if (!data.has("name") || !data.has("date") || !data.has("time") || !data.has("location") || !data.has("comments")) {
Expand All @@ -135,6 +156,13 @@ if (Astro.request.method === "POST") {
.join(","),
};
await db.insert(Event).values(event);

await logsnag.track({
channel: "actions",
event: "event_create",
icon: "📅",
user_id: session.user.id,
});
}
} catch (error) {
if (error instanceof Error) {
Expand Down
15 changes: 15 additions & 0 deletions src/pages/join/[joinCode].astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import { Invite } from "astro:db";
import { like } from "astro:db";
import { Organization } from "astro:db";

import { LogSnag } from "logsnag";

const logsnag = new LogSnag({
token: "f269dd8e8ec57f9a73737e76c5e0024a",
project: "magicsnap",
});

type ExtendedSession = {
team: string;
teamName: string;
Expand Down Expand Up @@ -75,6 +82,14 @@ if (session && session.user.role === "guest") {
name: session.teamName,
image: session.teamImage,
});

await logsnag.track({
channel: "join-code",
event: "create-team",
icon: "🪄",
user_id: session.user.id,
notify: true,
});
}
---

Expand Down
14 changes: 14 additions & 0 deletions src/pages/messages.astro
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ if (!session) {
});
}

import { LogSnag } from "logsnag";

const logsnag = new LogSnag({
token: "f269dd8e8ec57f9a73737e76c5e0024a",
project: "magicsnap",
});

import { db, like, and, User, Organization } from "astro:db";
let users = await db.select().from(User).where(like(User.team, session.team));

Expand Down Expand Up @@ -146,6 +153,13 @@ if (Astro.request.method === "POST") {
ok: true,
message: `Message sent successfully to ${userList.length} users.`,
};

await logsnag.track({
channel: "actions",
event: "message_sent",
icon: "📧",
user_id: session.user.id,
});
}
}
} else {
Expand Down
Loading