Skip to content

Commit

Permalink
Merge pull request #33 from anilkumarkammalapalli/termCreate
Browse files Browse the repository at this point in the history
Integrate StopWatch for detailed function tracking
  • Loading branch information
anilkumarkammalapalli authored Aug 7, 2024
2 parents c430bc1 + d6fa30d commit 4898f63
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.sunbird.common.dto.Property;
import org.sunbird.common.dto.Request;
import org.sunbird.common.exception.ResourceNotFoundException;
Expand Down Expand Up @@ -109,8 +110,10 @@ public static Node getNodeById(String graphId, Long nodeId, Boolean getTags, Req
* @return the node by unique id
*/
public static Node getNodeByUniqueId(String graphId, String nodeId, Boolean getTags, Request request) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
TelemetryManager.log("Graph Id: " + graphId + "\nNode Id: " + nodeId + "\nGet Tags:" + getTags);

TelemetryManager.log("Neo4JBoltSearchOperations getNodeByUniqueId function started");
if (StringUtils.isBlank(graphId))
throw new ClientException(DACErrorCodeConstants.INVALID_GRAPH.name(),
DACErrorMessageConstants.INVALID_GRAPH_ID + " | ['Get Node By Unique Id' Operation Failed.]");
Expand All @@ -122,6 +125,9 @@ public static Node getNodeByUniqueId(String graphId, String nodeId, Boolean getT

Node node = (Node) NodeCacheManager.getDataNode(graphId, nodeId);
if (null != node) {
stopWatch.stop();
long durationInSeconds = stopWatch.getTime() / 1000; // Duration in seconds
TelemetryManager.info("Execution time for Neo4JBoltSearchOperations getNodeByUniqueId function returning node from cache: " + durationInSeconds + " seconds");
TelemetryManager.info("Fetched node from in-memory cache: "+node.getIdentifier());
return node;
} else {
Expand Down Expand Up @@ -156,6 +162,9 @@ public static Node getNodeByUniqueId(String graphId, String nodeId, Boolean getT
startNodeMap, endNodeMap);
}
}
stopWatch.stop();
long durationInSeconds = stopWatch.getTime() / 1000; // Duration in seconds
TelemetryManager.info("Execution time for Neo4JBoltSearchOperations getNodeByUniqueId function: " + durationInSeconds + " seconds");
return node;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.sunbird.common.dto.Property;
import org.sunbird.graph.dac.enums.GraphDACParams;
import org.sunbird.graph.dac.enums.SystemProperties;
Expand Down Expand Up @@ -38,7 +39,9 @@ public static String generateGetNodeByIdCypherQuery(Map<String, Object> paramete
}

public static String generateGetNodeByUniqueIdCypherQuery(Map<String, Object> parameterMap) {

StopWatch stopWatch = new StopWatch();
stopWatch.start();
TelemetryManager.log("SearchQueryGenerationUtil generateGetNodeByUniqueIdCypherQuery function started");
StringBuilder query = new StringBuilder();
if (null != parameterMap) {
String graphId = (String) parameterMap.get(GraphDACParams.graphId.name());
Expand All @@ -58,6 +61,9 @@ public static String generateGetNodeByUniqueIdCypherQuery(Map<String, Object> pa
}

TelemetryManager.log("Returning Get Node By Unique Id Cypher Query: " + query);
stopWatch.stop();
long durationInSeconds = stopWatch.getTime() / 1000; // Duration in seconds
TelemetryManager.info("Execution time for SearchQueryGenerationUtil generateGetNodeByUniqueIdCypherQuery function : " + durationInSeconds + " seconds");
return query.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.StopWatch;
import org.sunbird.common.dto.Property;
import org.sunbird.common.dto.Request;
import org.sunbird.common.dto.Response;
Expand All @@ -23,6 +24,7 @@
import org.sunbird.graph.dac.model.SubGraph;
import org.sunbird.graph.dac.model.Traverser;
import org.sunbird.graph.service.operation.Neo4JBoltSearchOperations;
import org.sunbird.telemetry.logger.TelemetryManager;

public class Neo4JBoltSearchMgrImpl extends BaseDACMgr implements IGraphDACSearchMgr {

Expand All @@ -43,6 +45,9 @@ public Response getNodeById(Request request) {

@Override
public Response getNodeByUniqueId(Request request) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
TelemetryManager.log("Neo4jBoltSearchMgrImpl getNodeByUniqueId function started");
String graphId = (String) request.getContext().get(GraphHeaderParams.graph_id.name());
String nodeId = (String) request.get(GraphDACParams.node_id.name());
Boolean getTags = (Boolean) request.get(GraphDACParams.get_tags.name());
Expand All @@ -51,6 +56,9 @@ public Response getNodeByUniqueId(Request request) {
} else {
try {
Node node = Neo4JBoltSearchOperations.getNodeByUniqueId(graphId, nodeId, getTags, request);
stopWatch.stop();
long durationInSeconds = stopWatch.getTime() / 1000; // Duration in seconds
TelemetryManager.log("Neo4jBoltSearchMgrImpl getNodeByUniqueId function completed in " + durationInSeconds + " seconds");
return OK(GraphDACParams.node.name(), node);
} catch (Exception e) {
return ERROR(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.sunbird.common.dto.Request;
import org.sunbird.common.dto.Response;
import org.sunbird.common.exception.ClientException;
Expand Down Expand Up @@ -324,6 +325,9 @@ public void validateNode(Request request) {
*/
@Override
public void updateDataNode(final Request request) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
TelemetryManager.log("NodeManagerImpl updateDataNode function started");
final ActorRef parent = getSender();
String graphId = (String) request.getContext().get(GraphHeaderParams.graph_id.name());
String nodeId = (String) request.get(GraphDACParams.node_id.name());
Expand Down Expand Up @@ -407,8 +411,10 @@ public void updateDataNode(final Request request) {
ERROR(GraphEngineErrorCodes.ERR_GRAPH_UPDATE_NODE_NOT_FOUND.name(), "Node Not Found",
ResponseCode.RESOURCE_NOT_FOUND, GraphDACParams.messages.name(), messages, parent);
}

}
stopWatch.stop();
long durationInSeconds = stopWatch.getTime() / 1000;
TelemetryManager.log("NodeManagerImpl updateDataNode function completed in " + durationInSeconds + " seconds");
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.sunbird.common.dto.Property;
import org.sunbird.common.dto.Request;
import org.sunbird.common.exception.ClientException;
Expand Down Expand Up @@ -102,6 +103,9 @@ public void getNodeDefinitionFromCache(Request request) {

@Override
public void getDataNode(Request request) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
TelemetryManager.log("SearchManagerImpl getDataNode function started");
String nodeId = (String) request.get(GraphDACParams.node_id.name());
if (!validateRequired(nodeId)) {
throw new ClientException(GraphEngineErrorCodes.ERR_GRAPH_SEARCH_MISSING_REQ_PARAMS.name(),
Expand All @@ -115,6 +119,9 @@ public void getDataNode(Request request) {
handleException(e, getSender());
}
}
stopWatch.stop();
long durationInSeconds = stopWatch.getTime() / 1000; // Duration in seconds
TelemetryManager.log("SearchManagerImpl getDataNode function completed in " + durationInSeconds + " seconds");
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.sunbird.common.Platform;
import org.sunbird.common.dto.Request;
import org.sunbird.common.dto.Response;
Expand Down Expand Up @@ -53,6 +54,9 @@ public class FrameworkHierarchy extends BaseManager {
* @throws Exception
*/
public void generateFrameworkHierarchy(String id) throws Exception {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
TelemetryManager.log("FrameworkHierarchy generateFrameworkHierarchy function started");
Response responseNode = getDataNode(GRAPH_ID, id);
if (checkError(responseNode))
throw new ResourceNotFoundException("ERR_DATA_NOT_FOUND", "Data not found with id : " + id);
Expand All @@ -61,7 +65,6 @@ public void generateFrameworkHierarchy(String id) throws Exception {
FrameworkCache.delete(id);
Map<String, Object> frameworkDocument = new HashMap<>();
Map<String, Object> frameworkHierarchy = getHierarchy(node.getIdentifier(), 0, false, true);
TelemetryManager.info("frameworkHierarchy map::: "+frameworkHierarchy);
CategoryCache.setFramework(node.getIdentifier(), frameworkHierarchy);

frameworkDocument.putAll(frameworkHierarchy);
Expand All @@ -74,6 +77,9 @@ public void generateFrameworkHierarchy(String id) throws Exception {
frameworkDocument.put(field, node.getMetadata().get(field));
}
hierarchyStore.saveOrUpdateHierarchy(node.getIdentifier(),frameworkDocument);
stopWatch.stop();
long durationInSeconds = stopWatch.getTime() / 1000; // Duration in seconds
TelemetryManager.info("Execution time for FrameworkHierarchy generateFrameworkHierarchy function: " + durationInSeconds + " seconds");
} else {
throw new ClientException(ResponseCode.CLIENT_ERROR.name(), "The object with given identifier is not a framework: " + id);
}
Expand All @@ -96,6 +102,9 @@ public Map<String, Object> getFrameworkHierarchy(String frameworkId) throws Exce

@SuppressWarnings("unchecked")
private Map<String, Object> getHierarchy(String id, int index, boolean includeMetadata, boolean includeRelations) throws Exception {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
TelemetryManager.log("FrameworkHierarchy getHierarchy function started");
Map<String, Object> data = new HashMap<String, Object>();
Response responseNode = getDataNode(GRAPH_ID, id);
if (checkError(responseNode))
Expand Down Expand Up @@ -184,6 +193,9 @@ private Map<String, Object> getHierarchy(String id, int index, boolean includeMe
}
String jsonData = mapper.writeValueAsString(data);
TelemetryManager.info("printing Hierarchy data: "+jsonData);
stopWatch.stop();
long durationInSeconds = stopWatch.getTime() / 1000; // Duration in seconds
TelemetryManager.info("Execution time for FrameworkHierarchy getHierarchy function: " + durationInSeconds + " seconds");
return data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.lang3.time.StopWatch;
import org.sunbird.cassandra.connector.util.CassandraConnector;
import org.sunbird.cassandra.store.CassandraStore;
import org.sunbird.common.Platform;
Expand Down Expand Up @@ -40,12 +41,18 @@ public HierarchyStore(String keyspace, String table, String objectType, boolean

public void saveOrUpdateHierarchy(String contentId, Map<String, Object> hierarchy) {
try {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
TelemetryManager.log("HierarchyStore saveOrUpdateHierarchy function started");
String query = "UPDATE " + getKeyspace() + "." + getTable() + " SET hierarchy = ? WHERE identifier = ?";
String hierarchyData = mapper.writeValueAsString(hierarchy);
Session session = CassandraConnector.getSession();
PreparedStatement statement = session.prepare(query);
BoundStatement boundStatement = new BoundStatement(statement);
session.execute(boundStatement.bind(hierarchyData, contentId));
stopWatch.stop();
long durationInSeconds = stopWatch.getTime() / 1000; // Duration in seconds
TelemetryManager.info("Execution time for HierarchyStore saveOrUpdateHierarchy function: " + durationInSeconds + " seconds");
} catch (JsonProcessingException e) {
TelemetryManager.error("Error while updating collection hierarchy for ID" + contentId, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.codehaus.jackson.map.ObjectMapper;
import org.sunbird.common.dto.NodeDTO;
import org.sunbird.graph.dac.enums.SystemProperties;
Expand All @@ -22,6 +23,9 @@ public class ConvertToGraphNode {

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Node convertToGraphNode(Map<String, Object> map, DefinitionDTO definition, Node graphNode) throws Exception {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
TelemetryManager.info("Started convertToGraphNode function");
Node node = new Node();
if (null != map && !map.isEmpty()) {
Map<String, String> inRelDefMap = new HashMap<String, String>();
Expand Down Expand Up @@ -89,6 +93,10 @@ public static Node convertToGraphNode(Map<String, Object> map, DefinitionDTO def
node.setInRelations(inRelations);
node.setOutRelations(outRelations);
node.setMetadata(metadata);
stopWatch.stop();
long durationInSeconds = stopWatch.getTime() / 1000; // Duration in seconds
TelemetryManager.info("Execution time for convertToGraphNode function: " + durationInSeconds + " seconds");
TelemetryManager.log("NodeData inRelations and nodeMetadata and outRelations "+(List<Relation>) node.getInRelations()+", ::"+(Map<String,Object>)node.getMetadata() + ", ::" +(List<Relation>)node.getOutRelations());
}
return node;
}
Expand All @@ -114,6 +122,9 @@ private static void getRelDefMaps(DefinitionDTO definition, Map<String, String>
}

private static Map<String, List<Relation>> getDBRelations(DefinitionDTO definition, Node graphNode, Map<String, Object> map) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
TelemetryManager.log("ConvertToGraphNode getDBRelations started");
List<Relation> inRelations = null;
List<Relation> outRelations = null;
if (null != graphNode) {
Expand Down Expand Up @@ -152,6 +163,9 @@ private static Map<String, List<Relation>> getDBRelations(DefinitionDTO definiti
Map<String, List<Relation>> relationMaps = new HashMap<String, List<Relation>>();
relationMaps.put("in", inRelations);
relationMaps.put("out", outRelations);
stopWatch.stop();
long durationInSeconds = stopWatch.getTime() / 1000; // Duration in seconds
TelemetryManager.info("Execution time for getDBRelations function: " + durationInSeconds + " seconds");
return relationMaps;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.sunbird.common.Platform;
import org.sunbird.common.Slug;
import org.sunbird.common.dto.Request;
Expand Down Expand Up @@ -102,7 +103,9 @@ protected Response read(String identifier, String objectType, String responseObj
protected Response update(String identifier, String objectType, Map<String, Object> map) {
if (map.containsKey("translations"))
validateTranslation(map);

StopWatch stopWatch = new StopWatch();
stopWatch.start();
TelemetryManager.log("BaseFramework update function started");
DefinitionDTO definition = getDefinition(GRAPH_ID, objectType);
Response getNodeResponse = getDataNode(GRAPH_ID, identifier);
Node graphNode = (Node) getNodeResponse.get(GraphDACParams.node.name());
Expand All @@ -112,6 +115,9 @@ protected Response update(String identifier, String objectType, Map<String, Obje
domainObj.setIdentifier(identifier);
domainObj.setObjectType(objectType);
Response updateResponse = updateDataNode(domainObj);
stopWatch.stop();
long durationInSeconds = stopWatch.getTime() / 1000; // Duration in seconds
TelemetryManager.info("Execution time for BaseFrameworkManager update function: " + durationInSeconds + " seconds");
return updateResponse;
} catch (Exception e) {
return ERROR("ERR_SERVER_ERROR", "Internal error", ResponseCode.SERVER_ERROR, e.getMessage(), null);
Expand Down

0 comments on commit 4898f63

Please sign in to comment.