Skip to content

Commit a6fb1ee

Browse files
author
lucifer
committed
feat: 自动统计用户信息
1 parent 32d014f commit a6fb1ee

File tree

11 files changed

+89
-21
lines changed

11 files changed

+89
-21
lines changed

.github/workflows/schedule.yaml

+22
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,25 @@ jobs:
2424
git config --global user.name 'robot'
2525
git commit -am "feat: from daily schedule"
2626
git push
27+
on:
28+
push:
29+
jobs:
30+
generate:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- name: Check out repository code
34+
uses: actions/checkout@v2
35+
- name: run npm
36+
uses: actions/setup-node@v2
37+
with:
38+
node-version: "14"
39+
- run: npm install
40+
- name: generate users
41+
env:
42+
secret: ${{secrets.secret}
43+
run: npm run generate
44+
- name: Commit static
45+
run: |
46+
git config --global user.name 'robot'
47+
git commit -am "feat: automate generate users"
48+
git push

config/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ mosihan
310310
dpgirl
311311
prhCSer
312312
rufengnanren
313+
theonebemine
314+
Moin-Jer
313315
Peg-008
314316
NorthSeacoder`),
315317
];

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"dev": "NODE_ENV=development ./node_modules/.bin/nodemon bin/www",
88
"prd": "pm2 start bin/www",
99
"test": "echo \"Error: no test specified\" && exit 1",
10-
"daily-schedule": "node ./schedule/daily-problem.js && node ./schedule/daily-check.js"
10+
"daily-schedule": "node ./schedule/daily-problem.js && node ./schedule/daily-check.js",
11+
"generate": "node ./static/users/generate.js && node ./static/solution/generate.js && node ./static/lectures/generate.js"
1112
},
1213
"dependencies": {
1314
"@koa/cors": "^3.1.0",

schedule/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
## daily-check
66

77
第二题汇总**前一天**的打卡情况,每天定时 00:01 触发。
8+
9+
## stastic
10+
11+
从第 8 天起,每天触发,统计之前七天的数据。

schedule/stastic.js

+23-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
const { getDay } = require("../utils/day");
2-
const { db } = require("../config/index");
2+
const us = require("../static/users/index");
33
const mySolutions = require("../static/my/solutions.json");
44

55
// 下面代码没有考虑一个人中途加入,那么他没有加入之前也会被统计为未打卡。
6-
const allUsers = JSON.parse(JSON.stringify(db));
6+
const allUsers = JSON.parse(JSON.stringify(us));
77

8-
function noCheckWith7Days() {
9-
const users = [];
8+
// 返回7天内所有已经打卡的
9+
function checkWithin7Days() {
10+
const users = {};
1011
const day = getDay();
11-
if (day < 7) return users;
12+
if (day <= 7) return allUsers;
1213
for (const name in mySolutions) {
1314
const solutions = mySolutions[name];
14-
let i = 0;
15+
if (!(name in allUsers)) {
16+
console.log(`震惊! ${name} 竟然发生了这种事情,真相令人唏嘘!`);
17+
continue;
18+
}
19+
let i = Math.max(day - 7, getDay(allUsers[name].createTime));
20+
1521
let count = 0;
1622

17-
while (i < day - 1) {
23+
while (i < day) {
1824
if (!solutions[i]) {
1925
count += 1;
2026
} else {
@@ -26,11 +32,18 @@ function noCheckWith7Days() {
2632
i++;
2733
}
2834

29-
if (count > 7) {
30-
users.push(name);
35+
if (count < 7) {
36+
users[name] = true;
3137
}
3238
}
3339
return users;
3440
}
3541

36-
console.log(noCheckWith7Days());
42+
function diff(A, B) {
43+
for (const name in B) {
44+
delete A[name];
45+
}
46+
return Object.keys(A);
47+
}
48+
49+
console.log(diff(allUsers, checkWithin7Days()).length);

static/lectures/generate.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const fs = require("fs");
2-
2+
const path = require("path");
33
const { encrypt } = require("../../utils/crypto.js");
44

55
function merge(A, B) {
@@ -267,24 +267,32 @@ const lectures = {
267267

268268
// 先导
269269
lectures.intro["-4"].content = encrypt(
270-
fs.readFileSync("../../91alg-4/algo.md")
270+
fs.readFileSync(path.resolve(__dirname, "../../91alg-4/algo.md"))
271271
);
272272

273273
lectures.intro["-3"].content = encrypt(
274-
fs.readFileSync("../../91alg-4/bigO.md")
274+
fs.readFileSync(path.resolve(__dirname, "../../91alg-4/bigO.md"))
275275
);
276276

277277
// 基础
278278

279279
[1, 2, 3, 4, 5, 6, 7].forEach((i) => {
280280
lectures.basic[i].content = encrypt(
281-
fs.readFileSync(`../../91alg-4/lecture/basic-${i < 10 ? "0" + i : i}.md`)
281+
fs.readFileSync(
282+
path.resolve(
283+
__dirname,
284+
`../../91alg-4/lecture/basic-${i < 10 ? "0" + i : i}.md`
285+
)
286+
)
282287
);
283288
});
284289

285-
fs.writeFileSync("./lectures-by-category.json", JSON.stringify(lectures));
290+
fs.writeFileSync(
291+
__dirname + "/lectures-by-category.json",
292+
JSON.stringify(lectures)
293+
);
286294

287295
fs.writeFileSync(
288-
"./lectures-by-id.json",
296+
__dirname + "/lectures-by-id.json",
289297
JSON.stringify(merge(lectures.intro, lectures.basic))
290298
);

static/lectures/lectures-by-category.json

+1-1
Large diffs are not rendered by default.

static/lectures/lectures-by-id.json

+1-1
Large diffs are not rendered by default.

static/solution/generate.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require("fs");
2+
const path = require("path");
23

34
const { encrypt } = require("../../utils/crypto.js");
45

@@ -41,7 +42,9 @@ function matchWioutPaddingLine(reg, txt) {
4142
// 基础篇
4243
Array.from({ length: 28 }, (_, i) => i + 1).forEach((i) => {
4344
solutions[i] = solutions[i] || {};
44-
const rawMDBuffer = fs.readFileSync(`../../91alg-4/solution/basic/d${i}.md`);
45+
const rawMDBuffer = fs.readFileSync(
46+
path.resolve(__dirname, `../../91alg-4/solution/basic/d${i}.md`)
47+
);
4548
const rawMD = rawMDBuffer.toString();
4649
const regs = {
4750
...getSatelliteDataReg(),
@@ -67,4 +70,4 @@ Array.from({ length: 28 }, (_, i) => i + 1).forEach((i) => {
6770
};
6871
});
6972

70-
fs.writeFileSync("./solutions.json", JSON.stringify(solutions));
73+
fs.writeFileSync(__dirname + "/solutions.json", JSON.stringify(solutions));

static/users/generate.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const fs = require("fs");
2+
const { db } = require("../../config/index");
3+
const users = require("./index.json");
4+
5+
for (const name in db) {
6+
if (!(name in users)) {
7+
users[name] = {
8+
login: name,
9+
createTime: new Date().getTime(),
10+
};
11+
}
12+
}
13+
14+
fs.writeFileSync(__dirname + "/index.json", JSON.stringify(users));

static/users/index.json

+1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)