Commit b9b888c3 authored by Roberto Loayza's avatar Roberto Loayza

Mantenimiento proceso de Eliminación de data sensible y calendario

parent b57fda7f
......@@ -5,6 +5,7 @@ import lombok.Getter;
import lombok.Setter;
import java.math.BigInteger;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
@Getter
......@@ -17,9 +18,9 @@ public class WeekSchedulerBean {
@Expose
private int dayOfWeek;
@Expose
private OffsetTime from;
private OffsetDateTime from;
@Expose
private OffsetTime to;
private OffsetDateTime to;
@Expose
private String calendarID;
@Expose
......
......@@ -18,9 +18,13 @@ import com.bytesw.bytebot.etl.enums.IntentTypeEnum;
import com.bytesw.bytebot.etl.services.DeleteSensMessageService;
import com.bytesw.bytebot.etl.services.ProcessMessageService;
import com.bytesw.bytebot.jdbc.DeleteDataSensJDBCRepository;
import com.bytesw.bytebot.model.*;
import com.bytesw.bytebot.model.enums.AgentParameterEnum;
import com.bytesw.bytebot.model.enums.AgentStatusEnum;
import com.bytesw.bytebot.repository.AgentRepository;
import com.bytesw.bytebot.model.enums.FrequencyType;
import com.bytesw.bytebot.model.enums.ProcessETLEnum;
import com.bytesw.bytebot.repository.*;
import com.bytesw.xdf.exception.NotFoundException;
import com.bytesw.xdf.multitenant.core.ThreadLocalStorage;
import com.google.gson.Gson;
import lombok.extern.log4j.Log4j2;
......@@ -42,9 +46,11 @@ import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.sql.Timestamp;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.Calendar;
import java.util.concurrent.ScheduledFuture;
// Descomentar lo siguiente para que funcione
......@@ -118,6 +124,22 @@ public class ScheduleService implements SchedulingConfigurer {
@Autowired
private IntentRepository intentRepository;
@Autowired
private ProcessETLRepository processETLRepository;
@Autowired
private SchedulerTaskRepository schedulerTaskRepository;
@Autowired
private WeekSchedulerRepository weekSchedulerRepository;
@Autowired
private CalendarExceptionFullRepository calendarExceptionFullRepository;
private boolean schedulerFlag = false;
private List<String> keys = new ArrayList<>();
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
if (scheduledTaskRegistrar == null) {
......@@ -149,19 +171,102 @@ public class ScheduleService implements SchedulingConfigurer {
private void configureTask(String tenantIdentifier, String identifier, ScheduledTaskRegistrar taskRegistrar) {
Trigger trigger = new CronTrigger(cronExpression, TimeZone.getDefault());
/* ETL de Dashboard */
String key = String.format("%s-%s", tenantIdentifier, identifier);
futureMap.put(key, taskRegistrar.getScheduler().schedule(() -> scheduleCron(createJob(tenantIdentifier), tenantIdentifier), trigger));
/*ETL eliminacion de data sensible*/
List<DeleteDataSensBean> deleteDataSensBeans = deleteDataSensJDBCRepository
.getListAgentChannel(AgentStatusEnum.DEPLOYED.getName(), AgentParameterEnum.ACCESS_TWILIO.getName());
String keyDataSens = "";
for (DeleteDataSensBean data : deleteDataSensBeans) {
keyDataSens = String.format("%s-%s", tenantIdentifier, data.getValue());
futureMap.put(keyDataSens, taskRegistrar.getScheduler()
.schedule(() -> scheduleCron(createJobDataSens(tenantIdentifier, data), tenantIdentifier), trigger));
// String key = String.format("%s-%s", tenantIdentifier, identifier);
// futureMap.put(key, taskRegistrar.getScheduler().schedule(() -> scheduleCron(createJob(tenantIdentifier), tenantIdentifier), trigger));
/* Extraer el id del proceso - Delete */
ProcessETL processDelete = processETLRepository.findByName(ProcessETLEnum.PROCESS_DELETE.getName());
/* Busca las listas de SchedulerTask */
List<SchedulerTask> listSchedulerTask = schedulerTaskRepository.findByEtlId(processDelete.getId());
System.out.println(listSchedulerTask);
for (SchedulerTask schedulerTask : listSchedulerTask) {
Trigger triggerDelete = new CronTrigger(schedulerTask.getCronExpression(), TimeZone.getDefault());
String keyScheduler = String.format("%s-%s", tenantIdentifier, schedulerTask.getCalendarID());
futureMap.put(keyScheduler, taskRegistrar.getScheduler()
.schedule(() -> processDeleteData(tenantIdentifier, taskRegistrar, schedulerTask.getCalendarID().trim()), triggerDelete));
}
}
/*ETL eliminacion de data sensible*/
public void processDeleteData(String tenantIdentifier, ScheduledTaskRegistrar taskRegistrar, String calendarId) {
Optional<List<WeekScheduler>> dates = weekSchedulerRepository.findByCalendarId(calendarId);
System.out.println(dates);
OffsetDateTime actual = OffsetDateTime.now();
for(WeekScheduler weekScheduler: dates.get()) {
if (actual.isBefore(weekScheduler.getTo()) && actual.isAfter(weekScheduler.getFrom())) {
//schedulerFlag = true;
Optional<List<CalendarExceptionFull>> calendarException = calendarExceptionFullRepository.findByCalendarId(calendarId);
if (calendarException.isPresent()){
schedulerFlag = validateException(calendarException.get());
break;
}
} else {
schedulerFlag = false;
}
}
if (schedulerFlag) {
Trigger trigger = new CronTrigger(cronExpression, TimeZone.getDefault());
List<DeleteDataSensBean> deleteDataSensBeans = deleteDataSensJDBCRepository
.getListAgentChannel(AgentStatusEnum.DEPLOYED.getName(), AgentParameterEnum.ACCESS_TWILIO.getName());
String keyDataSens = "";
for (DeleteDataSensBean data : deleteDataSensBeans) {
keyDataSens = String.format("deleteSensible-%s-%s", tenantIdentifier, data.getValue());
if (!futureMap.containsKey(keyDataSens)) {
keys.add(keyDataSens);
futureMap.put(keyDataSens, taskRegistrar.getScheduler()
.schedule(() -> scheduleCron(createJobDataSens(tenantIdentifier, data), tenantIdentifier), trigger));
System.out.println("Hola mundo");
}
}
} else {
keys.forEach(x -> {
futureMap.remove(x);
});
}
}
private boolean validateException(List<CalendarExceptionFull> calendarException){
boolean result = true;
FrequencyType[] frecuencyType = FrequencyType.values();
OffsetDateTime now = OffsetDateTime.now();
for (CalendarExceptionFull exception : calendarException){
for (int i = 0; i < frecuencyType.length; i++) {
if (frecuencyType[i].getName().equals(exception.getFrequencyType())) {
if (exception.getFrequencyType().equals(FrequencyType.ANNUAL.getName())) {
if (now.getMonth().getValue() == exception.getMonth().intValue() && exception.getDayOfMonth().intValue() == now.getDayOfMonth()){
result = false;
}
} else if (exception.getFrequencyType().equals(FrequencyType.RANGE.getName())) {
if (now.isBefore(exception.getTo()) && now.isAfter(exception.getFrom())){
result = false;
}
} else if (exception.getFrequencyType().equals(FrequencyType.UNIQUE.getName())) {
if (now.toLocalDate() == exception.getDate()) {
result = false;
}
} else if (exception.getFrequencyType().equals(FrequencyType.UniqueWeekly.getName())) {
Calendar ca1 = Calendar.getInstance();
ca1.set(now.getYear(), now.getMonth().getValue(), now.getDayOfMonth());
ca1.setMinimalDaysInFirstWeek(1);
if (now.getMonth().getValue() == exception.getMonth().intValue() && exception.getDayOfWeek().intValue() == now.getDayOfWeek().getValue()
&& exception.getWeekOfMonth().intValue() == ca1.WEEK_OF_MONTH){
result = false;
}
}
break;
}
}
if (!result) {
break;
}
}
return result;
}
/* Métodos utilizados para ETL de dashboard */
......@@ -239,7 +344,7 @@ public class ScheduleService implements SchedulingConfigurer {
DeleteDataSensControlBean control = deleteDataSensJDBCRepository.getControl(data.getAgenId());
OffsetDateTime endDate = OffsetDateTime.now();
Long id = 0L;
Long id = 13000L;
int differenceTime = 0;
if (control != null) {
......
......@@ -45,7 +45,7 @@ public class Agent implements Serializable {
@Column(name = "agen_tzone")
private String timezone;
@Column(name = "agen_delete_period")
@Column(name = "agen_delete_sensible_period")
private String period;
@Lob
......
package com.bytesw.bytebot.model;
import com.bytesw.bytebot.model.converters.CalendarExceptionTypeConverter;
import com.bytesw.bytebot.model.converters.FrequencyTypeConverter;
import com.bytesw.bytebot.model.enums.CalendarExceptionType;
import lombok.EqualsAndHashCode;
import lombok.Getter;
......
package com.bytesw.bytebot.model;
import com.bytesw.bytebot.model.converters.CalendarExceptionTypeConverter;
import com.bytesw.bytebot.model.enums.CalendarExceptionType;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.*;
import java.io.Serializable;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.OffsetDateTime;
//@Audited
@Entity
@Inheritance(
strategy = InheritanceType.SINGLE_TABLE
)
@DiscriminatorColumn(name="CALE_EXC_FRECU", discriminatorType = DiscriminatorType.STRING)
@Table(name = "AVB_CALENDAR_EXCEPTION")
@Getter
@Setter
@EqualsAndHashCode
@ToString(exclude = "id")
public class CalendarExceptionFull implements Serializable {
@Id
@Column(name = "CALE_EXC_ID")
// @SequenceGenerator(name = "AVB_CALENDAR_EXCEPTION_GENERATOR", sequenceName = "AVB_CALENDAR_EXCEPTION_SEQ", initialValue = 1, allocationSize = 1)
// @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AVB_CALENDAR_EXCEPTION_GENERATOR")
private BigInteger id;
@Version
@Column(name = "CALE_EXC_VERSION")
@Basic(optional = false)
private long version;
@Column(name = "CALE_EXC_TYPE")
@Convert(converter = CalendarExceptionTypeConverter.class)
private CalendarExceptionType calendarExceptionType;
@Column(name = "CALE_EXC_FRECU", insertable = false, updatable = false)
private String frequencyType;
@Column(name = "CALE_EXC_DESCR")
private String description;
@Column(name = "CALE_ID")
private String calendarID;
// @ManyToOne(optional = false)
// @JoinColumn(name = "CALE_ID", referencedColumnName = "CALE_ID", nullable = false)
// private Calendar calendar;
@Column(name = "CALE_EXC_MONTH")
private BigInteger month;
@Column(name = "CALE_EXC_DAYMO")
private BigInteger dayOfMonth;
@Column(name = "CALE_EXC_WEEK")
private BigInteger weekOfMonth;
@Column(name = "CALE_EXC_DAYWE")
private BigInteger dayOfWeek;
@Column(name = "CALE_EXC_DATE")
private LocalDate date;
@Column(name = "CALE_EXC_FROM")
private OffsetDateTime from;
@Column(name = "CALE_EXC_TO")
private OffsetDateTime to;
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ import org.hibernate.envers.Audited;
import javax.persistence.*;
import java.io.Serializable;
import java.math.BigInteger;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
//@Audited
......@@ -38,10 +39,10 @@ public class WeekScheduler implements Serializable {
private int dayOfWeek;
@Column(name = "WESC_FROM", nullable = false)
private OffsetTime from;
private OffsetDateTime from;
@Column(name = "WESC_TO", nullable = false)
private OffsetTime to;
private OffsetDateTime to;
@Column(name = "CALE_ID")
private String calendarID;
......
package com.bytesw.bytebot.model.converters;
import com.bytesw.bytebot.model.enums.FrequencyType;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter
public class FrequencyTypeConverter implements AttributeConverter<FrequencyType, String> {
@Override
public String convertToDatabaseColumn(FrequencyType frequencyType) {
if (frequencyType == null) return null;
return frequencyType.getName();
}
@Override
public FrequencyType convertToEntityAttribute(String s) {
return FrequencyType.fromString(s);
}
}
package com.bytesw.bytebot.model.enums;
import com.bytesw.bytebot.model.AnnualCalendarException;
import com.bytesw.bytebot.model.RangeCalendarException;
import com.bytesw.bytebot.model.UniqueCalendarException;
import com.bytesw.bytebot.model.UniqueWeeklyCalendarException;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
public enum FrequencyType {
ANNUAL(AnnualCalendarException.DISCRIMINATOR_VALUE),
UNIQUE(UniqueCalendarException.DISCRIMINATOR_VALUE),
UniqueWeekly(UniqueWeeklyCalendarException.DISCRIMINATOR_VALUE),
RANGE(RangeCalendarException.DISCRIMINATOR_VALUE);
private final String name;
private static final Map<String, FrequencyType> map = new HashMap<>();
static {
for (FrequencyType type : FrequencyType.values()) {
map.put(type.name, type);
}
}
FrequencyType(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static FrequencyType fromString(String name) {
if (map.containsKey(name)) {
return map.get(name);
}
throw new NoSuchElementException(name + " not found");
}
}
package com.bytesw.bytebot.model.enums;
import lombok.Getter;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
public enum ProcessETLEnum {
PROCESS_DELETE ( "Eliminación de Data Sensible");
private final String name;
private static final Map<String, ProcessETLEnum> map = new HashMap<>();
static {
for (ProcessETLEnum type : ProcessETLEnum.values()) {
map.put(type.name, type);
}
}
ProcessETLEnum (String name) {
this.name = name;
}
public String getName() {
return name;
}
public static ProcessETLEnum fromString(String name) {
if (map.containsKey(name)) {
return map.get(name);
}
throw new NoSuchElementException(name + " not found");
}
}
\ No newline at end of file
package com.bytesw.bytebot.repository;
import com.bytesw.bytebot.model.CalendarException;
import com.bytesw.bytebot.model.CalendarExceptionFull;
import com.bytesw.bytebot.model.enums.CalendarExceptionType;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;
public interface CalendarExceptionFullRepository extends CrudRepository<CalendarExceptionFull, BigInteger>, JpaSpecificationExecutor<CalendarExceptionFull> {
@Query("select s from CalendarExceptionFull s where s.calendarID = :calendarId")
Optional<List<CalendarExceptionFull>> findByCalendarId(@Param("calendarId") String calendarId);
}
......@@ -2,7 +2,13 @@ package com.bytesw.bytebot.repository;
import com.bytesw.bytebot.model.ProcessETL;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
public interface ProcessETLRepository extends CrudRepository<ProcessETL, Integer>, JpaSpecificationExecutor<ProcessETL> {
@Query("select b from ProcessETL b where b.name = :etlName")
ProcessETL findByName(@Param("etlName") String etlName);
}
......@@ -15,4 +15,7 @@ public interface SchedulerTaskRepository extends CrudRepository<SchedulerTask, B
@Query("select b from SchedulerTask b where b.calendar.id = :calendarId")
Optional<List<SchedulerTask>> findByCalendarId(@Param("calendarId") String calendarId);
@Query("select b from SchedulerTask b where b.processETL = :processId")
List<SchedulerTask> findByEtlId(@Param("processId") int processId);
}
......@@ -14,4 +14,5 @@ public interface WeekSchedulerRepository extends CrudRepository<WeekScheduler, B
@Query("select b from WeekScheduler b where b.calendarID = :caleId")
Optional<List<WeekScheduler>> findByCalendarId(@Param("caleId") String caleId);
}
......@@ -9,7 +9,7 @@
chpv_value as channelValue,
AG.agen_iden as value,
AG.agen_id as agenId,
AG.agen_delete_period as deletePeriod
AG.agen_delete_sensible_period as deletePeriod
FROM AVB_DEPLOYMENT_CHANNEL_PARAM_VALUE ADCP
JOIN AVB_CHANNEL_PARAM ACP ON ACP.chpa_id=ADCP.chpa_id
JOIN avb_deployment_channel AD on AD.dcha_id=ADCP.dcha_id
......
......@@ -9,7 +9,7 @@
chpv_value as channelValue,
AG.agen_iden as value,
AG.agen_id as agenId,
AG.agen_delete_period as deletePeriod
AG.agen_delete_sensible_period as deletePeriod
FROM AVB_DEPLOYMENT_CHANNEL_PARAM_VALUE ADCP
JOIN AVB_CHANNEL_PARAM ACP ON ACP.chpa_id=ADCP.chpa_id
JOIN avb_deployment_channel AD on AD.dcha_id=ADCP.dcha_id
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment