Skip to content

Commit 569811f

Browse files
committed
fix: batch fixes
- query/wiki projection - search/wiki _total
1 parent f36584a commit 569811f

3 files changed

Lines changed: 79 additions & 44 deletions

File tree

api/query.ts

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { VercelRequest, VercelResponse } from '@vercel/node'
22
import { Document } from 'mongodb'
33
import { getProjectSrotFromStr } from 'serverless-kit'
4-
import { router } from './utils'
4+
import { isValidUrl, router } from './utils'
55

66
export default async (req: VercelRequest, res: VercelResponse) => {
77
router.endpoint('/api/query')
@@ -337,47 +337,28 @@ export default async (req: VercelRequest, res: VercelResponse) => {
337337
$group: {
338338
_id: {
339339
siteUrl: '$siteUrl',
340-
userName: '$userName',
341340
featureID: '$featureID',
342341
},
343342
_total: { $sum: 1 },
343+
siteUrl: { $first: '$siteUrl' },
344344
siteName: { $first: '$siteName' },
345345
},
346346
},
347-
// Group users
348-
{
349-
$group: {
350-
_id: {
351-
siteUrl: '$_id.siteUrl',
352-
featureID: '$_id.featureID',
353-
},
354-
_total: { $sum: '$_total' },
355-
siteUrl: { $first: '$_id.siteUrl' },
356-
siteName: { $first: '$siteName' },
357-
users: {
358-
$push: {
359-
userName: '$_id.userName',
360-
count: '$_total',
361-
},
362-
},
363-
},
364-
},
365347
// Group features
366348
{
367349
$group: {
368350
_id: {
369351
siteUrl: '$_id.siteUrl',
370352
},
371353
_total: { $sum: '$_total' },
372-
siteUrl: { $first: '$_id.siteUrl' },
354+
siteUrl: { $first: '$siteUrl' },
373355
siteName: { $first: '$siteName' },
374356
features: {
375357
$addToSet: {
376358
featureID: '$_id.featureID',
377359
count: '$_total',
378360
},
379361
},
380-
users: { $first: '$users' },
381362
},
382363
},
383364
{
@@ -397,6 +378,12 @@ export default async (req: VercelRequest, res: VercelResponse) => {
397378
})
398379
}
399380

381+
if (ctx.project) {
382+
aggregate.unshift({
383+
$project: ctx.project,
384+
})
385+
}
386+
400387
const sites = await ctx.col.aggregate(aggregate).toArray()
401388
let hasNext = false
402389
if (sites.length > ctx.limit) {
@@ -413,6 +400,32 @@ export default async (req: VercelRequest, res: VercelResponse) => {
413400
}
414401
})
415402

403+
// GET /query/site/users
404+
router
405+
.addRoute()
406+
.path(['site', 'wiki'])
407+
.path('users')
408+
.parseLimits()
409+
.check((ctx) => {
410+
if (!ctx.req.query.siteUrl) {
411+
ctx.status = 400
412+
ctx.message = 'Missing siteUrl'
413+
return false
414+
} else if (isValidUrl(ctx.req.query.siteUrl as string)) {
415+
ctx.status = 400
416+
ctx.message = 'Invalid siteUrl'
417+
return false
418+
}
419+
})
420+
.action((ctx) => {
421+
const { siteUrl } = ctx.req.query
422+
const aggregate: Document[] = [
423+
{
424+
$match: { siteUrl },
425+
},
426+
]
427+
})
428+
416429
return router.init(req, res)
417430
}
418431

api/search.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { VercelRequest, VercelResponse } from '@vercel/node'
2+
import { getProjectSrotFromStr } from 'serverless-kit'
23
import { isValidUserName, router } from './utils'
34
import { isValidUrl } from './utils'
45

