Skip to content

Commit

Permalink
add index
Browse files Browse the repository at this point in the history
  • Loading branch information
DamirLut committed Apr 24, 2024
1 parent cceb2ee commit a0f7972
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/entities/events.entity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { Entity, Index, PrimaryKey, Property } from '@mikro-orm/core';

import { BotEvents } from '#config/constants';
import { BaseEntity } from '#lib/orm/BaseEntity';

@Entity({ tableName: 'bot_events' })
@Index({ properties: ['guild_id', 'type'] })
export class BotEventsEntity extends BaseEntity {
@PrimaryKey()
id: number;
Expand Down
3 changes: 2 additions & 1 deletion src/entities/guild-setting.entity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { Entity, Index, PrimaryKey, Property } from '@mikro-orm/core';

import { GuildSettings } from '#config/constants';
import { BaseEntity } from '#lib/orm/BaseEntity';

@Entity({ tableName: 'guild_settings' })
@Index({ properties: ['guild_id', 'key'] })
export class GuildSettingEntity extends BaseEntity {
@PrimaryKey()
id: number;
Expand Down
3 changes: 2 additions & 1 deletion src/entities/invite.entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { Entity, Index, PrimaryKey, Property } from '@mikro-orm/core';

import { BaseEntity } from '#lib/orm/BaseEntity';

@Entity({ tableName: 'guild_invites' })
@Index({ properties: ['guild_id'] })
export class InviteEntity extends BaseEntity {
@PrimaryKey()
id: string;
Expand Down
3 changes: 2 additions & 1 deletion src/entities/reaction-weight.entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { Entity, Index, PrimaryKey, Property } from '@mikro-orm/core';

import { BaseEntity } from '#base/lib/orm/BaseEntity';

@Entity({ tableName: 'emoji_weight' })
@Index({ properties: ['guild_id', 'emoji'] })
export class ReactionWeightEntity extends BaseEntity {
@PrimaryKey()
id: number;
Expand Down
3 changes: 2 additions & 1 deletion src/entities/role-reaction.entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { Entity, Index, PrimaryKey, Property } from '@mikro-orm/core';

import { BaseEntity } from '#base/lib/orm/BaseEntity';

@Entity({ tableName: 'role_reactions' })
@Index({ properties: ['guild_id', 'role_id'] })
export class RoleReactionEntity extends BaseEntity {
@PrimaryKey()
id: number;
Expand Down
3 changes: 2 additions & 1 deletion src/entities/role.entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { Entity, Index, PrimaryKey, Property } from '@mikro-orm/core';

import { BaseEntity } from '#lib/orm/BaseEntity';

@Entity({ tableName: 'guild_roles' })
@Index({ properties: ['guild_id', 'role_id'] })
export class RoleEntity extends BaseEntity {
@PrimaryKey()
id: number;
Expand Down
3 changes: 2 additions & 1 deletion src/entities/stats.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { Entity, Index, PrimaryKey, Property } from '@mikro-orm/core';

import { BaseEntity } from '#lib/orm/BaseEntity';

Expand All @@ -9,6 +9,7 @@ export const enum StatsPeriod {
}

@Entity({ tableName: 'stats' })
@Index({ properties: ['guild_id', 'user_id'] })
export class StatsEntity extends BaseEntity {
@PrimaryKey()
id: number;
Expand Down
3 changes: 2 additions & 1 deletion src/entities/user-roles.entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { Entity, Index, PrimaryKey, Property } from '@mikro-orm/core';

import { BaseEntity } from '#lib/orm/BaseEntity';

@Entity({ tableName: 'user_roles' })
@Index({ properties: ['guild_id', 'user_id'] })
export class UserRolesEntity extends BaseEntity {
@PrimaryKey()
id: number;
Expand Down
3 changes: 2 additions & 1 deletion src/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { Entity, Index, PrimaryKey, Property } from '@mikro-orm/core';

import { BaseEntity } from '#lib/orm/BaseEntity';

@Entity({ tableName: 'users' })
@Index({ properties: ['guild_id', 'user_id'] })
export class UserEntity extends BaseEntity {
@PrimaryKey()
id: number;
Expand Down
98 changes: 98 additions & 0 deletions src/lib/orm/migrations/.snapshot-rgdbot_dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@
"name": "bot_events",
"schema": "public",
"indexes": [
{
"keyName": "bot_events_guild_id_type_index",
"columnNames": [
"guild_id",
"type"
],
"composite": true,
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "bot_events_pkey",
"columnNames": [
Expand Down Expand Up @@ -237,6 +248,17 @@
"name": "guild_settings",
"schema": "public",
"indexes": [
{
"keyName": "guild_settings_guild_id_key_index",
"columnNames": [
"guild_id",
"key"
],
"composite": true,
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "guild_settings_pkey",
"columnNames": [
Expand Down Expand Up @@ -327,6 +349,16 @@
"name": "guild_invites",
"schema": "public",
"indexes": [
{
"keyName": "guild_invites_guild_id_index",
"columnNames": [
"guild_id"
],
"composite": false,
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "guild_invites_pkey",
"columnNames": [
Expand Down Expand Up @@ -406,6 +438,17 @@
"name": "emoji_weight",
"schema": "public",
"indexes": [
{
"keyName": "emoji_weight_guild_id_emoji_index",
"columnNames": [
"guild_id",
"emoji"
],
"composite": true,
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "emoji_weight_pkey",
"columnNames": [
Expand Down Expand Up @@ -503,6 +546,17 @@
"name": "guild_roles",
"schema": "public",
"indexes": [
{
"keyName": "guild_roles_guild_id_role_id_index",
"columnNames": [
"guild_id",
"role_id"
],
"composite": true,
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "guild_roles_pkey",
"columnNames": [
Expand Down Expand Up @@ -600,6 +654,17 @@
"name": "role_reactions",
"schema": "public",
"indexes": [
{
"keyName": "role_reactions_guild_id_role_id_index",
"columnNames": [
"guild_id",
"role_id"
],
"composite": true,
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "role_reactions_pkey",
"columnNames": [
Expand Down Expand Up @@ -709,6 +774,17 @@
"name": "stats",
"schema": "public",
"indexes": [
{
"keyName": "stats_guild_id_user_id_index",
"columnNames": [
"guild_id",
"user_id"
],
"composite": true,
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "stats_pkey",
"columnNames": [
Expand Down Expand Up @@ -928,6 +1004,17 @@
"name": "users",
"schema": "public",
"indexes": [
{
"keyName": "users_guild_id_user_id_index",
"columnNames": [
"guild_id",
"user_id"
],
"composite": true,
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "users_pkey",
"columnNames": [
Expand Down Expand Up @@ -1007,6 +1094,17 @@
"name": "user_roles",
"schema": "public",
"indexes": [
{
"keyName": "user_roles_guild_id_user_id_index",
"columnNames": [
"guild_id",
"user_id"
],
"composite": true,
"constraint": false,
"primary": false,
"unique": false
},
{
"keyName": "user_roles_pkey",
"columnNames": [
Expand Down
45 changes: 45 additions & 0 deletions src/lib/orm/migrations/Migration20240424131705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20240424131705 extends Migration {

async up(): Promise<void> {
this.addSql('create index "bot_events_guild_id_type_index" on "bot_events" ("guild_id", "type");');

this.addSql('create index "guild_settings_guild_id_key_index" on "guild_settings" ("guild_id", "key");');

this.addSql('create index "guild_invites_guild_id_index" on "guild_invites" ("guild_id");');

this.addSql('create index "emoji_weight_guild_id_emoji_index" on "emoji_weight" ("guild_id", "emoji");');

this.addSql('create index "guild_roles_guild_id_role_id_index" on "guild_roles" ("guild_id", "role_id");');

this.addSql('create index "role_reactions_guild_id_role_id_index" on "role_reactions" ("guild_id", "role_id");');

this.addSql('create index "stats_guild_id_user_id_index" on "stats" ("guild_id", "user_id");');

this.addSql('create index "users_guild_id_user_id_index" on "users" ("guild_id", "user_id");');

this.addSql('create index "user_roles_guild_id_user_id_index" on "user_roles" ("guild_id", "user_id");');
}

async down(): Promise<void> {
this.addSql('drop index "bot_events_guild_id_type_index";');

this.addSql('drop index "guild_settings_guild_id_key_index";');

this.addSql('drop index "guild_invites_guild_id_index";');

this.addSql('drop index "emoji_weight_guild_id_emoji_index";');

this.addSql('drop index "guild_roles_guild_id_role_id_index";');

this.addSql('drop index "role_reactions_guild_id_role_id_index";');

this.addSql('drop index "stats_guild_id_user_id_index";');

this.addSql('drop index "users_guild_id_user_id_index";');

this.addSql('drop index "user_roles_guild_id_user_id_index";');
}

}
4 changes: 2 additions & 2 deletions src/lib/sapphire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ export function setupLogFile() {
});

container.logger.write = (...args) => {
const [level, message] = args;
const [level, ...message] = args;

const formatter =
container.logger['formats'].get(level) ??
container.logger['formats'].get(LogLevel.None);

const output = formatter
.run(container.logger['preprocess']([message]))
.run(container.logger['preprocess'](message))
.replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
'',
Expand Down
26 changes: 12 additions & 14 deletions src/listeners/members/member-voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,26 @@ export class MemberVoice extends Listener<typeof Events.VoiceStateUpdate> {

const key = 'guild:' + guild.id;
const hasTime = await this.redis.hGet(key, member.id);
if (!hasTime) return;

if (newState.channel) {
if (hasTime) {
if (newState.selfDeaf) {
await this.saveTime(member);
}
return;
}
await this.redis.hSet(key, member.id, Date.now());
} else {
/// left voice
if (!hasTime) return;
if (!newState.channel) {
/// left
await this.saveTime(member);
return;
}

if (newState.selfDeaf) {
await this.saveTime(member);
return;
}

await this.redis.hSet(key, member.id, Date.now());
}

private async saveTime(member: GuildMember) {
const key = 'guild:' + member.guild.id;
const enteredTime = await this.redis.hGet(key, member.id).then(Number);
const elapsedTime = Math.floor(
Math.min(Date.now() - enteredTime, Time.Minute * 10) / 1_000,
);
const elapsedTime = Math.floor((Date.now() - enteredTime) / 1_000);

const user = await UserService.Instance.get(member.guild.id, member.id);
user.voice_time += elapsedTime ?? 0;
Expand Down

0 comments on commit a0f7972

Please sign in to comment.