This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
list.js
174 lines (146 loc) · 3.95 KB
/
list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* houston/controller/api/list.js
* Publishes some GET lists for AppCenter
*
* @exports {Object} - Koa router
*/
import Cache from 'node-cache'
import moment from 'moment'
import Router from 'koa-router'
import Download from 'lib/database/download'
import Project from 'lib/database/project'
const cache = new Cache({ stdTTL: 600 })
const route = new Router({
prefix: '/newest'
})
/**
* findReleases
* Finds the projects with the newest releases.
*
* @return {string[]}
*/
const findReleases = async () => {
const cachedRes = cache.get('findReleases')
if (cachedRes != null) {
return cachedRes
}
const projects = await Project.aggregate([
{ $unwind: '$releases' },
{ $match: {
'releases._status': 'DEFER',
'releases.date.published': { $exists: true }
}},
{ $sort: { 'releases.date.published': 1 } },
{ $group: { _id: '$_id', 'name': { $first: '$name' }, 'release': { $first: '$releases' } } },
{ $sort: { 'release.date.published': -1 } },
{ $limit: 10 }
])
.then((res) => res.map((p) => p.name))
cache.set('findReleases', projects)
return projects
}
/**
* findProjects
* Finds the newest projects that have been released.
*
* @return {string[]}
*/
const findProjects = async () => {
const cachedRes = cache.get('findProjects')
if (cachedRes != null) {
return cachedRes
}
const cachedReleases = await findReleases()
const projects = await Project.aggregate([
{ $unwind: '$releases' },
{ $match: {
'releases._status': 'DEFER',
'releases.date.published': { $exists: true }
}},
{ $sort: { 'releases.date.published': 1 } },
{ $group: { _id: '$_id', 'name': { $first: '$name' }, 'release': { $first: '$releases' } } },
{ $sort: { 'release.date.published': -1 } },
{ $match: { 'name': { $nin: cachedReleases } } },
{ $limit: 10 }
])
.then((res) => res.map((p) => p.name))
cache.set('findProjects', projects)
return projects
}
/**
* findDownloads
* Finds the projects with the newest releases.
*
* @return {string[]}
*/
const findDownloads = async () => {
const cachedRes = cache.get('findDownloads')
if (cachedRes != null) {
return cachedRes
}
const [cachedReleases, cachedProjects] = await Promise.all([findReleases(), findProjects()])
const currentDay = moment.utc().get('date')
const projects = await Download.aggregate([
{ $match: { type: 'month' } },
{ $lookup: {
from: 'projects',
localField: 'release',
foreignField: 'releases._id',
as: 'project'
}},
{ $group: {
_id: '$project._id',
'name': { $first: '$project.name' },
'count': { $sum: `$month.${currentDay}` },
'total': { $sum: '$current.total' }
}},
{ $sort: { 'count': -1, 'total': -1 } },
{ $match: { 'name': { $size: 1 }, 'name.0': { $nin: [...cachedReleases, ...cachedProjects] } } }, // Avoid deleted projects
{ $limit: 10 }
])
.then((res) => res.map((p) => p.name[0]))
cache.set('findDownloads', projects)
return projects
}
/**
* GET /api/newest/project
* Finds the newest _first_ published project
*/
route.get('/project', async (ctx) => {
const projects = await findReleases()
ctx.status = 200
ctx.body = { data: projects }
return
})
/**
* GET /api/newest
* Finds the newest _first_ published project
* DEPRECATED: 05/06/2017 use /api/newest/project endpoint instead
*/
route.get('/', async (ctx) => {
const projects = await findProjects()
ctx.status = 200
ctx.body = { data: projects }
return
})
/**
* GET /api/newest/release
* Finds the newest released project
*/
route.get('/release', async (ctx) => {
const projects = await findProjects()
ctx.status = 200
ctx.body = { data: projects }
return
})
/**
* GET /api/newest/downloads
* Finds the projects with the most downloads in the current day.
*/
route.get('/downloads', async (ctx) => {
const projects = await findDownloads()
ctx.status = 200
ctx.body = { data: projects }
return
})
export default route