59 lines
2.2 KiB
Java
59 lines
2.2 KiB
Java
package com.goi.integration.samsara.job;
|
|
|
|
import com.goi.integration.common.config.ScheduleJobConfigDto;
|
|
import com.goi.integration.common.config.ScheduleJobConfigProvider;
|
|
import com.goi.integration.common.dto.ExtIngestResult;
|
|
import com.goi.integration.samsara.client.SamsaraClient;
|
|
import com.goi.integration.samsara.service.InspectionIngestService;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.time.Duration;
|
|
import java.time.Instant;
|
|
|
|
@Slf4j
|
|
@Component
|
|
@RequiredArgsConstructor
|
|
public class DvirIngestJob {
|
|
|
|
private static final String JOB_CODE = "SAMSARA_DVIR";
|
|
|
|
private final ScheduleJobConfigProvider configProvider;
|
|
private final SamsaraClient samsaraClient;
|
|
private final InspectionIngestService ingestService;
|
|
|
|
@Scheduled(cron = "${ext.samsara.jobs.dvir.cron:0 */10 * * * *}")
|
|
public void run() {
|
|
|
|
ScheduleJobConfigDto cfg = configProvider.getJobConfig(JOB_CODE)
|
|
.filter(ScheduleJobConfigDto::getSjcEnabled)
|
|
.orElse(null);
|
|
|
|
if (cfg == null) {
|
|
log.info("[{}] disabled or config missing", JOB_CODE);
|
|
return;
|
|
}
|
|
|
|
int limit = (cfg.getSjcMaxRecords() != null) ? cfg.getSjcMaxRecords() : 252;
|
|
|
|
Instant now = Instant.now();
|
|
long lookbackSec = Duration.ofHours(cfg.getSjcLookbackHours() != null ? cfg.getSjcLookbackHours() : 24).getSeconds();
|
|
long overlapSec = Duration.ofMinutes(cfg.getSjcOverlapMinutes() != null ? cfg.getSjcOverlapMinutes() : 0).getSeconds();
|
|
|
|
Instant start = now.minusSeconds(lookbackSec + overlapSec);
|
|
Instant end = now;
|
|
|
|
log.info("[{}] calling samsara dvir history start={} end={} limit={}", JOB_CODE, start, end, limit);
|
|
|
|
String rawJson = samsaraClient.getDvirHistory(limit, start, end);
|
|
|
|
// opr-rest-api ingest 호출
|
|
ExtIngestResult result = ingestService.ingestFromRawJson(rawJson);
|
|
|
|
log.info("[{}] ingest result received={}, inserted={}, updated={}, skipped={}",
|
|
JOB_CODE, result.getReceived(), result.getInserted(), result.getUpdated(), result.getSkipped());
|
|
}
|
|
} |