
org.glowroot.ui.AlertConfigJsonService Maven / Gradle / Ivy
/*
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.glowroot.ui;
import java.util.List;
import org.glowroot.agent.shaded.fasterxml.jackson.databind.ObjectMapper;
import org.glowroot.agent.shaded.google.common.annotations.VisibleForTesting;
import org.glowroot.agent.shaded.google.common.base.Optional;
import org.glowroot.agent.shaded.google.common.collect.ImmutableList;
import org.glowroot.agent.shaded.google.common.collect.Lists;
import org.glowroot.agent.shaded.google.common.collect.Ordering;
import org.glowroot.agent.shaded.netty.handler.codec.http.HttpResponseStatus;
import org.immutables.value.Value;
import org.glowroot.agent.shaded.slf4j.Logger;
import org.glowroot.agent.shaded.slf4j.LoggerFactory;
import org.glowroot.common.util.ObjectMappers;
import org.glowroot.storage.repo.ConfigRepository;
import org.glowroot.storage.repo.ConfigRepository.DuplicateMBeanObjectNameException;
import org.glowroot.storage.repo.config.AlertConfig;
import org.glowroot.storage.repo.config.ImmutableAlertConfig;
import static org.glowroot.agent.shaded.netty.handler.codec.http.HttpResponseStatus.CONFLICT;
@JsonService
class AlertConfigJsonService {
private static final String SERVER_ID = "";
private static final Logger logger = LoggerFactory.getLogger(AlertConfigJsonService.class);
private static final ObjectMapper mapper = ObjectMappers.create();
@VisibleForTesting
static final Ordering orderingByName = new Ordering() {
@Override
public int compare(AlertConfig left, AlertConfig right) {
return left.transactionType().compareToIgnoreCase(right.transactionType());
}
};
private final ConfigRepository configRepository;
AlertConfigJsonService(ConfigRepository configRepository) {
this.configRepository = configRepository;
}
@GET("/backend/config/alerts")
String getAlert(String queryString) throws Exception {
AlertConfigRequest request = QueryStrings.decode(queryString, AlertConfigRequest.class);
Optional version = request.version();
if (version.isPresent()) {
AlertConfig alertConfig = configRepository.getAlertConfig(SERVER_ID, version.get());
if (alertConfig == null) {
throw new JsonServiceException(HttpResponseStatus.NOT_FOUND);
}
return mapper.writeValueAsString(AlertConfigDto.fromConfig(alertConfig));
} else {
List alertConfigDtos = Lists.newArrayList();
List alertConfigs = configRepository.getAlertConfigs(SERVER_ID);
alertConfigs = orderingByName.immutableSortedCopy(alertConfigs);
for (AlertConfig alertConfig : alertConfigs) {
alertConfigDtos.add(AlertConfigDto.fromConfig(alertConfig));
}
return mapper.writeValueAsString(alertConfigDtos);
}
}
@POST("/backend/config/alerts/add")
String addAlert(String content) throws Exception {
AlertConfigDto alertConfigDto = mapper.readValue(content, ImmutableAlertConfigDto.class);
AlertConfig alertConfig = alertConfigDto.toConfig();
try {
configRepository.insertAlertConfig(alertConfigDto.serverId().get(), alertConfig);
} catch (DuplicateMBeanObjectNameException e) {
// log exception at debug level
logger.debug(e.getMessage(), e);
throw new JsonServiceException(CONFLICT, "mbeanObjectName");
}
return mapper.writeValueAsString(AlertConfigDto.fromConfig(alertConfig));
}
@POST("/backend/config/alerts/update")
String updateAlert(String content) throws Exception {
AlertConfigDto alertConfigDto = mapper.readValue(content, ImmutableAlertConfigDto.class);
AlertConfig alertConfig = alertConfigDto.toConfig();
configRepository.updateAlertConfig(alertConfigDto.serverId().get(), alertConfig,
alertConfigDto.version().get());
return mapper.writeValueAsString(AlertConfigDto.fromConfig(alertConfig));
}
@POST("/backend/config/alerts/remove")
void removeAlert(String content) throws Exception {
AlertConfigRequest request = mapper.readValue(content, ImmutableAlertConfigRequest.class);
configRepository.deleteAlertConfig(SERVER_ID, request.version().get());
}
@Value.Immutable
interface AlertConfigRequest {
Optional version();
}
@Value.Immutable
abstract static class AlertConfigDto {
abstract Optional serverId(); // only used in request
abstract String transactionType();
abstract double percentile();
abstract int timePeriodMinutes();
abstract int thresholdMillis();
abstract int minTransactionCount();
abstract ImmutableList emailAddresses();
abstract Optional version(); // absent for insert operations
private static AlertConfigDto fromConfig(AlertConfig alertConfig) {
return ImmutableAlertConfigDto.builder()
.transactionType(alertConfig.transactionType())
.percentile(alertConfig.percentile())
.timePeriodMinutes(alertConfig.timePeriodMinutes())
.thresholdMillis(alertConfig.thresholdMillis())
.minTransactionCount(alertConfig.minTransactionCount())
.addAllEmailAddresses(alertConfig.emailAddresses())
.version(alertConfig.version())
.build();
}
private AlertConfig toConfig() {
return ImmutableAlertConfig.builder()
.transactionType(transactionType())
.percentile(percentile())
.timePeriodMinutes(timePeriodMinutes())
.thresholdMillis(thresholdMillis())
.minTransactionCount(minTransactionCount())
.addAllEmailAddresses(emailAddresses())
.build();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy