Skip to content

Commit

Permalink
[InsightReport] DataInsight Manual trigger Job (open-metadata#11538)
Browse files Browse the repository at this point in the history
* Add Manual Trigger for Insight Reports

* unrequ change

* add time diff comment
  • Loading branch information
mohityadav766 authored May 10, 2023
1 parent c027799 commit 5bbd49c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.text.ParseException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -54,6 +53,7 @@
import org.openmetadata.schema.dataInsight.type.TotalEntitiesByTier;
import org.openmetadata.schema.dataInsight.type.TotalEntitiesByType;
import org.openmetadata.schema.entity.events.EventSubscription;
import org.openmetadata.schema.entity.events.TriggerConfig;
import org.openmetadata.schema.entity.teams.Team;
import org.openmetadata.schema.type.Include;
import org.openmetadata.service.Entity;
Expand Down Expand Up @@ -81,10 +81,9 @@ public void execute(JobExecutionContext jobExecutionContext) {
(RestHighLevelClient) jobExecutionContext.getJobDetail().getJobDataMap().get(ES_REST_CLIENT);
EventSubscription dataReport =
(EventSubscription) jobExecutionContext.getJobDetail().getJobDataMap().get(EVENT_SUBSCRIPTION);
Date nextFireTime = jobExecutionContext.getTrigger().getNextFireTime();
Long currentTime = Instant.now().toEpochMilli();
Long timeDifference = nextFireTime.getTime() - currentTime;
Long scheduleTime = currentTime - timeDifference;
// Calculate time diff
long currentTime = Instant.now().toEpochMilli();
long scheduleTime = currentTime - getTimeFromSchedule(dataReport.getTrigger());
int numberOfDaysChange = getNumberOfDays(dataReport.getTrigger());
try {
sendReportsToTeams(repository, client, scheduleTime, currentTime, numberOfDaysChange);
Expand Down Expand Up @@ -507,4 +506,19 @@ private TreeMap<Long, List<Object>> getSortedDate(
}
return dateWithDataMap;
}

private long getTimeFromSchedule(TriggerConfig config) {
if (config.getTriggerType() == TriggerConfig.TriggerType.SCHEDULED) {
TriggerConfig.ScheduleInfo scheduleInfo = config.getScheduleInfo();
switch (scheduleInfo) {
case DAILY:
return 86400000L;
case WEEKLY:
return 604800000L;
case MONTHLY:
return 2592000000L;
}
}
throw new IllegalArgumentException("Invalid Trigger Type, Can only be Scheduled.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.openmetadata.schema.entity.events.EventSubscription;
Expand Down Expand Up @@ -130,4 +132,17 @@ public static void shutDown() throws SchedulerException {
INSTANCE.reportScheduler.shutdown();
}
}

public Response triggerExistingDataInsightJob(EventSubscription dataReport) throws SchedulerException {
JobDetail jobDetail = getJobKey(dataReport.getId());
if (jobDetail != null) {
JobDataMap dataMap = new JobDataMap();
dataMap.put(JOB_CONTEXT_CHART_REPO, this.chartRepository);
dataMap.put(ES_REST_CLIENT, restHighLevelClient);
dataMap.put(EVENT_SUBSCRIPTION, dataReport);
reportScheduler.triggerJob(jobDetail.getKey(), dataMap);
return Response.status(Response.Status.OK).entity("Job Triggered Successfully.").build();
}
throw new BadRequestException("Job with given Id does not exist");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,12 @@ public Map<String, Double> getTierMap() {
public void setTierMap(Map<String, Double> tierMap) {
this.tierMap = tierMap;
}

public int getNumberOfDaysChange() {
return numberOfDaysChange;
}

public void setNumberOfDaysChange(int numberOfDaysChange) {
this.numberOfDaysChange = numberOfDaysChange;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class DataInsightTotalAssetTemplate {
private Double totalDataAssets;
private Double percentChangeTotalAssets;
private String completeMessage;

private int numberOfDaysChange;

public DataInsightTotalAssetTemplate(
Expand Down Expand Up @@ -57,4 +58,12 @@ public String getCompleteMessage() {
public void setCompleteMessage(String completeMessage) {
this.completeMessage = completeMessage;
}

public int getNumberOfDaysChange() {
return numberOfDaysChange;
}

public void setNumberOfDaysChange(int numberOfDaysChange) {
this.numberOfDaysChange = numberOfDaysChange;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,33 @@ public Response createOrUpdateEventSubscription(
return response;
}

@PUT
@Path("/trigger/{id}")
@Operation(
operationId = "triggerDataInsightJob",
summary = "Trigger a existing Data Insight Report Job to run",
description = "Trigger a existing Data Insight Report Job to run",
responses = {
@ApiResponse(
responseCode = "200",
description = "create Event Subscription",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = CreateEventSubscription.class))),
@ApiResponse(responseCode = "400", description = "Bad request")
})
public Response triggerDataInsightJob(
@Context UriInfo uriInfo,
@Context SecurityContext securityContext,
@Parameter(description = "Id of the event Subscription", schema = @Schema(type = "UUID")) @PathParam("id")
UUID id)
throws IOException, SchedulerException {
// authorizer.authorizeAdmin(securityContext);
EventSubscription eventSub = dao.get(null, id, dao.getFields("id,name"));
return ReportsHandler.getInstance().triggerExistingDataInsightJob(eventSub);
}

@PATCH
@Path("/{id}")
@Operation(
Expand Down

0 comments on commit 5bbd49c

Please sign in to comment.