Skip to content

Commit 2723464

Browse files
committed
🔧 Оптимизированы пайплайны: упрощен код для обработки данных в файлах achievements.ts, dynamic.ts, ladder.ts, metrics.ts, ruler.ts, seasons.ts и users.ts
1 parent 7a3b63b commit 2723464

7 files changed

Lines changed: 30 additions & 134 deletions

File tree

app/rep/mongo/pipelines/achievements.ts

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,27 @@ import type { PipelineStage } from "mongoose";
22

33
const tz = "Europe/Moscow";
44

5-
export const pAchTotalPulls = (userId: number): PipelineStage[] => [
6-
{ $match: { user_id: userId } },
7-
{ $count: "count" },
8-
];
5+
export const pAchTotalPulls = (userId: number): PipelineStage[] => [{ $match: { user_id: userId } }, { $count: "count" }];
96

10-
export const pAchTotalSize = (userId: number): PipelineStage[] => [
11-
{ $match: { user_id: userId } },
12-
{ $group: { _id: null, total: { $sum: "$size" } } },
13-
];
7+
export const pAchTotalSize = (userId: number): PipelineStage[] => [{ $match: { user_id: userId } }, { $group: { _id: null, total: { $sum: "$size" } } }];
148

15-
export const pAchSniper30cm = (userId: number): PipelineStage[] => [
16-
{ $match: { user_id: userId, size: 30 } },
17-
{ $count: "count" },
18-
];
9+
export const pAchSniper30cm = (userId: number): PipelineStage[] => [{ $match: { user_id: userId, size: 30 } }, { $count: "count" }];
1910

20-
export const pAchHalfHundred50cm = (userId: number): PipelineStage[] => [
21-
{ $match: { user_id: userId, size: 50 } },
22-
{ $count: "count" },
23-
];
11+
export const pAchHalfHundred50cm = (userId: number): PipelineStage[] => [{ $match: { user_id: userId, size: 50 } }, { $count: "count" }];
2412

25-
export const pAchMaximalist61cm = (userId: number): PipelineStage[] => [
26-
{ $match: { user_id: userId, size: 61 } },
27-
{ $count: "count" },
28-
];
13+
export const pAchMaximalist61cm = (userId: number): PipelineStage[] => [{ $match: { user_id: userId, size: 61 } }, { $count: "count" }];
2914

3015
export const pAchBeautifulNumbers = (userId: number): PipelineStage[] => [
3116
{ $match: { user_id: userId, size: { $in: [11, 22, 33, 44, 55] } } },
3217
{ $group: { _id: "$size" } },
3318
{ $count: "count" },
3419
];
3520

36-
export const pAchRecent10 = (userId: number): PipelineStage[] => [
37-
{ $match: { user_id: userId } },
38-
{ $sort: { requested_at: -1 } },
39-
{ $limit: 10 },
40-
{ $sort: { requested_at: 1 } },
41-
];
21+
export const pAchRecent10 = (userId: number): PipelineStage[] => [{ $match: { user_id: userId } }, { $sort: { requested_at: -1 } }, { $limit: 10 }, { $sort: { requested_at: 1 } }];
4222

43-
export const pAchMaxSize = (userId: number): PipelineStage[] => [
44-
{ $match: { user_id: userId } },
45-
{ $group: { _id: null, max: { $max: "$size" } } },
46-
];
23+
export const pAchMaxSize = (userId: number): PipelineStage[] => [{ $match: { user_id: userId } }, { $group: { _id: null, max: { $max: "$size" } } }];
4724

48-
export const pAchMinSize = (userId: number): PipelineStage[] => [
49-
{ $match: { user_id: userId } },
50-
{ $group: { _id: null, min: { $min: "$size" } } },
51-
];
25+
export const pAchMinSize = (userId: number): PipelineStage[] => [{ $match: { user_id: userId } }, { $group: { _id: null, min: { $min: "$size" } } }];
5226

