Skip to content

Commit

Permalink
Merge branch 'feat/starSupport' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
mehtaanshul committed Aug 8, 2023
2 parents 5e4b4f7 + 5750df2 commit 8561a5e
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ public enum SupportedFileExtensions { XLSX, XLS, CSV }
public static final String ATTR_VIEWER_USERS = "viewerUsers";
public static final String ATTR_VIEWER_GROUPS = "viewerGroups";

public static final String ATTR_STARRED_BY = "starredBy";
public static final String ATTR_STARRED_COUNT = "starredCount";
public static final String ATTR_STARRED_DETAILS_LIST = "starredDetailsList";
public static final String ATTR_ASSET_STARRED_BY = "assetStarredBy";
public static final String ATTR_ASSET_STARRED_AT = "assetStarredAt";

public static final String STRUCT_STARRED_DETAILS = "StarredDetails";

public static final String KEYCLOAK_ROLE_ADMIN = "$admin";
public static final String KEYCLOAK_ROLE_MEMBER = "$member";
public static final String KEYCLOAK_ROLE_GUEST = "$guest";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public interface AtlasElement {

<V> List<V> getMultiValuedProperty(String propertyName, Class<V> elementType);

<V> Set<V> getMultiValuedSetProperty(String propertyName, Class<V> elementType);

/**
* Gets the value of a multiplicity one property whose value is a list. It
* attempts to convert the elements in the list to the specified type. Currently
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ public <V> List<V> getMultiValuedProperty(String propertyName, Class<V> elementT
return value;
}

@Override
public <V> Set<V> getMultiValuedSetProperty(String propertyName, Class<V> elementType) {
Set<V> value = new HashSet<>();
Iterator<? extends Property<Object>> it = getWrappedElement().properties(propertyName);

while (it.hasNext()) {
Property currentProperty = it.next();
Object currentPropertyValue = currentProperty.value();
value.add((V) currentPropertyValue);
}
return value;
}

@Override
public void setListProperty(String propertyName, List<String> values) {
setProperty(propertyName, values);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public enum Status { ACTIVE, DELETED, PURGED }
private Date createTime = null;
private Date updateTime = null;
private Long version = 0L;
private Boolean starred = null;

private Map<String, Object> relationshipAttributes;
private List<AtlasClassification> classifications;
Expand Down Expand Up @@ -280,6 +281,14 @@ public void setStatus(Status status) {
this.status = status;
}

public Boolean getStarred() {
return starred;
}

public void setStarred(Boolean starred) {
this.starred = starred;
}

public String getCreatedBy() {
return createdBy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1501,16 +1501,10 @@ private EntityMutationResponse createOrUpdate(EntityStream entityStream, boolean
}

AtlasEntity diffEntity = reqContext.getDifferentialEntity(entity.getGuid());

if (diffEntity != null &&
MapUtils.isNotEmpty(diffEntity.getRelationshipAttributes()) &&
diffEntity.getRelationshipAttributes().containsKey("meanings") &&
diffEntity.getRelationshipAttributes().size() == 1 &&
MapUtils.isEmpty(diffEntity.getAttributes()) &&
MapUtils.isEmpty(diffEntity.getCustomAttributes()) &&
MapUtils.isEmpty(diffEntity.getBusinessAttributes()) &&
CollectionUtils.isEmpty(diffEntity.getClassifications()) &&
CollectionUtils.isEmpty(diffEntity.getLabels())) {
boolean skipAuthBaseConditions = diffEntity != null && MapUtils.isEmpty(diffEntity.getCustomAttributes()) && MapUtils.isEmpty(diffEntity.getBusinessAttributes()) && CollectionUtils.isEmpty(diffEntity.getClassifications()) && CollectionUtils.isEmpty(diffEntity.getLabels());
boolean skipAuthMeaningsUpdate = diffEntity != null && MapUtils.isNotEmpty(diffEntity.getRelationshipAttributes()) && diffEntity.getRelationshipAttributes().containsKey("meanings") && diffEntity.getRelationshipAttributes().size() == 1 && MapUtils.isEmpty(diffEntity.getAttributes());
boolean skipAuthStarredDetailsUpdate = diffEntity != null && MapUtils.isEmpty(diffEntity.getRelationshipAttributes()) && MapUtils.isNotEmpty(diffEntity.getAttributes()) && diffEntity.getAttributes().size() == 3 && diffEntity.getAttributes().containsKey(ATTR_STARRED_BY) && diffEntity.getAttributes().containsKey(ATTR_STARRED_COUNT) && diffEntity.getAttributes().containsKey(ATTR_STARRED_DETAILS_LIST);
if (skipAuthBaseConditions && (skipAuthMeaningsUpdate || skipAuthStarredDetailsUpdate)) {
//do nothing, only diff is relationshipAttributes.meanings, allow update
} else {
AtlasAuthorizationUtils.verifyUpdateEntityAccess(typeRegistry, entityHeader,"update entity: type=" + entity.getTypeName());
Expand Down Expand Up @@ -1596,6 +1590,8 @@ private EntityMutationContext preCreateOrUpdate(EntityStream entityStream, Entit

AtlasVertex vertex = getResolvedEntityVertex(discoveryContext, entity);

autoUpdateStarredDetailsAttributes(entity, vertex);

try {
if (vertex != null) {
if (!isPartialUpdate) {
Expand Down Expand Up @@ -1692,6 +1688,102 @@ private EntityMutationContext preCreateOrUpdate(EntityStream entityStream, Entit
return context;
}

private void autoUpdateStarredDetailsAttributes(AtlasEntity entity, AtlasVertex vertex) {

MetricRecorder metric = RequestContext.get().startMetricRecord("autoUpdateStarredDetailsAttributes");

Boolean starEntityForUser = entity.getStarred();

if (starEntityForUser != null) {

long requestTime = RequestContext.get().getRequestTime();
String requestUser = RequestContext.get().getUser();

Set<String> starredBy = new HashSet<>();
Set<AtlasStruct> starredDetailsList = new HashSet<>();
int starredCount = 0;

if (vertex != null) {
Set<String> vertexStarredBy = vertex.getMultiValuedSetProperty(ATTR_STARRED_BY, String.class);
if (vertexStarredBy != null) {
starredBy = vertexStarredBy;
}

Iterable<AtlasEdge> starredDetailsEdges = vertex.getEdges(AtlasEdgeDirection.OUT, "__" + ATTR_STARRED_DETAILS_LIST);
for (AtlasEdge starredDetailsEdge : starredDetailsEdges) {
AtlasVertex starredDetailsVertex = starredDetailsEdge.getInVertex();
String assetStarredBy = starredDetailsVertex.getProperty(ATTR_ASSET_STARRED_BY, String.class);
Long assetStarredAt = starredDetailsVertex.getProperty(ATTR_ASSET_STARRED_AT, Long.class);
AtlasStruct starredDetails = getStarredDetailsStruct(assetStarredBy, assetStarredAt);
starredDetailsList.add(starredDetails);
}

starredCount = starredBy.size();
}

if (starEntityForUser) {
addUserToStarredAttributes(requestUser, requestTime, starredBy, starredDetailsList);
} else {
removeUserFromStarredAttributes(requestUser, starredBy, starredDetailsList);
}

// Update entity attributes
if (starredBy.size() != starredCount) {
entity.setAttribute(ATTR_STARRED_BY, starredBy);
entity.setAttribute(ATTR_STARRED_DETAILS_LIST, starredDetailsList);
entity.setAttribute(ATTR_STARRED_COUNT, starredBy.size());
}

}

RequestContext.get().endMetricRecord(metric);
}

private void addUserToStarredAttributes(String requestUser, long requestTime, Set<String> starredBy, Set<AtlasStruct> starredDetailsList) {
//Check and update starredBy Attribute
if (!starredBy.contains(requestUser)){
starredBy.add(requestUser);
}

//Check and update starredDetailsList Attribute
boolean isStarredDetailsListUpdated = false;
for (AtlasStruct starredDetails : starredDetailsList) {
String assetStarredBy = (String) starredDetails.getAttribute(ATTR_ASSET_STARRED_BY);
if (assetStarredBy.equals(requestUser)) {
starredDetails.setAttribute(ATTR_ASSET_STARRED_AT, requestTime);
isStarredDetailsListUpdated = true;
break;
}
}
if (!isStarredDetailsListUpdated) {
AtlasStruct starredDetails = getStarredDetailsStruct(requestUser, requestTime);
starredDetailsList.add(starredDetails);
}
}

private void removeUserFromStarredAttributes(String requestUser, Set<String> starredBy, Set<AtlasStruct> starredDetailsList) {
//Check and update starredBy Attribute
if (starredBy.contains(requestUser)){
starredBy.remove(requestUser);
}

for (AtlasStruct starredDetails : starredDetailsList) {
String assetStarredBy = (String) starredDetails.getAttribute(ATTR_ASSET_STARRED_BY);
if (assetStarredBy.equals(requestUser)) {
starredDetailsList.remove(starredDetails);
break;
}
}
}

private AtlasStruct getStarredDetailsStruct(String assetStarredBy, long assetStarredAt) {
AtlasStruct starredDetails = new AtlasStruct();
starredDetails.setTypeName(STRUCT_STARRED_DETAILS);
starredDetails.setAttribute(ATTR_ASSET_STARRED_BY, assetStarredBy);
starredDetails.setAttribute(ATTR_ASSET_STARRED_AT, assetStarredAt);
return starredDetails;
}

public PreProcessor getPreProcessor(String typeName) {
PreProcessor preProcessor = null;

Expand Down Expand Up @@ -1983,6 +2075,12 @@ private void flushAutoUpdateAttributes(AtlasEntity entity, AtlasEntityType entit
}
}

// for (String attrName : entityType.getAllAttributes().keySet()) {
// if (ATTR_STARRED_BY.equals(attrName) || ATTR_STARRED_COUNT.equals(attrName) || ATTR_STARRED_DETAILS_LIST.equals(attrName)) {
// flushAttributes.add(attrName);
// }
// }

flushAttributes.forEach(entity::removeAttribute);
}
}
Expand Down

0 comments on commit 8561a5e

Please sign in to comment.