|
1 | 1 | import { compareIsoDates } from './-utils'; |
2 | 2 |
|
3 | | -export function register(server) { |
4 | | - server.get('/api/v1/summary', function (schema) { |
5 | | - let crates = schema.crates.all(); |
| 3 | +export function summary(schema) { |
| 4 | + let crates = schema.crates.all(); |
| 5 | + |
| 6 | + let just_updated = crates.sort((a, b) => compareIsoDates(b.updated_at, a.updated_at)).slice(0, 10); |
| 7 | + let most_downloaded = crates.sort((a, b) => b.downloads - a.downloads).slice(0, 10); |
| 8 | + let new_crates = crates.sort((a, b) => compareIsoDates(b.created_at, a.created_at)).slice(0, 10); |
| 9 | + let most_recently_downloaded = crates.sort((a, b) => b.recent_downloads - a.recent_downloads).slice(0, 10); |
6 | 10 |
|
7 | | - let just_updated = crates.sort((a, b) => compareIsoDates(b.updated_at, a.updated_at)).slice(0, 10); |
8 | | - let most_downloaded = crates.sort((a, b) => b.downloads - a.downloads).slice(0, 10); |
9 | | - let new_crates = crates.sort((a, b) => compareIsoDates(b.created_at, a.created_at)).slice(0, 10); |
10 | | - let most_recently_downloaded = crates.sort((a, b) => b.recent_downloads - a.recent_downloads).slice(0, 10); |
| 11 | + let num_crates = crates.length; |
| 12 | + // eslint-disable-next-line unicorn/no-reduce |
| 13 | + let num_downloads = crates.models.reduce((sum, crate) => sum + crate.downloads, 0); |
11 | 14 |
|
12 | | - let num_crates = crates.length; |
13 | | - // eslint-disable-next-line unicorn/no-reduce |
14 | | - let num_downloads = crates.models.reduce((sum, crate) => sum + crate.downloads, 0); |
| 15 | + let popular_categories = schema.categories |
| 16 | + .all() |
| 17 | + .sort((a, b) => b.crates_cnt - a.crates_cnt) |
| 18 | + .slice(0, 10); |
| 19 | + let popular_keywords = schema.keywords |
| 20 | + .all() |
| 21 | + .sort((a, b) => b.crates_cnt - a.crates_cnt) |
| 22 | + .slice(0, 10); |
15 | 23 |
|
16 | | - let popular_categories = schema.categories |
17 | | - .all() |
18 | | - .sort((a, b) => b.crates_cnt - a.crates_cnt) |
19 | | - .slice(0, 10); |
20 | | - let popular_keywords = schema.keywords |
21 | | - .all() |
22 | | - .sort((a, b) => b.crates_cnt - a.crates_cnt) |
23 | | - .slice(0, 10); |
| 24 | + return { |
| 25 | + just_updated: this.serialize(just_updated).crates.map(it => ({ ...it, versions: null })), |
| 26 | + most_downloaded: this.serialize(most_downloaded).crates.map(it => ({ ...it, versions: null })), |
| 27 | + new_crates: this.serialize(new_crates).crates.map(it => ({ ...it, versions: null })), |
| 28 | + most_recently_downloaded: this.serialize(most_recently_downloaded).crates.map(it => ({ |
| 29 | + ...it, |
| 30 | + versions: null, |
| 31 | + })), |
| 32 | + num_crates, |
| 33 | + num_downloads, |
| 34 | + popular_categories: this.serialize(popular_categories).categories, |
| 35 | + popular_keywords: this.serialize(popular_keywords).keywords, |
| 36 | + }; |
| 37 | +} |
24 | 38 |
|
25 | | - return { |
26 | | - just_updated: this.serialize(just_updated).crates.map(it => ({ ...it, versions: null })), |
27 | | - most_downloaded: this.serialize(most_downloaded).crates.map(it => ({ ...it, versions: null })), |
28 | | - new_crates: this.serialize(new_crates).crates.map(it => ({ ...it, versions: null })), |
29 | | - most_recently_downloaded: this.serialize(most_recently_downloaded).crates.map(it => ({ |
30 | | - ...it, |
31 | | - versions: null, |
32 | | - })), |
33 | | - num_crates, |
34 | | - num_downloads, |
35 | | - popular_categories: this.serialize(popular_categories).categories, |
36 | | - popular_keywords: this.serialize(popular_keywords).keywords, |
37 | | - }; |
38 | | - }); |
| 39 | +export function register(server) { |
| 40 | + server.get('/api/v1/summary', summary); |
39 | 41 | } |
0 commit comments