-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(change-event): add change events for DataProcessInstanceRunEvent
- Loading branch information
aditya-radhakrishnan
committed
Oct 31, 2022
1 parent
a7de2f5
commit f662410
Showing
8 changed files
with
288 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...din/metadata/timeline/eventgenerator/DataProcessInstanceRunEventChangeEventGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.linkedin.metadata.timeline.eventgenerator; | ||
|
||
import com.linkedin.common.AuditStamp; | ||
import com.linkedin.common.urn.Urn; | ||
import com.linkedin.dataprocess.DataProcessInstanceRelationships; | ||
import com.linkedin.dataprocess.DataProcessInstanceRunEvent; | ||
import com.linkedin.dataprocess.DataProcessRunStatus; | ||
import com.linkedin.metadata.entity.EntityService; | ||
import com.linkedin.metadata.timeline.data.ChangeCategory; | ||
import com.linkedin.metadata.timeline.data.ChangeEvent; | ||
import com.linkedin.metadata.timeline.data.ChangeOperation; | ||
import java.net.URISyntaxException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
import static com.linkedin.metadata.Constants.*; | ||
|
||
|
||
public class DataProcessInstanceRunEventChangeEventGenerator | ||
extends EntityChangeEventGenerator<DataProcessInstanceRunEvent> { | ||
public DataProcessInstanceRunEventChangeEventGenerator(@Nonnull final EntityService entityService) { | ||
super(entityService); | ||
} | ||
|
||
@Override | ||
public List<ChangeEvent> getChangeEvents(@Nonnull Urn urn, @Nonnull String entity, @Nonnull String aspect, | ||
@Nonnull Aspect<DataProcessInstanceRunEvent> from, @Nonnull Aspect<DataProcessInstanceRunEvent> to, | ||
@Nonnull AuditStamp auditStamp) { | ||
return computeDiffs(from.getValue(), to.getValue(), urn.toString(), auditStamp); | ||
} | ||
|
||
private List<ChangeEvent> computeDiffs(DataProcessInstanceRunEvent baseAspect, | ||
DataProcessInstanceRunEvent targetAspect, String entityUrn, AuditStamp auditStamp) { | ||
final DataProcessRunStatus baseStatus = getStatus(baseAspect); | ||
final DataProcessRunStatus targetStatus = getStatus(targetAspect); | ||
|
||
if (targetStatus != null && !targetStatus.equals(baseStatus)) { | ||
String operationType = targetStatus.toString().equals("COMPLETE") ? "COMPLETED" : "STARTED"; | ||
|
||
return Collections.singletonList(ChangeEvent.builder() | ||
.category(ChangeCategory.RUN) | ||
.operation(ChangeOperation.valueOf(operationType)) | ||
.auditStamp(auditStamp) | ||
.entityUrn(entityUrn) | ||
.parameters(buildParamsMap(targetAspect, entityUrn)) | ||
.build()); | ||
} | ||
|
||
return Collections.emptyList(); | ||
} | ||
|
||
@Nullable | ||
private DataProcessRunStatus getStatus(DataProcessInstanceRunEvent dataProcessInstanceRunEvent) { | ||
return dataProcessInstanceRunEvent != null ? dataProcessInstanceRunEvent.getStatus() : null; | ||
} | ||
|
||
@Nonnull | ||
private Map<String, Object> buildParamsMap(DataProcessInstanceRunEvent targetAspect, String entityUrnString) { | ||
final Map<String, Object> paramsMap = new HashMap<>(); | ||
if (targetAspect.hasAttempt()) { | ||
paramsMap.put(ATTEMPT_KEY, targetAspect.getAttempt()); | ||
} | ||
if (targetAspect.hasResult()) { | ||
paramsMap.put(RUN_RESULT_KEY, targetAspect.getResult().getType().toString()); | ||
} | ||
|
||
Urn entityUrn; | ||
try { | ||
entityUrn = Urn.createFromString(entityUrnString); | ||
} catch (URISyntaxException e) { | ||
return paramsMap; | ||
} | ||
|
||
final DataProcessInstanceRelationships dataProcessInstanceRelationships = | ||
(DataProcessInstanceRelationships) _entityService.getLatestAspect(entityUrn, | ||
DATA_PROCESS_INSTANCE_RELATIONSHIPS_ASPECT_NAME); | ||
if (dataProcessInstanceRelationships == null) { | ||
return paramsMap; | ||
} | ||
|
||
if (dataProcessInstanceRelationships.hasParentInstance()) { | ||
paramsMap.put(PARENT_INSTANCE_URN_KEY, dataProcessInstanceRelationships.getParentInstance().toString()); | ||
} | ||
|
||
if (dataProcessInstanceRelationships.hasParentTemplate()) { | ||
Urn parentTemplateUrn = dataProcessInstanceRelationships.getParentTemplate(); | ||
if (parentTemplateUrn.getEntityType().equals(DATA_FLOW_ENTITY_NAME)) { | ||
paramsMap.put(DATA_FLOW_URN_KEY, parentTemplateUrn.toString()); | ||
} else if (parentTemplateUrn.getEntityType().equals(DATA_JOB_ENTITY_NAME)) { | ||
paramsMap.put(DATA_JOB_URN_KEY, parentTemplateUrn.toString()); | ||
} | ||
} | ||
return paramsMap; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.