Commit 75c31b2a authored by a001188's avatar a001188
Browse files

Some functions work

parent 64527781
......@@ -7,6 +7,7 @@ var bodyParser = require('body-parser');
var lessMiddleware = require('less-middleware');
var $ = require("jquery");
var index = require('./routes/index');
var relation = require('./routes/relation');
var organisation = require('./routes/organisation');
var structure = require('./routes/structure');
var users = require('./routes/users');
......@@ -29,6 +30,7 @@ app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/view', index);
app.use('/users', users);
app.use('/cmdb/api/v1/relation', relation);
app.use('/cmdb/api/v1/organisation', organisation);
app.use('/cmdb/api/v1/structure', structure);
......
function relationGraph(){
$.ajax({
url: '/cmdb/api/v1/relation/nodes/'
}).then(function(nodes) {
$.ajax({
url: '/cmdb/api/v1/relation/edges/'
}).then(function(edges) {
// create a network
var container = document.getElementById('cmdb-net');
// provide the data in the vis format
var data = {
nodes: nodes,
edges: edges
};
var options = {};
// options.nodes = {
// color: 'gray'
// }
// initialize your network!
var network = new vis.Network(container, data, options);
});
});
}
\ No newline at end of file
function relationNodesTable(){
$('#cmdb-table').jtable({
title: "Nodlista",
paging: false, //Enable paging
pageSize: 10, //Set page size (default: 10)
pageList: 'minimal',
sorting: false, //Enable sorting
multiSorting: false,
defaultSorting: 'display_name ASC', //Set default sorting
actions: {
listAction: '/cmdb/api/v1/relation/node/list'
},
fields: {
id: {
title: 'id',
width: '25%',
list: false,
key: true
},
label: {
title: 'Namn',
width: '25%'
},
description: {
title: 'Beskrivning',
width: '70%'
}
}
});
$('#cmdb-table').jtable('load');
}
function relationTypesTable(){
$('#cmdb-table').jtable({
title: "Typer av relationer",
paging: false, //Enable paging
pageSize: 10, //Set page size (default: 10)
pageList: 'minimal',
sorting: false, //Enable sorting
multiSorting: false,
defaultSorting: 'display_name ASC', //Set default sorting
actions: {
listAction: '/cmdb/api/v1/relation/types/'
},
fields: {
id: {
title: 'id',
width: '25%',
list: false,
key: true
},
display_name: {
title: 'Namn',
width: '25%'
},
description: {
title: 'Beskrivning',
width: '70%'
}
}
});
$('#cmdb-table').jtable('load');
}
$(document).ready(function () {
var ref = window.location.href;
if(ref.endsWith('family-class')){
if(ref.endsWith('family-and-class')){
structureTable();
structureGraph(0);
}
......@@ -9,5 +9,11 @@ $(document).ready(function () {
organiationTable();
organisationGraph(0);
}
else;
if(ref.endsWith('relation-types')){
relationTypesTable();
}
if(ref.endsWith('access')){
relationNodesTable();
relationGraph();
}
});
\ No newline at end of file
var express = require('express');
var router = express.Router();
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';
router.post('/types', (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.relation_type order by display_name;";
var parameter = [];
client.query(sql, parameter, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"relation_type"}'));
}
else {
var response = '{"Result":"OK", "Records":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
}
});
});
});
router.post('/node/list', (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();
console.log(err);
return res.status(500).json({success: false , data: err});
}
// SQL Query > Select Data
var sql = "select id, display_name as label, description from cmdb.ci c inner join cmdb.relation r on c.id=r.to_ci_id ";
sql+= "union ";
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){
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.get('/nodes', (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 id, display_name as label, description from cmdb.ci c inner join cmdb.relation r on c.id=r.to_ci_id ";
sql+= "union ";
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 "
var parameter = [];
client.query(sql, [], function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + sql + '"}'));
}
else {
return res.json(result.rows);
}
});
});
});
router.get('/edges', (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;";
client.query(sql, [], function(err, result){
done();
if (err){
console.log('paramter: ' + JSON.stringify(parameter));
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + sql + '"}'));
}
else {
return res.json(result.rows);
}
});
});
});
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});
}
// all leaf
var sql = "WITH RECURSIVE tree(id, display_name, parent_id) AS (";
sql+= "SELECT id, display_name, parent_id FROM cmdb.ci c WHERE c.id = $1 ";
sql+= "UNION ";
sql+= "SELECT sub.id, sub.display_name, sub.parent_id ";
sql+= "FROM tree, cmdb.ci sub ";
sql+= "WHERE tree.id = sub.parent_id) ";
sql+= "SELECT id, display_name as label ";
sql+= "FROM tree order by display_name ";
sql+= "limit 100; ";
/*
too root
WITH RECURSIVE org(id, display_name, parent_organisation_id) AS (
SELECT id, display_name, parent_organisation_id FROM cmdb.organisation org WHERE org.id = 6
UNION
SELECT sub.id, sub.display_name, sub.parent_organisation_id
FROM org, cmdb.organisation sub
WHERE sub.id = org.parent_organisation_id
)
SELECT id, display_name, parent_organisation_id
FROM org
limit 10
*/
// var sql = "select id, concat(id, '. ', (select display_name from cmdb.organisation_type as ot where// ot.id=organisation_type), ': ', display_name) as label from cmdb.organisation where id >0;";
var centralNode = req.params['centralNode'];
client.query(sql, [centralNode], function(err, result){
done();
if (err){ //
//console.log(err.message);
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + err.message + '"}'));
}
else {
return res.json(result.rows);
}
});
});
});
router.get('/list/edges/: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});
}
var sql = "WITH RECURSIVE tree(id, display_name, parent_id) AS (";
sql+= "SELECT id, display_name, parent_id FROM cmdb.ci c WHERE c.id = $1 ";
sql+= "UNION ";
sql+= "SELECT sub.id, sub.display_name, sub.parent_id ";
sql+= "FROM tree, cmdb.ci sub ";
sql+= "WHERE tree.id = sub.parent_id) ";
sql+= "SELECT id as from, parent_id as to ";
sql+= "FROM tree ";
sql+= "limit 100; ";
/*
too root
WITH RECURSIVE org(id, display_name, parent_organisation_id) AS (
SELECT id, display_name, parent_organisation_id FROM cmdb.organisation org WHERE org.id = 6
UNION
SELECT sub.id, sub.display_name, sub.parent_organisation_id
FROM org, cmdb.organisation sub
WHERE sub.id = org.parent_organisation_id
)
SELECT id, display_name, parent_organisation_id
FROM org
limit 10
*/
var centralNode = [req.params['centralNode']];
client.query(sql, centralNode, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + sql + '"}'));
}
else {
return res.json(result.rows);
}
});
});
});
module.exports = router;
......@@ -22,6 +22,9 @@ html
script(type='text/javascript', src='/javascripts/dashlets/organisation-graph.js')
script(type='text/javascript', src='/javascripts/dashlets/structure-table.js')
script(type='text/javascript', src='/javascripts/dashlets/structure-graph.js')
script(type='text/javascript', src='/javascripts/dashlets/relationTypes-table.js')
script(type='text/javascript', src='/javascripts/dashlets/relation-table.js')
script(type='text/javascript', src='/javascripts/dashlets/relation-graph.js')
body
block menu
......
......@@ -14,21 +14,28 @@ block menu
a(href="#") Struktur
ul
li
a(href="/view/family-class") Familjer och Klasser
a(href="/view/family-and-class") Familjer och Klasser
li
a(href="/view/relation") Relationer
li
a(href="/view/account") Kontoplan
li
a(href="/view/site") Siter och lokaler
a(href="/view/relation-types") Relationstyper
li
a(href="#") Konfiguration
a(href="#") Teknisk konfiguration
ul
li
a(href="/view/enterprise-service") Enterprise Service
li
a(href="/view/infrastructure") Infrastruktur
li
a(href="/view/access") Behörighet
li
a(href="#") Övrig konfiguration
ul
li
a(href="/view/organisation") Organisation
li
a(href="/view/person") Person
li
a(href="/view/financial-account") Kontoplan
li
a(href="/view/site") Siter och lokaler
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