From 5bbd49cc604cfa729cda4d43887ad429d294b2b2 Mon Sep 17 00:00:00 2001 From: Mohit Yadav <105265192+mohityadav766@users.noreply.github.com> Date: Wed, 10 May 2023 23:49:50 +0530 Subject: [PATCH] [InsightReport] DataInsight Manual trigger Job (#11538) * Add Manual Trigger for Insight Reports * unrequ change * add time diff comment --- .../scheduled/DataInsightsReportJob.java | 24 +++++++++++++---- .../events/scheduled/ReportsHandler.java | 15 +++++++++++ ...ataInsightDescriptionAndOwnerTemplate.java | 8 ++++++ .../DataInsightTotalAssetTemplate.java | 9 +++++++ .../EventSubscriptionResource.java | 27 +++++++++++++++++++ 5 files changed, 78 insertions(+), 5 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/DataInsightsReportJob.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/DataInsightsReportJob.java index df6f728475bd..8449f4dfddd6 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/DataInsightsReportJob.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/DataInsightsReportJob.java @@ -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; @@ -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; @@ -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); @@ -507,4 +506,19 @@ private TreeMap> 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."); + } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/ReportsHandler.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/ReportsHandler.java index 9a4dcbf9e256..59fd6ea6293e 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/ReportsHandler.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/ReportsHandler.java @@ -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; @@ -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"); + } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/template/DataInsightDescriptionAndOwnerTemplate.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/template/DataInsightDescriptionAndOwnerTemplate.java index dda79c169027..9f7885fd8dde 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/template/DataInsightDescriptionAndOwnerTemplate.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/template/DataInsightDescriptionAndOwnerTemplate.java @@ -146,4 +146,12 @@ public Map getTierMap() { public void setTierMap(Map tierMap) { this.tierMap = tierMap; } + + public int getNumberOfDaysChange() { + return numberOfDaysChange; + } + + public void setNumberOfDaysChange(int numberOfDaysChange) { + this.numberOfDaysChange = numberOfDaysChange; + } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/template/DataInsightTotalAssetTemplate.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/template/DataInsightTotalAssetTemplate.java index 1fd7500864b6..7d941208baa2 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/template/DataInsightTotalAssetTemplate.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/template/DataInsightTotalAssetTemplate.java @@ -17,6 +17,7 @@ public class DataInsightTotalAssetTemplate { private Double totalDataAssets; private Double percentChangeTotalAssets; private String completeMessage; + private int numberOfDaysChange; public DataInsightTotalAssetTemplate( @@ -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; + } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java index fda1e4ca1be5..4732a445f5ad 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java @@ -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(