Skip to content
Snippets Groups Projects
Commit cd83f476 authored by gwen-soleil's avatar gwen-soleil
Browse files

add filtering for selecting or excluding attributes in attribute ArchiversErrorList

parent e997c58f
Branches
No related tags found
No related merge requests found
......@@ -13,11 +13,10 @@ import org.tango.archiving.monitoring.database.domain.AttributeInsertionStatus;
import org.tango.archiving.monitoring.database.infra.IDatabaseAccess;
import org.tango.utils.DevFailedUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.regex.Pattern;
public class MonitoringAgent {
......@@ -30,10 +29,6 @@ public class MonitoringAgent {
}
public Map<InsertionModes, InsertionStatus> getAttributes(final List<String> exclusions, final List<String> selections) {
List<String> exclusionsLower = getRegexList(exclusions.stream().map(String::toLowerCase)
.collect(Collectors.toList()));
List<String> selectionsLower = getRegexList(selections.stream().map(String::toLowerCase)
.collect(Collectors.toList()));
// Get started in DB
Map<InsertionModes, InsertionStatus> runningAttributes = databaseAccess.getAllRunningAttributes();
Map<InsertionModes, InsertionStatus> runningAttributesResult = new HashMap<>(runningAttributes);
......@@ -41,8 +36,8 @@ public class MonitoringAgent {
// Remove not selected attributes
for (InsertionModes modes : runningAttributes.keySet()) {
boolean toKeep = false;
for (String selection : selectionsLower) {
if (modes.getAttributeName().toLowerCase().matches(selection)) {
for (String selection : selections) {
if (Pattern.compile(selection, Pattern.CASE_INSENSITIVE).matcher(modes.getAttributeName()).matches()) {
toKeep = true;
LOGGER.debug("monitoring {}", modes.getAttributeName());
}
......@@ -54,8 +49,8 @@ public class MonitoringAgent {
} else if (!exclusions.isEmpty()) {
// Remove excluded list
for (InsertionModes modes : runningAttributes.keySet()) {
for (String exclusion : exclusionsLower) {
if (modes.getAttributeName().toLowerCase().matches(exclusion)) {
for (String exclusion : exclusions) {
if (Pattern.compile(exclusion, Pattern.CASE_INSENSITIVE).matcher(modes.getAttributeName()).matches()) {
runningAttributesResult.remove(modes);
LOGGER.info("not monitoring {}", modes.getAttributeName());
}
......@@ -65,19 +60,6 @@ public class MonitoringAgent {
return runningAttributesResult;
}
private List<String> getRegexList(List<String> list) {
List<String> regexList = new ArrayList<>();
// format exclusion String to regex
list.forEach(exclusion ->
regexList.add(
exclusion.replace("*", ".*").replace("..*", ".*").
replace("?", ".?").replace("..?", ".?")
)
);
LOGGER.info("regex list is {}", regexList);
return regexList;
}
public AttributeInsertionReport checkAttribute(final Map.Entry<InsertionModes, InsertionStatus> attribute, final int safetyPeriod) {
String attributeName = attribute.getKey().getAttributeName();
AttributeInsertionReport report = new AttributeInsertionReport();
......
......@@ -2,6 +2,7 @@ package org.tango.archiving.server.archiver;
import fr.esrf.Tango.DevFailed;
import fr.esrf.Tango.DispLevel;
import fr.esrf.TangoApi.ApiUtil;
import fr.esrf.TangoApi.DeviceData;
import fr.esrf.TangoApi.Group.Group;
......@@ -39,12 +40,15 @@ import org.tango.server.device.DeviceManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
@Device(transactionType = TransactionType.NONE)
public class ArchiversMonitor {
......@@ -95,8 +99,12 @@ public class ArchiversMonitor {
private ScheduledFuture<?> future;
@DeviceProperty(description = "List of attributes to exclude, can contain wildcar *")
private String[] excludedAttributes = new String[]{};
private List<String> excludedAttributesRegex;
@DeviceProperty(description = "List of attributes to select, can contain wildcar *")
private String[] selectedAttributes = new String[]{};
private List<String> selectedAttributesRegex;
private boolean doFilterAttributes = false;
public static void main(final String[] args) {
VERSION = ResourceBundle.getBundle("application").getString("project.version");
......@@ -117,10 +125,28 @@ public class ArchiversMonitor {
public void setExcludedAttributes(final String[] excludedAttributes) {
this.excludedAttributes = excludedAttributes;
excludedAttributesRegex = getRegexList(excludedAttributes);
doFilterAttributes = true;
logger.info("will exclude attributes with regex = {}", selectedAttributesRegex);
}
private List<String> getRegexList(String[] array) {
List<String> regexList = new ArrayList<>();
// format exclusion String to regex
Arrays.stream(array).forEach(exclusion ->
regexList.add(
exclusion.trim().replace("*", ".*").replace("..*", ".*").
replace("?", ".?").replace("..?", ".?")
)
);
return regexList;
}
public void setSelectedAttributes(final String[] selectedAttributes) {
this.selectedAttributes = selectedAttributes;
selectedAttributesRegex = getRegexList(selectedAttributes);
logger.info("will select attributes with regex = {}", selectedAttributesRegex);
doFilterAttributes = true;
}
public void setPauseBetweenAttributes(final int pauseBetweenAttributes) {
......@@ -270,7 +296,7 @@ public class ArchiversMonitor {
}
databaseAccess.connectDatabase(parameters);
MonitoringAgent agent = new MonitoringAgent(databaseAccess);
monitoringProcess = new MonitoringProcess(agent, Arrays.asList(excludedAttributes), Arrays.asList(selectedAttributes),
monitoringProcess = new MonitoringProcess(agent, excludedAttributesRegex, selectedAttributesRegex,
errorMarginPeriod * 60000, pauseBetweenAttributes);
executor = Executors.newScheduledThreadPool(1);
// start database monitoring
......@@ -376,7 +402,7 @@ public class ArchiversMonitor {
return listeners.getReport();
}
@Attribute
@Attribute(displayLevel = DispLevel._EXPERT)
@AttributeProperties(description = "Errors found while reading report value")
public String[][] getArchiversErrorReport() {
return listeners.getReportError();
......@@ -385,14 +411,14 @@ public class ArchiversMonitor {
@Attribute
@AttributeProperties(description = "List archiving attributes in error")
public String[][] getArchiversErrorList() throws DevFailed {
List<String[]> result = new ArrayList<>();
Set<String[]> result = new LinkedHashSet<>();
result.add(new String[]{"Device name", "Attribute in error name", "Error message"});
getArchiverErrors("GetErrorMessages", result);
getArchiverErrors("GetInsertionErrorMessages", result);
getArchiverErrors("GetErrorMessages", result);
return result.toArray(new String[][]{});
}
private void getArchiverErrors(String cmdName, List<String[]> result) throws DevFailed {
private void getArchiverErrors(String cmdName, Set<String[]> result) throws DevFailed {
GroupCmdReplyList groupResult = group.command_inout(cmdName, true);
final Enumeration<?> tmpReplyEnumeration = groupResult.elements();
while (tmpReplyEnumeration.hasMoreElements()) {
......@@ -401,8 +427,33 @@ public class ArchiversMonitor {
final DeviceData tmpDeviceData = tmpReply.get_data();
for (String error : tmpDeviceData.extractStringArray()) {
String[] split = error.split(":");
result.add(new String[]{tmpReply.dev_name(), split[0],
String.join(":", Arrays.copyOfRange(split, 1, split.length))});
String attributeName = split[0].trim();
String errorMsg = String.join(":", Arrays.copyOfRange(split, 1, split.length));
if (doFilterAttributes) {
if (!selectedAttributesRegex.isEmpty()) {
for (String selection : selectedAttributesRegex) {
if (Pattern.compile(selection, Pattern.CASE_INSENSITIVE).matcher(attributeName).matches()) {
result.add(new String[]{tmpReply.dev_name(), attributeName, errorMsg});
break;
}
}
} else if (!excludedAttributesRegex.isEmpty()) {
boolean doRemove = false;
for (String exclusion : excludedAttributesRegex) {
// Check if the attribute name matches the exclusion pattern
if (Pattern.compile(exclusion, Pattern.CASE_INSENSITIVE).matcher(attributeName).matches()) {
logger.debug("excluding attribute {}", attributeName);
doRemove = true;
}
}
// Add to result if not marked for removal
if (!doRemove) {
result.add(new String[]{tmpReply.dev_name(), attributeName, errorMsg});
}
}
} else {
result.add(new String[]{tmpReply.dev_name(), attributeName, errorMsg});
}
}
} catch (final DevFailed e) {
result.add(new String[]{tmpReply.dev_name(), "device not available", e.toString()});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment