From 101abe5dfd23c68bc5a7ffd95dfd012953cca81d Mon Sep 17 00:00:00 2001 From: jinhai Date: Wed, 20 Mar 2019 19:49:17 +0800 Subject: [PATCH] Before add real logic code --- README.md | 20 +++++++ pyengine/engine/controller/IndexManage.py | 67 +++++++++++++++++----- pyengine/engine/controller/VectorEngine.py | 0 pyengine/engine/model/GroupTable.py | 2 +- 4 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 pyengine/engine/controller/VectorEngine.py diff --git a/README.md b/README.md index b46ef88f61..5893b42cba 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,23 @@ - `python` # enter python3 interaction environment - `from engine import db` - `db.create_all()` + +- table desc +group_table ++-------------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++-------------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| group_name | varchar(100) | YES | | NULL | | +| file_number | int(11) | YES | | NULL | | ++-------------+--------------+------+-----+---------+----------------+ + +file_table ++------------+--------------+------+-----+---------+----------------+ +| Field | Type | Null | Key | Default | Extra | ++------------+--------------+------+-----+---------+----------------+ +| id | int(11) | NO | PRI | NULL | auto_increment | +| group_name | varchar(100) | YES | | NULL | | +| filename | varchar(100) | YES | | NULL | | +| row_number | int(11) | YES | | NULL | | ++------------+--------------+------+-----+---------+----------------+ diff --git a/pyengine/engine/controller/IndexManage.py b/pyengine/engine/controller/IndexManage.py index 70bb5fca91..1930a5a7b6 100644 --- a/pyengine/engine/controller/IndexManage.py +++ b/pyengine/engine/controller/IndexManage.py @@ -1,31 +1,36 @@ -from flask import Flask +from flask import Flask, jsonify, request from flask_restful import Resource, Api from engine import app, db +from engine.model.GroupTable import GroupTable # 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('groupid', type=str) - self.__parser.add_argument('vec', type=str) + self.__parser.add_argument('vector', type=float, action='append', location=['json']) - def post(self): - # args = self.__parser.parse_args() - # vec = args['vec'] - # groupid = args['groupid'] + def post(self, groupid): + args = self.__parser.parse_args() + vector = args['vector'] + # add vector into file + print("vector: ", vector) return "vector post" class VectorSearch(Resource): def __init__(self): self.__parser = reqparse.RequestParser() - self.__parser.add_argument('groupid', type=str) + self.__parser.add_argument('vector', type=float, action='append', location=['json']) - def post(self): + def post(self, groupid): + args = self.__parser.parse_args() + print('vector: ', args['vector']) + # go to search every thing return "vectorSearch post" @@ -35,6 +40,7 @@ class Index(Resource): self.__parser.add_argument('groupid', type=str) def post(self): + # go to create index for specific group return "index post" @@ -44,25 +50,56 @@ class Group(Resource): self.__parser.add_argument('groupid', type=str) def post(self, groupid): - return "group post" + args = self.__parser.parse_args() + group = GroupTable.query.filter(GroupTable.group_name==groupid).first() + if(group): + return jsonify({'code': 1, 'group_name': groupid, 'file_number': group.file_number}) + else: + new_group = GroupTable(groupid) + db.session.add(new_group) + db.session.commit() + return jsonify({'code': 0, 'group_name': groupid, 'file_number': 0}) + def get(self, groupid): - return "group get" + args = self.__parser.parse_args() + group = GroupTable.query.filter(GroupTable.group_name==groupid).first() + if(group): + return jsonify({'code': 0, 'group_name': groupid, 'file_number': group.file_number}) + else: + return jsonify({'code': 1, 'group_name': groupid, 'file_number': 0}) # not found def delete(self, groupid): - return "group delete" + args = self.__parser.parse_args() + group = GroupTable.query.filter(GroupTable.group_name==groupid).first() + if(group): + # old_group = GroupTable(groupid) + db.session.delete(group) + db.session.commit() + return jsonify({'code': 0, 'group_name': groupid, 'file_number': group.file_number}) + else: + return jsonify({'code': 0, 'group_name': groupid, 'file_number': 0}) class GroupList(Resource): def get(self): - return "grouplist get" + group = GroupTable.query.all() + group_list = [] + for group_tuple in group: + group_item = {} + group_item['group_name'] = group_tuple.group_name + group_item['file_number'] = group_tuple.file_number + group_list.append(group_item) + + print(group_list) + return jsonify(results = group_list) -api.add_resource(Vector, '/vector') +api.add_resource(Vector, '/vector/add/') api.add_resource(Group, '/vector/group/') api.add_resource(GroupList, '/vector/group') api.add_resource(Index, '/vector/index') -api.add_resource(VectorSearch, '/vector/search') +api.add_resource(VectorSearch, '/vector/search/') # if __name__ == '__main__': diff --git a/pyengine/engine/controller/VectorEngine.py b/pyengine/engine/controller/VectorEngine.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/pyengine/engine/model/GroupTable.py b/pyengine/engine/model/GroupTable.py index c66ee31566..9cf4f51793 100644 --- a/pyengine/engine/model/GroupTable.py +++ b/pyengine/engine/model/GroupTable.py @@ -11,4 +11,4 @@ class GroupTable(db.Model): self.file_number = 0 def __repr__(self): - return '' % self.group_name \ No newline at end of file + return '' % self.group_name \ No newline at end of file