SceneCleanOrigServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //package com.fdkankan.scene.service.impl;
  2. //
  3. //import cn.hutool.core.collection.CollUtil;
  4. //import cn.hutool.core.exceptions.ExceptionUtil;
  5. //import cn.hutool.core.util.StrUtil;
  6. //import com.alibaba.fastjson.JSON;
  7. //import com.alibaba.fastjson.JSONObject;
  8. //import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  9. //import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  10. //import com.fdkankan.common.constant.CommonSuccessStatus;
  11. //import com.fdkankan.fyun.face.FYunFileServiceInterface;
  12. //import com.fdkankan.model.constants.ConstantFilePath;
  13. //import com.fdkankan.model.constants.UploadFilePath;
  14. //import com.fdkankan.model.utils.SceneUtil;
  15. //import com.fdkankan.redis.constant.RedisKey;
  16. //import com.fdkankan.redis.util.RedisUtil;
  17. //import com.fdkankan.scene.bean.SceneBean;
  18. //import com.fdkankan.scene.entity.Camera;
  19. //import com.fdkankan.scene.entity.SceneCleanOrig;
  20. //import com.fdkankan.scene.mapper.ISceneCleanOrigMapper;
  21. //import com.fdkankan.scene.service.*;
  22. //import lombok.extern.slf4j.Slf4j;
  23. //import org.springframework.beans.factory.annotation.Autowired;
  24. //import org.springframework.beans.factory.annotation.Value;
  25. //import org.springframework.cloud.context.config.annotation.RefreshScope;
  26. //import org.springframework.stereotype.Service;
  27. //
  28. //import java.util.List;
  29. //import java.util.Map;
  30. //import java.util.Set;
  31. //import java.util.stream.Collectors;
  32. //
  33. ///**
  34. // * <p>
  35. // * 删除oss原始资源记录 服务实现类
  36. // * </p>
  37. // *
  38. // * @author
  39. // * @since 2023-03-29
  40. // */
  41. //@RefreshScope
  42. //@Slf4j
  43. //@Service
  44. //public class SceneCleanOrigServiceImpl extends ServiceImpl<ISceneCleanOrigMapper, SceneCleanOrig> implements ISceneCleanOrigService {
  45. //
  46. // @Value("${scene.cleanOrig.month}")
  47. // private Integer cleanOrigMonth;
  48. // @Value("${scene.coldStorage.month}")
  49. // private Integer coldStorageMonth;
  50. // @Value("${scene.cleanDeleted.month}")
  51. // private Integer cleanDeletedMonth;
  52. // @Value("#{'${scene.cleanTestCamera.snCode:}'.split(',')}")
  53. // private List<String> testSnCodeList;
  54. // @Value("${scene.cleanTestCamera.month}")
  55. // private Integer cleanTestCameraMonth;
  56. // @Value("${fyun.bucket}")
  57. // private String bucket;
  58. // @Value("${fyun.coldBucket}")
  59. // private String coldBucket;
  60. // @Autowired
  61. // private ICameraService cameraService;
  62. // @Autowired
  63. // private ISceneColdStorageService sceneColdStorageService;
  64. //
  65. //
  66. // @Autowired
  67. // private ISceneProService sceneProService;
  68. // @Autowired
  69. // private IScenePlusService scenePlusService;
  70. // @Autowired
  71. // private IScenePlusExtService scenePlusExtService;
  72. // @Autowired
  73. // private FYunFileServiceInterface fYunFileService;
  74. // @Autowired
  75. // private RedisUtil redisUtil;
  76. // @Autowired
  77. // private ISceneColdStorageLogService sceneColdStorageLogService;
  78. //
  79. // @Override
  80. // public void cleanOrigV4() {
  81. // //查询所有计算时间超过限定时间的场景,计算成功、未被删除、最后一次计算后未被删除过的
  82. // List<SceneBean> sceneBeans = scenePlusService.listCleanOrigScene(cleanOrigMonth);
  83. // this.cleanOrig(sceneBeans);
  84. // }
  85. //
  86. // @Override
  87. // public void cleanOrigV3() {
  88. // //查询所有计算时间超过限定时间的场景,计算成功、未被删除
  89. // List<SceneBean> sceneBeans = sceneProService.listCleanOrigScene(cleanOrigMonth);
  90. // this.cleanOrig(sceneBeans);
  91. //
  92. // }
  93. //
  94. // private void cleanOrig(List<SceneBean> sceneBeans){
  95. // if(CollUtil.isEmpty(sceneBeans)){
  96. // return;
  97. // }
  98. // sceneBeans.parallelStream().forEach(scene->{
  99. // boolean lock = this.lock(scene.getDataSource());
  100. // try {
  101. // if(lock) {
  102. // this.cleanOrigHandler(scene);
  103. // this.saveLog(scene.getNum(), 1, CommonSuccessStatus.SUCCESS.code(), null);
  104. // }
  105. // }catch (Exception e){
  106. // log.error("删除原始资源失败,num : " + scene.getNum(), e);
  107. // this.saveLog(scene.getNum(), 1, CommonSuccessStatus.FAIL.code(), ExceptionUtil.stacktraceToString(e, 3000));
  108. // }finally {
  109. // this.releaseLock(scene.getDataSource());
  110. // }
  111. // });
  112. // }
  113. //
  114. // private void cleanOrigHandler(SceneBean scene){
  115. // String dataSource = scene.getDataSource();
  116. // if(StrUtil.isNotEmpty(dataSource)){
  117. // String homePath = dataSource.replace(ConstantFilePath.BUILD_MODEL_PATH, ConstantFilePath.OSS_PREFIX);
  118. // //由于国内测试和生产用的bucket是同一个,这里需要做一个安全校验,保证不会删错
  119. // String fileContent = fYunFileService.getFileContent(homePath.concat("/").concat("data.fdage"));
  120. // if(StrUtil.isNotBlank(fileContent)){
  121. // JSONObject jsonObject = JSON.parseObject(fileContent);
  122. // String snCode = jsonObject.getJSONObject("cam").getString("uuid");
  123. // String uuidTime = jsonObject.getString("uuidtime");
  124. // if(StrUtil.isEmpty(snCode)
  125. // || StrUtil.isEmpty(uuidTime)
  126. // || !homePath.contains(snCode)
  127. // || !homePath.contains(uuidTime)){
  128. // throw new RuntimeException("dataSource与data.fdage文件不匹配");
  129. // }else{
  130. // fYunFileService.deleteFolder(homePath);
  131. // }
  132. // }
  133. // }
  134. // }
  135. //
  136. //
  137. //
  138. //
  139. // private void saveLog(String num, int type, int status, String reason){
  140. // //清除旧的日志
  141. // this.remove(new LambdaQueryWrapper<SceneCleanOrig>().eq(SceneCleanOrig::getNum, num));
  142. // SceneCleanOrig sceneCleanOrig = new SceneCleanOrig();
  143. // sceneCleanOrig.setNum(num);
  144. // sceneCleanOrig.setType(type);
  145. // sceneCleanOrig.setState(status);
  146. // sceneCleanOrig.setReason(reason);
  147. // this.saveOrUpdate(sceneCleanOrig);
  148. // }
  149. //
  150. // private boolean lock(String dataSource){
  151. // Map<String, String> property = SceneUtil.getPropertyFromDataSource(dataSource);
  152. // String homePath = property.get("homePath");
  153. // String uuid = property.get("uuid");
  154. //
  155. // String uploadLock = redisUtil.get(String.format(RedisKey.SCENE_OSS_HOME_DIR_UPLOAD, uuid));
  156. // //场景正在上传,不删除
  157. // if(StrUtil.isNotEmpty(uploadLock)){
  158. // return false;
  159. // }
  160. // redisUtil.set(String.format(RedisKey.SCENE_OSS_HOME_DIR_DELETE, uuid), homePath, 8*60*60);
  161. // return true;
  162. // }
  163. //
  164. // private void releaseLock(String dataSource){
  165. // Map<String, String> property = SceneUtil.getPropertyFromDataSource(dataSource);
  166. // String uuid = property.get("uuid");
  167. // redisUtil.del(String.format(RedisKey.SCENE_OSS_HOME_DIR_DELETE, uuid));
  168. // }
  169. //
  170. //
  171. // @Override
  172. // public void cleanOss4DeletedSceneV3() {
  173. // List<SceneBean> sceneBeans = sceneProService.listCleanOss4DeletedScene(cleanDeletedMonth);
  174. // this.cleanOrig4Delete(sceneBeans, false, 2);
  175. // }
  176. //
  177. // @Override
  178. // public void cleanOss4DeletedSceneV4() {
  179. // //查询所有计算时间超过限定时间的场景,计算成功、未被删除、最后一次计算后未被删除过的
  180. // List<SceneBean> sceneBeans = scenePlusService.listCleanOss4DeletedScene(cleanDeletedMonth);
  181. // this.cleanOrig4Delete(sceneBeans, true, 2);
  182. // }
  183. //
  184. // /**
  185. // * 删除已删除场景的原始资源及caches目录(v3场景不需要删除caches目录)
  186. // * @param sceneBeans
  187. // * @param deleteCaches 是否需要删除caches目录
  188. // */
  189. // private void cleanOrig4Delete(List<SceneBean> sceneBeans, boolean deleteCaches, Integer type){
  190. // if(CollUtil.isEmpty(sceneBeans)){
  191. // return;
  192. // }
  193. // sceneBeans.parallelStream().forEach(scene->{
  194. // try {
  195. // //删除caches文件
  196. // if(deleteCaches){
  197. // this.deleteResultCaches(scene.getNum());
  198. // }
  199. // //删除原始资源
  200. // this.cleanOrigHandler(scene);
  201. //
  202. // this.saveLog(scene.getNum(), type, CommonSuccessStatus.SUCCESS.code(), null);
  203. // }catch (Exception e){
  204. // log.error("删除已删除场景资源失败,num : " + scene.getNum(), e);
  205. // this.saveLog(scene.getNum(), type, CommonSuccessStatus.FAIL.code(), ExceptionUtil.stacktraceToString(e, 3000));
  206. // }
  207. // });
  208. // }
  209. //
  210. // private void deleteResultCaches(String num){
  211. // String cachesPath = String.format(UploadFilePath.scene_result_data_path, num).concat("caches");
  212. // if(CollUtil.isEmpty(fYunFileService.listRemoteFiles(cachesPath))){
  213. // return;
  214. // }
  215. // fYunFileService.deleteFolder(cachesPath);
  216. // }
  217. //
  218. // @Override
  219. // public void cleanOss4TestCameraV3() {
  220. // List<Camera> cameras = cameraService.listBySnCodes(testSnCodeList);
  221. // if(CollUtil.isEmpty(cameras)){
  222. // return;
  223. // }
  224. // Set<Long> cameraIds = cameras.stream().map(Camera::getId).collect(Collectors.toSet());
  225. // List<SceneBean> sceneBeans = sceneProService.listCleanOss4TestCamera(cameraIds, cleanTestCameraMonth);
  226. // this.cleanOrig4Delete(sceneBeans, false, 3);
  227. // }
  228. //
  229. // @Override
  230. // public void cleanOss4TestCameraV4() {
  231. // List<Camera> cameras = cameraService.listBySnCodes(testSnCodeList);
  232. // if(CollUtil.isEmpty(cameras)){
  233. // return;
  234. // }
  235. // Set<Long> cameraIds = cameras.stream().map(Camera::getId).collect(Collectors.toSet());
  236. // List<SceneBean> sceneBeans = scenePlusService.listCleanOss4TestCamera(cameraIds, cleanTestCameraMonth);
  237. // this.cleanOrig4Delete(sceneBeans, true, 3);
  238. // }
  239. //
  240. // @Override
  241. // public void coldStorageHomeV3() {
  242. // //查询所有计算时间超过限定时间的场景,计算成功、未被删除
  243. // List<SceneBean> sceneBeans = sceneProService.listColdStorageScene(coldStorageMonth);
  244. // this.coldStorage(sceneBeans);
  245. // }
  246. //
  247. // @Override
  248. // public void coldStorageHomeV4() {
  249. // //查询所有计算时间超过限定时间的场景,计算成功、未被删除
  250. // List<SceneBean> sceneBeans = scenePlusService.listColdStorageScene(coldStorageMonth);
  251. // this.coldStorage(sceneBeans);
  252. // }
  253. //
  254. // private void coldStorage(List<SceneBean> sceneBeans){
  255. // if(CollUtil.isEmpty(sceneBeans)){
  256. // return;
  257. // }
  258. // sceneBeans.parallelStream().forEach(scene->{
  259. // boolean lock = this.lock(scene.getDataSource());
  260. // try {
  261. // if(lock) {
  262. // this.coldStorageHandler(scene);
  263. // sceneColdStorageLogService.saveLog(scene.getNum(), scene.getDataSource(), 1, 1, null);
  264. // sceneColdStorageService.save(scene.getNum(), 1, coldBucket, bucket);
  265. // }
  266. // }catch (Exception e){
  267. // log.error("冷归档失败,num:{}" + scene.getNum(), e);
  268. // sceneColdStorageLogService.saveLog(scene.getNum(), scene.getDataSource(),1, CommonSuccessStatus.FAIL.code(), ExceptionUtil.stacktraceToString(e, 3000));
  269. // }finally {
  270. // this.releaseLock(scene.getDataSource());
  271. // }
  272. // });
  273. // }
  274. //
  275. // private void coldStorageHandler(SceneBean scene){
  276. // String dataSource = scene.getDataSource();
  277. // if(StrUtil.isEmpty(dataSource) || dataSource.length() < 10) {
  278. // return;
  279. // }
  280. // String homePath = dataSource.replace(ConstantFilePath.BUILD_MODEL_PATH, ConstantFilePath.OSS_PREFIX);
  281. //
  282. // //将文件复制到冷归档bucket
  283. // fYunFileService.copyFileBetweenBucket(bucket, homePath, coldBucket, homePath);
  284. //
  285. // List<String> origList = fYunFileService.listRemoteFiles(bucket, homePath);
  286. // List<String> coldList = fYunFileService.listRemoteFiles(coldBucket, homePath);
  287. // if(origList.size() != coldList.size()){
  288. // throw new RuntimeException("复制文件到冷归档bucket失败");
  289. // }
  290. //
  291. // //删除标准bucket文件
  292. // fYunFileService.deleteFolder(homePath);
  293. // }
  294. //}