Beispiel #1
0
def reg_items(kw):
	"""注册items
	"""	
	if 'gkey' not in kw:
		return stat_base.err_handle.gkey_not_set
	if 'items' not in kw:
		return stat_base.err_handle.items_not_set
	prefix = kw.pop('prefix','') 	
	res,desc = stat_base.reg_items(kw['gkey'],kw['items'],prefix)
	#注册成功,写入redis缓存
	if res ==0:
		stat_base.reg_items2redis(kw['gkey'],[k[1] for k in kw['items']])
	return res,desc
Beispiel #2
0
def set(kw):
	"""上传值,先从redis缓存中取出item的id,再匹配itemkey入history库
	参数说明:
	gkey group 的gkey
	data 数据集
	  {'itemkey':{'value':值,'clock':生成时间,默认为当前时间戳}}
	"""
	if 'gkey' not in kw:
		return stat_base.err_handle.gkey_not_set
	if 'data' not in kw:
		return stat_base.err_handle.data_not_found
	res,desc = stat_base.set(kw['gkey'],kw['data'])
	if res <0:
		stat_base.reg_items2redis(kw['gkey'],list(kw['data'].keys()))
		return stat_base.set(kw['gkey'],kw['data'])
	else:		
		return 	res,desc
Beispiel #3
0
def reg_items_mysql(name,info):
	"""items来源于mysql数据表,自动注册items
	"""
	k = info['source']
	dbinfo = get_host_by_data(k)
	if not dbinfo:
		return [-1,"%s not find." % k]
	dbinfo['dbname'] = k.split('.')[-1]
	db = mysqlwrap.dbclass(dbinfo)
	res,desc = db.connect()
	if res ==-1:
		return res,desc
	
	idfield = info.get("id","id")
	key_prefix = info.get("key_prefix","")
	sql_item={}
	sql_item['table']=info['table']
	sql_item['fields']="%s,%s,%s" %(idfield,info['key'],info['name'])
	where =  info['where'] if 'where' in info and info['where'] else ""
	sql_item['where'] = where
	sql_item['limit'] = 1000
	id = 0
	item_total=0	
	while True:		
		sql_item['where'] = "%s and %s>%s" %(where,idfield,id) if where else "%s>%s" %(idfield,id)
		res,desc = db.query(sql_item)
		if res==-1 or res==0 and not desc:
			 break
		itm=[]
		for row in desc:
			itm.append([row[info['name']],row[info['key']]])
			print(itm[-1])
			id = row[idfield]
		rs,ds = stat_base.reg_items(name,itm,key_prefix)
		#print(rs,ds)
		if rs==0:
			item_total+=len(itm)
	stat_base.reg_items2redis(name)
	return [0,item_total]
Beispiel #4
0
def reg_items2redis(kw):
	if 'gkey' not in kw:
		return [-1,'gkey not set.']
	itms = kw.get('items',[])
	
	return 	stat_base.reg_items2redis(kw['gkey'],itms)