shikeying
2022-09-27 26f5dd8ef80e5671cda8fc0e6c0d0298c4e678ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;
 
/**
 * 视频相似度查询以及写入结果执行任务实现。
 * <pre>
 *     1.采集源头为读取数据库最新批次信息。
 *     2.根据批次结果,扫描本机批次目录中所有视频图片,检索相似度并计算结果。
 * </pre>
 * @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<Object> transferResultData(List<Object> resultList) {
        return resultList;
    }
 
    /**
     * 执行具体任务处理,主要由相似度执行器执行其<code>execute</code>方法。
     * @param srcName
     * @param createTableSQL
     * @param parameter
     * @param data 该参数是从表: rc_task_status 中查询出来的可用批次记录,若存在多个就抽取一个处理。
     * @return
     */
    @Override
    protected Object execute(String srcName, String createTableSQL, Object parameter, List<Object> data) {
        if(StringUtils.isEmptyList(data)){
           logger.warn("数据库中检索出可处理: rc_task_status 记录,但集合为空");
           return null;
        }
        Map<String, Object> rcTaskStatus = (Map<String, Object>)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(), this.videoSimilarProperties.isTestMode());
        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();
    }
}