hubot-6.0

增删改查部分

(function() {
module.exports = function(robot) {
    const elasticsearch = require('elasticsearch');
    const esClient = new elasticsearch.Client({
          host: 'http://10.16.238.93:9200/',
          log: 'error'
        });
	const uuid = require('node-uuid');
	
	function getSearch(keyOrValue,andOr,way) {
		var search;
		if(way == "byValue") {
			search = {
				index: "test_index",
				type: "test_type",
				body: {
					query: {
					  match: {
						value: {
							query : keyOrValue,
							operator:andOr
						}
					  }
					}
				}
			};
		} else {
			search = {
				index: "test_index",
				type: "test_type",
				body: {
					query: {
					  match: {
						key: {
							query : keyOrValue,
							operator:andOr
						}
					  }
					}
				}
			};
		}
		return search;
	}
	
	// 写入数据-----------------------------------------------------------
    robot.respond(/put (.*)(\s)([\s\S]*)/i, function(msg) {
		var key = msg.match[1];
		var value = msg.match[3];
		var operator = msg.envelope.user.name;
		putDataToES(key,value,msg,operator);
	});
	
	function putDataToES(key,value,msg,operator) {
        var data = {
            index: 'test_index',
            type: 'test_type',
            id: uuid.v4(),
            body: {
                key : key,
                value : value,
				operator : operator,
				date : (new Date()).toLocaleString()
            }
        };
		var search = getSearch(key,"and");
		esClient.search(search).then(function (res) {
			if(res.hits.hits.length > 0) {
				for(var i = 0; i < res.hits.hits.length; i++) {
					if(key.length == (res.hits.hits[i]._source.key).length) {
						msg.reply(" \n "+"This key : [ "+key+" ]already exsit!");
						return;
					} else {
							esClient.create(data).then(function (res) {
								msg.reply(" \n "+"[ INSERT SUCCESS ]");
							});
						}
				}
			} else {
					esClient.create(data).then(function (res) {
						msg.reply(" \n "+"[ INSERT SUCCESS ]");
					});
			}
		});

    }
	//  写入结束-----------------------------------------------------------
	
	//  更新开始-----------------------------------------------------------
    robot.respond(/update (.*)(\s)([\s\S]*)/i, function(msg) {
		var key = msg.match[1];
		var value = msg.match[3];
		var operatorNew = msg.envelope.user.name
		getMessByKey(key,value,operatorNew,msg,"update")
    });
	
	function updateDataById(id,value,operatorNew,msg) {
		var search = {
			index:'test_index', 
			type:'test_type', 
			id:id
        };
		esClient.get(search).then(function (res) {
			if(operatorNew != res._source.operator) {
				msg.reply(" \n "+"Permission is not enough to operator.");
				return;
			} else {
				var data = {
					index:'test_index', 
					type:'test_type', 
					id:id, 
					body:{ 
						doc:{ 
							value: value,
							date: (new Date()).toLocaleString()
						} 
					}
				};
		
				esClient.update(data).then(function (res) {
					msg.reply(" \n "+"[ UPDATE SUCCESS ]");
				});
			}
		});


	}
	//  更新结束-----------------------------------------------------------
	
	//  删除开始-----------------------------------------------------------
    robot.respond(/delete (.*)/i, function(msg) {
		var operatorNew = msg.envelope.user.name
		var key = msg.match[1];
		getMessByKey(key,null,operatorNew,msg,"delete")
	});

	function deleteDateById(id,operatorNew,msg) {
		var search = {
			index:'test_index', 
			type:'test_type', 
			id:id
        };
		
		esClient.get(search).then(function (res) {
			if(operatorNew != res._source.operator) {
				msg.reply(" \n "+"Permission is not enough to operator.");
				return;
			} else {
				var data = {
					index: 'test_index',
					type: 'test_type',
					id: id
				};
				esClient.delete(data).then(function (res) {
					msg.reply(" \n "+"[ DELETE SUCCESS ]");
				});
			}
		});
	}
	//  删除结束-----------------------------------------------------------
	
	// 查找输入的key是否存在
	function getMessByKey(key,value,operatorNew,msg,command) {
		var search = getSearch(key,"and");
		esClient.search(search).then(function (res) {
			var temString = res.hits.hits;
			var len = temString.length;
			var id;
			var operatorOld;
			
			if(len != 0) {
				operatorOld = temString[0]._source.operator;
				for(var i = 0; i < len; i++) {
					if(key.length == (temString[i]._source.key).length) {
						id = temString[i]._id;
						if(command == "update"){
							updateDataById(id,value,operatorNew,msg);
						} else if(command == "delete") {
							deleteDateById(id,operatorNew,msg);
						} 
					}
				}
			} else {
				msg.reply(" \n "+"This key : [ "+key+" ]not exsit!");
			}
		});
    }
 };
}).call(this);

