com.spring.boxes.dollar.ShuntUtils Maven / Gradle / Ivy
package com.spring.boxes.dollar;
import com.google.common.base.Preconditions;
import com.spring.boxes.dollar.enums.TermShuntType;
import com.spring.boxes.dollar.support.cryptor.IdEncryptor;
import com.spring.boxes.dollar.support.valve.ValveExecutor;
import com.spring.boxes.dollar.term.TermShunt;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Objects;
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ShuntUtils extends ValveExecutor {
/**
* Shunt默认是取模尾号匹配机制,此方法在尾号机制上扩展为百分比比率匹配
* 判断一个值是否在给定的百分比范围内,
*
* @param percent 百分比[0-100]
* @param value 需要进行判断的值
* @return 如果值在给定的百分比范围内,则返回true;否则返回false
*/
public static boolean isPercentOnFor(int percent, int value) {
Preconditions.checkArgument(percent >= 0 && percent <= 100, "percent should be [0-100]");
if (percent < 1) {
return false;
}
if (percent == 1) {
return ShuntUtils.isOnFor("100;0;;", value);
}
return ShuntUtils.isOnFor(String.format("100;0-%s", percent - 1), value);
}
public static boolean isOnFor(long userId, String deviceId, List termShunts){
if(Objects.isNull(termShunts)){
return false;
}
for (TermShunt shunt : termShunts) {
if (TermShuntType.USER_ID.getLabel().equalsIgnoreCase(shunt.getType())) {
boolean grayHit = ShuntUtils.isOnFor(shunt.getConfig(), userId);
if (!grayHit){
return false;
}
}
if(TermShuntType.DEVICE_ID.getLabel().equalsIgnoreCase(shunt.getType())){
long hashId = ValveExecutor.getDeviceHash(deviceId);
boolean grayHit = ShuntUtils.isOnFor(shunt.getConfig(), hashId);
if (!grayHit){
return false;
}
}
}
return true;
}
/**
* 判断投放是否命中灰度
*
* @param userId 用户编号
* @param termShunts 投放数据
* @return 是否命中灰度
*/
public static boolean isShuntOnFor(long userId, String deviceId, List termShunts) {
if (CollectionUtils.isEmpty(termShunts)) {
return true;
}
for (TermShunt shunt : termShunts) {
if (TermShuntType.USER_ID.getLabel().equalsIgnoreCase(shunt.getType())) {
boolean grayHit = ShuntUtils.isOnFor(shunt.getConfig(), userId);
if (!grayHit) {
log.info(String.format("用户ID(%s)未命中灰度", userId));
return false;
}
}
if (TermShuntType.DEVICE_ID.getLabel().equalsIgnoreCase(shunt.getType())) {
boolean grayHit = ShuntUtils.isOnFor(shunt.getConfig(), IdEncryptor.skipDecryptId(deviceId));
if (!grayHit) {
log.info(String.format("设备ID(%s)未命中灰度", deviceId));
return false;
}
}
}
return true;
}
public static void main(String[] args) {
/*String[] arr = {"100", "0-99", "123,456,789,100,", "111,222,333,444,"};
System.out.println(parseList(arr, 2));
System.out.println(parseList(arr, 3));*/
String config = "100;0-15;18,19;11,12";
for (int i = 0; i < 20; i++) {
System.out.println(i + "灰度结果:" + isOnFor(config, i));
}
/*System.out.println("测试黑白名单");
System.out.println(isOnFor(config, 123));
System.out.println(isOnFor(config, 456));
System.out.println(isOnFor(config, 111));
System.out.println(isOnFor(config, 222));*/
config = "100;0-15,17-19;18,20;11,12";
System.out.println("0灰度结果:" + isOnFor(config, 0));
System.out.println("11灰度结果:" + isOnFor(config, 11));
System.out.println("14灰度结果:" + isOnFor(config, 14));
System.out.println("15灰度结果:" + isOnFor(config, 15));
System.out.println("16灰度结果:" + isOnFor(config, 16));
System.out.println("17灰度结果:" + isOnFor(config, 17));
System.out.println("20灰度结果:" + isOnFor(config, 20));
System.out.println("21灰度结果:" + isOnFor(config, 21));
/*for (int i = 0; i < 10; i++) {
System.out.println(isOnFor(config, i));
}
System.out.println("测试黑白名单");
System.out.println(isOnFor(config, 123));
System.out.println(isOnFor(config, 456));
System.out.println(isOnFor(config, 111));
System.out.println(isOnFor(config, 222));*/
/*RangeSet rangeSet = TreeRangeSet.create();
rangeSet.add(Range.closed(1, 10));
System.out.println(rangeSet); // [[1..10]]
rangeSet.add(Range.closedOpen(11, 15));
System.out.println(rangeSet); // [[1..10], [11..15)]
rangeSet.add(Range.open(15, 20));
System.out.println(rangeSet); // [[1..10], [11..15), (15..20)]
rangeSet.add(Range.openClosed(0, 0));
System.out.println(rangeSet); // [[1..10], [11..15), (15..20)]
rangeSet.remove(Range.open(5, 10));
System.out.println(rangeSet); // [[1..5], [10..10], [11..15), (15..20)]*/
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy