Search in sources :

Example 1 with ClusterSettingKey

use of io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey in project datarouter by hotpads.

the class ClusterSettingsHandler method browseSettings.

@Handler
public Mav browseSettings(OptionalString name) {
    Mav mav = new Mav(files.jsp.admin.datarouter.setting.browseSettingsJsp);
    String path = servletContext.getContextPath() + paths.datarouter.settings.customSettings.toSlashedString();
    mav.put("customSettingsPath", path);
    String requestedNodeName = name.orElse(settingRootFinder.getName());
    mav.put("nodeName", name.orElse(""));
    Map<SimpleSettingCategory, Set<SettingNodeJspDto>> categoryMap = new LinkedHashMap<>();
    for (Entry<SimpleSettingCategory, Set<SettingRoot>> entry : settingRootFinder.getRootNodesByCategory().entrySet()) {
        var nodes = entry.getValue().stream().map(SettingNodeJspDto::new).collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SettingNodeJspDto::getShortName))));
        categoryMap.put(entry.getKey(), nodes);
    }
    Optional<String> currentCategory = settingRootFinder.getRootNodesByCategory().entrySet().stream().findFirst().map(Entry::getKey).map(SimpleSettingCategory::getDisplay);
    mav.put("currentCategory", currentCategory.orElse(""));
    mav.put("categoryMap", categoryMap);
    mav.put("serverTypeOptions", serverTypes.getHtmlSelectOptionsVarNames());
    Optional<SettingNode> node = getSettingNode(requestedNodeName);
    // if the node was not found, get the most recent ancestor to search its settings list
    boolean trySearchingForSpecificSetting = node.isEmpty();
    if (trySearchingForSpecificSetting) {
        node = getMostRecentAncestorSettingNode(requestedNodeName);
    }
    if (node.isEmpty()) {
        return mav;
    }
    node.ifPresent(settingNode -> {
        String rootName = StringTool.getStringBeforeFirstOccurrence('.', settingNode.getName());
        settingRootFinder.getRootNodesSortedByShortName().stream().filter(rootNode -> rootNode.getShortName().equals(rootName)).findFirst().map(SettingRoot.class::cast).map(SettingRoot::getSettingCategory).map(SettingCategory::getDisplay).ifPresent(category -> mav.put("currentCategory", category));
        mav.put("currentRootName", rootName);
    });
    mav.put("ancestors", Scanner.of(settingRootFinder.getDescendants(node.get().getName())).map(SettingNodeJspDto::new).list());
    mav.put("currentRootName", node.get().getName().substring(0, node.get().getName().indexOf('.')));
    mav.put("children", Scanner.of(node.get().getListChildren()).map(SettingNodeJspDto::new).list());
    List<CachedSetting<?>> settingsList = node.get().getListSettings();
    if (trySearchingForSpecificSetting) {
        settingsList = settingsList.stream().filter(setting -> setting.getName().equals(requestedNodeName)).collect(Collectors.toList());
    }
    Map<String, List<ClusterSettingJspDto>> customSettingsByName = new HashMap<>();
    for (CachedSetting<?> setting : settingsList) {
        ClusterSettingKey settingKey = new ClusterSettingKey(setting.getName(), null, null, null);
        List<ClusterSetting> settingsInDb = clusterSettingDao.scanWithPrefix(settingKey).list();
        Optional<ClusterSetting> mostSpecificSetting = clusterSettingService.getMostSpecificClusterSetting(settingsInDb);
        boolean isActive = setting.getMostSpecificDatabeanValue().isPresent();
        Scanner.of(settingsInDb).map(settingFromDb -> {
            boolean isWinner = mostSpecificSetting.isPresent() && settingFromDb.equals(mostSpecificSetting.get());
            ClusterSettingJspDto jspDto = new ClusterSettingJspDto(settingFromDb, isActive, isWinner);
            return jspDto;
        }).flush(customSettings -> customSettingsByName.put(setting.getName(), customSettings));
    }
    mav.put("listSettings", Scanner.of(settingsList).map(SettingJspDto::new).list());
    mav.put("mightBeDevelopment", datarouterServerTypeDetector.mightBeDevelopment());
    mav.put("mapListsCustomSettings", customSettingsByName);
    return mav;
}
Also used : Scanner(io.datarouter.scanner.Scanner) ServerTypes(io.datarouter.storage.servertype.ServerTypes) ServerType(io.datarouter.storage.servertype.ServerType) ClusterSettingLogKey(io.datarouter.clustersetting.storage.clustersettinglog.ClusterSettingLogKey) ObjectTool(io.datarouter.util.lang.ObjectTool) DatarouterHtmlEmailService(io.datarouter.web.email.DatarouterHtmlEmailService) ZonedDateTime(java.time.ZonedDateTime) CachedSetting(io.datarouter.storage.setting.cached.CachedSetting) OptionalString(io.datarouter.web.handler.types.optional.OptionalString) DatarouterClusterSettingRoot(io.datarouter.clustersetting.config.DatarouterClusterSettingRoot) DatarouterSettingTag(io.datarouter.storage.setting.DatarouterSettingTag) ClusterSettingLogAction(io.datarouter.clustersetting.ClusterSettingLogAction) ChangelogRecorder(io.datarouter.instrumentation.changelog.ChangelogRecorder) ClusterSetting(io.datarouter.clustersetting.storage.clustersetting.ClusterSetting) ClusterSettingEmailType(io.datarouter.email.type.DatarouterEmailTypes.ClusterSettingEmailType) DatarouterServerTypeDetector(io.datarouter.storage.servertype.DatarouterServerTypeDetector) Map(java.util.Map) J2HtmlDatarouterEmailBuilder(io.datarouter.email.html.J2HtmlDatarouterEmailBuilder) SimpleSettingCategory(io.datarouter.storage.setting.SettingCategory.SimpleSettingCategory) SettingRootFinder(io.datarouter.storage.setting.SettingRoot.SettingRootFinder) ClusterSettingLogByReversedCreatedMsKey(io.datarouter.clustersetting.storage.clustersettinglog.ClusterSettingLogByReversedCreatedMsKey) DatarouterEmailLinkBuilder(io.datarouter.email.email.DatarouterEmailLinkBuilder) ServerTypeDetector(io.datarouter.storage.servertype.ServerTypeDetector) DatarouterChangelogDtoBuilder(io.datarouter.instrumentation.changelog.ChangelogRecorder.DatarouterChangelogDtoBuilder) Set(java.util.Set) ClusterSettingValidity(io.datarouter.clustersetting.ClusterSettingValidity) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) ContainerTag(j2html.tags.ContainerTag) Range(io.datarouter.util.tuple.Range) List(java.util.List) Setting(io.datarouter.storage.setting.Setting) ClusterSettingLogJspDto(io.datarouter.clustersetting.web.dto.ClusterSettingLogJspDto) SettingNode(io.datarouter.storage.setting.SettingNode) Entry(java.util.Map.Entry) Optional(java.util.Optional) DatarouterClusterSettingFiles(io.datarouter.clustersetting.config.DatarouterClusterSettingFiles) DatarouterClusterSettingPaths(io.datarouter.clustersetting.config.DatarouterClusterSettingPaths) SettingNameMatchResult(io.datarouter.clustersetting.service.ClusterSettingSearchService.SettingNameMatchResult) ClusterSettingService(io.datarouter.clustersetting.service.ClusterSettingService) TagCreator.text(j2html.TagCreator.text) SettingJspDto(io.datarouter.clustersetting.web.dto.SettingJspDto) ClusterSettingJspDto(io.datarouter.clustersetting.web.dto.ClusterSettingJspDto) LocalDateTime(java.time.LocalDateTime) DatarouterClusterSettingLogDao(io.datarouter.clustersetting.storage.clustersettinglog.DatarouterClusterSettingLogDao) HashMap(java.util.HashMap) ClusterSettingSearchService(io.datarouter.clustersetting.service.ClusterSettingSearchService) TreeSet(java.util.TreeSet) ClusterSettingScope(io.datarouter.clustersetting.ClusterSettingScope) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) SettingRoot(io.datarouter.storage.setting.SettingRoot) CachedClusterSettingTags(io.datarouter.storage.setting.cached.CachedClusterSettingTags) SettingNodeJspDto(io.datarouter.clustersetting.web.dto.SettingNodeJspDto) ClusterSettingKey(io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey) DatarouterWebPaths(io.datarouter.web.config.DatarouterWebPaths) StandardDatarouterEmailHeaderService(io.datarouter.web.email.StandardDatarouterEmailHeaderService) Mav(io.datarouter.web.handler.mav.Mav) SettingCategory(io.datarouter.storage.setting.SettingCategory) StringTool(io.datarouter.util.string.StringTool) TagCreator.a(j2html.TagCreator.a) ZonedDateFormatterTool(io.datarouter.util.time.ZonedDateFormatterTool) OptionalBoolean(io.datarouter.web.handler.types.optional.OptionalBoolean) DomContent(j2html.tags.DomContent) DatarouterClusterSettingDao(io.datarouter.clustersetting.storage.clustersetting.DatarouterClusterSettingDao) J2HtmlLegendTable(io.datarouter.web.html.j2html.J2HtmlLegendTable) ClusterSettingLog(io.datarouter.clustersetting.storage.clustersettinglog.ClusterSettingLog) DateTimeFormatter(java.time.format.DateTimeFormatter) BaseHandler(io.datarouter.web.handler.BaseHandler) Comparator(java.util.Comparator) Pair(io.datarouter.util.tuple.Pair) CachedSetting(io.datarouter.storage.setting.cached.CachedSetting) SettingNodeJspDto(io.datarouter.clustersetting.web.dto.SettingNodeJspDto) Mav(io.datarouter.web.handler.mav.Mav) Set(java.util.Set) TreeSet(java.util.TreeSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OptionalString(io.datarouter.web.handler.types.optional.OptionalString) SettingNode(io.datarouter.storage.setting.SettingNode) LinkedHashMap(java.util.LinkedHashMap) DatarouterClusterSettingRoot(io.datarouter.clustersetting.config.DatarouterClusterSettingRoot) SettingRoot(io.datarouter.storage.setting.SettingRoot) SettingJspDto(io.datarouter.clustersetting.web.dto.SettingJspDto) ClusterSettingJspDto(io.datarouter.clustersetting.web.dto.ClusterSettingJspDto) TreeSet(java.util.TreeSet) List(java.util.List) ArrayList(java.util.ArrayList) ClusterSettingKey(io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey) SimpleSettingCategory(io.datarouter.storage.setting.SettingCategory.SimpleSettingCategory) ClusterSettingJspDto(io.datarouter.clustersetting.web.dto.ClusterSettingJspDto) ClusterSetting(io.datarouter.clustersetting.storage.clustersetting.ClusterSetting) BaseHandler(io.datarouter.web.handler.BaseHandler)

Example 2 with ClusterSettingKey

use of io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey in project datarouter by hotpads.

the class ClusterSettingService method scanClusterSettingAndValidityWithPrefix.

public Scanner<ClusterSettingAndValidityJspDto> scanClusterSettingAndValidityWithPrefix(String prefix) {
    WebappInstance currentWebappInstance = webappInstanceDao.get(webappInstanceService.buildCurrentWebappInstanceKey());
    Range<ClusterSettingKey> range = prefix == null ? Range.everything() : KeyRangeTool.forPrefixWithWildcard(prefix, name -> new ClusterSettingKey(name, null, null, null));
    return clusterSettingDao.scan(range).map(setting -> {
        ClusterSettingValidity validity = getValidityForWebappInstance(setting, currentWebappInstance);
        return new ClusterSettingAndValidityJspDto(setting, validity);
    });
}
Also used : Scanner(io.datarouter.scanner.Scanner) DatarouterServerTypeSupplier(io.datarouter.storage.config.properties.DatarouterServerTypeSupplier) ServerTypes(io.datarouter.storage.servertype.ServerTypes) ServerType(io.datarouter.storage.servertype.ServerType) ClusterSettingFinder(io.datarouter.clustersetting.ClusterSettingFinder) CachedSetting(io.datarouter.storage.setting.cached.CachedSetting) DatarouterClusterSettingLogDao(io.datarouter.clustersetting.storage.clustersettinglog.DatarouterClusterSettingLogDao) Singleton(javax.inject.Singleton) DatarouterClusterSettingRoot(io.datarouter.clustersetting.config.DatarouterClusterSettingRoot) ServerName(io.datarouter.storage.config.properties.ServerName) Function(java.util.function.Function) DatarouterSettingTag(io.datarouter.storage.setting.DatarouterSettingTag) ClusterSettingScope(io.datarouter.clustersetting.ClusterSettingScope) ClusterSettingAndValidityJspDto(io.datarouter.clustersetting.web.dto.ClusterSettingAndValidityJspDto) Inject(javax.inject.Inject) ClusterSetting(io.datarouter.clustersetting.storage.clustersetting.ClusterSetting) CachedClusterSettingTags(io.datarouter.storage.setting.cached.CachedClusterSettingTags) Map(java.util.Map) KeyRangeTool(io.datarouter.storage.util.KeyRangeTool) SettingRootFinder(io.datarouter.storage.setting.SettingRoot.SettingRootFinder) ClusterSettingScopeComparator(io.datarouter.clustersetting.ClusterSettingScopeComparator) DefaultSettingValue(io.datarouter.storage.setting.DefaultSettingValue) WebappInstanceService(io.datarouter.webappinstance.service.WebappInstanceService) ClusterSettingKey(io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey) ClusterSettingValidity(io.datarouter.clustersetting.ClusterSettingValidity) WebappInstance(io.datarouter.webappinstance.storage.webappinstance.WebappInstance) StringTool(io.datarouter.util.string.StringTool) Objects(java.util.Objects) Range(io.datarouter.util.tuple.Range) List(java.util.List) EnvironmentName(io.datarouter.storage.config.properties.EnvironmentName) ClusterSettingComparisonTool(io.datarouter.clustersetting.ClusterSettingComparisonTool) DatarouterClusterSettingDao(io.datarouter.clustersetting.storage.clustersetting.DatarouterClusterSettingDao) DatarouterEnvironmentTypeSupplier(io.datarouter.storage.config.properties.DatarouterEnvironmentTypeSupplier) BooleanTool(io.datarouter.util.BooleanTool) Optional(java.util.Optional) DatarouterWebappInstanceDao(io.datarouter.webappinstance.storage.webappinstance.DatarouterWebappInstanceDao) ClusterSettingKey(io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey) ClusterSettingAndValidityJspDto(io.datarouter.clustersetting.web.dto.ClusterSettingAndValidityJspDto) ClusterSettingValidity(io.datarouter.clustersetting.ClusterSettingValidity) WebappInstance(io.datarouter.webappinstance.storage.webappinstance.WebappInstance)

Example 3 with ClusterSettingKey

use of io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey in project datarouter by hotpads.

the class ClusterSettingScopeValue method parse.

public static ClusterSettingScopeValue parse(String persistentString) {
    if (persistentString == null) {
        return null;
    }
    String[] parts = persistentString.split("_", 2);
    if (parts.length == 0) {
        return null;
    }
    ClusterSettingScope scope = ClusterSettingScope.fromPersistentStringStatic(parts[0]);
    switch(scope) {
        case DEFAULT_SCOPE:
        case SERVER_TYPE:
            return new ClusterSettingScopeValue(new ClusterSettingKey(null, scope, parts[1], null));
        case SERVER_NAME:
            return new ClusterSettingScopeValue(new ClusterSettingKey(null, scope, null, parts[1]));
        default:
            throw new RuntimeException("Failed to parse ClusterSettingScopeValue from string: " + persistentString);
    }
}
Also used : ClusterSettingKey(io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey)

Example 4 with ClusterSettingKey

use of io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey in project datarouter by hotpads.

the class ClusterSettingsHandler method putSettingFromParams.

/*-------------------------------- private -------------------------------*/
private ClusterSettingActionResultJson putSettingFromParams() {
    ClusterSettingKey clusterSettingKey = parseClusterSettingKeyFromParams();
    Optional<String> comment = parseCommentFromParams();
    String value = params.optional("value").orElse(null);
    var newClusterSetting = new ClusterSetting(clusterSettingKey, value);
    Optional<CachedSetting<?>> setting = settingRootFinder.getSettingByName(newClusterSetting.getName());
    ClusterSettingLogAction action;
    Optional<ClusterSetting> dbSetting = clusterSettingDao.find(clusterSettingKey);
    if (dbSetting.isPresent()) {
        action = ClusterSettingLogAction.UPDATED;
    } else {
        action = ClusterSettingLogAction.INSERTED;
    }
    var result = new ClusterSettingActionResultJson(action);
    if (setting.isPresent() && !setting.get().isValid(newClusterSetting.getValue())) {
        String badNewValue = newClusterSetting.getValue();
        String error = "Invalid value detected, setting did not accept new value: \"" + badNewValue + "\"";
        return result.markError(error);
    }
    String oldValue = dbSetting.map(ClusterSetting::getValue).orElse("?");
    String changedBy = getRequestorsUsername();
    var log = new ClusterSettingLog(newClusterSetting, action, changedBy, comment.orElse(null));
    clusterSettingDao.put(newClusterSetting);
    clusterSettingLogDao.put(log);
    sendEmail(log, oldValue);
    recordChangelog(log.getKey().getName(), log.getAction().getPersistentString(), log.getChangedBy(), Optional.ofNullable(log.getComment()));
    return result.markSuccess();
}
Also used : CachedSetting(io.datarouter.storage.setting.cached.CachedSetting) ClusterSetting(io.datarouter.clustersetting.storage.clustersetting.ClusterSetting) ClusterSettingKey(io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey) ClusterSettingLog(io.datarouter.clustersetting.storage.clustersettinglog.ClusterSettingLog) OptionalString(io.datarouter.web.handler.types.optional.OptionalString) ClusterSettingLogAction(io.datarouter.clustersetting.ClusterSettingLogAction)

Example 5 with ClusterSettingKey

use of io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey in project datarouter by hotpads.

the class ClusterSettingsHandler method delete.

@Handler
public ClusterSettingActionResultJson delete() {
    ClusterSettingLogAction action = ClusterSettingLogAction.DELETED;
    var result = new ClusterSettingActionResultJson(action);
    ClusterSettingKey clusterSettingKey = parseClusterSettingKeyFromParams();
    Optional<String> comment = parseCommentFromParams();
    String changedBy = getRequestorsUsername();
    ClusterSetting clusterSetting = clusterSettingDao.get(clusterSettingKey);
    var clusterSettingLog = new ClusterSettingLog(clusterSetting, action, changedBy, comment.orElse(null));
    clusterSettingDao.delete(clusterSettingKey);
    clusterSettingLogDao.put(clusterSettingLog);
    String oldValue = clusterSetting.getValue();
    sendEmail(clusterSettingLog, oldValue);
    recordChangelog(clusterSettingLog.getKey().getName(), clusterSettingLog.getAction().getPersistentString(), clusterSettingLog.getChangedBy(), comment);
    return result.markSuccess();
}
Also used : ClusterSetting(io.datarouter.clustersetting.storage.clustersetting.ClusterSetting) ClusterSettingKey(io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey) ClusterSettingLog(io.datarouter.clustersetting.storage.clustersettinglog.ClusterSettingLog) OptionalString(io.datarouter.web.handler.types.optional.OptionalString) ClusterSettingLogAction(io.datarouter.clustersetting.ClusterSettingLogAction) BaseHandler(io.datarouter.web.handler.BaseHandler)

Aggregations

ClusterSettingKey (io.datarouter.clustersetting.storage.clustersetting.ClusterSettingKey)7 ClusterSetting (io.datarouter.clustersetting.storage.clustersetting.ClusterSetting)4 ClusterSettingLogAction (io.datarouter.clustersetting.ClusterSettingLogAction)3 ClusterSettingScope (io.datarouter.clustersetting.ClusterSettingScope)3 ClusterSettingLog (io.datarouter.clustersetting.storage.clustersettinglog.ClusterSettingLog)3 CachedSetting (io.datarouter.storage.setting.cached.CachedSetting)3 OptionalString (io.datarouter.web.handler.types.optional.OptionalString)3 ClusterSettingValidity (io.datarouter.clustersetting.ClusterSettingValidity)2 DatarouterClusterSettingRoot (io.datarouter.clustersetting.config.DatarouterClusterSettingRoot)2 DatarouterClusterSettingDao (io.datarouter.clustersetting.storage.clustersetting.DatarouterClusterSettingDao)2 DatarouterClusterSettingLogDao (io.datarouter.clustersetting.storage.clustersettinglog.DatarouterClusterSettingLogDao)2 Scanner (io.datarouter.scanner.Scanner)2 ServerType (io.datarouter.storage.servertype.ServerType)2 ServerTypes (io.datarouter.storage.servertype.ServerTypes)2 DatarouterSettingTag (io.datarouter.storage.setting.DatarouterSettingTag)2 SettingRootFinder (io.datarouter.storage.setting.SettingRoot.SettingRootFinder)2 CachedClusterSettingTags (io.datarouter.storage.setting.cached.CachedClusterSettingTags)2 StringTool (io.datarouter.util.string.StringTool)2 Range (io.datarouter.util.tuple.Range)2 List (java.util.List)2