<commit_before>/**
*  Copyright 2014 IBM
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/

var topologyPoolModel = require('../models/poolmodel');
var Topology = require('../models/topologymodel');
var minstance= require('../models/instancemodel');

exports.findAllView = function(req, res) {
  topologyPoolModel.find({},function(err, docs) {
    if (!docs) {
      console.log('no pools found');
      docs = [];
    }
    res.render('topology/pools/poolindex', {
      title: 'Available Pools',
      docs: docs
    });
  }).populate('topologyRef', 'name');
};

exports.addViewSetup = function(req, res) {
  Topology.find({},function(err, topdocs) {
    if (!topdocs) {
      console.log('no topology found');
      topdocs = [];
    }
    
    res.render('topology/pools/newpool.jade', {
                title: 'Create new pool', 
                topdocs: topdocs
    });
  });
};

var validator = require('validator');
var validatePool = function(doc, next) {
	console.log("validating Pool document");
	console.log(doc);
    if (validator.isNull(doc.name)) {
		next(new Error('Name of the pool can not be null'));
    }
    else {
		console.log('validated json');
		next(null);
	}
};


exports.addViewExecute = function(req, res) {
	validatePool(req.body.pool, function(err) {
		if (! err) {
			var pool = new topologyPoolModel(req.body.pool);
			pool.save(function(err) {
				if (! err) {
					res.redirect('/topology/pools');
				}else {
					console.log('error saving pool');
					console.log(err);
					res.redirect('/topology/pools/new');
				}
			});
		}else {
			console.log('error in addViewExecute validating pool');
			console.log(err);
			res.redirect('/topology/pools');
		}
	});
};

exports.findAll = function(req, res) {
	topologyPoolModel.find({},function(err, docs) {
		if (!docs) {
			console.log('findAll could not find any documents');
			console.log('printing our err');
			console.log(err);
			res.send(404);
		}else {
			res.send(docs);
		}
	});
};

exports.editViewSetup = function(req, res) {
  topologyPoolModel.findById(req.params.id, function(err, doc) {
    res.render('topology/pools/editpool', {
      title: 'Edit Pool',
      pool: doc
    });
  }).populate('topologyRef', 'name');;
};
exports.editViewExecute = function(req, res) {
   topologyPoolModel.findById(req.params.id, function(err, doc) {
    
    doc.name = req.body.pool.name;
    doc.description = req.body.pool.description;
    doc.poolMethod = req.body.pool.poolMethod;
    doc.poolMinAvailable = req.body.pool.poolMinAvailable;
    doc.poolMaxTotal = req.body.pool.poolMaxTotal;
    console.log('attempting to update document');
    console.log(doc);
    validatePool(doc, function(err) {
		if (!err) {
			doc.save(function(err) {
				if (!err) {
					res.redirect('/topology/pools');
				}
				else {
					console.log('error saving to database');
					res.redirect('/topology/pools');
				}
			});
		}else {
			console.log('invalid data:' + err);
			res.redirect('/topology/pools');
		}
    });
  });
};

exports.getInstanceView = function(req, res) {
   console.log('req.params.id:'+req.params.id);
   minstance.find({poolRef:req.params.id}, function(err, instances) {
   if (err) {
     console.log ( 'error get instances associated with pool');
     console.err(err);
     return;
   }   
   instances.forEach( function (instance){
     console.log ( JSON.stringify(instance));
     //res.send( JSON.stringify(instance))
    
    });
  });
};

exports.create = function(req, res) {
	try {
		console.log('creating new topology pool');
		console.log(req.body);
		topologyPoolModel.create(req.body, function(err, pool){ 
			if (! err){
				//return the created topology pool information
				console.log('created new topology pool');
				console.log(pool);
				res.json(pool);
			}else{
				//return an error 
				console.log('failed to create pool using:' + res.json);
				console.log(pool);
				console.log(err);
				res.send(err, 400);
			}
		}); 
	}catch (err) {
		console.log('could not create new topology pool, most likely due to invalid data');
		console.log(err);
		res.send(err, 400);
	}
};
exports.find = function(req, res) {
	console.log('finding ' + req.params.id);
	topologyPoolModel.findById(req.params.id, function(err, doc) {
		if (! doc) {
			res.send(404);
		}else {
			res.send(doc);
		}
	});
};
exports.delete = function(req, res) {
	topologyPoolModel.findById(req.params.id, function(err, doc) {
		if (!doc) {
			res.send(404);
		}else {
			console.log('delete removing');
			doc.remove(function() {
			res.send(200);
		});
		console.log('removed');
		}
	});
};
exports.create = function(req, res) {
	try {
		console.log('creating new topology pool');
		console.log(req.body);
		topologyPoolModel.create(req.body, function(err, pool){ 
			if (! err){
				//return the created topology pool information
				console.log('created new topology pool');
				console.log(pool);
				res.json(pool);
			}else{
				//return an error 
				console.log('failed to create pool using:' + res.json);
				console.log(pool);
				console.log(err);
				res.send(err, 400);
			}
		}); 
	}catch (err) {
		console.log('could not create new topology pool, most likely due to invalid data');
		console.log(err);
		res.send(err, 400);
	}
};
exports.find = function(req, res) {
	console.log('finding ' + req.params.id);
	topologyPoolModel.findById(req.params.id, function(err, doc) {
		if (! doc) {
			res.send(404);
		}else {
			res.send(doc);
		}
	});
};
exports.delete = function(req, res) {
	topologyPoolModel.findById(req.params.id, function(err, doc) {
		if (!doc) {
			res.send(404);
		}else {
			console.log('delete removing');
			doc.remove(function() {
			res.send(200);
		});
		console.log('removed');
		}
	});
};
<commit_msg>check in pool.js<commit_after>/**
*  Copyright 2014 IBM
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/

var topologyPoolModel = require('../models/poolmodel');
var Topology = require('../models/topologymodel');
var minstance= require('../models/instancemodel');

exports.findAllView = function(req, res) {
  topologyPoolModel.find({},function(err, docs) {
    if (!docs) {
      console.log('no pools found');
      docs = [];
    }
    res.render('topology/pools/poolindex', {
      title: 'Available Pools',
      docs: docs
    });
  }).populate('topologyRef', 'name');
};

exports.addViewSetup = function(req, res) {
  Topology.find({},function(err, topdocs) {
    if (!topdocs) {
      console.log('no topology found');
      topdocs = [];
    }
    
    res.render('topology/pools/newpool.jade', {
                title: 'Create new pool', 
                topdocs: topdocs
    });
  });
};

var validator = require('validator');
var validatePool = function(doc, next) {
	console.log("validating Pool document");
	console.log(doc);
    if (validator.isNull(doc.name)) {
		next(new Error('Name of the pool can not be null'));
    }
    else {
		console.log('validated json');
		next(null);
	}
};


exports.addViewExecute = function(req, res) {
	validatePool(req.body.pool, function(err) {
		if (! err) {
			var pool = new topologyPoolModel(req.body.pool);
			pool.save(function(err) {
				if (! err) {
					res.redirect('/topology/pools');
				}else {
					console.log('error saving pool');
					console.log(err);
					res.redirect('/topology/pools/new');
				}
			});
		}else {
			console.log('error in addViewExecute validating pool');
			console.log(err);
			res.redirect('/topology/pools');
		}
	});
};

exports.findAll = function(req, res) {
	topologyPoolModel.find({},function(err, docs) {
		if (!docs) {
			console.log('findAll could not find any documents');
			console.log('printing our err');
			console.log(err);
			res.send(404);
		}else {
			res.send(docs);
		}
	});
};

exports.editViewSetup = function(req, res) {
  topologyPoolModel.findById(req.params.id, function(err, doc) {
    res.render('topology/pools/editpool', {
      title: 'Edit Pool',
      pool: doc
    });
  }).populate('topologyRef', 'name');;
};
exports.editViewExecute = function(req, res) {
   topologyPoolModel.findById(req.params.id, function(err, doc) {
    
    doc.name = req.body.pool.name;
    doc.description = req.body.pool.description;
    doc.poolMethod = req.body.pool.poolMethod;
    doc.poolMinAvailable = req.body.pool.poolMinAvailable;
    doc.poolMaxTotal = req.body.pool.poolMaxTotal;
    console.log('attempting to update document');
    console.log(doc);
    validatePool(doc, function(err) {
		if (!err) {
			doc.save(function(err) {
				if (!err) {
					res.redirect('/topology/pools');
				}
				else {
					console.log('error saving to database');
					res.redirect('/topology/pools');
				}
			});
		}else {
			console.log('invalid data:' + err);
			res.redirect('/topology/pools');
		}
    });
  });
};

exports.getInstanceView = function(req, res) {
   console.log('req.params.id:'+req.params.id);
   minstance.find({poolRef:req.params.id}, function(err, instances) {
   if (err) {
     console.log ( 'error get instances associated with pool');
     console.err(err);
     return;
   }   
   res.render('topology/instances/instanceindex', {
      title: 'Associated Instances',
      docs: instances
    });
  });
};

exports.create = function(req, res) {
	try {
		console.log('creating new topology pool');
		console.log(req.body);
		topologyPoolModel.create(req.body, function(err, pool){ 
			if (! err){
				//return the created topology pool information
				console.log('created new topology pool');
				console.log(pool);
				res.json(pool);
			}else{
				//return an error 
				console.log('failed to create pool using:' + res.json);
				console.log(pool);
				console.log(err);
				res.send(err, 400);
			}
		}); 
	}catch (err) {
		console.log('could not create new topology pool, most likely due to invalid data');
		console.log(err);
		res.send(err, 400);
	}
};
exports.find = function(req, res) {
	console.log('finding ' + req.params.id);
	topologyPoolModel.findById(req.params.id, function(err, doc) {
		if (! doc) {
			res.send(404);
		}else {
			res.send(doc);
		}
	});
};
exports.delete = function(req, res) {
	topologyPoolModel.findById(req.params.id, function(err, doc) {
		if (!doc) {
			res.send(404);
		}else {
			console.log('delete removing');
			doc.remove(function() {
			res.send(200);
		});
		console.log('removed');
		}
	});
};
exports.create = function(req, res) {
	try {
		console.log('creating new topology pool');
		console.log(req.body);
		topologyPoolModel.create(req.body, function(err, pool){ 
			if (! err){
				//return the created topology pool information
				console.log('created new topology pool');
				console.log(pool);
				res.json(pool);
			}else{
				//return an error 
				console.log('failed to create pool using:' + res.json);
				console.log(pool);
				console.log(err);
				res.send(err, 400);
			}
		}); 
	}catch (err) {
		console.log('could not create new topology pool, most likely due to invalid data');
		console.log(err);
		res.send(err, 400);
	}
};
exports.find = function(req, res) {
	console.log('finding ' + req.params.id);
	topologyPoolModel.findById(req.params.id, function(err, doc) {
		if (! doc) {
			res.send(404);
		}else {
			res.send(doc);
		}
	});
};
exports.delete = function(req, res) {
	topologyPoolModel.findById(req.params.id, function(err, doc) {
		if (!doc) {
			res.send(404);
		}else {
			console.log('delete removing');
			doc.remove(function() {
			res.send(200);
		});
		console.log('removed');
		}
	});
};

{'fraction_non_alphanumeric': 0.16285966177767092, 'fraction_numerical': 0.006572092650476877, 'mean_word_length': 3.270362765229295, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6531674', 'n_tokens_mistral': 4672, 'n_tokens_neox': 4147, 'n_words': 905}