Skip to content

Commit 7119195

Browse files
committed
stats-updater: Fix timeout, remove deprecated fields
stats-updater,api,web,whattheduck: Make notation nullable instead of setting -1
1 parent f758dba commit 7119195

File tree

9 files changed

+91
-103
lines changed

9 files changed

+91
-103
lines changed

apps/stats-updater/docker-compose-dev.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

apps/stats-updater/index.ts

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
Prisma,
1313
} from "~prisma-schemas/schemas/coa";
1414
import { prismaClient as prismaCoa } from "~prisma-schemas/schemas/coa/client";
15-
import type { authorUser } from "~prisma-schemas/schemas/dm";
15+
import type { authorUser, issue as dmIssue } from "~prisma-schemas/schemas/dm";
1616
import { prismaClient as prismaDm } from "~prisma-schemas/schemas/dm/client";
1717
import type { authorStory } from "~prisma-schemas/schemas/dm_stats";
1818
import { prismaClient as prismaDmStats } from "~prisma-schemas/schemas/dm_stats/client";
@@ -109,6 +109,17 @@ const runQuery = async (sql: string) => {
109109
const runQueryFile = async (dbName: string, sqlFile: string) =>
110110
runQuery(`USE ${dbName};` + readFileSync(sqlFile).toString());
111111

112+
// Direct connection for heavy SQL (avoids Prisma adapter issues). Use ANALYZE after bulk insert — OPTIMIZE rebuilds can run long and close the socket.
113+
const runQueryOnDirectConnection = async (sql: string, dbName?: string) => {
114+
if (dbName) {
115+
await connection.query(`USE ${dbName}`);
116+
}
117+
await connection.query(
118+
"SET SESSION net_read_timeout = 28800, net_write_timeout = 28800",
119+
);
120+
await connection.query(sql);
121+
};
122+
112123
connect().then(async () => {
113124
await runQuery(`DROP DATABASE IF EXISTS ${DATABASE_NAME_DM_STATS_NEW}`);
114125
await runQuery(`CREATE DATABASE ${DATABASE_NAME_DM_STATS_NEW}`);
@@ -132,21 +143,23 @@ connect().then(async () => {
132143
});
133144

134145
await prismaDmStats.issueSimple.createMany({
135-
data: await prismaDm.issue.findMany({
136-
distinct: ["issuecode", "userId"],
137-
select: {
138-
issuecode: true,
139-
userId: true,
140-
},
141-
where: {
142-
issuecode: {
143-
not: null,
146+
data: (
147+
await prismaDm.issue.findMany({
148+
distinct: ["issuecode", "userId"],
149+
select: {
150+
issuecode: true,
151+
userId: true,
144152
},
145-
userId: {
146-
in: userIdsWithPersoncodes,
153+
where: {
154+
issuecode: {
155+
not: null,
156+
},
157+
userId: {
158+
in: userIdsWithPersoncodes,
159+
},
147160
},
148-
},
149-
}),
161+
})
162+
).map((issue) => issue as dmIssue & { issuecode: string }),
150163
});
151164

152165
console.log("Creating storyIssue entries");
@@ -208,27 +221,39 @@ connect().then(async () => {
208221
await prismaDmStats.$executeRaw`OPTIMIZE TABLE utilisateurs_histoires_manquantes`;
209222

210223
console.log("Creating missingIssueForUser entries");
211-
await prismaDmStats.$executeRaw`
212-
insert into utilisateurs_publications_manquantes(ID_User, personcode, storycode, issuecode, Notation)
224+
await runQueryOnDirectConnection(
225+
`insert into utilisateurs_publications_manquantes(ID_User, personcode, storycode, issuecode, Notation)
213226
select distinct u_h_m.ID_User AS userId,
214227
u_h_m.personcode,
215228
u_h_m.storycode,
216229
h_p.issuecode,
217230
a_p.Notation AS notation
218231
from utilisateurs_histoires_manquantes u_h_m
219232
inner join histoires_publications h_p using (storycode)
220-
inner join auteurs_pseudos a_p on a_p.ID_User = u_h_m.ID_User and u_h_m.personcode = a_p.NomAuteurAbrege`;
233+
inner join auteurs_pseudos a_p on a_p.ID_User = u_h_m.ID_User and u_h_m.personcode = a_p.NomAuteurAbrege`,
234+
DATABASE_NAME_DM_STATS_NEW,
235+
);
221236

222-
await prismaDmStats.$executeRaw`OPTIMIZE TABLE utilisateurs_publications_manquantes`;
237+
console.log("ANALYZING TABLE utilisateurs_publications_manquantes");
238+
await runQueryOnDirectConnection(
239+
"ANALYZE TABLE utilisateurs_publications_manquantes",
240+
DATABASE_NAME_DM_STATS_NEW,
241+
);
223242

224243
console.log("Creating suggestedIssueForUser entries");
225-
await prismaDmStats.$executeRaw`
226-
insert into utilisateurs_publications_suggerees(ID_User, issuecode, oldestdate, Score)
244+
await runQueryOnDirectConnection(
245+
`insert into utilisateurs_publications_suggerees(ID_User, issuecode, oldestdate, Score)
227246
select ID_User AS userId, issuecode, '0000-00-00', sum(Notation) AS score
228247
from utilisateurs_publications_manquantes
229-
group by ID_User, issuecode`;
248+
group by ID_User, issuecode`,
249+
DATABASE_NAME_DM_STATS_NEW,
250+
);
230251

231-
await prismaDmStats.$executeRaw`OPTIMIZE TABLE utilisateurs_publications_suggerees`;
252+
console.log("ANALYZING TABLE utilisateurs_publications_suggerees");
253+
await runQueryOnDirectConnection(
254+
"ANALYZE TABLE utilisateurs_publications_suggerees",
255+
DATABASE_NAME_DM_STATS_NEW,
256+
);
232257

233258
await runQuery(`DROP DATABASE IF EXISTS ${DATABASE_NAME_DM_STATS}_old`);
234259
await runQuery(`CREATE DATABASE ${DATABASE_NAME_DM_STATS}_old`);

apps/web/src/components/AuthorList.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
</b-col>
4343
<b-col lg="2">
4444
<StarRating
45-
v-model:rating="author.notation"
45+
:rating="author.notation ?? 5"
4646
:readonly="false"
4747
:max-rating="10"
4848
@update:rating="updateRating(author)"

apps/web/src/pages/expand/suggestions.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const countryNamesWithAllCountriesOption = $computed(
9494
],
9595
);
9696
const watchedAuthorsWithNotation = $computed(() =>
97-
ratings.value?.filter(({ notation }) => notation > 0),
97+
ratings.value?.filter(({ notation }) => notation !== null),
9898
);
9999
100100
watch(

apps/whattheduck/src/views/Authors.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</ion-col>
3232
<ion-col size="5">
3333
<StarRating
34-
v-model:rating="author.notation"
34+
:rating="author.notation ?? 5"
3535
:readonly="isOffline"
3636
:max-rating="10"
3737
@update:rating="updateRating(author)"

packages/api/services/collection/watched-authors/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default ({ _socket }: UserServices) => ({
2929

3030
addWatchedAuthor: async (personcode: string) => {
3131
try {
32-
await upsertAuthorUser(personcode, _socket.data.user.id);
32+
await upsertAuthorUser(personcode, _socket.data.user.id, null);
3333
} catch (e) {
3434
console.log(e);
3535
return { error: "Error", errorDetails: (e as Error).message };
@@ -59,7 +59,7 @@ export default ({ _socket }: UserServices) => ({
5959
const upsertAuthorUser = async (
6060
personcode: string,
6161
userId: number,
62-
notation?: number,
62+
notation: number | null,
6363
) => {
6464
const criteria = {
6565
personcode,
@@ -84,7 +84,7 @@ const upsertAuthorUser = async (
8484
await prismaDm.authorUser.create({
8585
data: {
8686
...criteria,
87-
notation: 5,
87+
notation,
8888
},
8989
});
9090
}

packages/prisma-schemas/schemas/dm/schema.prisma

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ model authorUser {
4343
id Int @id @default(autoincrement()) @map("ID")
4444
personcode String @map("NomAuteurAbrege") @db.VarChar(79)
4545
userId Int @map("ID_user")
46-
notation Int @default(-1) @map("Notation")
46+
notation Int? @map("Notation") @db.UnsignedTinyInt
4747
4848
@@unique([userId, personcode], map: "auteurs_pseudos_uindex")
49+
@@index([userId], map: "auteurs_pseudos_ID_user_index")
4950
@@map("auteurs_pseudos")
5051
}
5152

packages/prisma-schemas/schemas/dm_stats/migrations/0_init/migration.sql

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@ CREATE TABLE `auteurs_histoires` (
1111

1212
-- CreateTable
1313
CREATE TABLE `auteurs_pseudos` (
14+
`ID` INTEGER NOT NULL AUTO_INCREMENT,
1415
`NomAuteurAbrege` VARCHAR(79) NOT NULL,
15-
`ID_user` INTEGER NOT NULL,
16-
`Notation` INTEGER NOT NULL DEFAULT -1,
16+
`ID_User` INTEGER NOT NULL,
17+
`Notation` TINYINT UNSIGNED NULL,
1718

18-
INDEX `auteurs_pseudos_ID_user_index`(`ID_user`),
19-
UNIQUE INDEX `auteurs_pseudos_uindex`(`ID_user`, `NomAuteurAbrege`)
19+
INDEX `auteurs_pseudos_ID_user_index`(`ID_User`),
20+
UNIQUE INDEX `auteurs_pseudos_uindex`(`ID_User`, `NomAuteurAbrege`),
21+
PRIMARY KEY (`ID`)
2022
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2123

2224
-- CreateTable
2325
CREATE TABLE `numeros_simple` (
2426
`ID` INTEGER NOT NULL AUTO_INCREMENT,
2527
`ID_Utilisateur` INTEGER NOT NULL,
26-
`issuecode` VARCHAR(25) NULL DEFAULT (concat(convert(`Pays` using utf8mb3),'/',convert(`Magazine` using utf8mb3),' ',`Numero`)),
28+
`issuecode` VARCHAR(25) NOT NULL,
2729

28-
INDEX `Utilisateur`(`ID_Utilisateur`),
29-
INDEX `issuecode`(`issuecode`),
30-
INDEX `issuecode_user`(`issuecode`, `ID_Utilisateur`),
30+
INDEX `ID_Utilisateur`(`ID_Utilisateur`),
31+
INDEX `user_issue`(`issuecode`),
32+
UNIQUE INDEX `unique_index`(`ID_Utilisateur`, `issuecode`),
3133
PRIMARY KEY (`ID`)
3234
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
3335

3436
-- CreateTable
3537
CREATE TABLE `histoires_publications` (
3638
`ID` INTEGER NOT NULL AUTO_INCREMENT,
3739
`storycode` VARCHAR(19) NOT NULL,
38-
`publicationcode` VARCHAR(12) NULL,
39-
`issuenumber` VARCHAR(12) NULL,
4040
`issuecode` VARCHAR(25) NOT NULL,
4141
`oldestdate` VARCHAR(10) NULL,
4242

@@ -64,35 +64,27 @@ CREATE TABLE `utilisateurs_publications_manquantes` (
6464
`ID_User` INTEGER NOT NULL,
6565
`personcode` VARCHAR(79) NOT NULL,
6666
`storycode` VARCHAR(19) NOT NULL,
67-
`publicationcode` VARCHAR(12) NULL,
68-
`issuenumber` VARCHAR(12) NULL,
6967
`issuecode` VARCHAR(25) NOT NULL,
7068
`oldestdate` VARCHAR(10) NULL,
71-
`Notation` TINYINT UNSIGNED NOT NULL,
69+
`Notation` TINYINT UNSIGNED NULL,
7270

73-
INDEX `missing_user_issue`(`ID_User`, `publicationcode`, `issuenumber`),
74-
INDEX `suggested`(`ID_User`, `publicationcode`, `issuenumber`, `oldestdate`),
71+
INDEX `missing_user_issue`(`ID_User`, `issuecode`),
72+
INDEX `suggested`(`ID_User`, `issuecode`, `oldestdate`),
7573
INDEX `user_stories`(`ID_User`, `personcode`, `storycode`),
76-
INDEX `missing_user_issue_issuecode`(`ID_User`, `issuecode`),
77-
INDEX `suggested_issuecode`(`ID_User`, `issuecode`, `oldestdate`),
78-
UNIQUE INDEX `unique_index`(`ID_User`, `personcode`, `storycode`, `publicationcode`, `issuenumber`),
79-
UNIQUE INDEX `unique_index_issuecode`(`ID_User`, `personcode`, `storycode`, `issuecode`),
74+
UNIQUE INDEX `unique_index`(`ID_User`, `personcode`, `storycode`, `issuecode`),
8075
PRIMARY KEY (`ID`)
8176
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
8277

8378
-- CreateTable
8479
CREATE TABLE `utilisateurs_publications_suggerees` (
8580
`ID` INTEGER NOT NULL AUTO_INCREMENT,
8681
`ID_User` INTEGER NOT NULL,
87-
`publicationcode` VARCHAR(12) NULL,
88-
`issuenumber` VARCHAR(12) NULL,
8982
`issuecode` VARCHAR(25) NOT NULL,
9083
`oldestdate` VARCHAR(10) NULL,
9184
`Score` INTEGER NOT NULL,
9285

9386
INDEX `suggested_issue_user`(`ID_User`),
94-
UNIQUE INDEX `suggested_issue_for_user`(`ID_User`, `publicationcode`, `issuenumber`),
95-
UNIQUE INDEX `suggested_issue_for_user_issuecode`(`ID_User`, `issuecode`),
87+
UNIQUE INDEX `suggested_issue_for_user`(`ID_User`, `issuecode`),
9688
PRIMARY KEY (`ID`)
9789
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
9890

packages/prisma-schemas/schemas/dm_stats/schema.prisma

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ model authorStory {
1818
}
1919

2020
model authorUser {
21+
id Int @id @default(autoincrement()) @map("ID")
2122
userId Int @map("ID_User")
2223
personcode String @map("NomAuteurAbrege") @db.VarChar(79)
23-
notation Int? @map("Notation") @db.TinyInt
24+
notation Int? @map("Notation") @db.UnsignedTinyInt
2425
25-
@@id([userId, personcode])
26+
@@unique([userId, personcode], map: "auteurs_pseudos_uindex")
27+
@@index([userId], map: "auteurs_pseudos_ID_user_index")
2628
@@map("auteurs_pseudos")
2729
}
2830

2931
model issueSimple {
30-
userId Int @map("ID_Utilisateur")
31-
Publicationcode String @db.VarChar(12)
32-
Numero String @db.VarChar(12)
32+
id Int @id @default(autoincrement()) @map("ID")
33+
userId Int @map("ID_Utilisateur")
34+
issuecode String @db.VarChar(25)
3335
34-
@@id([userId, Publicationcode, Numero])
36+
@@unique([userId, issuecode], map: "unique_index")
3537
@@index([userId], map: "ID_Utilisateur")
36-
@@index([Publicationcode, Numero], map: "user_issue")
38+
@@index([issuecode], map: "user_issue")
3739
@@map("numeros_simple")
3840
}
3941

@@ -61,17 +63,13 @@ model missingStoryForUser {
6163
}
6264

6365
model missingIssueForUser {
64-
ID Int @id @default(autoincrement())
65-
userId Int @map("ID_User")
66-
personcode String @db.VarChar(79)
67-
storycode String @db.VarChar(19)
68-
/// @deprecated
69-
publicationcode String? @db.VarChar(12)
70-
/// @deprecated
71-
issuenumber String? @db.VarChar(12)
72-
issuecode String @db.VarChar(25)
73-
oldestdate String? @db.VarChar(10)
74-
notation Int @map("Notation") @db.UnsignedTinyInt
66+
id Int @id @default(autoincrement()) @map("ID")
67+
userId Int @map("ID_User")
68+
personcode String @db.VarChar(79)
69+
storycode String @db.VarChar(19)
70+
issuecode String @db.VarChar(25)
71+
oldestdate String? @db.VarChar(10)
72+
notation Int? @map("Notation") @db.UnsignedTinyInt
7573
7674
@@unique([userId, personcode, storycode, issuecode], map: "unique_index")
7775
@@index([userId, issuecode], map: "missing_user_issue")
@@ -81,15 +79,11 @@ model missingIssueForUser {
8179
}
8280

8381
model suggestedIssueForUser {
84-
ID Int @id @default(autoincrement())
85-
userId Int @map("ID_User")
86-
/// @deprecated
87-
publicationcode String? @db.VarChar(12)
88-
/// @deprecated
89-
issuenumber String? @db.VarChar(12)
90-
issuecode String @db.VarChar(25)
91-
oldestdate String? @db.VarChar(10)
92-
score Int @map("Score")
82+
id Int @id @default(autoincrement()) @map("ID")
83+
userId Int @map("ID_User")
84+
issuecode String @db.VarChar(25)
85+
oldestdate String? @db.VarChar(10)
86+
score Int @map("Score")
9387
9488
@@unique([userId, issuecode], map: "suggested_issue_for_user")
9589
@@index([userId], map: "suggested_issue_user")

0 commit comments

Comments
 (0)