/stats?all=true

This commit is contained in:
Klemek
2021-04-06 09:58:51 +02:00
parent c3e53c7df8
commit 8a9b9cdcfe
4 changed files with 133 additions and 62 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "gitblog.md", "name": "gitblog.md",
"version": "1.3.2", "version": "1.3.3",
"description": "A static blog using Markdown pulled from your git repository.", "description": "A static blog using Markdown pulled from your git repository.",
"main": "src/server.js", "main": "src/server.js",
"dependencies": { "dependencies": {
+16
View File
@@ -201,9 +201,25 @@ module.exports = (config) => {
}); });
app.get('/stats', (req, res) => { app.get('/stats', (req, res) => {
if (config['modules']['hit_counter']) { if (config['modules']['hit_counter']) {
if (req.query['all']) {
const keys = Object.keys(articles).filter(key => !articles[key].draft);
keys.unshift('/');
const read = (i, outputData) => {
if (i >= keys.length) {
res.json(outputData);
} else {
hc.read(keys[i], (data) => {
outputData.push(data);
read(i + 1, outputData);
});
}
};
read(0, []);
} else {
hc.read('/', (data) => { hc.read('/', (data) => {
res.json(data); res.json(data);
}); });
}
} else { } else {
showError(req, res, 404); showError(req, res, 404);
} }
+3 -1
View File
@@ -42,13 +42,15 @@ module.exports = (config, onConnect, onError) => {
const read = (path, cb) => { const read = (path, cb) => {
if (!client.connected) { if (!client.connected) {
cb({ cb({
path: path,
hits: 0, hits: 0,
total_visitors: 0, total_visitors: 0,
current_visitors: 0, current_visitors: cleanVisitors(path),
}); });
} else { } else {
client.hgetall(path, (_, value) => { client.hgetall(path, (_, value) => {
cb({ cb({
path: path,
hits: value ? parseInt(value.h) || 0 : 0, hits: value ? parseInt(value.h) || 0 : 0,
total_visitors: value ? parseInt(value.v) || 0 : 0, total_visitors: value ? parseInt(value.v) || 0 : 0,
current_visitors: cleanVisitors(path), current_visitors: cleanVisitors(path),
+110 -57
View File
@@ -197,26 +197,6 @@ describe('Test root path', () => {
}); });
}, fail); }, fail);
}); });
test('404 index no stats', (done) => {
request(app).get('/stats')
.then((response) => {
expect(response.statusCode).toBe(404);
done();
});
});
test('200 index stats', (done) => {
config['modules']['hit_counter'] = true;
request(app).get('/stats')
.then((response) => {
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({
hits: 0,
total_visitors: 0,
current_visitors: 0,
});
done();
});
});
}); });
describe('Test RSS feed', () => { describe('Test RSS feed', () => {
@@ -455,44 +435,7 @@ describe('Test articles rendering', () => {
}); });
}, fail); }, fail);
}); });
test('404 article no stats', (done) => {
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, testTemplate),
]);
app.reload(() => {
request(app).get('/2019/05/05/hello/stats')
.then((response) => {
expect(response.statusCode).toBe(404);
done();
}); });
});
});
test('200 index stats', (done) => {
config['modules']['hit_counter'] = true;
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, testTemplate),
]);
app.reload(() => {
request(app).get('/2019/05/05/anything/stats')
.then((response) => {
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({
hits: 0,
total_visitors: 0,
current_visitors: 0,
});
done();
});
});
});
});
describe('Test static files', () => { describe('Test static files', () => {
test('404 invalid file no error page', (done) => { test('404 invalid file no error page', (done) => {
@@ -574,3 +517,113 @@ describe('Test other requests', () => {
}); });
}); });
}); });
describe('Test stats', () => {
test('404 index no stats', (done) => {
request(app).get('/stats')
.then((response) => {
expect(response.statusCode).toBe(404);
done();
});
});
test('200 index stats', (done) => {
config['modules']['hit_counter'] = true;
request(app).get('/stats')
.then((response) => {
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({
path: '/',
hits: 0,
total_visitors: 0,
current_visitors: 0,
});
done();
});
});
test('200 index stats all no article', (done) => {
config['modules']['hit_counter'] = true;
app.reload(() => {
request(app).get('/stats?all=true')
.then((response) => {
expect(response.statusCode).toBe(200);
expect(response.body).toEqual([
{
path: '/',
hits: 0,
total_visitors: 0,
current_visitors: 0,
},
]);
done();
});
});
});
test('200 index stats all 2 article 1 drafted', (done) => {
config['modules']['hit_counter'] = true;
utils.createEmptyDirs([
path.join(dataDir, '2019', '05', '05'),
path.join(dataDir, '2019', '04', '05'),
]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, '2019', '04', '05', 'draft.md'),
]);
app.reload(() => {
request(app).get('/stats?all=true')
.then((response) => {
expect(response.statusCode).toBe(200);
expect(response.body).toEqual([
{
path: '/',
hits: 0,
total_visitors: 0,
current_visitors: 0,
},
{
path: '2019/05/05',
hits: 0,
total_visitors: 0,
current_visitors: 0,
},
]);
done();
});
});
});
test('404 article no stats', (done) => {
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, testTemplate),
]);
app.reload(() => {
request(app).get('/2019/05/05/hello/stats')
.then((response) => {
expect(response.statusCode).toBe(404);
done();
});
});
});
test('200 article stats', (done) => {
config['modules']['hit_counter'] = true;
utils.createEmptyDirs([ path.join(dataDir, '2019', '05', '05') ]);
utils.createEmptyFiles([
path.join(dataDir, '2019', '05', '05', 'index.md'),
path.join(dataDir, testTemplate),
]);
app.reload(() => {
request(app).get('/2019/05/05/anything/stats')
.then((response) => {
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({
path: '2019/05/05',
hits: 0,
total_visitors: 0,
current_visitors: 0,
});
done();
});
});
});
});