From 7092f66b538f38b789f74d272ff355527e0153df Mon Sep 17 00:00:00 2001 From: haungxing <1203316822@qq.com> Date: Wed, 28 Aug 2024 08:33:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=B4=E6=97=B6=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/service/DataSourceService.java | 5 +- .../bigmodel/database/service/WeaviateService.java | 87 +++++++++++++++++++--- .../controller/FontEndInteractiveController.java | 2 +- .../interactive/service/IHznlmInvokeService.java | 2 + .../service/impl/HznlmInvokeServiceImpl.java | 28 +++++++ .../inspect/obj/mapper/ObjectTemplateMapper.xml | 2 +- 6 files changed, 114 insertions(+), 12 deletions(-) diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/service/DataSourceService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/service/DataSourceService.java index c616d0a..ac1c3c8 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/service/DataSourceService.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/service/DataSourceService.java @@ -58,7 +58,10 @@ public class DataSourceService { throw new HzServiceException(ResultCode.FAILURE,"查询语句中存在未进行鉴权的表,查询失败!"); }); if("1".equals(propertise.get(0).getAuthType())) { - String tableSubStr = "(SELECT * FROM " + tableAuthVO.getTableName() + " where" + userAuthDataSQL +") temp"; + String tableSubStr = "(SELECT * FROM " + tableAuthVO.getTableName() + " where is_deleted = 0 and " + userAuthDataSQL +") temp"; + sql = sql.replace(tableAuthVO.getTableName(),tableSubStr); + } else { + String tableSubStr = "(SELECT * FROM " + tableAuthVO.getTableName() + " where is_deleted = 0) temp"; sql = sql.replace(tableAuthVO.getTableName(),tableSubStr); } } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/service/WeaviateService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/service/WeaviateService.java index 931a6a8..cbc926b 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/service/WeaviateService.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/service/WeaviateService.java @@ -6,28 +6,26 @@ import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; -import com.google.protobuf.ServiceException; import com.hnac.hzims.bigmodel.configuration.BigModelInvokeApi; import com.hnac.hzinfo.exception.HzServiceException; import io.weaviate.client.base.Result; import io.weaviate.client.v1.data.api.ObjectCreator; import io.weaviate.client.v1.data.api.ObjectDeleter; import io.weaviate.client.v1.data.api.ObjectUpdater; +import io.weaviate.client.v1.data.api.ObjectsGetter; import io.weaviate.client.v1.data.model.WeaviateObject; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.tool.utils.BeanUtil; import org.springblade.core.tool.utils.Func; -import org.springblade.core.tool.utils.StringUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import org.springframework.util.Assert; import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.*; -import java.util.stream.Collector; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -43,9 +41,11 @@ public class WeaviateService { private final ObjectCreator objectCreator; private final ObjectUpdater objectUpdater; private final ObjectDeleter objectDeleter; + private final ObjectsGetter objectsGetter; private final BigModelInvokeApi invokeApi; + @Value("${gglm.vectorUrl}") - private final String vectorUrl; + private String vectorUrl; /** * 对象保存向量数据库 @@ -89,7 +89,10 @@ public class WeaviateService { if(Func.isNotEmpty(vectorStrs)) { // 若解析出来的向量存在值 Float[] vectors = this.compute(vectorStrs); - + List> vector = this.splitVector(entities.size(), attrsMap, vectors); + for(int i = 0; i < entities.size(); i++) { + creator.withProperties(BeanUtil.toMap(entities.get(i))).withVectors(vector.get(i)).run(); + } } else { entities.forEach(entity -> creator.withProperties(BeanUtil.toMap(entity)).run()); return true; @@ -98,6 +101,63 @@ public class WeaviateService { } /** + * 删除向量数据库(表名) + * @param className 表名 + * @return 删除结果 + */ + public Boolean deleteByClassName(String className) { + Result result = objectDeleter.withClassName(className).run(); + return !result.hasErrors(); + } + + /** + * 删除向量数据库(ID) + * @param id 向量数据库ID + * @return 删除结果 + */ + public Boolean deleteById(String id) { + Result result = objectDeleter.withID(id).run(); + return !result.hasErrors(); + } + + /** + * 更新数据库(通过ID) + * @param id 向量数据库ID + * @return 更新结果 + */ + public Boolean updateById(String id, Object entity, String className, Map attrMap) { + ObjectUpdater updater = objectUpdater.withClassName(className).withID(id).withProperties(BeanUtil.toMap(entity)); + // 计算向量 + Map vector = new HashMap<>(); + if(Func.isNotEmpty(attrMap)) { + attrMap.forEach((k,v) -> { + String fieldValue = this.getFieldValue(v, entity); + Float[] compute = this.compute(Lists.newArrayList(fieldValue)); + vector.put(k,compute); + }); + } + if(Func.isNotEmpty(vector)) { + updater.withVectors(vector); + } + Result result = updater.run(); + return !result.hasErrors(); + } + + public List list(String id,String className) { + if(Func.isNotEmpty(id)) { + objectsGetter.withID(id); + } + if(Func.isNotEmpty(className)) { + objectsGetter.withClassName(className); + } + Result> result = objectsGetter.run(); + if(result.hasErrors()) { + throw new HzServiceException("查询失败!"); + } + return result.getResult().stream().map(WeaviateObject::getProperties).collect(Collectors.toList()); + } + + /** * 拆解计算出来的向量Float[] * @param entitySize 对象列表size * @param attrsMap 待计算的列信息 key-向量名 value-实体类对象属性,多个按逗号分隔 @@ -106,13 +166,22 @@ public class WeaviateService { */ private List> splitVector(Integer entitySize,Map attrsMap,Float[] vectorTotal) { List> result = Lists.newArrayList(); - + List vectorTotalList = Lists.newArrayList(vectorTotal); // 获取待切割的下标 List indexes = this.getSplitIndex(vectorTotal.length, entitySize); + int step = vectorTotal.length / entitySize; indexes.forEach(index -> { - + List vectors = vectorTotalList.subList(index, index + step); + Map vectorMap = new HashMap<>(); + List splitIndex = this.getSplitIndex(vectors.size(), attrsMap.size()); + AtomicInteger i = new AtomicInteger(); + attrsMap.forEach((k,v) -> { + List vector = vectors.subList(splitIndex.get(i.get()), splitIndex.get(i.get() + (vectors.size() / attrsMap.size()))); + vectorMap.put(k, vector.toArray(new Float[vector.size()])); + i.getAndIncrement(); + }); }); - return null; + return result; } /** diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java index 21b05f4..b7cab25 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java @@ -64,7 +64,7 @@ public class FontEndInteractiveController { @ApiOperationSupport(order = 5) @GetMapping("/interruptSession") public R interruptSession(@RequestParam(value = "id") String sessionId) { - hznlmInvokeService.askAbort(sessionId); + hznlmInvokeService.interruptSession(sessionId); return R.success("操作成功!"); } } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IHznlmInvokeService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IHznlmInvokeService.java index 51ed3f3..2ef2107 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IHznlmInvokeService.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IHznlmInvokeService.java @@ -81,6 +81,8 @@ public interface IHznlmInvokeService { */ void askAbort(String sessionId); + void interruptSession(String sessionId); + /** * 发起机组发电量智能报表分析问答 * @param req 待分析的数据 diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java index b8bdb15..48e9881 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java @@ -17,6 +17,7 @@ import com.hnac.hzims.bigmodel.utils.RequestClientUtil; import com.hnac.hzims.bigmodel.websocket.constants.RedisKeyConstants; import com.hnac.hzims.bigmodel.websocket.sessionManager.InteractiveSessionManager; import com.hnac.hzims.common.constant.CommonConstant; +import com.hnac.hzinfo.exception.HzServiceException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.tool.api.R; @@ -126,6 +127,33 @@ public class HznlmInvokeServiceImpl implements IHznlmInvokeService { sessionRedisManager.removeSessionId(sessionId); } + public void interruptSession(String sessionId) { + this.askAbort(sessionId); + // 循环获取该会话ID中断状态,当状态等于-2或重连超6次则中断返回结果 + int status = 999; + int attempts = 0; + while (status > 0) { + // 若重连超过10次 则抛出错误 + if(attempts >= 8) { + throw new HzServiceException("中断失败!长时间未获取到中断状态"); + } + List answers = this.getAnswerBySessionIds(sessionId); + if(Func.isNotEmpty(answers)) { + log.info(answers.get(0).getStatus()+""); + status = answers.get(0).getStatus(); + } + // 若获取到的状态不等于2 则延时0.5秒 + if(status > 0) { + try { + Thread.sleep(500); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + attempts ++; + } + } + @Override public void smartReportGeneratePower(RunReportAnalyseRequest req) { Map params = new HashMap<>(); diff --git a/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/obj/mapper/ObjectTemplateMapper.xml b/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/obj/mapper/ObjectTemplateMapper.xml index 6136502..8220245 100644 --- a/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/obj/mapper/ObjectTemplateMapper.xml +++ b/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/obj/mapper/ObjectTemplateMapper.xml @@ -3,7 +3,7 @@