|
@@ -1,15 +1,16 @@
|
|
|
package com.winhc.data.push.service.impl;
|
|
|
|
|
|
import cn.cocowwy.dingtalk.DingTalkGroupApi;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
|
|
|
import com.aliyun.oss.OSS;
|
|
|
import com.aliyun.oss.model.CopyObjectResult;
|
|
|
import com.aliyun.oss.model.PutObjectRequest;
|
|
|
import com.mongodb.client.FindIterable;
|
|
|
import com.mongodb.client.MongoDatabase;
|
|
|
-import com.mongodb.client.model.Filters;
|
|
|
import com.mongodb.client.model.FindOneAndUpdateOptions;
|
|
|
import com.mongodb.client.model.UpdateOptions;
|
|
|
+import com.winhc.data.push.common.Constant;
|
|
|
import com.winhc.data.push.common.TaskEnum;
|
|
|
import com.winhc.data.push.configuration.OSSAccessProperties;
|
|
|
import com.winhc.data.push.framework.MongoDbFastScan;
|
|
@@ -24,6 +25,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.File;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
@@ -33,7 +35,11 @@ import java.util.stream.StreamSupport;
|
|
|
|
|
|
import static cn.hutool.crypto.SecureUtil.md5;
|
|
|
import static com.mongodb.client.model.Filters.*;
|
|
|
+import static com.winhc.data.push.common.Constant.*;
|
|
|
import static com.winhc.data.push.constant.BaseParam.*;
|
|
|
+import static com.winhc.data.push.utils.BaseUtils.getObjectId;
|
|
|
+import static com.winhc.data.push.utils.BaseUtils.getPrcessDate;
|
|
|
+import static com.winhc.data.push.utils.OssUtils.listKeys;
|
|
|
import static com.winhc.data.push.utils.TransToExcelNewCompany.dataPathPrefix;
|
|
|
import static com.winhc.data.push.utils.TransToExcelNewCompany.run;
|
|
|
|
|
@@ -87,6 +93,44 @@ public class SynDataServiceImpl implements SynDataService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public void pushTc(String ds) {
|
|
|
+ this.pushTc(null, ds);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同城数据输出
|
|
|
+ *
|
|
|
+ * @param tns
|
|
|
+ * @param ds
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void pushTc(List<String> tns, String ds) {
|
|
|
+ String id = getObjectId(ds);
|
|
|
+ try {
|
|
|
+ updateTaskStatus(SYN_TC_UPLOAD_TASKS, ds, TaskEnum.STATUS.RUNNING.code, id);
|
|
|
+ sendMessage("task running !!! " + id);
|
|
|
+ String project = "winhc_ng";
|
|
|
+ if (touchService.export2OSSTc(project, ds, tns)) {
|
|
|
+ saveObject(ds, TC_BUCKET_NAME, TC_OSS_PRE);
|
|
|
+ updateTaskStatus(SYN_TC_UPLOAD_TASKS, ds, TaskEnum.STATUS.SUCCESS.code, id);
|
|
|
+ sendMessage("导出 任务 成功 !!! " + id);
|
|
|
+ } else {
|
|
|
+ log.error("导出OSS 失败 !!!");
|
|
|
+ sendMessage("导出 OSS 失败!!! " + id);
|
|
|
+ updateTaskStatus(SYN_TC_UPLOAD_TASKS, ds, TaskEnum.STATUS.ERROR.code, id);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ sendMessage("任务失败!!! " + id + " | error " + e.getMessage());
|
|
|
+ updateTaskStatus(SYN_TC_UPLOAD_TASKS, ds, TaskEnum.STATUS.TASK_ERROR.code, id);
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("export error: {}", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
public String saveTask(String tn, String ds) {
|
|
|
String id = tn + "_" + ds;
|
|
|
if (findTaskExists(new Document("_id", id))) {
|
|
@@ -106,6 +150,28 @@ public class SynDataServiceImpl implements SynDataService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public String saveTask(String collectionName, List<String> tns, String ds) {
|
|
|
+ String id = getObjectId(ds);
|
|
|
+ if (findTaskExists(collectionName, new Document("_id", id))) {
|
|
|
+ return "task repeat";
|
|
|
+ } else {
|
|
|
+ if (tns == null || tns.size() == 0) {
|
|
|
+ tns = new ArrayList<>();
|
|
|
+ }
|
|
|
+ Document d = new Document();
|
|
|
+ d.put("_id", id);
|
|
|
+ d.put("create_time", new Date());
|
|
|
+ d.put("update_time", new Date());
|
|
|
+ d.put("status", "create");
|
|
|
+ d.put("tns", tns);
|
|
|
+ d.put("ds", ds);
|
|
|
+ mongoTemplate.getCollection(collectionName).insertOne(d);
|
|
|
+ return "task create success";
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public void sendMessage(String message) {
|
|
|
try {
|
|
|
dingTalkApi.sendTextByPhones("data_push_message", "winhc\n" + message, Collections.singletonList("17602140784"));
|
|
@@ -131,6 +197,7 @@ public class SynDataServiceImpl implements SynDataService {
|
|
|
return StreamSupport.stream(it.spliterator(), false).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
public List<Document> findAllTask(String collectionName, Document doc) {
|
|
|
FindIterable<Document> it = mongoTemplate.getCollection(collectionName).find(doc);
|
|
@@ -153,12 +220,25 @@ public class SynDataServiceImpl implements SynDataService {
|
|
|
|
|
|
@Override
|
|
|
public Boolean updateTaskStatus(String collectionName, String ds, String status) {
|
|
|
+ return this.updateTaskStatus(collectionName, ds, status, ds);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean updateTaskStatus(String collectionName, Document d, String id) {
|
|
|
+ mongoTemplate.getCollection(collectionName)
|
|
|
+ .findOneAndUpdate(eq("_id", id), new Document("$set", d), new FindOneAndUpdateOptions().upsert(true));
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean updateTaskStatus(String collectionName, String ds, String status, String id) {
|
|
|
Document d = new Document();
|
|
|
d.put("update_time", new Date());
|
|
|
d.put("status", status);
|
|
|
d.put("ds", ds);
|
|
|
mongoTemplate.getCollection(collectionName)
|
|
|
- .findOneAndUpdate(eq("_id", ds), new Document("$set", d), new FindOneAndUpdateOptions().upsert(true));
|
|
|
+ .findOneAndUpdate(eq("_id", id), new Document("$set", d), new FindOneAndUpdateOptions().upsert(true));
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -247,4 +327,25 @@ public class SynDataServiceImpl implements SynDataService {
|
|
|
ossClient.putObject(putObjectRequest);
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void saveObject(String ds, String targetBucketName, String ossPre) {
|
|
|
+ String prcessDate = getPrcessDate(ds);
|
|
|
+ String pre = ossPre + "/" + prcessDate + "/";
|
|
|
+ List<String> keys = listKeys(targetBucketName, pre);
|
|
|
+ List<String> filters_keys = keys.stream()
|
|
|
+ .filter(k -> tc_tns.contains(getTn(k)))
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ String sum = JSON.toJSONString(filters_keys);
|
|
|
+ String targetKey = pre + "out_tc.success";
|
|
|
+ PutObjectRequest putObjectRequest = new PutObjectRequest(targetBucketName, targetKey, new ByteArrayInputStream(sum.getBytes()));
|
|
|
+ ossClient.putObject(putObjectRequest);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getTn(String key) {
|
|
|
+ String re = key.substring(0, key.lastIndexOf("/"));
|
|
|
+ return re.substring(re.lastIndexOf("/") + 1);
|
|
|
+ }
|
|
|
}
|