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

Delete ci.js

parent 3360e429
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.storageConfig;
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 * , (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));
}
});
});
});
//select * , (select count(*) from cmdb.ci child where child.parent_id = id) as childs from cmdb.ci where parent_id = 27 order by display_name
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 * , (select count(*) as childs from cmdb.ci child where child.parent_id = parent.id) as childs from cmdb.ci parent where parent.parent_id = $1 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('/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});
}
// 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