Commit 9df48e67 authored by a001188's avatar a001188
Browse files

Some functions works

parent 160966c2
function accessGraph(){
function accessGraph(node_id){
$.ajax({
url: '/cmdb/api/v1/access/nodes'
url: '/cmdb/api/v1/access/node/' + node_id
}).then(function(nodes) {
$.ajax({
url: '/cmdb/api/v1/access/edges/'
url: '/cmdb/api/v1/access/edge/' + node_id
}).then(function(edges) {
// create a network
var container = document.getElementById('cmdb-net');
......@@ -15,7 +15,7 @@ function accessGraph(){
};
for(var i = 0; i<nodes.length; i++){
if(nodes[i].type==='account'){
if(nodes[i].type_id===11){
data.nodes[i].color = '#ffa0a0';
}
else{
......
......@@ -13,10 +13,17 @@ function accessNodesTable(){
fields: {
id: {
title: 'id',
width: '25%',
width: '2%',
list: true,
key: true
},
click: {
title: 'Visa',
display: function(data){
return '<img src="/images/structure.png" title="Nästa nivå" onclick="accessGraph(' + data.record.id + ')"></img>';
},
width: '5%'
},
display_name: {
title: 'Namn',
width: '25%'
......@@ -24,11 +31,16 @@ function accessNodesTable(){
class_id: {
title: 'Typ',
options: '/cmdb/api/v1/options/classes',
width: '70%'
width: '15%'
},
parent_id:{
title: 'Ägare',
options: '/cmdb/api/v1/options/parent',
width: '20%'
},
description: {
title: 'Beskrivning',
width: '70%'
width: '30%'
}
}
});
......
......@@ -16,6 +16,6 @@ $(document).ready(function () {
}
if(ref.includes('/access/')){
accessNodesTable();
accessGraph();
accessGraph(-1);
}
});
\ No newline at end of file
......@@ -5,6 +5,7 @@ const pg = require('pg');
const jQuery = require('jquery');
const path = require('path');
const connectionString = process.env.DATABASE_URL || 'postgres://jasper.u:Me4sgyAp@postgresql-utv.smhi.se:5432/jasper';
const winston = require('winston');
router.post('/types', (req, res, next) => {
......@@ -53,7 +54,7 @@ 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, [], function(err, result){
client.query(sql, parameter, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + sql + '"}'));
......@@ -84,7 +85,7 @@ router.post('/security/:id', (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, [], function(err, result){
client.query(sql, parameter, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + sql + '"}'));
......@@ -120,7 +121,7 @@ router.post('/node/list', (req, res, next) => {
sql+= "select id, display_name as label, description from cmdb.ci c inner join cmdb.relation r on c.id=r.from_ci_id ";
sql+= "group by id, display_name, description"
var parameter = [];
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 + '"}'));
......@@ -150,7 +151,7 @@ router.get('/nodes', (req, res, next) => {
// SQL Query > Select Data
var sql = "select id, display_name as label, 'group' as type from cmdb.ci_family_group union select id, display_name as label, 'account' as type from cmdb.ci_family_account";
var parameter = [];
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 + '"}'));
......@@ -188,6 +189,109 @@ router.get('/edges', (req, res, next) => {
});
});
});
router.get('/node/: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 c.id, c.display_name as label, (select class_type_id from cmdb.class c2 where c2.id=c.group_type_id) as type_id from cmdb.ci_family_group c inner join cmdb.relation r on r.from_ci_id = c.id where r.relation_type_id=0 and r.to_ci_id = $1 ";
sql+= "UNION ";
sql+= "select c.id, c.display_name as label, (select class_type_id from cmdb.class c2 where c2.id=c.group_type_id) as type_id from cmdb.ci_family_group c inner join cmdb.relation r on r.to_ci_id = c.id where r.relation_type_id=0 and r.from_ci_id = $1 ";
sql+= "UNION ";
sql+= "select c.id, c.display_name as label, (select class_type_id from cmdb.class c2 where c2.id=c.group_type_id) as type_id from cmdb.ci_family_group c where c.id = $1 ";
sql+= "UNION ";
sql+= "select c.id, c.display_name as label, (select class_type_id from cmdb.class c2 where c2.id=c.account_type_id) as type_id from cmdb.ci_family_account c inner join cmdb.relation r on r.from_ci_id = c.id where r.relation_type_id=0 and r.to_ci_id = $1 ";
sql+= "UNION ";
sql+= "select c.id, c.display_name as label, (select class_type_id from cmdb.class c2 where c2.id=c.account_type_id) from cmdb.ci_family_account c inner join cmdb.relation r on r.to_ci_id = c.id where r.relation_type_id=0 and r.from_ci_id = $1 ";
sql+= "UNION ";
sql+= "select c.id, c.display_name as label, (select class_type_id from cmdb.class c2 where c2.id=c.account_type_id) from cmdb.ci_family_account c where c.id = $1 ";
var parameter = [req.params['id']];
client.query(sql, parameter, function(err, result){
done();
if (err){
winston.log('error', '/access/node/:id', {
paramter: parameter,
sql: sql
})
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + sql + '"}'));
}
else {
return res.json(result.rows);
}
});
});
});
router.get('/edge/: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_ci_id as from, to_ci_id as to from cmdb.relation where from_ci_id = $1 or to_ci_id = $1;";
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 {
return res.json(result.rows);
}
});
});
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment