如何检查每个 URL 的特定阈值?
How to check for particular threshold for each URL?
我正在开发一个应用程序,其中我有机器列表,对于每台机器,我需要点击 URL,这将在 XML 中返回响应,我需要解析XML 为每个服务器从中提取一个字段(dataKey 为字段名)值,然后检查该字段值是否与我的阈值匹配。如果它连续三次与我的阈值匹配,则将该服务器添加到我的另一个列表中。
因此,如果 machineA
dataKey
字段连续三次匹配我的阈值,则将 machineA
添加到我的列表中,如果我的 machineB
dataKey
字段值也连续三次匹配我相同的阈值,然后将 machineB
添加到相同的列表中。
将机器添加到列表后,重置计数器并重新启动。
下面是我的MachineStats
class,其中包含每台机器的信息-
public class MachineStats {
private String serverName;
private String serverURL;
private int dataKey = 0;
public MachineStats(String serverName, String serverURL) {
this.serverName = serverName;
this.serverURL = serverURL;
}
// getters here
public boolean isEligibleForEmail(Integer dataKeyCount, int thresholdLimit) {
try {
if (!TestUtils.isEmpty(dataKeyCount)) {
if (dataKeyCount >= thresholdLimit) {
dataKey++;
} else {
dataKey = 0;
}
if (dataKey > 3) {
dataKey = 0;
return true;
}
return false;
} else {
return false;
}
} catch (NumberFormatException ex) {
// log an exception
return false;
}
}
}
下面是我的主要方法中的代码,它是 serverList
保存机器列表的起点。
// this holds machineA and machineB information
private static List<MachineStats> serverList = new LinkedList<MachineStats>();
while(true) {
ServerMetricsTask task = new ServerMetricsTask(serverList, thresholdLimit);
task.run();
TimeUnit.MINUTES.sleep(2); // sleep for 2 minutes
}
下面是我的 ServerMetricsTask
class -
public class ServerMetricsTask {
private List<MachineStats> listOfServers;
private int thresholdLimit;
public ServerMetricsTask(List<MachineStats> listOfServers, int thresholdLimit) {
this.listOfServers = listOfServers;
this.thresholdLimit = thresholdLimit;
}
public void run() {
try {
List<String> holder = new LinkedList<String>();
for (MachineStats stats : listOfServers) {
Integer dataKeyCount = TestUtils.extractKey(stats.getServerName(), stats.getServerURL());
if (stats.isEligibleForEmail(dataKeyCount, thresholdLimit)) {
holder.add(stats.getServerName());
}
}
if (!holder.isEmpty()) {
// send an email with the machine name which has met the threshold
}
} catch (Exception ex) {
// log an exception
}
}
}
问题陈述:-
现在上面的代码可以正常工作了。截至目前,我只检查一个 属性,即每个服务器的 dataKey
。现在我需要为每个服务器的两个字段做同样的事情。第一个字段是 dataKey
,第二个字段是 customerKey
,这两个字段将出现在每个服务器的 XML 响应中。
所以我有一个 fieldHolder
地图,其中包含我需要检查的字段名称。
此处 fieldHolder
地图持有 -
key - field which we want to extract from the response coming from the URL.
value - is the threshold for that field which is a key.
现在我已经像这样修改了我的代码 -
// this holds machineA and machineB information
private static List<MachineStats> serverList = new LinkedList<MachineStats>();
private static Map<String, String> fieldHolder;
while(true) {
ServerMetricsTask task = new ServerMetricsTask(serverList, fieldHolder);
task.run();
TimeUnit.MINUTES.sleep(2); // sleep for 2 minutes
}
下面是我的 ServerMetricsTask
class -
public class ServerMetricsTask {
private List<MachineStats> listOfServers;
private int thresholdLimit;
public ServerMetricsTask(List<MachineStats> listOfServers, Map<String, String> fieldHolder) {
this.listOfServers = listOfServers;
this.fieldHolder = fieldHolder;
}
public void run() {
try {
List<String> holder = new LinkedList<String>();
for (MachineStats stats : listOfServers) {
//now what should I do here?
// I guess I need to iterate fieldHolder map
}
if (!holder.isEmpty()) {
// send an email with the machine name which has met the threshold
}
} catch (Exception ex) {
// log an exception
}
}
}
我面临的唯一问题是我不知道应该如何添加逻辑,以便它可以通过在迭代 fieldHolder
映射后执行 URL 来提取两个字段值,然后还检查这两个领域或其中任何一个是否连续三次达到阈值限制。如果他们连续三次达到阈值限制,则将该机器名称添加到列表持有者中。
我已经知道如何通过解析 XML 响应来提取给定 URL 的那些字段值。
首先,将MachineStats中的dataKey改为keyMap
public class MachineStats {
...
private HashMap<String,int> keyMap = new HashMap<String,int>;
...
public HashMap<String,int> getKeyMap() {
return this.keyMap;
}
}
每次创建新的 MachineStats 时,使用所需的密钥填充其 keyMap。
keyMap.put("Key1", 0);
keyMap.put("Key2", 0);
// etc. I don't know how you will do it
现在,您需要更改 TestUtils.extractKey()
以便它接受并提取任意键名。与我在评论中所说的相反,最好不要更改其内部逻辑,因此我们不会在那里进行任何更改。
TestUtils.extractKey(String keyName, String serverName, string serverURL);
现在,ServerMetricsTask 迭代机器列表,也迭代机器的键盘映射:
public class ServerMetricsTask {
...
for (MachineStats stats : listOfServers) {
//now what should I do here?
// I guess I need to iterate fieldHolder map
HashMap<String, int> keymap = stats.getKeyMap();
int key_count = keymap.size();
int valid_count = 0;
Set<String> keys = keymap.keySet();
for (String key : keys) {
int count = keymap.get(key);
int result = TestUtils.extractKey(key, stats.getServerName(), stats.getServerURL());
if (stats.isEligibleForEmail(result, thresholdLimit)) {
count++;
} else {
// here you can do count = 0 if you need strictly continuous appearance of the key
}
keymap.put(key, count);
if (count >= 3) valid_count++;
}
// now decide if you need all keys to be valid or just some
if (valid_count == key_count) holder.add(stats.getServerName());
...
我正在开发一个应用程序,其中我有机器列表,对于每台机器,我需要点击 URL,这将在 XML 中返回响应,我需要解析XML 为每个服务器从中提取一个字段(dataKey 为字段名)值,然后检查该字段值是否与我的阈值匹配。如果它连续三次与我的阈值匹配,则将该服务器添加到我的另一个列表中。
因此,如果 machineA
dataKey
字段连续三次匹配我的阈值,则将 machineA
添加到我的列表中,如果我的 machineB
dataKey
字段值也连续三次匹配我相同的阈值,然后将 machineB
添加到相同的列表中。
将机器添加到列表后,重置计数器并重新启动。
下面是我的MachineStats
class,其中包含每台机器的信息-
public class MachineStats {
private String serverName;
private String serverURL;
private int dataKey = 0;
public MachineStats(String serverName, String serverURL) {
this.serverName = serverName;
this.serverURL = serverURL;
}
// getters here
public boolean isEligibleForEmail(Integer dataKeyCount, int thresholdLimit) {
try {
if (!TestUtils.isEmpty(dataKeyCount)) {
if (dataKeyCount >= thresholdLimit) {
dataKey++;
} else {
dataKey = 0;
}
if (dataKey > 3) {
dataKey = 0;
return true;
}
return false;
} else {
return false;
}
} catch (NumberFormatException ex) {
// log an exception
return false;
}
}
}
下面是我的主要方法中的代码,它是 serverList
保存机器列表的起点。
// this holds machineA and machineB information
private static List<MachineStats> serverList = new LinkedList<MachineStats>();
while(true) {
ServerMetricsTask task = new ServerMetricsTask(serverList, thresholdLimit);
task.run();
TimeUnit.MINUTES.sleep(2); // sleep for 2 minutes
}
下面是我的 ServerMetricsTask
class -
public class ServerMetricsTask {
private List<MachineStats> listOfServers;
private int thresholdLimit;
public ServerMetricsTask(List<MachineStats> listOfServers, int thresholdLimit) {
this.listOfServers = listOfServers;
this.thresholdLimit = thresholdLimit;
}
public void run() {
try {
List<String> holder = new LinkedList<String>();
for (MachineStats stats : listOfServers) {
Integer dataKeyCount = TestUtils.extractKey(stats.getServerName(), stats.getServerURL());
if (stats.isEligibleForEmail(dataKeyCount, thresholdLimit)) {
holder.add(stats.getServerName());
}
}
if (!holder.isEmpty()) {
// send an email with the machine name which has met the threshold
}
} catch (Exception ex) {
// log an exception
}
}
}
问题陈述:-
现在上面的代码可以正常工作了。截至目前,我只检查一个 属性,即每个服务器的 dataKey
。现在我需要为每个服务器的两个字段做同样的事情。第一个字段是 dataKey
,第二个字段是 customerKey
,这两个字段将出现在每个服务器的 XML 响应中。
所以我有一个 fieldHolder
地图,其中包含我需要检查的字段名称。
此处 fieldHolder
地图持有 -
key - field which we want to extract from the response coming from the URL.
value - is the threshold for that field which is a key.
现在我已经像这样修改了我的代码 -
// this holds machineA and machineB information
private static List<MachineStats> serverList = new LinkedList<MachineStats>();
private static Map<String, String> fieldHolder;
while(true) {
ServerMetricsTask task = new ServerMetricsTask(serverList, fieldHolder);
task.run();
TimeUnit.MINUTES.sleep(2); // sleep for 2 minutes
}
下面是我的 ServerMetricsTask
class -
public class ServerMetricsTask {
private List<MachineStats> listOfServers;
private int thresholdLimit;
public ServerMetricsTask(List<MachineStats> listOfServers, Map<String, String> fieldHolder) {
this.listOfServers = listOfServers;
this.fieldHolder = fieldHolder;
}
public void run() {
try {
List<String> holder = new LinkedList<String>();
for (MachineStats stats : listOfServers) {
//now what should I do here?
// I guess I need to iterate fieldHolder map
}
if (!holder.isEmpty()) {
// send an email with the machine name which has met the threshold
}
} catch (Exception ex) {
// log an exception
}
}
}
我面临的唯一问题是我不知道应该如何添加逻辑,以便它可以通过在迭代 fieldHolder
映射后执行 URL 来提取两个字段值,然后还检查这两个领域或其中任何一个是否连续三次达到阈值限制。如果他们连续三次达到阈值限制,则将该机器名称添加到列表持有者中。
我已经知道如何通过解析 XML 响应来提取给定 URL 的那些字段值。
首先,将MachineStats中的dataKey改为keyMap
public class MachineStats {
...
private HashMap<String,int> keyMap = new HashMap<String,int>;
...
public HashMap<String,int> getKeyMap() {
return this.keyMap;
}
}
每次创建新的 MachineStats 时,使用所需的密钥填充其 keyMap。
keyMap.put("Key1", 0);
keyMap.put("Key2", 0);
// etc. I don't know how you will do it
现在,您需要更改 TestUtils.extractKey()
以便它接受并提取任意键名。与我在评论中所说的相反,最好不要更改其内部逻辑,因此我们不会在那里进行任何更改。
TestUtils.extractKey(String keyName, String serverName, string serverURL);
现在,ServerMetricsTask 迭代机器列表,也迭代机器的键盘映射:
public class ServerMetricsTask {
...
for (MachineStats stats : listOfServers) {
//now what should I do here?
// I guess I need to iterate fieldHolder map
HashMap<String, int> keymap = stats.getKeyMap();
int key_count = keymap.size();
int valid_count = 0;
Set<String> keys = keymap.keySet();
for (String key : keys) {
int count = keymap.get(key);
int result = TestUtils.extractKey(key, stats.getServerName(), stats.getServerURL());
if (stats.isEligibleForEmail(result, thresholdLimit)) {
count++;
} else {
// here you can do count = 0 if you need strictly continuous appearance of the key
}
keymap.put(key, count);
if (count >= 3) valid_count++;
}
// now decide if you need all keys to be valid or just some
if (valid_count == key_count) holder.add(stats.getServerName());
...