-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.js
101 lines (94 loc) · 2.72 KB
/
api.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
const Router = require("koa-router");
const router = new Router();
const User = require("./models/user");
const Notification = require("./models/notification");
const DailyRecord = require("./models/dailyRecord");
router.get("/", async (ctx, next) => {
const today = new Date();
today.setHours(0, 0, 0, 0);
const yesterday = new Date(today.getTime()).setDate(today.getDate() - 1);
const twoDaysAgo = new Date(today.getTime()).setDate(today.getDate() - 2);
const tenDaysAgo = new Date(today.getTime()).setDate(today.getDate() - 10);
/* ctx.body = JSON.stringify({
website: {
twoDaysAgo: await User.countDocuments({
websiteAccess: { $gte: twoDaysAgo, $lt: yesterday },
}),
yesterday: await User.countDocuments({
websiteAccess: { $gte: yesterday, $lt: today },
}),
today: await User.countDocuments({
websiteAccess: { $gte: today },
}),
allTime: await User.countDocuments({
websiteAccess: { $exists: true },
}),
},
shell: {
tenDaysAgo: await User.countDocuments({
shellAccess: { $gte: tenDaysAgo, $lt: twoDaysAgo },
}),
twoDaysAgo: await User.countDocuments({
shellAccess: { $gte: twoDaysAgo, $lt: yesterday },
}),
yesterday: await User.countDocuments({
shellAccess: { $gte: yesterday, $lt: today },
}),
today: await User.countDocuments({
shellAccess: { $gte: today },
}),
allTime: await User.countDocuments({
shellAccess: { $exists: true },
}),
},
}); */
const lastTenDaysRecord = await DailyRecord.find({
date: { $gte: tenDaysAgo },
});
console.log(lastTenDaysRecord);
ctx.type = "html";
ctx.body = `
<html>
<body>
<strong>Last 10 days</strong>
<table>
${lastTenDaysRecord.reduce(
(updated, latest) =>
updated.concat(
`<tr><td>${new Date(latest.date).toLocaleDateString()}</td><td>${
latest.count
}</td></tr>`
),
""
)}
</table>
</body>
</html>
`;
next();
});
router.get("/website", async (ctx, next) => {
ctx.user.websiteAccess = Date.now();
ctx.status = 200;
next();
});
router.get("/shell", async (ctx, next) => {
ctx.user.shellAccess = Date.now();
ctx.status = 200;
next();
});
router.get("/notifications", async (ctx, next) => {
ctx.user.shellAccess = Date.now();
let lastCheck = new Date(ctx.query.lastCheck) || new Date();
const unreadNotifications = await Notification.find({
date: { $gte: new Date(lastCheck) },
});
ctx.body = unreadNotifications;
next();
});
router.get("/github", async (ctx, next) => {
ctx.user.githubAccess = Date.now();
ctx.status = 200;
next();
});
module.exports = router;