Skip to content

Commit

Permalink
top IP list (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
matyaskopp committed Dec 1, 2021
1 parent e6d878c commit 38ac725
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
15 changes: 10 additions & 5 deletions app/db/ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function date2string(date=new Date(), level='month', type='full'){


function createFilter(filter){

logger.warn('TODO: validate input');
logger.warn('IP filter is not implemented');
var query={
'pivot_ip': {
Expand All @@ -81,7 +81,7 @@ function createFilter(filter){
}
};
var values={};
var header=['ip'];
var header=[];

values.level = filter.period.level;
values.period_interval = '1 ' + values.level;
Expand Down Expand Up @@ -126,6 +126,7 @@ exports.getTop = (filter,
) => {
logger.trace();
logger.warn('db.ip.getTop() uses only default settings !!!');
min_exist = parseInt(min_exist);
const {query, values, header} = createFilter({
'period': {
'start': moment(period_start),
Expand All @@ -138,11 +139,15 @@ exports.getTop = (filter,
},
...filter
});

return new promise((resolve, reject) => {
db.any(`
SELECT *
SELECT
ip,
`+ header.map(v => `COALESCE("${v}",0)`).join('+') +` AS sum,
`+ header.map(v => `"${v}"`).join(',') +`
FROM crosstab(
'SELECT top.ip,to_char(data.period_start_date,''YYYY-MM-DD 00:00:00''),COALESCE(data.cnt_${measure},0)
'SELECT top.ip, to_char(data.period_start_date,''YYYY-MM-DD 00:00:00''), data.cnt_${measure}
FROM
( SELECT DISTINCT ip
FROM log_ip_aggr lg
Expand Down Expand Up @@ -172,7 +177,7 @@ FROM crosstab(
.then(data => {
logger.trace();
if (data) {
resolve({data: data, header: header}); // data
resolve({data: data, header: ['ip','sum',...header]}); // data
}
else {
reject({
Expand Down
23 changes: 22 additions & 1 deletion app/routes/adminRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,28 @@ router.get('/admin/user/:userId', function (req, res, next) {
router.get('/admin/ips', function (req, res, next) {
logger.trace();
let user = req.session.user;
res.render('ips', {user: user, ips_active: true});
let val = {
measure: req.query.measure || 'units',
level: req.query.level || 'month',
start: req.query.start || (new Date().getFullYear())+'-01-01',
end: req.query.end || (new Date().getFullYear()+1)+'-01-01',
min_exist: req.query.min_exist || 0
};
var table_params = [];
logger.error('post ws get !!! ',req);

for (const [k,v] of Object.entries(val)){
table_params.push(`${k}=${v}`);
logger.error(`${k}=${v}`);
}
res.render('ips', {
user: user,
ips_active: true,
measures: ["units", "requests", "body_bytes_sent"],
levels: ["month", "day"],
val: val,
table_params: table_params.join('&')
});
});

router.get('/admin/ip/:ip', function (req, res, next) {
Expand Down
10 changes: 9 additions & 1 deletion app/routes/apiRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,22 @@ router.get('/api/users', function (req, res, next) {
router.get('/api/ips', function (req, res, next) {
logger.trace();
let user = req.session.user;

if (!user.is_admin) {
res.status(403);
res.send({
status : false,
error : 'Permission Denied.'
});
} else {
dataController.getTopIPs(user.user_id).then(data => {
dataController.getTopIPs(user.user_id,
{},
(req.query.start || (new Date().getFullYear())+'-01-01')+' 00:00:00',
(req.query.end || (new Date().getFullYear()+1)+'-01-01')+' 00:00:00',
req.query.measure || 'units',
req.query.level || 'month',
req.query.min_exist || 0
).then(data => {
res.json(data);
}).catch();
}
Expand Down
42 changes: 40 additions & 2 deletions app/views/ips.pug
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,46 @@ block content
.divider
.row
.col.s12
h6 Last three months IP list
table.striped(type='lb-datatable' data-type='json' data-field='data' data-url='api/ips' data-header-field='header' data-items-field='header' )
form(method="get")
.row
.input-field.col.s12
i.material-icons.prefix format_size
.select-wrapper
select.browser-default#level(name="level")
each l in levels
option(value=l, selected= (l==val.level) ? 'selected' : false)= l
label(for="level") Period level size (table column)
.row
.input-field.col.s6
i.material-icons.prefix compare
.select-wrapper
select.browser-default#measure(name="measure")
each m in measures
option(value=m, selected= (m==val.measure) ? 'selected' : false)= m
label(for="measure") Measure
.input-field.col.s6
i.material-icons.prefix chevron_right
input#min_exist.validate(name="min_exist" type="number" step="1" min="0" required="" aria-required="true" value=val.min_exist )
label(for="min_exist") minimum required value
span.helper-text(data-error="Number Required")
.row
.input-field.col.s6
i.material-icons.prefix date_range
input#start.validate(name="start" type="date" required="" aria-required="true" value=val.start )
label(for="start") Start date (included)
span.helper-text(data-error="Date Required")
.input-field.col.s6
i.material-icons.prefix last_page
input#end.validate(name="end" type="date" required="" aria-required="true" value=val.end )
label(for="end") End date (excluded)
span.helper-text(data-error="Date Required")
.row
.input-field.col.s12
button.btn.grey.waves-effect.col.s12.white-text(type="submit")
strong Get Data
.row
.col.s12
table.striped(type='lb-datatable' data-type='json' data-field='data' data-url='api/ips?'+table_params data-header-field='header' data-items-field='header' )
script.
[ {
"targets": 0,
Expand Down

0 comments on commit 38ac725

Please sign in to comment.