Commit 02a7df6c authored by a001188's avatar a001188
Browse files

Sort implemented

parent dc09ad00
function search_ciTable(){ function search_ciTable(family_name){
$('#cmdb-table').jtable(createSearchCiTable(search_ciColumnDef())); $('#cmdb-table').jtable(createSearchCiTable(search_ciColumnDef(), family_name));
$('#cmdb-table').jtable('load'); $('#cmdb-table').jtable('load');
} }
...@@ -9,13 +9,13 @@ function search_ciColumnDef(){ ...@@ -9,13 +9,13 @@ function search_ciColumnDef(){
prev: { prev: {
title: '', title: '',
width: '1%', width: '1%',
display: renderSearchCiSuperTable, display: renderSearchCiParentTable,
sorting: false sorting: false
}, },
next: { next: {
title: '', title: '',
width: '1%', width: '1%',
display: renderSearchCiSubTable, display: renderSearchCiChildTable,
sorting: false sorting: false
}, },
sibling: { sibling: {
...@@ -95,9 +95,14 @@ function search_ciColumnDef(){ ...@@ -95,9 +95,14 @@ function search_ciColumnDef(){
function createSearchCiTable(ciColumnDef){ function createSearchCiTable(ciColumnDef, family_name){
var table = { var table = {
title: "Sök", title: function (){
if(family_name !== 'Sök')
return 'Sök: ' + family_name;
else
return 'Sök';
},
paging: true, //Enable paging paging: true, //Enable paging
pageSize: 10, //Set page size (default: 10) pageSize: 10, //Set page size (default: 10)
pageList: 'minimal', pageList: 'minimal',
...@@ -115,7 +120,7 @@ function search_ciColumnDef(){ ...@@ -115,7 +120,7 @@ function search_ciColumnDef(){
return $.Deferred(function ($dfd) { return $.Deferred(function ($dfd) {
$.ajax({ $.ajax({
url: '/cmdb/api/v1/ci/search/list?jtStartIndex=' + jtParams.jtStartIndex + '&jtPageSize=' + jtParams.jtPageSize + '&jtSorting=' + jtParams.jtSorting, url: '/cmdb/api/v1/ci/search/list/' + family_name + '?jtStartIndex=' + jtParams.jtStartIndex + '&jtPageSize=' + jtParams.jtPageSize + '&jtSorting=' + jtParams.jtSorting,
type: 'POST', type: 'POST',
dataType: 'json', dataType: 'json',
data: postData, data: postData,
...@@ -136,19 +141,19 @@ function search_ciColumnDef(){ ...@@ -136,19 +141,19 @@ function search_ciColumnDef(){
function renderSearchCiSubTable (data) { function renderSearchCiChildTable (data) {
if(data.record.childs > 0){ if(data.record.childs > 0){
return $('<img src="/images/child.png" title="Barn"/>').click(function () { return $('<img src="/images/child.png" title="Barn"/>').click(function () {
createSearchCiSubTable(data, $(this).closest('tr')); createSearchCiChildTable(data, $(this).closest('tr'));
}); });
} }
} }
function createSearchCiSubTable(data, row){ function createSearchCiChildTable(data, row){
$('#cmdb-table').jtable('openChildTable', row, { $('#cmdb-table').jtable('openChildTable', row, {
title: 'Barn till ' + data.record.family_type_name + ' ' + data.record.family_name + ' ' + data.record.ci_name, title: 'Barn till: ' + data.record.family_type_name + ' ' + data.record.family_name + ' ' + data.record.ci_name,
paging: true, //Enable paging paging: true, //Enable paging
pageSize: 10, //Set page size (default: 10) pageSize: 10, //Set page size (default: 10)
pageList: 'minimal', pageList: 'minimal',
...@@ -178,7 +183,7 @@ function renderSearchCiSiblingTable(data) { ...@@ -178,7 +183,7 @@ function renderSearchCiSiblingTable(data) {
function createSearchCiSiblingTable(data, row){ function createSearchCiSiblingTable(data, row){
$('#cmdb-table').jtable('openChildTable', row, { $('#cmdb-table').jtable('openChildTable', row, {
title: 'Syskon till ' + data.record.family_type_name + ' ' + data.record.family_name + ' ' + data.record.ci_name, title: 'Syskon till: ' + data.record.family_type_name + ' ' + data.record.family_name + ' ' + data.record.ci_name,
paging: true, //Enable paging paging: true, //Enable paging
pageSize: 10, //Set page size (default: 10) pageSize: 10, //Set page size (default: 10)
pageList: 'minimal', pageList: 'minimal',
...@@ -196,19 +201,19 @@ function createSearchCiSiblingTable(data, row){ ...@@ -196,19 +201,19 @@ function createSearchCiSiblingTable(data, row){
} }
function renderSearchCiSuperTable (data) { function renderSearchCiParentTable (data) {
if(data.record.parents > 0){ if(data.record.parents > 0){
return $('<img src="/images/parent.png" title="Förälder"/>').click(function () { return $('<img src="/images/parent.png" title="Förälder"/>').click(function () {
createSearchCiSuperTable(data, $(this).closest('tr')); createSearchCiParentTable(data, $(this).closest('tr'));
}); });
} }
} }
function createSearchCiSuperTable(data, row){ function createSearchCiParentTable(data, row){
$('#cmdb-table').jtable('openChildTable', row, { $('#cmdb-table').jtable('openChildTable', row, {
title: 'Förälder till ' + data.record.family_type_name + ' ' + data.record.family_name + ' ' + data.record.ci_name, title: 'Förälder till: ' + data.record.family_type_name + ' ' + data.record.family_name + ' ' + data.record.ci_name,
paging: true, //Enable paging paging: true, //Enable paging
pageSize: 10, //Set page size (default: 10) pageSize: 10, //Set page size (default: 10)
pageList: 'minimal', pageList: 'minimal',
...@@ -216,7 +221,7 @@ function renderSearchCiSuperTable (data) { ...@@ -216,7 +221,7 @@ function renderSearchCiSuperTable (data) {
multiSorting: true, multiSorting: true,
defaultSorting: 'ci_name ASC', //Set default sorting defaultSorting: 'ci_name ASC', //Set default sorting
actions: { actions: {
listAction: '/cmdb/api/v1/ci/parent/list/' + data.record.parent_id listAction: '/cmdb/api/v1/ci/parent/list/' + data.record.ci_parent_id
}, },
fields: search_ciColumnDef() fields: search_ciColumnDef()
}, },
......
...@@ -22,6 +22,6 @@ $(document).ready(function () { ...@@ -22,6 +22,6 @@ $(document).ready(function () {
accessGraph(-1); accessGraph(-1);
} }
if(ref.includes('/search/')){ if(ref.includes('/search/')){
search_ciTable(); search_ciTable('Sök');
} }
}); });
\ No newline at end of file
...@@ -64,22 +64,26 @@ router.post('/infrastructure/list', (req, res, next) => { ...@@ -64,22 +64,26 @@ router.post('/infrastructure/list', (req, res, next) => {
}); });
function getSQLSearchQuery(query_type, req){ function getSQLSearchQuery(query_type, family_name, req){
var jtSorting = req.query['jtSorting'].trim(); var jtSorting = req.query['jtSorting'].trim();
var sql_select ="select ci1.id as ci_id, ci1.parent_id as ci_parent_id, ci1.class_information, ci1.attribute_information, 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, "; var sql_select ="select ci1.id as ci_id, ci1.parent_id as ci_parent_id, ci1.class_information, ci1.attribute_information, 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 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 where ci1.parent_id = ci2.id and ci2.id > 0) as parents, ";
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, "; 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_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_parent ="where ci1.id = $1 ";
var sql_where_child ="where ci1.parent_id = $1 and f.display_name <> 'Access' "; //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_list ="where ci1.id > 0 ";
if(query_type === SEARCH)
if(family_name !== 'Sök')
sql_where_list += "and f.display_name like '" + family_name + "' ";
var sql_where_search ="and (upper(ci1.display_name) like $1 "; 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.description) like $1 ";
sql_where_search+="or upper(ci1.class_information) like $1 "; sql_where_search+="or upper(ci1.class_information) like $1 ";
...@@ -88,6 +92,7 @@ function getSQLSearchQuery(query_type, req){ ...@@ -88,6 +92,7 @@ function getSQLSearchQuery(query_type, req){
sql_where_search+="or upper(f.display_name) 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+="or upper(ft.display_name) like $1 ";
sql_where_search+=") "; sql_where_search+=") ";
var sql_order_by="order by " + jtSorting + ";"; var sql_order_by="order by " + jtSorting + ";";
...@@ -130,8 +135,9 @@ function formateResult(req, res, result){ ...@@ -130,8 +135,9 @@ function formateResult(req, res, result){
} }
router.post('/search/list', (req, res, next) => { router.post('/search/list/:family_name', (req, res, next) => {
var sql = getSQLSearchQuery(SEARCH, req); var family_name = req.params['family_name'];
var sql = getSQLSearchQuery(SEARCH, family_name, req);
var ci_search = '%' + req.body.search_string.toUpperCase() + '%'; var ci_search = '%' + req.body.search_string.toUpperCase() + '%';
var parameters = [ci_search]; var parameters = [ci_search];
...@@ -159,9 +165,9 @@ router.post('/search/list', (req, res, next) => { ...@@ -159,9 +165,9 @@ router.post('/search/list', (req, res, next) => {
router.post('/child/list/:parent_id', (req, res, next) => { router.post('/child/list/:parent_id', (req, res, next) => {
var sql = getSQLSearchQuery(CHILD, req); var sql = getSQLSearchQuery(CHILD, '', req);
var parameter = [req.params['parent_id']]; var parameter = [req.params['parent_id']];
winston.log('info', '*** TRACE ***', { winston.log('info', '*** CHILD TRACE ***', {
parameter:parameter, parameter:parameter,
sql:sql sql:sql
}); });
...@@ -189,8 +195,12 @@ var pool = new pg.Pool({ ...@@ -189,8 +195,12 @@ var pool = new pg.Pool({
router.post('/parent/list/:id', (req, res, next) => { router.post('/parent/list/:id', (req, res, next) => {
var sql = getSQLSearchQuery(PARENT, req); var sql = getSQLSearchQuery(PARENT, '', req);
var parameter = [req.params['ci_id']]; var parameter = [req.params['id']];
winston.log('info', '*** PARENT TRACE ***', {
parameter:parameter,
sql:sql
});
var pool = new pg.Pool({ var pool = new pg.Pool({
connectionString: connectionString connectionString: connectionString
}); });
......
var express = require('express');
var router = express.Router();
const jQuery = require('jquery');
const path = require('path');
var options = require('./options');
const pg = require('pg');
const config = require('./config');
const winston = require('winston');
const connectionString = process.env.DATABASE_URL || config.db_url;
router.post('/enterprise_service/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});
}
var sql = "select *, id as account, id as group, (select count(*) from cmdb.ci c where c.parent_id = id) as childs from cmdb.ci where family_type_id = 1 and id > 0 order by family_type_id, family_id, display_name";
client.query(sql, [], 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('/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 and ci2.family_id <> 14) as childs, ";
sql_select+="(select count(*) from cmdb.ci ci2 where ci2.id = ci1.parent_id and ci1.parent_id>0) as parents, ";
sql_select+="(select count(*) from cmdb.ci ci2 where ci2.parent_id = ci1.parent_id) as siblings ";
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];
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){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' +sql_cnt + '"}'));
}
else {
cnt = result_cnt.rows[0];
winston.log('info', 'cnt1', {
head: '************************************************************',
cnt:cnt
});
}
});
});
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));
}
});
});
});
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});
}
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 + '"}'));
}
else {
var response = '{"Result":"OK", "Records":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
}
});
});
});
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, ";
sql+="(select count(*) 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+="parent.parent_id as parents, ";
sql+="(select count(*) from cmdb.ci sibling where sibling.parent_id = parent.parent_id) as siblings ";
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){
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('/families/:family_type_id', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
// Handle connection errors
if(err) {
done();
return res.status(500).json({success: false , data: err});
}
// SQL Query > Select Data
var sql = "select * from cmdb.family where family_type_id = $1 order by display_name;";
var parameter = [req.params['family_type_id']];
client.query(sql, parameter, function(err, result){
done();
if (err){
console.log('paramter: ' + JSON.stringify(parameter));
return res.json(JSON.parse('{"Result":"ERROR", "Message":"/family/:family_type_id"}'));
}
else {
var response = '{"Result":"OK", "Records":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
}
});
});
});
router.post('/class_types/:family_id', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
// Handle connection errors
if(err) {
done();
return res.status(500).json({success: false , data: err});
}
// SQL Query > Select Data
var sql = "select * from cmdb.class_type where family_id = $1 order by display_name;";
var parameter = [req.params['family_id']];
client.query(sql, parameter, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"class_type"}'));
}
else {
var response = '{"Result":"OK", "Records":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
}
});
});
});
router.post('/classes/:class_type_id', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
// Handle connection errors
if(err) {
done();
return res.status(500).json({success: false , data: err});
}
// SQL Query > Select Data
var sql = "select * from cmdb.class where class_type_id = $1 order by display_name;";
var parameter = [req.params['class_type_id']];
client.query(sql, parameter, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"class"}'));
}
else {
var response = '{"Result":"OK", "Records":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
}
});
});
});
router.get('/list/nodes/:centralNode', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
// Handle connection errors
if(err) {
done();
return res.status(500).json({success: false , data: err});
}
<