Skip to content

Commit

Permalink
Merge pull request #192 from Discord-InterChat/logging
Browse files Browse the repository at this point in the history
logging
  • Loading branch information
dev-737 authored Nov 2, 2024
2 parents bda66a4 + ee49e9f commit 59bfa62
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/commands/slash/Main/hub/browse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ export default class BrowseCommand extends HubCommand {
const customId = CustomID.parseCustomId(interaction.customId);
const [hubId] = customId.args;

if (!interaction.memberPermissions.has('ManageMessages')) return await interaction.deferUpdate();
if (!interaction.memberPermissions.has('ManageMessages')) {
await interaction.deferUpdate();
return;
}

const hub = await fetchHub(hubId);
if (!hub) {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/ConnectedListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { connectedList, Prisma } from '@prisma/client';
import db from '#utils/Db.js';
import { cacheData, getCachedData } from '#utils/CacheUtils.js';
import isEmpty from 'lodash/isEmpty.js';
import Logger from '#main/utils/Logger.js';

type whereUniuqeInput = Prisma.connectedListWhereUniqueInput;
type whereInput = Prisma.connectedListWhereInput;
Expand Down Expand Up @@ -31,18 +32,29 @@ export const getHubConnections = async (hubId: string): Promise<connectedList[]>

const fromDb = await db.connectedList.findMany({ where: { hubId } });
const keyValuePairs = fromDb.flatMap((c) => [c.id, JSON.stringify(c)]);

if (keyValuePairs.length === 0) return [];

Logger.debug(`Caching ${fromDb.length} connections for hub ${hubId}`);

await redis.hset(key, keyValuePairs);
await redis.expire(key, 10 * 60 * 1000); // 10 minutes

Logger.debug(`Cached ${fromDb.length} connections for hub ${hubId}`);

return fromDb;
};

export const cacheHubConnection = async (connection: connectedList) => {
const redis = getRedis();
const cached = await redis.hlen(`${RedisKeys.hubConnections}:${connection.hubId}`);

Logger.debug(`Caching connection ${connection.id} for hub ${connection.hubId}`);

if (!cached) {
Logger.debug(`No cached connections for hub ${connection.hubId}, fetching from database`);
await getHubConnections(connection.hubId);
Logger.debug(`Fetched connections for hub ${connection.hubId}`);
return;
}

Expand All @@ -51,6 +63,8 @@ export const cacheHubConnection = async (connection: connectedList) => {
connection.id,
JSON.stringify(connection),
);

Logger.debug(`Cached connection ${connection.id} for hub ${connection.hubId}`);
};

const purgeConnectionCache = async (channelId: string) =>
Expand Down
17 changes: 15 additions & 2 deletions src/utils/network/messageUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Constants, { RedisKeys } from '#main/config/Constants.js';
import Logger from '#main/utils/Logger.js';
import getRedis from '#main/utils/Redis.js';
import type { Message, Snowflake } from 'discord.js';
import isEmpty from 'lodash/isEmpty.js';
Expand All @@ -25,17 +26,23 @@ export const storeMessage = async (originalMsgId: string, messageData: OriginalM
const key = `${RedisKeys.message}:${originalMsgId}`;
const redis = getRedis();

Logger.debug(`Storing message ${originalMsgId} in cache`);

await redis.hset(key, messageData);
await redis.expire(key, 86400); // 1 day in seconds
};

export const getOriginalMessage = async (originalMsgId: string) => {
const key = `${RedisKeys.message}:${originalMsgId}`;
const res = (await getRedis().hgetall(key)) as unknown as OriginalMessage;
const res = await getRedis().hgetall(key);

if (isEmpty(res)) return null;

return res;
return {
...res,
mode: parseInt(res.mode),
timestamp: parseInt(res.timestamp),
} as OriginalMessage;
};

export const addBroadcasts = async (
Expand Down Expand Up @@ -66,11 +73,15 @@ export const addBroadcasts = async (
{ broadcastEntries: [] as string[], reverseLookups: [] as string[] },
);

Logger.debug(`Adding ${broadcasts.length} broadcasts for message ${originalMsgId}`);

// Add all broadcasts to the hash in a single operation
await redis.hset(broadcastsKey, broadcastEntries);
await redis.expire(broadcastsKey, 86400);
await redis.mset(reverseLookups);

Logger.debug(`Added ${broadcasts.length} broadcasts for message ${originalMsgId}`);

reverseLookups
.filter((_, i) => i % 2 === 0)
.forEach(async (key) => {
Expand Down Expand Up @@ -113,7 +124,9 @@ export const findOriginalMessage = async (broadcastedMessageId: string) => {
};

export const storeMessageTimestamp = async (message: Message) => {
Logger.debug(`Storing message timestamp for channel ${message.channelId}`);
await getRedis().hset(`${RedisKeys.msgTimestamp}`, message.channelId, message.createdTimestamp);
Logger.debug(`Stored message timestamp for channel ${message.channelId}`);
};

export const deleteMessageCache = async (originalMsgId: Snowflake) => {
Expand Down

0 comments on commit 59bfa62

Please sign in to comment.