package com.iplatform.recvideo.scheduler; import com.iplatform.core.BeanContextAware; import com.iplatform.recvideo.config.VideoSimilarProperties; import com.iplatform.recvideo.service.VideoExecutorServiceImpl; import com.iplatform.recvideo.support.DefaultSimilarExecutor; import com.walker.connector.Address; import com.walker.infrastructure.utils.StringUtils; import com.walker.jdbc.JdbcInspector; import com.walker.store.task.GenericGatherTask; import org.springframework.web.client.RestTemplate; import java.util.List; import java.util.Map; /** * 视频相似度查询以及写入结果执行任务实现。 *
 *     1.采集源头为读取数据库最新批次信息。
 *     2.根据批次结果,扫描本机批次目录中所有视频图片,检索相似度并计算结果。
 * 
* @author 时克英 * @date 2022-09-26 */ public class VideoSearchTask extends GenericGatherTask { private DefaultSimilarExecutor similarExecutor = null; private VideoSimilarProperties videoSimilarProperties; private VideoExecutorServiceImpl videoExecutorService; private RestTemplate restTemplate; private String sql = "select * from rc_task_status where task_type='video_load' and status='0'"; public VideoSearchTask(String name, Address address){ super(name, address); this.setDatabaseType(JdbcInspector.getInstance().getPrimaryDatabaseType()); this.videoSimilarProperties = BeanContextAware.getBeanByType(VideoSimilarProperties.class); } @Override protected List transferResultData(List resultList) { return resultList; } /** * 执行具体任务处理,主要由相似度执行器执行其execute方法。 * @param srcName * @param createTableSQL * @param parameter * @param data 该参数是从表: rc_task_status 中查询出来的可用批次记录,若存在多个就抽取一个处理。 * @return */ @Override protected Object execute(String srcName, String createTableSQL, Object parameter, List data) { if(StringUtils.isEmptyList(data)){ logger.warn("数据库中检索出可处理: rc_task_status 记录,但集合为空"); return null; } Map rcTaskStatus = (Map)data.get(0); String lastValue = rcTaskStatus.get("last_value").toString(); return new VideoSearchMeta(lastValue); } public String getSql(){ return this.sql; } public void checkExecutor(VideoSearchMeta videoSearchMeta){ if(this.similarExecutor != null){ logger.debug("DefaultSimilarExecutor 已经设置过,不再重复设置"); return; } DefaultSimilarExecutor defaultSimilarExecutor = new DefaultSimilarExecutor(); defaultSimilarExecutor.setRemoteUrl(this.videoSimilarProperties.getAiService()); defaultSimilarExecutor.setVideoExecutorService(BeanContextAware.getBeanByType(VideoExecutorServiceImpl.class)); defaultSimilarExecutor.setRestTemplate(BeanContextAware.getBeanByType(RestTemplate.class)); defaultSimilarExecutor.startup(this.videoSimilarProperties.getDataFolder() , videoSearchMeta.getBatchId(), this.videoSimilarProperties.getTopN(), true); this.similarExecutor = defaultSimilarExecutor; } /** * 调用一次结果查询及写入。 * @return -1 失败出现异常, 0 成功执行一次(继续下一次), 1 批次完成 * @throws Exception */ public int executeOneSearch() throws Exception{ if(this.similarExecutor == null){ throw new IllegalStateException("similarExecutor is required!"); } return this.similarExecutor.execute(); } }