Skip to content

Commit 376d688

Browse files
committed
refactor: simplify app statistics retrieval in HwidUserDevicesRepository
- Replaced complex CASE statements with SPLIT_PART for user agent parsing.
1 parent 9e67d70 commit 376d688

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

src/modules/hwid-user-devices/repositories/hwid-user-devices.repository.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -221,29 +221,11 @@ export class HwidUserDevicesRepository
221221
const appStats = await this.qb.kysely
222222
.selectFrom('hwidUserDevices')
223223
.select([
224-
sql<string>`
225-
CASE
226-
WHEN LOWER("user_agent") ~ '^(flclash|flclash x)' THEN 'FlClashX'
227-
WHEN LOWER("user_agent") ~ '^koala-clash' THEN 'KoalaClash'
228-
WHEN LOWER("user_agent") ~ '^v2raytun' THEN 'v2rayTUN'
229-
WHEN LOWER("user_agent") ~ '^happ' THEN 'Happ'
230-
ELSE 'Unknown'
231-
END
232-
`.as('app'),
224+
sql<string>`SPLIT_PART("user_agent", '/', 1)`.as('app'),
233225
(eb) => eb.fn.count('hwid').as('count'),
234226
])
235227
.where('userAgent', 'is not', null)
236-
.groupBy(
237-
sql`
238-
CASE
239-
WHEN LOWER("user_agent") ~ '^(flclash|flclash x)' THEN 'FlClashX'
240-
WHEN LOWER("user_agent") ~ '^koala-clash' THEN 'KoalaClash'
241-
WHEN LOWER("user_agent") ~ '^v2raytun' THEN 'v2rayTUN'
242-
WHEN LOWER("user_agent") ~ '^happ' THEN 'Happ'
243-
ELSE 'Unknown'
244-
END
245-
`,
246-
)
228+
.groupBy(sql`SPLIT_PART("user_agent", '/', 1)`)
247229
.orderBy('count', 'desc')
248230
.execute();
249231

@@ -267,10 +249,12 @@ export class HwidUserDevicesRepository
267249
platform: stat.platform || 'Unknown',
268250
count: Number(stat.count),
269251
})),
270-
byApp: appStats.map((stat) => ({
271-
app: stat.app,
272-
count: Number(stat.count),
273-
})),
252+
byApp: appStats
253+
.filter((stat) => !stat.app.startsWith('https:'))
254+
.map((stat) => ({
255+
app: stat.app,
256+
count: Number(stat.count),
257+
})),
274258
stats: {
275259
totalUniqueDevices: Number(totalStats.totalUniqueDevices),
276260
totalHwidDevices: Number(totalStats.totalHwidDevices),

0 commit comments

Comments
 (0)