@Override public Map<String, Object> asJson() { LinkedHashMap<String, Object> json = new LinkedHashMap<>(); ServerHealthStates allLogs = service.logs(); json.put("Messages Count", allLogs.size()); ArrayList<Map<String, String>> messages = new ArrayList<>(); for (ServerHealthState log : allLogs) { messages.add(log.asJson()); } json.put("Messages", messages); return json; }
public String getLogsAsText() { StringBuilder text = new StringBuilder(); for (ServerHealthState state : logs()) { text.append(state.getDescription()); text.append("\n\t"); text.append(state.getMessage()); text.append("\n"); } return text.toString(); }
public boolean containsError(HealthStateType type, HealthStateLevel level) { ServerHealthStates allLogs = logs(); for (ServerHealthState log : allLogs) { if (log.getType().equals(type) && log.getLogLevel() == level) { return true; } } return false; }
@Override public void onConfigChange(CruiseConfig newCruiseConfig) { for (ServerHealthState state : serverHealthService.logs()) { HealthStateScope currentScope = state.getType().getScope(); if (currentScope.isForJob()) { serverHealthService.removeByScope(currentScope); } } }
public void onConfigChange(CruiseConfig newCruiseConfig) { Set<HealthStateScope> materialScopes = toHealthStateScopes(newCruiseConfig.getAllUniqueMaterials()); for (ServerHealthState state : serverHealthService.logs()) { HealthStateScope currentScope = state.getType().getScope(); if (currentScope.isForMaterial() && !materialScopes.contains(currentScope)) { serverHealthService.removeByScope(currentScope); } } }
public boolean matchesSafely(ServerHealthService item) { for (ServerHealthState serverHealthState : item.logs()) { if (serverHealthState.getType().equals(healthStateType)) { entry = serverHealthState; return false; } } return true; }
@Override public void onEntityConfigChange(PipelineConfig pipelineConfig) { for (ServerHealthState state : serverHealthService.logs()) { HealthStateScope currentScope = state.getType().getScope(); if (currentScope.isForJob()) { String[] split = currentScope.getScope().split("/"); if (split.length > 0 && new CaseInsensitiveString(split[0]).equals(pipelineConfig.name())) { serverHealthService.removeByScope(currentScope); } } } } }
public String show(Request request, Response response) { ServerHealthStates allLogs = serverHealthService.logs(); String json = jsonizeAsTopLevelArray(request, outputListWriter -> ServerHealthMessagesRepresenter.toJSON(outputListWriter, allLogs)); String etag = etagFor(json); if (fresh(request, etag)) { return notModified(response); } setEtagHeader(response, etag); return json; } }
@Test public void shouldRemoveErrorLogWhenCorrespondingGroupIsMissing() throws Exception { serverHealthService.update(ServerHealthState.error("message", "description", groupId)); serverHealthService.purgeStaleHealthMessages(new BasicCruiseConfig()); assertThat(serverHealthService.logs().size(), is(0)); }
@Test public void shouldAddMultipleLogToServerHealth() throws Exception { assertThat(serverHealthService.update(ServerHealthState.error("message", "description", globalId)), is(globalId)); assertThat(serverHealthService.update(ServerHealthState.error("message", "description", pipelineId)), is(pipelineId)); assertThat(serverHealthService.logs().size(), is(2)); assertThat(serverHealthService, ServerHealthMatcher.containsState(globalId)); assertThat(serverHealthService, ServerHealthMatcher.containsState(pipelineId)); }
@Test public void shouldRemoveLogByCategoryFromServerHealth() throws Exception { HealthStateScope scope = forPipeline(PIPELINE_NAME); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(scope))); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.invalidLicense(scope))); serverHealthService.update(ServerHealthState.error("message", "description", globalId)); assertThat(serverHealthService.logs().size(), is(3)); serverHealthService.removeByScope(scope); assertThat(serverHealthService.logs().size(), is(1)); assertThat(serverHealthService, ServerHealthMatcher.containsState(globalId)); }
@Test public void shouldReturnErrorLogs() throws Exception { serverHealthService.update(ServerHealthState.error("message", "description", pipelineId)); CruiseConfig cruiseConfig = new BasicCruiseConfig(); new GoConfigMother().addPipeline(cruiseConfig, PIPELINE_NAME, "stageName", "jon"); serverHealthService.purgeStaleHealthMessages(cruiseConfig); assertThat(serverHealthService.logs().size(), is(1)); }
@Test public void shouldUpdateLogInServerHealth() throws Exception { ServerHealthState serverHealthState = ServerHealthState.error("message", "description", globalId); serverHealthService.update(serverHealthState); ServerHealthState newServerHealthState = ServerHealthState.error("updated message", "updated description", globalId); serverHealthService.update(newServerHealthState); assertThat(serverHealthService.logs().size(), is(1)); assertThat(serverHealthService, ServerHealthMatcher.containsState(globalId, HealthStateLevel.ERROR, "updated message")); }
@Test public void shouldRemoveErrorLogWhenCorrespondingPipelineIsMissing() throws Exception { serverHealthService.update(ServerHealthState.error("message", "description", pipelineId)); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(forPipeline("other")))); serverHealthService.purgeStaleHealthMessages(new BasicCruiseConfig()); assertThat(serverHealthService.logs().size(), is(0)); }
@Test public void globalStateRelatedPipelineNames() { HgMaterial hgMaterial = MaterialsMother.hgMaterial(); CruiseConfig config = new BasicCruiseConfig(); config.addPipeline("group", PipelineConfigMother.pipelineConfig(PIPELINE_NAME, new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline2", new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline3")); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.invalidConfig())); assertTrue((serverHealthService.logs().get(0)).getPipelineNames(config).isEmpty()); }
@Test public void materialStateRelatedPipelineNames() { HgMaterial hgMaterial = MaterialsMother.hgMaterial(); CruiseConfig config = new BasicCruiseConfig(); config.addPipeline("group", PipelineConfigMother.pipelineConfig(PIPELINE_NAME, new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline2", new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline3")); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(forMaterial(hgMaterial)))); Set<String> pipelines = (serverHealthService.logs().get(0)).getPipelineNames(config); assertEquals(Sets.newHashSet("pipeline", "pipeline2"), pipelines); }
@Test public void materialUpdateStateRelatedPipelineNames() { HgMaterial hgMaterial = MaterialsMother.hgMaterial(); CruiseConfig config = new BasicCruiseConfig(); config.addPipeline("group", PipelineConfigMother.pipelineConfig(PIPELINE_NAME, new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline2", new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline3")); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(forMaterialUpdate(hgMaterial)))); Set<String> pipelines = (serverHealthService.logs().get(0)).getPipelineNames(config); assertEquals(Sets.newHashSet("pipeline", "pipeline2"), pipelines); }
@Test public void faninErrorStateRelatedPipelineNames() { HgMaterial hgMaterial = MaterialsMother.hgMaterial(); CruiseConfig config = new BasicCruiseConfig(); config.addPipeline("group", PipelineConfigMother.pipelineConfig(PIPELINE_NAME, new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline2", new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline3")); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(HealthStateScope.forFanin("pipeline2")))); Set<String> pipelines = (serverHealthService.logs().get(0)).getPipelineNames(config); assertEquals(Sets.newHashSet("pipeline2"), pipelines); } }
@Test public void noPipelineMatchMaterialStateRelatedPipelineNames() { HgMaterial hgMaterial = MaterialsMother.hgMaterial(); CruiseConfig config = new BasicCruiseConfig(); config.addPipeline("group", PipelineConfigMother.pipelineConfig(PIPELINE_NAME, new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline2", new MaterialConfigs(hgMaterial.config()))); config.addPipeline("group", PipelineConfigMother.pipelineConfig("pipeline3")); serverHealthService.update(ServerHealthState.error("message", "description", HealthStateType.general(forMaterial(MaterialsMother.p4Material())))); assertTrue((serverHealthService.logs().get(0)).getPipelineNames(config).isEmpty()); }
@Test public void shouldRemoveErrorLogWhenCorrespondingMaterialIsMissing() throws Exception { serverHealthService.update(ServerHealthState.error("hg-message", "description", HealthStateType.general(forMaterial(MaterialsMother.hgMaterial())))); SvnMaterialConfig svnMaterialConfig = MaterialConfigsMother.svnMaterialConfig(); serverHealthService.update(ServerHealthState.error("svn-message", "description", HealthStateType.general(forMaterialConfig(svnMaterialConfig)))); CruiseConfig cruiseConfig = new BasicCruiseConfig(); cruiseConfig.addPipeline("defaultGroup", new PipelineConfig(new CaseInsensitiveString("dev"), new MaterialConfigs(svnMaterialConfig), new StageConfig(new CaseInsensitiveString("first"), new JobConfigs()))); serverHealthService.purgeStaleHealthMessages(cruiseConfig); assertThat(serverHealthService.logs().size(), is(1)); }