提交 c6a1b300 编写于 作者: J jinhai

Update test and some interface

上级 3da09021
......@@ -13,4 +13,4 @@ db = SQLAlchemy(app)
from engine.model.group_table import GroupTable
from engine.model.file_table import FileTable
from engine.controller import index_manager
from engine.controller import views
......@@ -10,8 +10,7 @@ class GroupHandler(object):
path=path.rstrip("\\")
if not os.path.exists(path):
os.makedirs(path)
print("CreateGroupDirectory, Path: ", path)
return path
@staticmethod
def DeleteGroupDirectory(group_id):
......@@ -20,7 +19,7 @@ class GroupHandler(object):
path=path.rstrip("\\")
if os.path.exists(path):
shutil.rmtree(path)
print("DeleteGroupDirectory, Path: ", path)
return path
@staticmethod
def GetGroupDirectory(group_id):
......
from flask import Flask, jsonify, request
from flask_restful import Resource, Api
from engine import app, db
from engine.model.group_table import GroupTable
from engine.controller.vector_engine import VectorEngine
# app = Flask(__name__)
api = Api(app)
from flask_restful import reqparse
from flask_restful import request
class Vector(Resource):
def __init__(self):
self.__parser = reqparse.RequestParser()
self.__parser.add_argument('vector', type=float, action='append', location=['json'])
def post(self, group_id):
args = self.__parser.parse_args()
vector = args['vector']
return VectorEngine.AddVector(group_id, vector)
class VectorSearch(Resource):
def __init__(self):
self.__parser = reqparse.RequestParser()
self.__parser.add_argument('vector', type=float, action='append', location=['json'])
self.__parser.add_argument('limit', type=int, action='append', location=['json'])
def post(self, group_id):
args = self.__parser.parse_args()
print('vector: ', args['vector'])
# go to search every thing
return VectorEngine.SearchVector(group_id, args['vector'], args['limit'])
class Index(Resource):
def __init__(self):
self.__parser = reqparse.RequestParser()
# self.__parser.add_argument('group_id', type=str)
def post(self, group_id):
return VectorEngine.CreateIndex(group_id)
class Group(Resource):
def __init__(self):
self.__parser = reqparse.RequestParser()
self.__parser.add_argument('group_id', type=str)
self.__parser.add_argument('dimension', type=int, action='append', location=['json'])
def post(self, group_id):
args = self.__parser.parse_args()
dimension = args['dimension']
return VectorEngine.AddGroup(group_id, dimension)
def get(self, group_id):
return VectorEngine.GetGroup(group_id)
def delete(self, group_id):
return VectorEngine.DeleteGroup(group_id)
class GroupList(Resource):
def get(self):
return VectorEngine.GetGroupList()
api.add_resource(Vector, '/vector/add/<group_id>')
api.add_resource(Group, '/vector/group/<group_id>')
api.add_resource(GroupList, '/vector/group')
api.add_resource(Index, '/vector/index/<group_id>')
api.add_resource(VectorSearch, '/vector/search/<group_id>')
# if __name__ == '__main__':
# app.run()
......@@ -10,13 +10,17 @@ import sys, os
class VectorEngine(object):
group_dict = None
SUCCESS_CODE = 0
FAULT_CODE = 1
GROUP_NOT_EXIST = 2
@staticmethod
def AddGroup(group_id, dimension):
group = GroupTable.query.filter(GroupTable.group_name==group_id).first()
if group:
print('Already create the group: ', group_id)
return jsonify({'code': 1, 'group_name': group_id, 'file_number': group.file_number})
return VectorEngine.FAULT_CODE, group_id, group.file_number
# return jsonify({'code': 1, 'group_name': group_id, 'file_number': group.file_number})
else:
print('To create the group: ', group_id)
new_group = GroupTable(group_id, dimension)
......@@ -25,18 +29,16 @@ class VectorEngine(object):
# add into database
db.session.add(new_group)
db.session.commit()
return jsonify({'code': 0, 'group_name': group_id, 'file_number': 0})
return VectorEngine.SUCCESS_CODE, group_id, 0
@staticmethod
def GetGroup(group_id):
group = GroupTable.query.filter(GroupTable.group_name==group_id).first()
if group:
print('Found the group: ', group_id)
return jsonify({'code': 0, 'group_name': group_id, 'file_number': group.file_number})
return VectorEngine.SUCCESS_CODE, group_id, group.file_number
else:
print('Not found the group: ', group_id)
return jsonify({'code': 1, 'group_name': group_id, 'file_number': 0}) # not found
return VectorEngine.FAULT_CODE, group_id, 0
@staticmethod
......@@ -54,9 +56,9 @@ class VectorEngine(object):
db.session.delete(record)
db.session.commit()
return jsonify({'code': 0, 'group_name': group_id, 'file_number': group.file_number})
return VectorEngine.SUCCESS_CODE, group_id, group.file_number
else:
return jsonify({'code': 0, 'group_name': group_id, 'file_number': 0})
return VectorEngine.SUCCESS_CODE, group_id, 0
@staticmethod
......@@ -70,12 +72,16 @@ class VectorEngine(object):
group_list.append(group_item)
print(group_list)
return jsonify(results = group_list)
return VectorEngine.SUCCESS_CODE, group_list
@staticmethod
def AddVector(group_id, vector):
print(group_id, vector)
code, _, _ = VectorEngine.GetGroup(group_id)
if code == VectorEngine.FAULT_CODE:
return VectorEngine.GROUP_NOT_EXIST
file = FileTable.query.filter(FileTable.group_name == group_id).filter(FileTable.type == 'raw').first()
if file:
print('insert into exist file')
......@@ -112,11 +118,16 @@ class VectorEngine(object):
db.session.add(FileTable(group_id, raw_filename, 'raw', 1))
db.session.commit()
return jsonify({'code': 0})
return VectorEngine.SUCCESS_CODE
@staticmethod
def SearchVector(group_id, vector, limit):
# Check the group exist
code, _, _ = VectorEngine.GetGroup(group_id)
if code == VectorEngine.FAULT_CODE:
return VectorEngine.GROUP_NOT_EXIST
# find all files
files = FileTable.query.filter(FileTable.group_name == group_id).all()
......@@ -127,26 +138,31 @@ class VectorEngine(object):
# train
# get topk
print('search in raw file: ', file.filename)
pass
else:
# get topk
print('search in index file: ', file.filename)
data = IndexFileHandler.Read(file.filename, file.type)
pass
# according to difference files get topk of each
# reduce the topk from them
# construct response and send back
return jsonify({'code': 0})
vector_id = 0
return VectorEngine.SUCCESS_CODE, vector_id
@staticmethod
def CreateIndex(group_id):
# Check the group exist
code, _, _ = VectorEngine.GetGroup(group_id)
if code == VectorEngine.FAULT_CODE:
return VectorEngine.GROUP_NOT_EXIST
# create index
file = FileTable.query.filter(FileTable.group_name == group_id).filter(FileTable.type == 'raw').first()
path = GroupHandler.GetGroupDirectory(group_id) + '/' + file.filename
print('Going to create index for: ', path)
return jsonify({'code': 0})
return VectorEngine.SUCCESS_CODE
@staticmethod
......@@ -156,6 +172,8 @@ class VectorEngine(object):
if VectorEngine.group_dict is None:
# print("VectorEngine.group_dict is None")
VectorEngine.group_dict = dict()
if not (group_id in VectorEngine.group_dict):
VectorEngine.group_dict[group_id] = []
VectorEngine.group_dict[group_id].append(vector)
......@@ -174,3 +192,9 @@ class VectorEngine(object):
def GetVectorListFromRawFile(group_id, filename):
return VectorEngine.group_dict[group_id]
@staticmethod
def ClearRawFile(group_id):
print("VectorEngine.group_dict: ", VectorEngine.group_dict)
del VectorEngine.group_dict[group_id]
return VectorEngine.SUCCESS_CODE
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册