5327
export const pAchEarlyBird = (userId: number): PipelineStage[] => [
5428
{ $match: { user_id: userId } },
@@ -155,22 +129,11 @@ export const pAchLightning = (userId: number): PipelineStage[] => [
155129
{ $count: "count" },
156130
];
157131

158-
export const pAchLast31 = (userId: number): PipelineStage[] => [
159-
{ $match: { user_id: userId } },
160-
{ $sort: { requested_at: -1 } },
161-
{ $limit: 31 },
162-
];
132+
export const pAchLast31 = (userId: number): PipelineStage[] => [{ $match: { user_id: userId } }, { $sort: { requested_at: -1 } }, { $limit: 31 }];
163133

164-
export const pAchRecent3 = (userId: number): PipelineStage[] => [
165-
{ $match: { user_id: userId } },
166-
{ $sort: { requested_at: -1 } },
167-
{ $limit: 3 },
168-
{ $sort: { requested_at: 1 } },
169-
];
134+
export const pAchRecent3 = (userId: number): PipelineStage[] => [{ $match: { user_id: userId } }, { $sort: { requested_at: -1 } }, { $limit: 3 }, { $sort: { requested_at: 1 } }];
170135

171-
export const pGlobalMaxMin = (): PipelineStage[] => [
172-
{ $group: { _id: null, max: { $max: "$size" }, min: { $min: "$size" } } },
173-
];
136+
export const pGlobalMaxMin = (): PipelineStage[] => [{ $group: { _id: null, max: { $max: "$size" }, min: { $min: "$size" } } }];
174137

175138
export const pCountSeasons = (userId: number): PipelineStage[] => [
176139
{ $group: { _id: null, first_cock_date: { $min: "$requested_at" } } },
@@ -192,10 +155,7 @@ export const pCountSeasons = (userId: number): PipelineStage[] => [
192155
{
193156
$project: {
194157
months_diff: {
195-
$subtract: [
196-
{ $add: [{ $multiply: ["$year", 12] }, "$month"] },
197-
{ $add: [{ $multiply: ["$first_year", 12] }, "$first_month"] },
198-
],
158+
$subtract: [{ $add: [{ $multiply: ["$year", 12] }, "$month"] }, { $add: [{ $multiply: ["$first_year", 12] }, "$first_month"] }],
199159
},
200160
},
201161
},
@@ -209,11 +169,7 @@ export const pCountSeasons = (userId: number): PipelineStage[] => [
209169
{ $count: "count" },
210170
];
211171

212-
export const pCheckTraveler = (userId: number): PipelineStage[] => [
213-
{ $match: { user_id: userId } },
214-
{ $group: { _id: "$size" } },
215-
{ $count: "unique_sizes" },
216-
];
172+
export const pCheckTraveler = (userId: number): PipelineStage[] => [{ $match: { user_id: userId } }, { $group: { _id: "$size" } }, { $count: "unique_sizes" }];
217173

218174
export const pCheckMuscovite = (userId: number, startDate: Date): PipelineStage[] => [
219175
{ $match: { user_id: userId, size: 50, requested_at: { $gte: startDate } } },

app/rep/mongo/pipelines/dynamic.ts

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ export const pIrk = (userId: number): PipelineStage[] => [
2323
{ $lte: ["$global_data.global_total_size", 0] },
2424
0,
2525
{
26-
$divide: [
27-
{ $log10: { $add: [1, "$user_total_size"] } },
28-
{ $log10: { $add: [1, "$global_data.global_total_size"] } },
29-
],
26+
$divide: [{ $log10: { $add: [1, "$user_total_size"] } }, { $log10: { $add: [1, "$global_data.global_total_size"] } }],
3027
},
3128
],
3229
},
@@ -96,10 +93,7 @@ export const pDailyDynamics = (userId: number): PipelineStage[] => [
9693
{
9794
$multiply: [
9895
{
99-
$divide: [
100-
{ $subtract: ["$curr_cock", "$prev_cock"] },
101-
{ $max: ["$prev_cock", 1] },
102-
],
96+
$divide: [{ $subtract: ["$curr_cock", "$prev_cock"] }, { $max: ["$prev_cock", 1] }],
10397
},
10498
100,
10599
],
@@ -125,9 +119,7 @@ export const pUserRecent = (userId: number): PipelineStage[] => [
125119
{ $project: { _id: 0, average: { $round: ["$avg_val", 0] } } },
126120
];
127121

128-
export const pOverallTotal = (): PipelineStage[] => [
129-
{ $group: { _id: null, size: { $sum: "$size" } } },
130-
];
122+
export const pOverallTotal = (): PipelineStage[] => [{ $group: { _id: null, size: { $sum: "$size" } } }];
131123

132124
export const pOverallRecent = (): PipelineStage[] => [
133125
{
@@ -156,10 +148,7 @@ export const pOverallRecent = (): PipelineStage[] => [
156148
{ $project: { _id: 0, median: 1, average: { $round: ["$average", 0] } } },
157149
];
158150

159-
export const pUniqueUsers = (): PipelineStage[] => [
160-
{ $group: { _id: "$user_id" } },
161-
{ $count: "count" },
162-
];
151+
export const pUniqueUsers = (): PipelineStage[] => [{ $group: { _id: "$user_id" } }, { $count: "count" }];
163152

164153
export const pDistribution = (): PipelineStage[] => [
165154
{
@@ -190,18 +179,10 @@ export const pDistribution = (): PipelineStage[] => [
190179
$project: {
191180
_id: 0,
192181
huge: {
193-
$cond: [
194-
{ $eq: ["$total", 0] },
195-
0,
196-
{ $multiply: [{ $divide: ["$huge", "$total"] }, 100] },
197-
],
182+
$cond: [{ $eq: ["$total", 0] }, 0, { $multiply: [{ $divide: ["$huge", "$total"] }, 100] }],
198183
},
199184
little: {
200-
$cond: [
201-
{ $eq: ["$total", 0] },
202-
0,
203-
{ $multiply: [{ $divide: ["$little", "$total"] }, 100] },
204-
],
185+
$cond: [{ $eq: ["$total", 0] }, 0, { $multiply: [{ $divide: ["$little", "$total"] }, 100] }],
205186
},
206187
},
207188
},
@@ -228,11 +209,7 @@ export const pOverallGrowthSpeed = (): PipelineStage[] => [
228209
growth_speed: {
229210
$round: [
230211
{
231-
$cond: [
232-
{ $gt: ["$count", 0] },
233-
{ $divide: [{ $sum: "$daily_totals" }, "$count"] },
234-
0,
235-
],
212+
$cond: [{ $gt: ["$count", 0] }, { $divide: [{ $sum: "$daily_totals" }, "$count"] }, 0],
236213
},
237214
1,
238215
],
@@ -264,14 +241,9 @@ export const pUserRecord = (userId: number): PipelineStage[] => [
264241
{ $project: { _id: 0, requested_at: 1, total: "$size" } },
265242
];
266243

267-
export const pTotalCocksCount = (): PipelineStage[] => [
268-
{ $count: "total_count" },
269-
];
244+
export const pTotalCocksCount = (): PipelineStage[] => [{ $count: "total_count" }];
270245

271-
export const pUserCocksCount = (userId: number): PipelineStage[] => [
272-
{ $match: { user_id: userId } },
273-
{ $count: "user_count" },
274-
];
246+
export const pUserCocksCount = (userId: number): PipelineStage[] => [{ $match: { user_id: userId } }, { $count: "user_count" }];
275247

276248
export const pLuck = (userId: number): PipelineStage[] => [
277249
{ $match: { user_id: userId } },
@@ -358,11 +330,7 @@ export const pUserGrowthSpeed = (userId: number): PipelineStage[] => [
358330
growth_speed: {
359331
$round: [
360332
{
361-
$cond: [
362-
{ $gt: ["$count", 0] },
363-
{ $divide: [{ $sum: "$daily_sizes" }, "$count"] },
364-
0,
365-
],
333+
$cond: [{ $gt: ["$count", 0] }, { $divide: [{ $sum: "$daily_sizes" }, "$count"] }, 0],
366334
},
367335
1,
368336
],

app/rep/mongo/pipelines/ladder.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ export const pTopUsersBySize = (limit = 13): PipelineStage[] => [
66
{ $limit: limit },
77
];
88

9-
export const pTotalCockersCount = (): PipelineStage[] => [
10-
{ $group: { _id: "$user_id" } },
11-
{ $count: "total" },
12-
];
9+
export const pTotalCockersCount = (): PipelineStage[] => [{ $group: { _id: "$user_id" } }, { $count: "total" }];
1310

1411
export const pUserPositionInLadder = (userId: number): PipelineStage[] => [
1512
{ $group: { _id: "$user_id", total_size: { $sum: "$size" } } },
@@ -23,16 +20,10 @@ export const pUserPositionInLadder = (userId: number): PipelineStage[] => [
2320
export const pNeighborhoodInLadder = (position: number): PipelineStage[] => {
2421
const skip = Math.max(position - 2, 0);
2522

26-
return [
27-
{ $group: { _id: "$user_id", total_size: { $sum: "$size" }, nickname: { $first: "$nickname" } } },
28-
{ $sort: { total_size: -1 } },
29-
{ $skip: skip },
30-
{ $limit: 3 },
31-
];
23+
return [{ $group: { _id: "$user_id", total_size: { $sum: "$size" }, nickname: { $first: "$nickname" } } }, { $sort: { total_size: -1 } }, { $skip: skip }, { $limit: 3 }];
3224
};
3325

3426
export const pUserTotalSize = (userId: number): PipelineStage[] => [
3527
{ $match: { user_id: userId } },
3628
{ $group: { _id: "$user_id", total_size: { $sum: "$size" }, nickname: { $first: "$nickname" } } },
3729
];
38-

app/rep/mongo/pipelines/metrics.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import type { PipelineStage } from "mongoose";
22

3-
export const pActiveUsersSince = (since: Date): PipelineStage[] => [
4-
{ $match: { requested_at: { $gte: since } } },
5-
{ $group: { _id: "$user_id" } },
6-
{ $count: "total" },
7-
];
3+
export const pActiveUsersSince = (since: Date): PipelineStage[] => [{ $match: { requested_at: { $gte: since } } }, { $group: { _id: "$user_id" } }, { $count: "total" }];
84

95
export const pSizeDistribution = (): PipelineStage[] => [
106
{

app/rep/mongo/pipelines/ruler.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ export const pRulerLeaders = (startDate: Date, endDate: Date, limit = 13): Pipel
1919
{ $limit: limit },
2020
];
2121

22-
export const pRulerParticipants = (startDate: Date, endDate: Date): PipelineStage[] => [
23-
...pRulerBase(startDate, endDate),
24-
{ $count: "total" },
25-
];
22+
export const pRulerParticipants = (startDate: Date, endDate: Date): PipelineStage[] => [...pRulerBase(startDate, endDate), { $count: "total" }];
2623

2724
export const pRulerUserPosition = (userId: number, startDate: Date, endDate: Date): PipelineStage[] => [
2825
...pRulerBase(startDate, endDate),
@@ -36,10 +33,5 @@ export const pRulerUserPosition = (userId: number, startDate: Date, endDate: Dat
3633
export const pRulerNeighborhood = (position: number, startDate: Date, endDate: Date): PipelineStage[] => {
3734
const skip = Math.max(position - 2, 0);
3835

39-
return [
40-
...pRulerBase(startDate, endDate),
41-
{ $sort: { size: -1, requested_at: 1 } },
42-
{ $skip: skip },
43-
{ $limit: 3 },
44-
];
36+
return [...pRulerBase(startDate, endDate), { $sort: { size: -1, requested_at: 1 } }, { $skip: skip }, { $limit: 3 }];
4537
};

app/rep/mongo/pipelines/seasons.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,4 @@ export const pAllUsersInSeason = (startDate: Date, endDate: Date): PipelineStage
4848
{ $sort: { total_size: -1 } },
4949
];
5050

51-
export const pFirstCockDate = (): PipelineStage[] => [
52-
{ $sort: { requested_at: 1 } },
53-
{ $limit: 1 },
54-
{ $project: { _id: 0, first_date: "$requested_at" } },
55-
];
51+
export const pFirstCockDate = (): PipelineStage[] => [{ $sort: { requested_at: 1 } }, { $limit: 1 }, { $project: { _id: 0, first_date: "$requested_at" } }];

app/rep/mongo/pipelines/users.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import type { PipelineStage } from "mongoose";
22

3-
export const pUserProfile = (userId: number): PipelineStage[] => [
4-
{ $match: { user_id: userId } },
5-
{ $project: { _id: 0, user_id: 1, username: 1, is_hidden: 1, updated_at: 1 } },
6-
];
3+
export const pUserProfile = (userId: number): PipelineStage[] => [{ $match: { user_id: userId } }, { $project: { _id: 0, user_id: 1, username: 1, is_hidden: 1, updated_at: 1 } }];
74

85
export const pUsersByIds = (userIds: number[]): PipelineStage[] => [
96
{ $match: { user_id: { $in: userIds } } },

0 commit comments

Comments
 (0)