Skip to content

Commit

Permalink
Webex reporter and sonar fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Dutoit committed Mar 15, 2024
1 parent 55b6c7b commit cfdf397
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 65 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.16</version>
<version>2.7.18</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/ch/mno/copper/collect/collectors/WebCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.gson.JsonArray;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONArray;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
Expand All @@ -22,10 +23,9 @@
/**
* Created by dutoitc on 29.01.2016.
*/
@Slf4j
public class WebCollector {

private static final Logger LOG = LoggerFactory.getLogger(WebCollector.class);

@SuppressWarnings("java:S2589")
public static List<String> query(String url, String username, String password, List<Pair<String, String>> valuesKept) {
String host;
Expand All @@ -47,9 +47,9 @@ public static List<String> query(String url, String username, String password, L

results = extractValues(data, valuesKept);
} catch (Exception e) {
LOG.error("Connector exception (server {}): {}", url, e.getMessage());
if (LOG.isTraceEnabled()) {
e.printStackTrace();
log.error("Connector exception (server {}): {}", url, e.getMessage());
if (log.isTraceEnabled()) {
log.trace("Connector error: " + e.getMessage(), e);
}
if (results == null) {
results = new ArrayList<>(valuesKept.size());
Expand Down Expand Up @@ -101,7 +101,7 @@ private static void addOtherToResult(HttpResponseData<String> data, List<String>
if (o instanceof JSONArray) {
var res = (JSONArray) o;
if (res.isEmpty()) {
LOG.info("Warning: jsonpath {} not found in {}", key, data);
log.info("Warning: jsonpath {} not found in {}", key, data);
results.add("ERR_NOT_FOUND");
} else if (res.size() > 1) {
results.add("TOO_MUCH_VALUES_FOUND");
Expand All @@ -116,7 +116,7 @@ private static void addOtherToResult(HttpResponseData<String> data, List<String>
results.add(o.toString());
}
} catch (PathNotFoundException e) {
LOG.error("JsonPath not found: {}", key);
log.error("JsonPath not found: {}", key);
results.add("?");
}
}
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/ch/mno/copper/collect/connectors/JdbcConnector.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.mno.copper.collect.connectors;


import lombok.extern.slf4j.Slf4j;
import oracle.jdbc.OracleDriver;

import java.sql.*;
Expand All @@ -10,6 +11,7 @@
/**
* Created by dutoitc on 31.01.2016.
*/
@Slf4j
public class JdbcConnector implements AutoCloseable {

private Connection connection;
Expand Down Expand Up @@ -71,17 +73,7 @@ public void close() throws Exception {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws ConnectorException {
try (
JdbcConnector conn = new JdbcConnector("jdbc:oracle:thin:@my-defcon:1521:myinstance", "auser", "apass")
) {
conn.query("select 1 from dual");
} catch (Exception e) {
e.printStackTrace();
log.debug("Close error: " + e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void close() {
jmxc.close();
}
} catch (IOException e) {
e.printStackTrace();
log.debug("Close error: " + e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.mno.copper.collect.connectors;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -9,9 +10,9 @@
/**
* Created by dutoitc on 30.01.2016.
*/
@Slf4j
public class SocketConnector extends AbstractConnector {

private static final Logger LOG = LoggerFactory.getLogger(SocketConnector.class);

private String host;
private int port;
Expand Down Expand Up @@ -49,7 +50,7 @@ public CONNECTION_CHECK checkConnection() {
return CONNECTION_CHECK.UNKNOWN_HOST;
} catch (IOException e) {
lastException = e;
LOG.error("IOException connecting to {}: {}, resolved to {}", host, port, (inteAddress==null?"null":inteAddress.getHostName()));
log.error("IOException connecting to {}: {}, resolved to {}", host, port, (inteAddress==null?"null":inteAddress.getHostName()));
return CONNECTION_CHECK.IO_EXCEPTION;
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/ch/mno/copper/report/CsvReporter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.mno.copper.report;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -8,6 +10,7 @@
/**
* Created by dutoitc on 31.01.2016.
*/
@Slf4j
public class CsvReporter implements AbstractReporter {

public enum PARAMETERS {FILENAME, HEADERS, LINE}
Expand All @@ -26,15 +29,15 @@ public void report(String message, Map<String, String> values) {
fw.append(line).append("\r\n");
fw.flush();
} catch (IOException e) {
e.printStackTrace();
log.debug("Exception: " + e.getMessage(), e);
}
} else {
try (FileWriter fw = new FileWriter(file)) {
fw.append(header).append("\r\n");
fw.append(line).append("\r\n");
fw.flush();
} catch (IOException e) {
e.printStackTrace();
log.debug("Exception: " + e.getMessage(), e);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/ch/mno/copper/report/MailReporterWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ch.mno.copper.helpers.SyntaxHelper;
import ch.mno.copper.store.ValuesStore;
import ch.mno.copper.stories.data.StoryGrammar;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -13,6 +14,7 @@
/**
* Created by dutoitc on 07.02.2016.
*/
@Slf4j
public class MailReporterWrapper implements AbstractReporterWrapper {

private String dest;
Expand Down Expand Up @@ -59,7 +61,7 @@ public void execute(Map<String, String> values, ValuesStore valuesStore) {
try {
reporter.report(message2, reporterValues);
} catch (ConnectorException e) {
e.printStackTrace();
log.debug("Exception: " + e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.mno.copper.collect.connectors.ConnectorException;
import ch.mno.copper.store.ValuesStore;
import ch.mno.copper.stories.data.StoryGrammar;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -12,6 +13,7 @@
/**
* Created by dutoitc on 07.02.2016.
*/
@Slf4j
public class PushoverReporterWrapper implements AbstractReporterWrapper {

private String applicationToken;
Expand Down Expand Up @@ -53,7 +55,7 @@ public void execute(Map<String, String> values, ValuesStore valuesStore) {
try {
reporter.report(message, reporterValues);
} catch (ConnectorException e) {
e.printStackTrace();
log.debug("Exception: " + e.getMessage(), e);
}
}

Expand Down
27 changes: 16 additions & 11 deletions src/main/java/ch/mno/copper/report/WebexDeltaReporterWrapper.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ch.mno.copper.report;

import ch.mno.copper.CopperException;
import ch.mno.copper.collect.connectors.ConnectorException;
import ch.mno.copper.store.ValuesStore;
import ch.mno.copper.stories.data.StoryGrammar;
import lombok.extern.slf4j.Slf4j;

import java.time.Instant;
import java.util.HashMap;
Expand All @@ -11,13 +13,14 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Slf4j
public class WebexDeltaReporterWrapper implements AbstractReporterWrapper {

private int delta;
private String token;
private String room_id;
private String keyFilter;
private String messageTemplate;
private final int delta;
private final String token;
private final String roomId;
private final String keyFilter;
private final String messageTemplate;
private WebexReporter reporter;

public WebexDeltaReporterWrapper(StoryGrammar grammar, String storyGiven) {
Expand All @@ -32,11 +35,11 @@ public WebexDeltaReporterWrapper(StoryGrammar grammar, String storyGiven) {
"+WITH message=\"(.*?)\"";
Matcher matcher = Pattern.compile(pattern, Pattern.DOTALL).matcher(storyGiven);
if (!matcher.find())
throw new RuntimeException("Cannot find a valid WEBEX pattern in " + storyGiven + ", pattern " + pattern);
throw new CopperException("Cannot find a valid WEBEX pattern in " + storyGiven + ", pattern " + pattern);

delta = Integer.parseInt(matcher.group(1));
token = matcher.group(2);
room_id = matcher.group(3);
roomId = matcher.group(3);
keyFilter = matcher.group(4);
messageTemplate = matcher.group(5);
reporter = new WebexReporter();
Expand All @@ -54,7 +57,7 @@ public static AbstractReporterWrapper buildReporter(StoryGrammar grammar, String
public void execute(Map<String, String> values, ValuesStore valuesStore) {
// Find values
var now = Instant.now();
var start = now.minusSeconds(60 * delta);
var start = now.minusSeconds(60L * delta);
var pat = Pattern.compile(keyFilter);
var deltaStr = valuesStore.queryValues(start, now)
.stream()
Expand All @@ -66,17 +69,19 @@ public void execute(Map<String, String> values, ValuesStore valuesStore) {

// Report
if (!deltaStr.isBlank()) {
String message = ReportHelper.expandMessage(values, messageTemplate, valuesStore);
String message = messageTemplate;
message = message.replace("{{STATUS}}", deltaStr);
ReportHelper.expandMessage(values, message, valuesStore);


Map<String, String> reporterValues = new HashMap<>();
reporterValues.put(WebexReporter.PARAMETERS.TOKEN.toString(), token);
reporterValues.put(WebexReporter.PARAMETERS.ROOM_ID.toString(), room_id);
reporterValues.put(WebexReporter.PARAMETERS.ROOM_ID.toString(), roomId);

try {
reporter.report(message, reporterValues);
} catch (ConnectorException e) {
e.printStackTrace();
log.debug("Exception: " + e.getMessage(), e);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/ch/mno/copper/store/db/DBServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.mno.copper.store.StoreException;
import ch.mno.copper.store.StoreValue;
import ch.mno.copper.store.data.InstantValues;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -25,6 +26,7 @@
* <p>
* Created by dutoitc on 25.05.2016.
*/
@Slf4j
public class DBServer implements AutoCloseable {

public static final Instant INSTANT_MAX = Instant.parse("3000-12-31T00:00:00.00Z");
Expand All @@ -45,7 +47,7 @@ public void clearAllData() {
LOG.info("Deleted {} lines", nbRows);
}
} catch (SQLException e) {
e.printStackTrace();
log.debug("Exception: " + e.getMessage(), e);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/ch/mno/copper/stories/data/StoryGrammar.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.mno.copper.stories.data;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -14,6 +16,7 @@
/**
* Created by dutoitc on 07.02.2016.
*/
@Slf4j
public class StoryGrammar {

public static final String SEPARATOR = "¦";
Expand All @@ -39,7 +42,7 @@ public StoryGrammar(InputStream source) {
}
}
} catch (IOException e) {
e.printStackTrace();
log.debug("Exception: " + e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,6 @@ void test() {
assertEquals("lastReload", valuesKept.get(1).getKey());
assertEquals("WEB_LAST_RELOAD", valuesKept.get(1).getValue());
assertEquals("[status, lastReload]", StringUtils.join(wrapper.getAs()));

/*
// Local wrapper test
try {
Map<String, String> res = wrapper.execute();
String status = res.get("WEB_STATUS");
String lastReload = res.get("WEB_LAST_RELOAD");
System.out.println("Values: " + status + "," + lastReload);
} catch (ConnectorException e) {
e.printStackTrace();
}*/
}


Expand Down
Loading

0 comments on commit cfdf397

Please sign in to comment.