分页查找部分

(function() {
module.exports = function(robot) {
    const elasticsearch = require('elasticsearch');
    const esClient = new elasticsearch.Client({
          host: 'http://10.16.238.93:9200/',
          log: 'error'
        });
	const uuid = require('node-uuid');
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
	robot.respond(/help/i, function(res) {
      return res.reply(" \n "+"[ bot put key enter value ] : put data to ES" + " \n "
                      +"[ bot keyword ] : get data from ES" + " \n "
					  +"[ bot log keyword ] : get data log" + " \n "
					  +"[ bot delete keyword ] : delete data from ES" + " \n "
					  +"[ bot update keyword ] : update data from ES" + " \n ")
    });

	function getSearch(keyOrValue,andOr,way) {
		var search;
		if(way == "byValue") {
			search = {
				index: "test_index",
				type: "test_type",
				body: {
					query: {
					  match: {
						value: {
							query : keyOrValue,
							operator:andOr
						}
					  }
					}
				}
			};
		} else {
			search = {
				index: "test_index",
				type: "test_type",
				body: {
					query: {
					  match: {
						key: {
							query : keyOrValue,
							operator:andOr
						}
					  }
					}
				}
			};
		}
		return search;
	}
	
	// 查找log开始-----------------------------------------------------------
	robot.respond(/log (.*)/i, function(msg) {
		var key = msg.match[1];
		findPageByKey(msg,key,"and","log");
    });
	// 查找log结束-----------------------------------------------------------
	
	//查找数据基本信息开始---------------------------------------------------
	robot.respond(/(.*)/i, function(msg) {
		var key = msg.match[1];
		var array = key.split(" ");
		
		if(array[0] == "put" || 
		   array[0] == "update" || 
		   array[0] == "delete" || 
		   array[0] == "log" || 
		   array[0] == "next" || 
		   array[0] == "front" ||
		   array[0] == "first" ||
		   array[0] == "last" ||
		   array[0] == "help") {
			return;
		}
		//	默认 key/and组合
		findPageByKey(msg,key,"and","base");
	});
	
	//	定义map key为用户名,value数据信息
	var myMap = new Map();
	//	每页大小
	var pageItems = 3;
	
	function findPageByKey(msg,keyOrValue,andOr,type) {
		//	默认 key and 组合查询
		var search = getSearch(keyOrValue,andOr);
		
		if(andOr == "or") {
			search = getSearch(keyOrValue,"or");
		} else if(andOr == "value") {
			search = getSearch(keyOrValue,"or","byValue");
		}
		
		esClient.search(search).then(function (res) {
			var temString = res.hits.hits;
			var len = temString.length;
			//	key/and查完没结果,换为key/or组合
			if(len == 0 && andOr == "and") {
				findPageByKey(msg,keyOrValue,"or",type);
				//	key/or查完没结果,用value/or查
			} else if(len == 0 && andOr == "or") {
				findPageByKey(msg,keyOrValue,"value",type)
				//	value查完没结果,那就是真没有结果了
			} else if(len == 0 && andOr == "value") {
				msg.reply(" \n "+"Sorry! No relevant records:["+keyOrValue+ "]");
				//	查完有结果,判断是查询什么信息:log / base
			} else if(type=="log") {
				var pagecontent = {
					totalItems:len, 
					currentPage:1, 
					pageItems:pageItems, 
					content:temString,
					type:type
				};
				
				var usrname = msg.envelope.user.name;
				myMap.set(usrname, pagecontent);
				show(msg,"log");
			} else if(type=="base") {
				var pagecontent = {
					totalItems:len, 
					currentPage:1, 
					pageItems:pageItems, 
					content:temString,
					type:type
				};
				
				var usrname = msg.envelope.user.name;
				myMap.set(usrname, pagecontent);
				show(msg,"base");
			}
		});
	}
	
	robot.respond(/next/i, function(msg) {
		var usrname = msg.envelope.user.name; 
		var pagecontent = myMap.get(usrname);
		var currentPage = pagecontent.currentPage;
		var totalItems = pagecontent.totalItems;
		var pageItems = pagecontent.pageItems;
		var temString = pagecontent.content;
		var type = pagecontent.type;
		console.log("type:"+type);
		if(currentPage * pageItems < totalItems) {
			pagecontent = {
				totalItems:totalItems, 
				currentPage:currentPage+1, 
				pageItems:pageItems, 
				content:temString,
				type:type
			}
			myMap.set(usrname,pagecontent);
			show(msg,type);
		} else {
			msg.send("Already on the last page");
		}
	});

	robot.respond(/front/i, function(msg) {
		var usrname = msg.envelope.user.name; 
		//	根据用户名从map中取出该用户查询的数据
		var pagecontent = myMap.get(usrname);
		var currentPage = pagecontent.currentPage;
		var totalItems = pagecontent.totalItems;
		var pageItems = pagecontent.pageItems;
		var temString = pagecontent.content;
		var type = pagecontent.type;
		//	如果当前页大于第一页,方可进行查询 前一页 的操作
		if(currentPage > 1) {
				pagecontent = {
				totalItems:totalItems, 
				currentPage:currentPage-1, 
				pageItems:pageItems, 
				content:temString,
				type:type
			}
			//	重新设置该map
			myMap.set(usrname,pagecontent);
			show(msg,type);
		} else {
			msg.send("Already on the first page");
		}
	});
	
	robot.respond(/first/i, function(msg) {
		var usrname = msg.envelope.user.name; 
		var pagecontent = myMap.get(usrname);
		var totalItems = pagecontent.totalItems;
		var pageItems = pagecontent.pageItems;
		var temString = pagecontent.content;
		var type = pagecontent.type;

		pagecontent = {
				totalItems:totalItems, 
				currentPage:1, 
				pageItems:pageItems, 
				content:temString,
				type:type
		}
		
		myMap.set(usrname,pagecontent);
		show(msg,type);
	});
	
	robot.respond(/last/i, function(msg) {
		var usrname = msg.envelope.user.name; 
		var pagecontent = myMap.get(usrname);
		var totalItems = pagecontent.totalItems;
		var pageItems = pagecontent.pageItems;
		var temString = pagecontent.content;
		var type = pagecontent.type;

		pagecontent = {
				totalItems:totalItems, 
				currentPage:Math.ceil(totalItems/pageItems),
				pageItems:pageItems, 
				content:temString,
				type:type
		}
		
		myMap.set(usrname,pagecontent);
		show(msg,type);
	});
	
	function show(msg,type) {
		var pagecontent = myMap.get(msg.envelope.user.name);
		var currentPage = pagecontent.currentPage;
		var totalItems = pagecontent.totalItems;
		var pageItems = pagecontent.pageItems;
		var temString = pagecontent.content;
		var resString = ""

		for(var i = (currentPage - 1) * pageItems; i < totalItems && i < currentPage * pageItems; i++) {
			if(type == "base") {
				resString = resString + (i+1) + " \r " 
				+ "----------------------------------------" + " \n " 
				+ "[ "+ temString[i]._source.key + " ] " + " \n " 
				+ temString[i]._source.value+ " \n " + " \n " 
			} else {
			    resString = resString + (i+1) + " \r " 
					+ "----------------------------------------" + " \n " 
					+ "[ "+ temString[i]._source.key + " ] " + " \n " 
					+ "id : " + temString[i]._id+ " \n " 
					+ "operator : " + temString[i]._source.operator+ " \n "
					+ "date : " + temString[i]._source.date+ " \n " + " \n " 
			}
		}
		resString += "CurrentPage:"+"["+currentPage+"]"+" \t "+" "
				+ "TotalPage:"+"["+Math.ceil(totalItems/pageItems)+"]"+" \t "
				+ "TotalItems:"+"["+totalItems+"]"+" \n "
		msg.send(resString);
	}
	//查找数据基本信息结束---------------------------------------------------
 };
}).call(this);

 

你可能感兴趣的:(Company-Data)