This is an automated email from the ASF dual-hosted git repository.

madhan pushed a commit to branch atlas-2.5
in repository https://gitbox.apache.org/repos/asf/atlas.git

commit 841c3a05df1a66d9802c8f1b355ef415dd39acf9
Author: pareshd <paresh.deva...@freestoneinfotech.com>
AuthorDate: Sat Feb 15 07:12:57 2025 +0530

    ATLAS-4867 : Improve Metrics API (#288)
    
    (cherry picked from commit 01e69f30d8f9028c3a7fc82872356f77fa4a19fc)
---
 .../org/apache/atlas/services/MetricsService.java  | 51 ++++++++++++----------
 .../apache/atlas/services/MetricsServiceTest.java  |  2 +-
 .../apache/atlas/web/resources/AdminResource.java  |  6 +--
 3 files changed, 32 insertions(+), 27 deletions(-)

diff --git 
a/repository/src/main/java/org/apache/atlas/services/MetricsService.java 
b/repository/src/main/java/org/apache/atlas/services/MetricsService.java
index 85b1634bf..f9b251941 100644
--- a/repository/src/main/java/org/apache/atlas/services/MetricsService.java
+++ b/repository/src/main/java/org/apache/atlas/services/MetricsService.java
@@ -57,6 +57,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 import java.util.stream.StreamSupport;
@@ -118,7 +119,7 @@ public class MetricsService {
     }
 
     @GraphTransaction
-    public AtlasMetrics getMetrics() {
+    public AtlasMetrics getMetrics(Boolean excludeTypeAndSubTypeEntity) {
         final AtlasTypesDef                typesDef                          = 
getTypesDef();
         Collection<AtlasEntityDef>         entityDefs                        = 
typesDef.getEntityDefs();
         Collection<AtlasClassificationDef> classificationDefs                = 
typesDef.getClassificationDefs();
@@ -158,30 +159,31 @@ public class MetricsService {
                 }
             }
 
-            for (AtlasEntityDef entityDef : entityDefs) {
-                AtlasEntityType entityType = 
typeRegistry.getEntityTypeByName(entityDef.getName());
+            if (!excludeTypeAndSubTypeEntity) {
+                entityDefs.stream().forEach(entityDef -> {
+                    AtlasEntityType entityType = 
typeRegistry.getEntityTypeByName(entityDef.getName());
 
-                long entityActiveCount  = 0;
-                long entityDeletedCount = 0;
-                long entityShellCount   = 0;
+                    long entityActiveCount = 
entityType.getTypeAndAllSubTypes().stream()
+                            .mapToLong(type -> 
activeEntityCount.getOrDefault(type, 0L))
+                            .sum();
 
-                for (String type : entityType.getTypeAndAllSubTypes()) {
-                    entityActiveCount  += activeEntityCount.get(type) == null 
? 0 : activeEntityCount.get(type);
-                    entityDeletedCount += deletedEntityCount.get(type) == null 
? 0 : deletedEntityCount.get(type);
-                    entityShellCount   += shellEntityCount.get(type) == null ? 
0 : shellEntityCount.get(type);
-                }
+                    long entityDeletedCount = 
entityType.getTypeAndAllSubTypes().stream()
+                            .mapToLong(type -> 
deletedEntityCount.getOrDefault(type, 0L))
+                            .sum();
 
-                if (entityActiveCount > 0) {
-                    
activeEntityCountTypeAndSubTypes.put(entityType.getTypeName(), 
entityActiveCount);
-                }
+                    long entityShellCount = 
entityType.getTypeAndAllSubTypes().stream()
+                            .mapToLong(type -> 
shellEntityCount.getOrDefault(type, 0L))
+                            .sum();
 
-                if (entityDeletedCount > 0) {
-                    
deletedEntityCountTypeAndSubTypes.put(entityType.getTypeName(), 
entityDeletedCount);
-                }
+                    Optional.ofNullable(entityActiveCount).filter(count -> 
count > 0)
+                            .ifPresent(count -> 
activeEntityCountTypeAndSubTypes.put(entityType.getTypeName(), count));
 
-                if (entityShellCount > 0) {
-                    
shellEntityCountTypeAndSubTypes.put(entityType.getTypeName(), entityShellCount);
-                }
+                    Optional.ofNullable(entityDeletedCount).filter(count -> 
count > 0)
+                            .ifPresent(count -> 
deletedEntityCountTypeAndSubTypes.put(entityType.getTypeName(), count));
+
+                    Optional.ofNullable(entityShellCount).filter(count -> 
count > 0)
+                            .ifPresent(count -> 
shellEntityCountTypeAndSubTypes.put(entityType.getTypeName(), count));
+                });
             }
         }
 
@@ -207,9 +209,12 @@ public class MetricsService {
         metrics.addMetric(ENTITY, METRIC_ENTITY_ACTIVE, activeEntityCount);
         metrics.addMetric(ENTITY, METRIC_ENTITY_DELETED, deletedEntityCount);
         metrics.addMetric(ENTITY, METRIC_ENTITY_SHELL, shellEntityCount);
-        metrics.addMetric(ENTITY, METRIC_ENTITY_ACTIVE_INCL_SUBTYPES, 
activeEntityCountTypeAndSubTypes);
-        metrics.addMetric(ENTITY, METRIC_ENTITY_DELETED_INCL_SUBTYPES, 
deletedEntityCountTypeAndSubTypes);
-        metrics.addMetric(ENTITY, METRIC_ENTITY_SHELL_INCL_SUBTYPES, 
shellEntityCountTypeAndSubTypes);
+
+        if (!excludeTypeAndSubTypeEntity) {
+            metrics.addMetric(ENTITY, METRIC_ENTITY_ACTIVE_INCL_SUBTYPES, 
activeEntityCountTypeAndSubTypes);
+            metrics.addMetric(ENTITY, METRIC_ENTITY_DELETED_INCL_SUBTYPES, 
deletedEntityCountTypeAndSubTypes);
+            metrics.addMetric(ENTITY, METRIC_ENTITY_SHELL_INCL_SUBTYPES, 
shellEntityCountTypeAndSubTypes);
+        }
 
         metrics.addMetric(TAG, METRIC_ENTITIES_PER_TAG, taggedEntityCount);
         metrics.addMetric(SYSTEM, METRIC_MEMORY, 
AtlasMetricJVMUtil.getMemoryDetails());
diff --git 
a/repository/src/test/java/org/apache/atlas/services/MetricsServiceTest.java 
b/repository/src/test/java/org/apache/atlas/services/MetricsServiceTest.java
index 49b3ac6bf..f6dbd910e 100644
--- a/repository/src/test/java/org/apache/atlas/services/MetricsServiceTest.java
+++ b/repository/src/test/java/org/apache/atlas/services/MetricsServiceTest.java
@@ -158,7 +158,7 @@ public class MetricsServiceTest extends AtlasTestBase {
 
     @Test(groups = "Metrics.CREATE")
     public void testGetMetrics() {
-        metrics = metricsService.getMetrics();
+        metrics = metricsService.getMetrics(false);
 
         assertNotNull(metrics);
 
diff --git 
a/webapp/src/main/java/org/apache/atlas/web/resources/AdminResource.java 
b/webapp/src/main/java/org/apache/atlas/web/resources/AdminResource.java
index 5ea7c0aee..0e0854271 100755
--- a/webapp/src/main/java/org/apache/atlas/web/resources/AdminResource.java
+++ b/webapp/src/main/java/org/apache/atlas/web/resources/AdminResource.java
@@ -413,10 +413,10 @@ public class AdminResource {
     @GET
     @Path("metrics")
     @Produces(Servlets.JSON_MEDIA_TYPE)
-    public AtlasMetrics getMetrics() {
+    public AtlasMetrics getMetrics(@QueryParam("excludeTypeAndSubTypeEntity") 
@DefaultValue("true") Boolean excludeTypeAndSubTypeEntity) {
         LOG.debug("==> AdminResource.getMetrics()");
 
-        AtlasMetrics metrics = metricsService.getMetrics();
+        AtlasMetrics metrics = 
metricsService.getMetrics(excludeTypeAndSubTypeEntity);
 
         LOG.debug("<== AdminResource.getMetrics()");
 
@@ -1112,7 +1112,7 @@ public class AdminResource {
     private void saveMetrics() throws AtlasBaseException {
         LOG.debug("==> AdminResource.saveMetrics()");
 
-        AtlasMetrics metrics = metricsService.getMetrics();
+        AtlasMetrics metrics = metricsService.getMetrics(false);
 
         AtlasMetricsStat metricsStat = new AtlasMetricsStat(metrics);
 

Reply via email to