-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
saxenakshitiz
committed
Aug 30, 2024
1 parent
d6d3c34
commit 740ffd7
Showing
2 changed files
with
82 additions
and
57 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.hypertrace.label.application.rule.config.service; | ||
|
||
import static java.util.function.Function.identity; | ||
|
||
import com.google.protobuf.util.JsonFormat; | ||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigFactory; | ||
import com.typesafe.config.ConfigObject; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import lombok.Getter; | ||
import lombok.SneakyThrows; | ||
import org.hypertrace.label.application.rule.config.service.v1.LabelApplicationRule; | ||
|
||
public class LabelApplicationRuleConfig { | ||
private static final String LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG = | ||
"label.application.rule.config.service"; | ||
private static final String MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT = | ||
"max.dynamic.label.application.rules.per.tenant"; | ||
private static final String SYSTEM_LABEL_APPLICATION_RULES = "system.label.application.rules"; | ||
private static final int DEFAULT_MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT = 100; | ||
|
||
@Getter private final int maxDynamicLabelApplicationRulesAllowed; | ||
@Getter private final List<LabelApplicationRule> systemLabelApplicationRules; | ||
@Getter private final Map<String, LabelApplicationRule> systemLabelApplicationRulesMap; | ||
|
||
public LabelApplicationRuleConfig(Config config) { | ||
Config labelApplicationRuleConfig = | ||
config.hasPath(LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG) | ||
? config.getConfig(LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG) | ||
: ConfigFactory.empty(); | ||
this.maxDynamicLabelApplicationRulesAllowed = | ||
labelApplicationRuleConfig.hasPath(MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT) | ||
? labelApplicationRuleConfig.getInt(MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT) | ||
: DEFAULT_MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT; | ||
if (labelApplicationRuleConfig.hasPath(SYSTEM_LABEL_APPLICATION_RULES)) { | ||
final List<? extends ConfigObject> systemLabelApplicationRulesObjectList = | ||
labelApplicationRuleConfig.getObjectList(SYSTEM_LABEL_APPLICATION_RULES); | ||
this.systemLabelApplicationRules = | ||
buildSystemLabelApplicationRuleList(systemLabelApplicationRulesObjectList); | ||
this.systemLabelApplicationRulesMap = | ||
this.systemLabelApplicationRules.stream() | ||
.collect(Collectors.toUnmodifiableMap(LabelApplicationRule::getId, identity())); | ||
} else { | ||
this.systemLabelApplicationRules = Collections.emptyList(); | ||
this.systemLabelApplicationRulesMap = Collections.emptyMap(); | ||
} | ||
} | ||
|
||
private List<LabelApplicationRule> buildSystemLabelApplicationRuleList( | ||
List<? extends com.typesafe.config.ConfigObject> configObjectList) { | ||
return configObjectList.stream() | ||
.map(LabelApplicationRuleConfig::buildLabelApplicationRuleFromConfig) | ||
.collect(Collectors.toUnmodifiableList()); | ||
} | ||
|
||
@SneakyThrows | ||
private static LabelApplicationRule buildLabelApplicationRuleFromConfig( | ||
com.typesafe.config.ConfigObject configObject) { | ||
String jsonString = configObject.render(); | ||
LabelApplicationRule.Builder builder = LabelApplicationRule.newBuilder(); | ||
JsonFormat.parser().merge(jsonString, builder); | ||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters