GaugeManager { public static double calcOverallOnlinePercentageGauge(ZooKeeperManager zkManager) { Set<String> fileSets = zkManager.getFileSetInfoMap().keySet(); int totalPartitions = 0, onlinePartitions = 0; for (String fileSet : fileSets) { FileSetInfo fileSetInfo = zkManager.getFileSetInfo(fileSet); if (fileSetInfo == null || fileSetInfo.servingInfo == null) { LOG.warn(String.format("FileSetInfo for %s is not available", fileSet)); continue; } ViewInfo viewInfo = zkManager.getViewInfo(fileSetInfo.servingInfo.helixResource); if (viewInfo == null) { LOG.warn(String.format("ViewInfo for %s is not available", fileSetInfo.servingInfo.helixResource)); continue; } totalPartitions += fileSetInfo.servingInfo.numPartitions; onlinePartitions += viewInfo.getNumOnlinePartitions(); } if (totalPartitions == 0) { return 0; } return (double)onlinePartitions / totalPartitions; } GaugeManager(ZooKeeperManager zkManager, int sleepSeconds); void shutdown(); static double calcOverallOnlinePercentageGauge(ZooKeeperManager zkManager);  }
@Test public void testCalcOverallOnlinePercentageGauge() { FileSetInfo mockFileSetInfo2 = mock(FileSetInfo.class); FileSetInfo.ServingInfo mockServingInfo2 = mock(FileSetInfo.ServingInfo.class); ViewInfo mockViewInfo2 = mock(ViewInfo.class); mockFileSetInfo2.servingInfo = mockServingInfo2; when(mockZKManager.getFileSetInfoMap()) .thenReturn(ImmutableMap.of("file_set1", mockFileSetInfo, "file_set2", mockFileSetInfo2)); when(mockZKManager.getFileSetInfo(eq("file_set1"))).thenReturn(mockFileSetInfo); when(mockZKManager.getFileSetInfo(eq("file_set2"))).thenReturn(mockFileSetInfo2); when(mockZKManager.getViewInfo("file_set1")).thenReturn(mockViewInfo); when(mockZKManager.getViewInfo("file_set2")).thenReturn(mockViewInfo2); when(mockViewInfo.getNumOnlinePartitions()).thenReturn(4); when(mockViewInfo2.getNumOnlinePartitions()).thenReturn(5); mockServingInfo.helixResource = "file_set1"; mockServingInfo2.helixResource = "file_set2"; mockServingInfo.numPartitions = 20; mockServingInfo2.numPartitions = 16; assertEquals(0.25, GaugeManager.calcOverallOnlinePercentageGauge(mockZKManager), 0); }