import sys
import os
dir_common = os.path.split(os.path.realpath(__file__))[0] + '/../'
sys.path.append(dir_common) # 将根目录添加到系统目录,才能正常引用common文件夹
# 使用hbase向数据库中添加数据,使用前需要启动hbase和thrift服务器
# 启动hbase /usr/local/hbase/bin/start-hbase.sh 默认端口为60000,信息端口为60010
# 启动thrift服务器 /usr/local/hbase/bin/hbase-daemon.sh start thrift 默认端口为9090
import logging
import time,datetime
import argparse
from aiohttp import web
import aiohttp
import asyncio
import base64
import logging
import uvloop
import time,datetime
import json
import requests,random
routes = web.RouteTableDef()
@routes.get('/')
async def hello(request):
return web.Response(text="Hello, world")
# 查询
@routes.post('/query')
async def query(request): # 异步监听,只要一有握手就开始触发
try:
data = await request.json() # 等待post数据完成接收,只有接收完成才能进行后续操作.data['key']获取参数
# print('query:\n', data)
except Exception as e:
logging.error("image file too large")
return web.json_response(write_response(1,"image file too large",8,{}))
logging.info('query request start %s' % datetime.datetime.now())
range_from = datetime.datetime.strptime(data['range']['from'], "%Y-%m-%dT%H:%M:%S.%fZ")
range_to = datetime.datetime.strptime(data['range']['to'], "%Y-%m-%dT%H:%M:%S.%fZ")
intervalMs = data['intervalMs']
print(range_from,range_to,intervalMs)
querys = data['targets']
results = []
for query in querys:
querystr = query['target'] # 查询语句
if querystr[0]!="{":
querystr = '{'+querystr+"}"
result = {
"target": querystr,
"datapoints": []
}
for timetamp in range(int(range_from.timestamp()*1000),int(range_to.timestamp()*1000),intervalMs):
habse数据= 你的业务逻辑查询出来的hbase数据
result['datapoints'].append([habse数据,timetamp])
results.append(result)
logging.info('query request finish %s, %s' % (datetime.datetime.now(),str(results)))
header = {"Access-Control-Allow-Origin": "*", 'Access-Control-Allow-Methods': 'GET,POST'}
# print(results)
return web.json_response(results,headers=header)
# 查询
@routes.post('/annotations')
async def annotations(request): # 异步监听,只要一有握手就开始触发
try:
data = await request.json() # 等待post数据完成接收,只有接收完成才能进行后续操作.data['key']获取参数
print('annotations:\n',data)
except Exception as e:
logging.error("image file too large")
return web.json_response(write_response(1,"image file too large",8,{}))
logging.info('annotations request start %s' % datetime.datetime.now())
header = {"Access-Control-Allow-Origin": "*", 'Access-Control-Allow-Methods': 'GET,POST'}
results=[
{
"annotation": data['annotations'],
"title": "hbase traffic",
"time": int(time.time()*1000),
"text": "Joe causes brain split",
"tags": "joe, cluster, failure"
}
]
return web.json_response(results,headers=header)
# 搜索,就是自动提示功能,当用户输入一个字符,返回可以补齐的内容
@routes.post('/search')
async def search(request): # 异步监听,只要一有握手就开始触发
try:
data = await request.json() # 等待post数据完成接收,只有接收完成才能进行后续操作.data['key']获取参数
searchstr=data['target'] # 搜索字符串
print('search:\n',data)
except Exception as e:
logging.error("image file too large")
return web.json_response(write_response(1,"image file too large",8,{}))
logging.info('face_det dect request start %s' % datetime.datetime.now())
result = ['提示内容1','提示内容2']
logging.info('face_det dect request finish %s, %s' % (datetime.datetime.now(),json.dumps(result)))
header = {"Access-Control-Allow-Origin": "*", 'Access-Control-Allow-Methods': 'GET,POST'}
return web.json_response(result,headers=header)
# 查询
@routes.post('/tag-keys')
async def tagkeys(request): # 异步监听,只要一有握手就开始触发
try:
data = await request.json() # 等待post数据完成接收,只有接收完成才能进行后续操作.data['key']获取参数
print('tag-keys:\n',data)
except Exception as e:
logging.error("image file too large")
return web.json_response(write_response(1,"image file too large",8,{}))
logging.info('face_det dect request start %s' % datetime.datetime.now())
result = {'data': 1}
logging.info('face_det dect request finish %s, %s' % (datetime.datetime.now(),json.dumps(result)))
header = {"Access-Control-Allow-Origin": "*", 'Access-Control-Allow-Methods': 'GET,POST'}
return web.json_response(result,headers=header)
# 查询
@routes.post('/tag-values')
async def tagvalues(request): # 异步监听,只要一有握手就开始触发
try:
data = await request.json() # 等待post数据完成接收,只有接收完成才能进行后续操作.data['key']获取参数
print('tag-values:\n',data)
except Exception as e:
logging.error("image file too large")
return web.json_response(write_response(1,"image file too large",8,{}))
result = {'data':1}
logging.info('face_det dect request finish %s, %s' % (datetime.datetime.now(),json.dumps(result)))
header = {"Access-Control-Allow-Origin": "*", 'Access-Control-Allow-Methods': 'GET,POST'}
return web.json_response(result,headers=header)
if __name__ == '__main__':
app = web.Application(client_max_size=2*1024**2) # 创建app,设置最大接收图片大小为2M
app.add_routes(routes) # 添加路由映射
web.run_app(app,host='0.0.0.0',port=4567) # 启动app
logging.info('server close:%s'% datetime.datetime.now())