@@ -81,6 +82,7 @@ export default async (req: VercelRequest, res: VercelResponse) => {
8182
return false
8283
}
8384
})
85+
.parseLimits()
8486
.action(async (ctx) => {
8587
const siteUrl = ctx.req.query.siteUrl as string
8688
const siteName = ctx.req.query.siteName as string
@@ -93,23 +95,39 @@ export default async (req: VercelRequest, res: VercelResponse) => {
9395
$match.siteName = new RegExp(siteName, 'ig')
9496
}
9597

96-
const sites = (
97-
await ctx.col
98-
.aggregate([
99-
{
100-
$match,
98+
const sites = await ctx.col
99+
.aggregate([
100+
{
101+
$match,
102+
},
103+
{
104+
$group: {
105+
_id: {
106+
siteUrl: '$siteUrl',
107+
siteName: '$siteName',
108+
},
109+
_total: { $sum: 1 },
101110
},
102-
{
103-
$group: {
104-
_id: {
105-
siteUrl: '$siteUrl',
106-
siteName: '$siteName',
107-
},
111+
},
112+
{
113+
$group: {
114+
_id: {
115+
siteUrl: '$_id.siteUrl',
108116
},
117+
_total: { $sum: '$_total' },
118+
siteUrl: { $first: '$_id.siteUrl' },
119+
siteName: { $first: '$_id.siteName' },
109120
},
110-
])
111-
.toArray()
112-
).map(({ _id }) => _id)
121+
},
122+
{
123+
$project: { _id: false },
124+
},
125+
])
126+
.sort(ctx.sort)
127+
.project(
128+
ctx.project || getProjectSrotFromStr('_total|siteUrl|siteName')
129+
)
130+
.toArray()
113131

114132
ctx.body = { search: sites, $match: { siteUrl, siteName } }
115133
})

docs/api-collections.http

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
# Install humao.rest-client to make quick tests
33

44
# Variables
5-
@PROD_URL = https://analytics.ipe.wiki/api
5+
# @ENDPOINT = https://analytics.ipe.wiki/api
6+
@ENDPOINT = http://localhost:3000/api
67
# @BEGIN_TIME = {{$datetime iso8601}}
78

89
### Index
9-
GET {{PROD_URL}}
10+
GET {{ENDPOINT}}
1011
?debug=true
1112

1213
### Query user
13-
GET {{PROD_URL}}/query/user
14+
GET {{ENDPOINT}}/query/user
1415
# required {string}
1516
?userName=机智的小鱼君
1617
# required {string}
@@ -23,7 +24,7 @@ GET {{PROD_URL}}/query/user
2324
&project=*
2425

2526
### Query site
26-
GET {{PROD_URL}}/query/site
27+
GET {{ENDPOINT}}/query/site
2728
# required {string}
2829
?siteUrl=https://zh.moegirl.org.cn/
2930
# {number}
@@ -32,9 +33,10 @@ GET {{PROD_URL}}/query/site
3233
&limit=5
3334
# {'*' | string}
3435
&project=*
36+
&sort=!_total
3537

3638
### Search users
37-
GET {{PROD_URL}}/search/user
39+
GET {{ENDPOINT}}/search/user
3840
# required {string}
3941
?userName=机智的小鱼君
4042
# {string}
@@ -47,7 +49,7 @@ GET {{PROD_URL}}/search/user
4749
&project=*
4850

4951
### Search sites
50-
GET {{PROD_URL}}/search/sites
52+
GET {{ENDPOINT}}/search/sites
5153
# {string}
5254
?siteName=minecraft
5355
# {string}
@@ -56,15 +58,17 @@ GET {{PROD_URL}}/search/sites
5658
&offset=0
5759
# {number}
5860
&limit=5
61+
#
62+
&sort=!_total
5963
# {'*' | string}
6064
&project=*
6165

6266
### Database status
63-
GET {{PROD_URL}}/status
67+
GET {{ENDPOINT}}/status
6468
?debug=true
6569

6670
### Submit data
67-
POST {{PROD_URL}}/submit
71+
POST {{ENDPOINT}}/submit
6872
Content-Type: application/json
6973

7074
{

0 commit comments

Comments
 (0)