Commit e0904bd7 authored by Peter Lundin's avatar Peter Lundin
Browse files

Delete structure.js

parent 4c8a6fff
var express = require('express');
var router = express.Router();
const jQuery = require('jquery');
const path = require('path');
const config = require('./config');
const pg = require('pg');
const winston = require('winston');
const connectionString = process.env.DATABASE_URL || config.storageConfig;
router.post('/family_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 *, (select count(id) from cmdb.family f where f.family_type_id=ft.id) as cnt from cmdb.family_type ft order by display_name;";
var parameter = [];
client.query(sql, parameter, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"/family_type"}'));
}
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 *, (select count(id) from cmdb.class_type ct where ct.family_id=f.id) as cnt from cmdb.family f where f.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){
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});
}
// 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;
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