def create(userJson): userSerialized = JSONSerializator().serialize(userJson, ignoreProperties=True) print(userSerialized.dumpModel()) user = DBUtil.findByUsername(User, userSerialized.username) if user is not None: return GenericResponseDto.createResponse( ResponseMessage.FORBIDDEN, ResponseCode.FORBIDDEN, "Username already exists!") app = ApplicationService.findAppById(userSerialized.appId) if app is None: return GenericResponseDto.createResponse( ResponseMessage.NOT_FOUND, ResponseCode.NOT_FOUND, "Application with ID=[{}] not found!".format( userSerialized.appId)) user = UserService.mapUser(userSerialized) status, data = DBUtil.insert(user) if not status: return GenericResponseDto.createResponse( ResponseMessage.INTERNAL_SERVER_ERROR, ResponseCode.INTERNAL_SERVER_ERROR, data) else: userDto = UserDto.createFromEntity(data) return GenericResponseDto.createResponse(ResponseMessage.OK, ResponseCode.OK, userDto.getJson())
def createApplication(name): if not ApplicationService.appNameExists(name): app = Application.create(name) DBUtil.insert(app) appDto = ApplicationDto.createFromEntity(app) return GenericResponseDto.createResponse(ResponseMessage.CREATED, ResponseCode.CREATED, appDto.getJson()) else: return GenericResponseDto.createResponse(ResponseMessage.CONFLICT, ResponseCode.CONFLICT, None)
def main(options, arguments): start = int(options.start) if options.start else 0 end = int(options.end) if options.end else 1000000 if len(arguments) == 0: print("请务必选择原始数据文件的路径!") return filePath = arguments[0] DBUtil.dumpRawData(filePath, start, end)
def main(options, arguments): method = RecommenderCategory( options.method) if options.method else RecommenderCategory.CONTENT topK = int(options.num) if options.num else 5 recommender = RecommenderFactory.getRecommender(method) recommender.topK = topK recommendation = recommender.recommendAll() CacheUtil.dumpRecommendation(recommendation) DBUtil.dumpRecommendation(recommendation)
def main(options, arguments): method = options.method if options.method else "day" if method == "day": day = int(options.day) if options.day else 20 DBUtil.splitByClickedDate(day) elif method == "random": ratio = int(options.ratio) if options.ratio else 0.8 DBUtil.randomSplit(ratio) else: print("请选择合适的切割方法!")
def deleteUserById(id): user = DBUtil.findById(User, id) if user.username == 'admin': flash("Admin cannot be deleted!") else: status = DBUtil.delete(user) if status: flash("User successfully deleted!", "success") else: flash("Error occurred!", "error")
def provideAllFromCompute(self): feature = [] for doc in self.corpus: feature.append(list(map(lambda x: x[1], doc))) if len(feature) == 0: feature = None elif self.isUpdate(): DBUtil.dumpArticleFeature(feature) self.setCache(feature) return feature
def compareWithCurrent(toCompare, current): changed = False if not toCompare.name == current.name: toCompare.name = current.name changed = True if not toCompare.type == current.type: toCompare.type = current.type changed = True if changed: DBUtil.commit()
def getHeadPointRedZone(): mapName = request.form.get('mapName') dBUtil = DBUtil() datas = dBUtil.query("select * from zk_redzone where map='%s'" % mapName) pointData = [] for data in datas: pointData.append({ 'lat': data[1], 'lng': data[0], 'count': data[2] / 10 + 1, 'radius': data[2] / 10 + 4 }) return str(json.dumps(pointData, check_circular=False, ensure_ascii=False))
def getHeadPointLastPoisonCircle(): mapName = request.form.get('mapName') dBUtil = DBUtil() datas = dBUtil.query( "select * from zc_lastpoisoncircle where map='%s' ORDER BY num desc limit 500" % mapName) pointData = [] for data in datas: pointData.append({ 'lat': data[1], 'lng': data[0], 'count': data[2] + 20, 'radius': data[2] + 10 }) return str(json.dumps(pointData, check_circular=False, ensure_ascii=False))
def getAllF(args, header, exportCsv=False): try: token = header.get(AUTH_TOKEN) status = TaskExecutionService.checkToken(token) if status is not None: return status except: return Utils.JsonMessage("Internal server error", 500) taskID = args.get(FILTER_TASK_ID) robotID = args.get(FILTER_ROBOT_ID) status = args.get(FILTER_STATUS) durationFrom = args.get(FILTER_DURATION_FROM) durationTo = args.get(FILTER_DURATION_TO) dateFrom = args.get(FILTER_DATE_FROM) dateTo = args.get(FILTER_DATE_TO) entityList = DBUtil.taskExecutionFilter(TaskExecution, taskId=taskID, robotId=robotID, status=status, durationFrom=durationFrom, durationTo=durationTo, dateFrom=dateFrom, dateTo=dateTo) dtoList = [] for e in entityList: dto = TaskExecutedResponseDto() dto.fromEntity(e) dtoList.append(dto.getJson()) jsonList = { 'executed': dtoList } if exportCsv: csvData = ""#StringIO() for e in entityList: csvData += "{};{};{};{};{};{};{};{};{};{}\n".format(e.id, e.uuid, e.task_id, e.robot_id, e.date, e.start_time, e.end_time, e.duration, e.status, e.created) print(csvData) return csvData else: return Utils.JsonResponse(jsonList, 200)
def checkLoginInformation(loginDto: LoginDto): user = DBUtil.findByUsername(User, loginDto.username) if user is None: return False, GenericResponseDto.createResponse( ResponseMessage.NOT_FOUND, ResponseCode.NOT_FOUND, "User not found!") else: if loginDto.password == user.pwd: app = ApplicationService.findAppByToken(loginDto.token) if app is None: return False, GenericResponseDto.createResponse( ResponseMessage.NOT_FOUND, ResponseCode.NOT_FOUND, "Invalid application token!") if app.id == user.app_id: return True, GenericResponseDto.createResponse( ResponseMessage.OK, ResponseCode.OK, UserDto.createFromEntity(user).getJson()) else: return False, GenericResponseDto.createResponse( ResponseMessage.NOT_ACCEPTABLE, ResponseCode.NOT_ACCEPTABLE, "Invalid application token!") else: return False, GenericResponseDto.createResponse( ResponseMessage.UNAUTHORIZED, ResponseCode.UNAUTHORIZED, "Password is not correct!")
def execute(id, args, header): try: token = header.get(AUTH_TOKEN) status = TaskExecutionService.checkToken(token) if status is not None: return status except: return Utils.JsonMessage("Internal server error", 500) task = DBUtil.findById(Task, id) if task is None: return Utils.JsonMessage("Task ID[{}] does not exists!".format(id), 404) robotID = args.get(ROBOT_ID) simulatedTime = args.get(SIMULATED_TIME) simulatedStatus = args.get(SIMULATED_STATUS) if robotID is None: Utils.JsonMessage("robot_id parameter is missing", 500) else: robot = DBUtil.findById(Robot, robotID) if robot is None: return Utils.JsonMessage("Robot ID[{}] does not exists!".format(robotID), 404) startTime = dt.now() d = startTime.date() t = startTime.time() ts = startTime.timestamp() execution = TaskExecution.create(int(id), int(robotID), d, t, str(int(ts))) if simulatedTime is not None: endTime = startTime + timedelta(seconds=int(simulatedTime)) else: endTime = startTime + timedelta(seconds=30) duration = endTime - startTime s = True if simulatedStatus is not None: s = bool(int(simulatedStatus)) execution.setEnd(endTime.time(), duration.seconds, s) status, model = DBUtil.insert(execution) if status: print(model) return Utils.JsonMessage("Task ID[{}] executed by robot ID[{}]!".format(id, robotID), 200) else: return Utils.JsonMessage("Task ID[{}] not executed!", 500)
def changeUserPassword(username, password): try: user = DBUtil.findByUsername(User, username) user.pwd = password db.session.commit() return True except Exception as e: print(e) db.session.rollback() return False
def updateUser(user): update = DBUtil.findById(User, user.id) update.require_change = user.require_change try: db.session.commit() return True except Exception as e: print(e) db.session.rollback() return False
def provideAllFromCompute(self): if not self.articleFeatureProvider: return None interest = [] feature = self.articleFeatureProvider.provideAll() for user in User.objects: clicked = user.getAllClicked() vec = np.array([0.0] * self.interestNum) for i in clicked: tmp = np.array(feature[i]) vec = vec + tmp vec /= max(len(clicked), 1) interest.append(list(vec)) # print(vec) print("User " + str(user.index) + " has computed!") if len(interest) > 0: self.setCache(interest) if self.isUpdate(): CacheUtil.dumpUserInterest(interest) DBUtil.dumpInterest(interest) return interest else: return None
def handleMethod(clazz, method, model, id=None): result = None status = False message = None code = None entityById = None if id is not None: entityById = DBUtil.findById(clazz, id) if method == 'POST': s, model = DBUtil.insert(model) status = s result = model code = 201 if not status: code = 500 message = "Entity not created [{}]".format(model) elif method == 'PUT': if entityById is not None: GenericHelperService.compareWithCurrent(entityById, model) status = True result = entityById else: status = False result = None if id is None: message = "PUT method [PATH param ID is missing]" code = 500 else: message = "Entity not found" code = 404 elif method == 'GET': if id is not None: result = DBUtil.findById(clazz, id) else: result = DBUtil.findAll(clazz) if result is None: status = False code = 404 message = "Entity not found" else: status = True elif method == 'DELETE': if entityById is not None: entityById.active = False DBUtil.commit() status = False code = 200 message = "Successfully deleted" else: status = False code = 404 message = "Entity not found" result = None return status, result, message, code
def trainAll(self): if not self.model: self.model = NearestNeighbors(n_neighbors=self.num + 1, algorithm='auto').fit( self.provider.provideAll()) res = [] distances, friends = self.model.kneighbors(self.provider.provideAll()) for count in range(len(friends)): friend = [] if distances[count][2] == 0: res.append(friend) continue similarity = self.distanceToSimilarity(distances[count])[1:] neighborList = friends[count][1:] for i in range(self.num): friend.append((neighborList[i], similarity[i])) res.append(friend) print("User " + str(count) + " finded!") if self.isUpdate(): # DBUtil.dumpFriends(res) CacheUtil.dumpUserFriends(res) DBUtil.dumpFriends(res) return res
def getConfigurationByName(name): entity = DBUtil.findByName(Configuration, name) if entity is not None: dto = ConfigurationDto.fromEntity(entity).getJson() return dto else: response = { 'code': 404, 'message': 'Not Found', 'data': { 'info': 'Configuration not found!', 'model': None } } return response
def getAll(header): try: token = header.get(AUTH_TOKEN) status = TaskExecutionService.checkToken(token) if status is not None: return status except: return Utils.JsonMessage("Internal server error", 500) entityList = DBUtil.findAll(TaskExecution) dtoList = [] for e in entityList: dto = TaskExecutedResponseDto() dto.fromEntity(e) dtoList.append(dto.getJson()) jsonList = { 'executed': dtoList } return Utils.JsonResponse(jsonList, 200)
def createConfiguration(name, schema, username, password, environment): entity = Configuration.create(name, schema, username, password, environment) status, data = DBUtil.insert(entity) if status: response = { 'code': 200, 'message': 'OK', 'data': { 'info': 'Configuration created!', 'model': ConfigurationDto.fromEntity(data).getJson() } } return json.dumps(response) else: response = { 'code': 500, 'message': 'Internal server error', 'data': { 'info': 'Configuration not created due to error!', 'model': data } } return json.dumps(response)
def getRobotTypeByValue(typeValue): entity = DBUtil.findByType(RobotType, typeValue) dto = RobotTypeResponseDto() dto.fromEntity(entity) print(dto.dumpModel())
def findAppByToken(token): app = DBUtil.findByToken(Application, token) return app
def findAppById(id): app = DBUtil.findById(Application, id) return app
def checkIfConfigurationExists(name): configEntity = DBUtil.findByName(Configuration, name) if configEntity is None: return False else: return True
def getModelById(clazz, id): e = DBUtil.findById(clazz, id) return e
def getUserById(id): user = DBUtil.findById(User, id) return user
def getAllUsers(): return DBUtil.findAll(User)
def action(method, modelDto, header, id=None): token = None try: token = header.get(AUTH_TOKEN) except: pass if token is None: return Utils.JsonMessage("Token parameter not found!", 500) if not LoginService.validateJWTToken(token): return Utils.JsonMessage("Unauthorized", 401) if id is not None: model = None if modelDto is not None: model = JSONSerializator().serialize(modelDto) status, result, message, code = GenericHelperService.handleMethod( Task, method, model, id) if status: taskResponse = SimpleTaskResponseDto() taskResponse.fromEntity(result) return Utils.JsonResponse(taskResponse.getJson(), 200) else: if code is None: code = 500 return Utils.JsonMessage(message, code) else: if modelDto is not None: taskRequestDto = TaskRequestDto().serialize(modelDto) serialized = JSONSerializator().serialize(modelDto) if Utils.check_object_propertis(TaskRequestDto(), serialized): entity = Task.createFromRequestDto( taskRequestDto, str(int(dt.now().timestamp()))) status, model, message, code = GenericHelperService.handleMethod( Task, method, entity, id) if status: e = DBUtil.findByName(Task, entity.name) taskResponseDto = SimpleTaskResponseDto() taskResponseDto.fromEntity(e) return Utils.JsonResponse(taskResponseDto.getJson(), 201) else: return Utils.JsonMessage(message, code) else: return Utils.JsonMessage( "JSON format is not valid. Check properties".format( id), 500) else: if method == 'GET': status, result, message, code = GenericHelperService.handleMethod( Task, method, None, None) dtoList = [] for e in result: dto = SimpleTaskResponseDto() dto.fromEntity(e) dtoList.append(dto.getJson()) listResponse = {'tasks': dtoList} return Utils.JsonResponse(listResponse, 200) return Utils.JsonMessage( "ID and body cannot be empty!".format(id), 500)
def getUserByUsername(username): user = DBUtil.findByUsername(User, username) return user