Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit f47e284

Browse files
committed
0.12
1 parent 91a4207 commit f47e284

3 files changed

Lines changed: 74 additions & 56 deletions

File tree

config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ module.exports = {
33
expiration: 3600, // expiration second
44
requires: ["*"], // CODE STATISTIC can only be parsed for allowed users. ( * indicates that all users are allowed )
55
port: 8000, // server port
6-
host: "localhost",
76
}

index.js

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const express = require('express');
2-
const engine = require('ejs-locals');
3-
const cache = new (require('./cache').ApiCache)();
42
const logger = (require("log4js")).getLogger("Backend");
5-
const utils = require('./utils');
3+
const stats = require('./stats');
64
const conf = require('./config');
75
const app = express();
86

@@ -15,54 +13,6 @@ const isAvailableUser = (username) => {
1513
return ( !! username ) && ( conf.requires.includes("*") || conf.requires.includes(username));
1614
}
1715

18-
async function getLanguage(user, repo) {
19-
return await cache.requestWithCache(`/repos/${user}/${repo}/languages`);
20-
}
21-
22-
async function langStatistics(queue) {
23-
const res = {};
24-
for (const idx in queue) {
25-
const task = queue[idx];
26-
if (task instanceof Promise) {
27-
const task_response = await task;
28-
for (const lang in task_response) {
29-
lang in res ?
30-
res[lang] += task_response[lang] :
31-
res[lang] = task_response[lang];
32-
}
33-
}
34-
}
35-
return res;
36-
}
37-
38-
async function getAccount(username) {
39-
const response = await cache.requestWithCache(`/users/${username}`);
40-
return {
41-
username: username,
42-
followers: response['followers'],
43-
repos: response['public_repos'],
44-
langs: await langStatistics(
45-
Object.values(await cache.requestWithCache(`/users/${username}/repos`)
46-
).map(async (resp) => {
47-
return await getLanguage(username, resp['name']);
48-
})),
49-
};
50-
}
51-
52-
async function getRepository(username, repo) {
53-
// get releases (700ms): (await cache.requestWithCache(`/repos/${username}/${repo}/releases`)).length
54-
const info = await cache.requestWithCache(`/repos/${username}/${repo}`);
55-
return {
56-
repo: `${username} / ${repo}`,
57-
size: utils.storeConvert(info['size'], 1),
58-
forks: info['forks'],
59-
stars: info['stargazers_count'],
60-
watchers: info['watchers_count'],
61-
license: info['license']['spdx_id'],
62-
langs: await getLanguage(username, repo),
63-
};
64-
}
65-
6616
app.get('/', function(req, res) {
6717
res.render('index');
6818
})
@@ -74,7 +24,7 @@ app.get('/user/:user/', async function (req, res) {
7424
return;
7525
}
7626
res.type('svg');
77-
res.render('user', await getAccount(username));
27+
res.render('user', await stats.getAccount(username));
7828
});
7929

8030
app.get('/repo/:user/:repo/', async function (req, res) {
@@ -84,9 +34,9 @@ app.get('/repo/:user/:repo/', async function (req, res) {
8434
return;
8535
}
8636
res.type('svg');
87-
res.send(await getRepository(username, repo));
37+
res.send(await stats.getRepository(username, repo));
8838
});
8939

9040

91-
app.listen(conf.port, conf.host, () =>
92-
logger.info(`Starting deployment server at http://${conf.host}:${conf.port}/.`));
41+
app.listen(conf.port, () =>
42+
logger.info(`Starting deployment server at http://127.0.0.1:${conf.port}/.`));

stats.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const cache = new (require('./cache').ApiCache)();
2+
3+
const units = ["b", "KiB", "MiB", "GiB", "TiB", "PiB"]
4+
const len_units = units.length - 1;
5+
6+
function storeConvert(size, idx=0) {
7+
if (size <= 0) {
8+
return "0";
9+
}
10+
11+
while (idx < len_units && size > 1024) {
12+
size /= 1024;
13+
idx ++;
14+
}
15+
return `${size.toFixed(1)} ${units[idx]}`;
16+
}
17+
18+
async function getLanguage(user, repo) {
19+
return await cache.requestWithCache(`/repos/${user}/${repo}/languages`);
20+
}
21+
22+
async function langStatistics(queue) {
23+
const res = {};
24+
for (const idx in queue) {
25+
const task = queue[idx];
26+
if (task instanceof Promise) {
27+
const task_response = await task;
28+
for (const lang in task_response) {
29+
lang in res ?
30+
res[lang] += task_response[lang] :
31+
res[lang] = task_response[lang];
32+
}
33+
}
34+
}
35+
return res;
36+
}
37+
38+
async function getAccount(username) {
39+
const response = await cache.requestWithCache(`/users/${username}`);
40+
return {
41+
username: username,
42+
followers: response['followers'],
43+
repos: response['public_repos'],
44+
langs: await langStatistics(
45+
Object.values(await cache.requestWithCache(`/users/${username}/repos`)
46+
).map(async (resp) => {
47+
return await getLanguage(username, resp['name']);
48+
})),
49+
};
50+
}
51+
52+
async function getRepository(username, repo) {
53+
// get releases (700ms): (await cache.requestWithCache(`/repos/${username}/${repo}/releases`)).length
54+
const info = await cache.requestWithCache(`/repos/${username}/${repo}`);
55+
return {
56+
repo: `${username} / ${repo}`,
57+
size: storeConvert(info['size'], 1),
58+
forks: info['forks'],
59+
stars: info['stargazers_count'],
60+
watchers: info['watchers_count'],
61+
license: info['license']['spdx_id'],
62+
langs: await getLanguage(username, repo),
63+
};
64+
}
65+
66+
module.exports = {
67+
getAccount: getAccount,
68+
getRepository: getRepository,
69+
}

0 commit comments

Comments
 (0)