|
@@ -1,8 +1,6 @@
|
|
|
package cn.com.ctop.job.bytedance.data.service.impl;
|
|
package cn.com.ctop.job.bytedance.data.service.impl;
|
|
|
|
|
|
|
|
-import cn.com.ctop.job.bytedance.data.entity.BytedanceAdvertiserReportDaily;
|
|
|
|
|
-import cn.com.ctop.job.bytedance.data.entity.BytedanceAdvertiserReportHour;
|
|
|
|
|
-import cn.com.ctop.job.bytedance.data.entity.BytedanceErrorLog;
|
|
|
|
|
|
|
+import cn.com.ctop.job.bytedance.data.entity.*;
|
|
|
import cn.com.ctop.job.bytedance.data.mapper.BytedanceAccountReportMapper;
|
|
import cn.com.ctop.job.bytedance.data.mapper.BytedanceAccountReportMapper;
|
|
|
import cn.com.ctop.job.bytedance.data.service.IBytedanceAccountReportService;
|
|
import cn.com.ctop.job.bytedance.data.service.IBytedanceAccountReportService;
|
|
|
import cn.com.ctop.job.bytedance.data.utils.AlertUtils;
|
|
import cn.com.ctop.job.bytedance.data.utils.AlertUtils;
|
|
@@ -13,19 +11,29 @@ import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
-import java.util.HashMap;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
|
+import javax.annotation.PreDestroy;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
|
|
+@EnableScheduling
|
|
|
public class BytedanceAccountReportServiceImpl implements IBytedanceAccountReportService {
|
|
public class BytedanceAccountReportServiceImpl implements IBytedanceAccountReportService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private BytedanceAccountReportMapper accountReportMapper;
|
|
private BytedanceAccountReportMapper accountReportMapper;
|
|
|
|
|
|
|
|
|
|
+ private static final int MAX_BATCH_SIZE = 5000;
|
|
|
|
|
+ private static final long MAX_WAIT_TIME_MS = 2 * 60 * 1000;
|
|
|
|
|
+
|
|
|
|
|
+ private final List<BytedanceAdvertiserReportHour> advertiserReportHourBuffer = Collections.synchronizedList(new ArrayList<>());
|
|
|
|
|
+ private final List<BytedanceAdvertiserReportDaily> advertiserReportDailyBuffer = Collections.synchronizedList(new ArrayList<>());
|
|
|
|
|
+ private long advertiserReportHourLastFlushTime = System.currentTimeMillis();
|
|
|
|
|
+ private long advertiserReportDailyLastFlushTime = System.currentTimeMillis();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public void getBytedanceRetryAccountHourReport(Long errorId, Long advertiserId, String accessToken, String startDate, String endDate, Integer page, Integer isSend) {
|
|
public void getBytedanceRetryAccountHourReport(Long errorId, Long advertiserId, String accessToken, String startDate, String endDate, Integer page, Integer isSend) {
|
|
|
try {
|
|
try {
|
|
@@ -79,14 +87,14 @@ public class BytedanceAccountReportServiceImpl implements IBytedanceAccountRepor
|
|
|
if (!Check.isNull(hours)) {
|
|
if (!Check.isNull(hours)) {
|
|
|
accountReportMapper.replaceAccountReportHour(hours);
|
|
accountReportMapper.replaceAccountReportHour(hours);
|
|
|
try {
|
|
try {
|
|
|
- accountReportMapper.replaceAccountReportHourSD(hours);
|
|
|
|
|
- }catch (Exception e) {
|
|
|
|
|
- log.error("replaceAccountReportHourSD",e);
|
|
|
|
|
|
|
+ advertiserReportHourBuffer.addAll(hours);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if (hours.size() >= 100) {
|
|
if (hours.size() >= 100) {
|
|
|
try {
|
|
try {
|
|
|
- Thread.sleep(200L);
|
|
|
|
|
|
|
+ Thread.sleep(100L);
|
|
|
} catch (InterruptedException e) {
|
|
} catch (InterruptedException e) {
|
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
}
|
|
@@ -154,9 +162,9 @@ public class BytedanceAccountReportServiceImpl implements IBytedanceAccountRepor
|
|
|
if (!Check.isNull(dailyList)) {
|
|
if (!Check.isNull(dailyList)) {
|
|
|
accountReportMapper.replaceAccountReportDaily(dailyList);
|
|
accountReportMapper.replaceAccountReportDaily(dailyList);
|
|
|
try {
|
|
try {
|
|
|
- accountReportMapper.replaceAccountReportDailySD(dailyList);
|
|
|
|
|
- }catch (Exception e) {
|
|
|
|
|
- log.error("replaceAccountReportDailySD",e);
|
|
|
|
|
|
|
+ advertiserReportDailyBuffer.addAll(dailyList);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("replaceAccountReportDailySD", e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if (dailyList.size() >= 100) {
|
|
if (dailyList.size() >= 100) {
|
|
@@ -170,7 +178,7 @@ public class BytedanceAccountReportServiceImpl implements IBytedanceAccountRepor
|
|
|
log.info("账户ID:{},日期:{},广告时报(账户粒度)数据请求成功", advertiserId, startDate);
|
|
log.info("账户ID:{},日期:{},广告时报(账户粒度)数据请求成功", advertiserId, startDate);
|
|
|
accountReportMapper.deleteInfoByErrorId(errorId);
|
|
accountReportMapper.deleteInfoByErrorId(errorId);
|
|
|
if (isSend == 1) {
|
|
if (isSend == 1) {
|
|
|
- AlertUtils.sendMessage(advertiserId, startDate, endDate, 1, "hour", 1,"account");
|
|
|
|
|
|
|
+ AlertUtils.sendMessage(advertiserId, startDate, endDate, 1, "hour", 1, "account");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -251,9 +259,9 @@ public class BytedanceAccountReportServiceImpl implements IBytedanceAccountRepor
|
|
|
if (!Check.isNull(hours)) {
|
|
if (!Check.isNull(hours)) {
|
|
|
accountReportMapper.replaceAccountReportHour(hours);
|
|
accountReportMapper.replaceAccountReportHour(hours);
|
|
|
try {
|
|
try {
|
|
|
- accountReportMapper.replaceAccountReportHourSD(hours);
|
|
|
|
|
- }catch (Exception e) {
|
|
|
|
|
- log.error("replaceAccountReportHourSD",e);
|
|
|
|
|
|
|
+ advertiserReportHourBuffer.addAll(hours);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if (hours.size() >= 100) {
|
|
if (hours.size() >= 100) {
|
|
@@ -342,9 +350,9 @@ public class BytedanceAccountReportServiceImpl implements IBytedanceAccountRepor
|
|
|
if (!Check.isNull(dailyList)) {
|
|
if (!Check.isNull(dailyList)) {
|
|
|
accountReportMapper.replaceAccountReportDaily(dailyList);
|
|
accountReportMapper.replaceAccountReportDaily(dailyList);
|
|
|
try {
|
|
try {
|
|
|
- accountReportMapper.replaceAccountReportDailySD(dailyList);
|
|
|
|
|
- }catch (Exception e) {
|
|
|
|
|
- log.error("replaceAccountReportDailySD",e);
|
|
|
|
|
|
|
+ advertiserReportDailyBuffer.addAll(dailyList);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("replaceAccountReportDailySD", e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
if (dailyList.size() >= 100) {
|
|
if (dailyList.size() >= 100) {
|
|
@@ -361,4 +369,72 @@ public class BytedanceAccountReportServiceImpl implements IBytedanceAccountRepor
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Scheduled(cron = "*/30 * * * * ?") // 每30秒检查一次
|
|
|
|
|
+ public void flushIfNeeded() {
|
|
|
|
|
+ log.info("开始执行账户报表定时批处理任务....");
|
|
|
|
|
+ synchronized (advertiserReportHourBuffer) {
|
|
|
|
|
+ long now = System.currentTimeMillis();
|
|
|
|
|
+ if (advertiserReportHourBuffer.size() >= MAX_BATCH_SIZE ||
|
|
|
|
|
+ (now - advertiserReportHourLastFlushTime) >= MAX_WAIT_TIME_MS && !advertiserReportHourBuffer.isEmpty()) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ log.info("Flushed advertiserReportHour batch size: {}", advertiserReportHourBuffer.size());
|
|
|
|
|
+ accountReportMapper.replaceAccountReportHourSD(new ArrayList<>(advertiserReportHourBuffer));
|
|
|
|
|
+ advertiserReportHourBuffer.clear();
|
|
|
|
|
+ advertiserReportHourLastFlushTime = now;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("Error flushing advertiserReportHour batch", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ synchronized (advertiserReportDailyBuffer) {
|
|
|
|
|
+ long now = System.currentTimeMillis();
|
|
|
|
|
+ if (advertiserReportDailyBuffer.size() >= MAX_BATCH_SIZE ||
|
|
|
|
|
+ (now - advertiserReportDailyLastFlushTime) >= MAX_WAIT_TIME_MS && !advertiserReportDailyBuffer.isEmpty()) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ log.info("Flushed advertiserReportDaily batch size: {}", advertiserReportDailyBuffer.size());
|
|
|
|
|
+ accountReportMapper.replaceAccountReportDailySD(new ArrayList<>(advertiserReportDailyBuffer));
|
|
|
|
|
+ advertiserReportDailyBuffer.clear();
|
|
|
|
|
+ advertiserReportDailyLastFlushTime = now;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("Error flushing advertiserReportDaily batch", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @PreDestroy
|
|
|
|
|
+ public void flushPreDestroy() {
|
|
|
|
|
+ log.info("项目重启,开始执行广告报表批处理任务....");
|
|
|
|
|
+ synchronized (advertiserReportHourBuffer) {
|
|
|
|
|
+ if (!advertiserReportHourBuffer.isEmpty()) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ log.info("Flushed advertiserReportHour batch size: {}", advertiserReportHourBuffer.size());
|
|
|
|
|
+ accountReportMapper.replaceAccountReportHourSD(new ArrayList<>(advertiserReportHourBuffer));
|
|
|
|
|
+ advertiserReportHourBuffer.clear();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("Error flushing advertiserReportHour batch", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ synchronized (advertiserReportDailyBuffer) {
|
|
|
|
|
+ if (!advertiserReportDailyBuffer.isEmpty()) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ log.info("Flushed advertiserReportDaily batch size: {}", advertiserReportDailyBuffer.size());
|
|
|
|
|
+ accountReportMapper.replaceAccountReportDailySD(new ArrayList<>(advertiserReportDailyBuffer));
|
|
|
|
|
+ advertiserReportDailyBuffer.clear();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("Error flushing advertiserReportDaily batch", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|