Commit 28af0f54 authored by a001188's avatar a001188
Browse files

Sort implemented

parent b5d094b5
......@@ -4,20 +4,22 @@ function accessNodesTable(){
paging: false, //Enable paging
pageSize: 10, //Set page size (default: 10)
pageList: 'minimal',
sorting: false, //Enable sorting
multiSorting: false,
sorting: true, //Enable sorting
multiSorting: true,
defaultSorting: 'display_name ASC', //Set default sorting
actions: {
listAction: '/cmdb/api/v1/access/security/list'
},
fields: {
id: {
sorting: true,
title: 'id',
width: '2%',
list: true,
key: true
},
click: {
sorting: false,
title: 'Visa',
display: function(data){
return '<img src="/images/structure.png" title="Visa behörighetsgraf" onclick="accessGraph(' + data.record.id + ')"></img>';
......@@ -25,20 +27,22 @@ function accessNodesTable(){
width: '5%'
},
display_name: {
sorting: true,
title: 'Namn',
width: '25%'
},
class_id: {
class_information: {
sorting: true,
title: 'Typ',
options: '/cmdb/api/v1/options/classes',
width: '15%'
width: '30%'
},
parent_id:{
owner:{
sorting: true,
title: 'Ägare',
options: '/cmdb/api/v1/options/parent',
width: '20%'
},
description: {
sorting: true,
title: 'Beskrivning',
width: '30%'
}
......
......@@ -24,39 +24,33 @@ function search_ciColumnDef(){
display: renderSearchCiSiblingTable,
sorting: false
},
family_type_id: {
family_type_name: {
title: 'Familjetyp',
options: '/cmdb/api/v1/options/family_type',
width: '5%',
sorting: false
sorting: true
},
family_id: {
family_name: {
title: 'Familj',
options: '/cmdb/api/v1/options/families',
width: '5%',
sorting: false
sorting: true
},
id: {
ci_id: {
title: 'id',
width: '1%',
list: false,
key: true
},
display_name: {
ci_name: {
title: 'Namn',
width: '8%',
sorting: true
},
description: {
ci_description: {
title: 'Beskrivning',
width: '10%'
},
kst: {
title: 'Kostnadsställe',
width: '3%'
},
product: {
title: 'Produkt',
ci_kst: {
title: 'Ekonomi',
width: '3%'
},
// sla: {
......@@ -71,26 +65,26 @@ function search_ciColumnDef(){
title: 'Attribut',
width: '15%'
},
// account: {
// title: 'Konto',
account: {
title: 'Konto',
// options: function(data) {
// if(data.source==='list')
// return '/cmdb/api/v1/options/accounts/';
// else
// return data.record.id;
// },
// width: '5%'
// },
// group: {
// title: 'Grupp',
width: '5%'
},
group: {
title: 'Grupp',
// options: function(data) {
// if(data.source==='list')
// return '/cmdb/api/v1/options/groups/';
// else
// return data.record.id;
// },
// width: '5%'
// },
width: '5%'
},
contacts: {
title: 'Kontaktperson',
width: '5%'
......@@ -109,7 +103,7 @@ function search_ciColumnDef(){
pageList: 'minimal',
sorting: true, //Enable sorting
multiSorting: true,
defaultSorting: 'display_name ASC', //Set default sorting
defaultSorting: 'ci_name ASC', //Set default sorting
actions: {
listAction: function(postData, jtParams){
var search_string = document.getElementById("ci_search_field").value;
......@@ -154,9 +148,15 @@ function renderSearchCiSubTable (data) {
function createSearchCiSubTable(data, row){
$('#cmdb-table').jtable('openChildTable', row, {
title: 'Barn till ' + data.record.display_name,
title: 'Barn till ' + data.record.family_type_name + ' ' + data.record.family_name + ' ' + data.record.ci_name,
paging: true, //Enable paging
pageSize: 10, //Set page size (default: 10)
pageList: 'minimal',
sorting: true, //Enable sorting
multiSorting: true,
defaultSorting: 'ci_name ASC', //Set default sorting
actions: {
listAction: '/cmdb/api/v1/ci/child/list/' + data.record.id
listAction: '/cmdb/api/v1/ci/child/list/' + data.record.ci_id
},
fields: search_ciColumnDef()
},
......@@ -166,8 +166,8 @@ function createSearchCiSubTable(data, row){
}
function renderSearchCiSiblingTable (data) {
if(data.record.childs > 0){
function renderSearchCiSiblingTable(data) {
if(data.record.siblings > 1){
return $('<img src="/images/sibling.png" title="Syskon"/>').click(function () {
createSearchCiSiblingTable(data, $(this).closest('tr'));
});
......@@ -178,9 +178,15 @@ function renderSearchCiSiblingTable (data) {
function createSearchCiSiblingTable(data, row){
$('#cmdb-table').jtable('openChildTable', row, {
title: 'Syskon till ' + data.record.display_name,
title: 'Syskon till ' + data.record.family_type_name + ' ' + data.record.family_name + ' ' + data.record.ci_name,
paging: true, //Enable paging
pageSize: 10, //Set page size (default: 10)
pageList: 'minimal',
sorting: true, //Enable sorting
multiSorting: true,
defaultSorting: 'ci_name ASC', //Set default sorting
actions: {
listAction: '/cmdb/api/v1/ci/child/list/' + data.record.parent_id
listAction: '/cmdb/api/v1/ci/child/list/' + data.record.ci_parent_id
},
fields: search_ciColumnDef()
},
......@@ -191,20 +197,26 @@ function createSearchCiSiblingTable(data, row){
function renderSearchCiSuperTable (data) {
if(data.record.childs > 0){
return $('<img src="/images/parent.png" title="Förälder"/>').click(function () {
createSearchCiSuperTable(data, $(this).closest('tr'));
});
}
if(data.record.parents > 0){
return $('<img src="/images/parent.png" title="Förälder"/>').click(function () {
createSearchCiSuperTable(data, $(this).closest('tr'));
});
}
}
function createSearchCiSuperTable(data, row){
$('#cmdb-table').jtable('openChildTable', row, {
title: 'Förälder till ' + data.record.display_name,
title: 'Förälder till ' + data.record.family_type_name + ' ' + data.record.family_name + ' ' + data.record.ci_name,
paging: true, //Enable paging
pageSize: 10, //Set page size (default: 10)
pageList: 'minimal',
sorting: true, //Enable sorting
multiSorting: true,
defaultSorting: 'ci_name ASC', //Set default sorting
actions: {
listAction: '/cmdb/api/v1/ci/list/' + data.record.parent_id
listAction: '/cmdb/api/v1/ci/parent/list/' + data.record.parent_id
},
fields: search_ciColumnDef()
},
......
......@@ -11,7 +11,7 @@ const connectionString = process.env.DATABASE_URL || config.db_url;
router.post('/types', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
connectionString: connectionString
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
......@@ -41,8 +41,12 @@ router.post('/types', (req, res, next) => {
router.post('/security/list', (req, res, next) => {
var jtStartIndex = req.query['jtStartIndex'];
var jtPageSize = req.query['jtPageSize'];
var jtSorting = req.query['jtSorting'];
var pool = new pg.Pool({
connectionString: connectionString,
connectionString: connectionString
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
......@@ -53,12 +57,29 @@ router.post('/security/list', (req, res, next) => {
}
// SQL Query > Select Data
var sql = "select *, group_type_id as class_id from cmdb.ci_family_group union select *, account_type_id as class_id from cmdb.ci_family_account order by class_id "
var parameter = [];
client.query(sql, parameter, function(err, result){
var _sql = "select *, group_type_id as class_id from cmdb.ci_family_group ";
_sql+= "union ";
_sql+= "select *, account_type_id as class_id from cmdb.ci_family_account ";
_sql+= "order by $3 limit $2 OFFSET $1";
var sql = "SELECT sub.* ";
sql+= "FROM (";
sql+= "SELECT *, (select display_name from cmdb.ci ci2 where ci2.id=ci1.parent_id) as owner, group_type_id as class_id from cmdb.ci_family_group ci1 ";
sql+= "union all ";
sql+= "SELECT *, (select display_name from cmdb.ci ci4 where ci4.id=ci3.parent_id) as owner, account_type_id as class_id from cmdb.ci_family_account ci3 ";
sql+= ") as sub ";
sql+= "order by $3 ";
sql+= "limit $2 offset $1";
var parameter = [jtStartIndex, jtPageSize, jtSorting];
winston.log('info', '/security/list', {
paramter: parameter,
sql: sql
});
client.query(sql, parameter, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + sql + '"}'));
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + err + '"}'));
}
else {
var response = '{"Result":"OK", "Records":';
......@@ -234,7 +255,7 @@ router.get('/node/:id', (req, res, next) => {
winston.log('error', '/access/node/:id', {
paramter: parameter,
sql: sql
})
});
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + sql + '"}'));
}
else {
......
......@@ -7,6 +7,9 @@ const pg = require('pg');
const config = require('./config');
const winston = require('winston');
const connectionString = process.env.DATABASE_URL || config.db_url;
const SEARCH = 'search';
const CHILD = 'child';
const PARENT = 'parent';
router.post('/enterprise_service/list/', (req, res, next) => {
var pool = new pg.Pool({
......@@ -35,164 +38,174 @@ router.post('/enterprise_service/list/', (req, res, next) => {
router.post('/search/list', (req, res, next) => {
var cnt = 0;
var jtStartIndex = req.query['jtStartIndex'];
var jtPageSize = req.query['jtPageSize'];
var jtSorting = req.query['jtSorting'];
var ci_search = '%' + req.body.search_string.toUpperCase() + '%';
var sql_select = "select *, id as account, id as group, ";
sql_select+="(select count(*) from cmdb.ci ci2 where ci2.parent_id = ci1.id) as childs ";
var sql_select_count="select count(*) as \"TotalRecordCount\" ";
var sql_from ="from cmdb.ci ci1 ";
var sql_where ="where id > 0 and (";
sql_where+="upper(ci1.display_name) like $1 ";
sql_where+="or upper(ci1.description) like $1 ";
sql_where+="or upper(ci1.class_information) like $1 ";
sql_where+="or upper(ci1.attribute_information) like $1 ";
sql_where+="or upper(ci1.contact_information) like $1 ";
sql_where+=") ";
var sql_order_by="order by $2 ";
var sql_limit ="LIMIT $3 OFFSET $4";
var sql = sql_select + sql_from + sql_where + sql_order_by + sql_limit;
var sql_cnt = sql_select_count + sql_from + sql_where;
var parameters = [ci_search, jtSorting, jtPageSize, jtStartIndex];
var parameters_cnt = [ci_search];
winston.log('info', '0', {
head: '************************************************************',
jtSorting:jtSorting
});
var pool = new pg.Pool({
connectionString: connectionString
});
pool.connect(function(err, client, done){
router.post('/infrastructure/list', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
pool.connect(function(err, client, done){
if(err) {
done();
return res.status(500).json({success: false, data: err});
}
client.query(sql_cnt, parameters_cnt, function(err, result_cnt){
return res.status(500).json({success: false , data: err});
}
var sql = "select * , (select count(*) from cmdb.ci ci2 where ci2.parent_id = ci1.id) as childs from cmdb.ci ci1 where ci1.family_type_id = 5 and ci1.id > 0 order by ci1.family_type_id, ci1.family_id, ci1.display_name";
client.query(sql, [], function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' +sql_cnt + '"}'));
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' +sql + '"}'));
}
else {
cnt = result_cnt.rows[0];
var response = '{"Result":"OK", "Records":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
}
});
});
});
});
function getSQLSearchQuery(query_type, req){
var jtSorting = req.query['jtSorting'].trim();
var sql_select ="select ci1.id as ci_id, ci1.parent_id as ci_parent_id, f.id as f_id, ft.id as ft_id, ci1.display_name as ci_name, ci1.kst as ci_kst, ci1.description as ci_description, f.display_name as family_name, ft.display_name as family_type_name, ";
sql_select+="(select count(*) from cmdb.ci ci2 inner join cmdb.family f2 on ci2.family_id = f2.id where ci2.parent_id = ci1.id and f2.display_name <> 'Access') as childs, ";//f.display_name <> 'Access') as childs, ";
sql_select+="(select count(*) from cmdb.ci ci2 where ci1.parent_id = ci2.id and ci2.id > 0) as parents, ";//f.display_name <> 'Access') as childs, ";
sql_select+="(select count(*) from cmdb.ci ci2 inner join cmdb.family f2 on ci2.family_id = f2.id where ci2.parent_id = ci1.parent_id and f2.display_name <> 'Access') as siblings ";//f.display_name <> 'Access') as childs, ";
var sql_from = "from (cmdb.ci ci1 inner join cmdb.family f on f.id=ci1.family_id) inner join cmdb.family_type ft on ci1.family_type_id=ft.id ";
var sql_where_parent ="where ci1.id = $1 "; //and f.display_name <> 'Access' ";
var sql_where_child ="where ci1.parent_id = $1 and f.display_name <> 'Access' "; //and f.display_name <> 'Access' ";
var sql_where_list ="where ci1.id > 0 ";
var sql_where_search ="and (upper(ci1.display_name) like $1 ";
sql_where_search+="or upper(ci1.description) like $1 ";
sql_where_search+="or upper(ci1.class_information) like $1 ";
sql_where_search+="or upper(ci1.attribute_information) like $1 ";
sql_where_search+="or upper(ci1.contact_information) like $1 ";
sql_where_search+="or upper(f.display_name) like $1 ";
sql_where_search+="or upper(ft.display_name) like $1 ";
sql_where_search+=") ";
var sql_order_by="order by " + jtSorting + ";";
if(query_type === SEARCH)
sql = sql_select + sql_from + sql_where_list + sql_where_search + sql_order_by;
else if(query_type === CHILD)
sql = sql_select + sql_from + sql_where_child + sql_order_by;
else if(query_type === PARENT)
sql = sql_select + sql_from + sql_where_parent + sql_order_by;
else
sql= sql_select + sql_from + sql_where_search + sql_order_by;
winston.log('info', '*** TRACE ***', {
sql:sql
});
return sql;
}
function formateResult(req, res, result){
var jtStartIndex = Number(req.query['jtStartIndex']);
var jtPageSize = Number(req.query['jtPageSize']);
var total_number_of_rows = result.rows.length;
var response = '{"Result":"OK", "Records":[';
for(var i = 0; i < jtPageSize; i++){
if(jtStartIndex + i >= total_number_of_rows)
break;
response+= JSON.stringify(result.rows[i + jtStartIndex]);
if(i + jtStartIndex < total_number_of_rows - 1)
if(i < jtPageSize-1)
response+=",";
}
response+= '],"TotalRecordCount":' + total_number_of_rows + '}';
return res.json(JSON.parse(response));
}
router.post('/search/list', (req, res, next) => {
var sql = getSQLSearchQuery(SEARCH, req);
var ci_search = '%' + req.body.search_string.toUpperCase() + '%';
var parameters = [ci_search];
var pool = new pg.Pool({
connectionString: connectionString
});
pool.connect(function(err, client, done){
if(err) {
done();
return res.status(500).json({success: false, data: err});
}
}
client.query(sql, parameters, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' +sql + '"}'));
}
else {
var count = cnt['TotalRecordCount'];
var response = '{"Result":"OK", "Records":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + count + '}';
return res.json(JSON.parse(response));
return formateResult(req, res, result);
}
});
});
});
router.post('/infrastructure/list', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
pool.connect(function(err, client, done){
router.post('/child/list/:parent_id', (req, res, next) => {
var sql = getSQLSearchQuery(CHILD, req);
var parameter = [req.params['parent_id']];
winston.log('info', '*** TRACE ***', {
parameter:parameter,
sql:sql
});
var pool = new pg.Pool({
connectionString: connectionString
});
pool.connect(function(err, client, done){
if(err) {
done();
return res.status(500).json({success: false , data: err});
}
var sql = "select * , (select count(*) from cmdb.ci ci2 where ci2.parent_id = ci1.id) as childs from cmdb.ci ci1 where ci1.family_type_id = 5 and ci1.id > 0 order by ci1.family_type_id, ci1.family_id, ci1.display_name";
client.query(sql, [], function(err, result){
client.query(sql, parameter, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' +sql + '"}'));
}
else {
var response = '{"Result":"OK", "Records":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
return formateResult(req, res, result);
}
});
});
});
router.post('/child/list/:parent_id', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
pool.connect(function(err, client, done){
if(err) {
done();
console.log(err);
return res.status(500).json({success: false , data: err});
}
var sql ="select *, id as account, id as group, (select count(*) as childs from cmdb.ci child where child.parent_id = parent.id and family_id <> 14) as childs "; //14 = IT-Tjänstekomponent Access med grupper och konton
sql+="from cmdb.ci parent ";
sql+="where parent.parent_id = $1 and family_id <> 14";
sql+="order by family_type_id, family_id, display_name";
var parameter = [req.params['parent_id']];
client.query(sql, parameter, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' +sql + '"}'));
}
else {
var response = '{"Result":"OK", "Records":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
}
});
});
});
router.post('/list/:id', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
pool.connect(function(err, client, done){
if(err) {
done();
console.log(err);
return res.status(500).json({success: false , data: err});
}
var sql ="select *, id as account, id as group, (select count(*) as childs from cmdb.ci child where child.parent_id = parent.id and family_id <> 14) as childs "; //14 = IT-Tjänstekomponent Access med grupper och konton
sql+="from cmdb.ci parent ";
sql+="where parent.id = $1 and family_id <> 14";
sql+="order by family_type_id, family_id, display_name";
var parameter = [req.params['id']];
client.query(sql, parameter, function(err, result){
done();
if (err){
router.post('/parent/list/:id', (req, res, next) => {
var sql = getSQLSearchQuery(PARENT, req);
var parameter = [req.params['ci_id']];
var pool = new pg.Pool({
connectionString: connectionString
});
pool.connect(function(err, client, done){
if(err) {
done();
return res.status(500).json({success: false , data: err});
}
client.query(sql, parameter, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' +sql + '"}'));
}
else {
var response = '{"Result":"OK", "Records":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
return formateResult(req, res, result);
}
});
});
......@@ -204,7 +217,7 @@ router.post('/list/:id', (req, res, next) => {
router.post('/families/:family_type_id', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
connectionString: connectionString
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
......@@ -236,7 +249,7 @@ router.post('/families/:family_type_id', (req, res, next) => {
router.post('/class_types/:family_id', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
connectionString: connectionString