-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #314 from powsybl/dynamic_simulation_logs
Dynamic simulation logs
- Loading branch information
Showing
26 changed files
with
3,791 additions
and
137 deletions.
There are no files selected for viewing
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
80 changes: 80 additions & 0 deletions
80
commons/src/main/java/com/powsybl/dynawo/commons/AbstractCsvParser.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,80 @@ | ||
/** | ||
* Copyright (c) 2023, RTE (http://www.rte-france.com/) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
package com.powsybl.dynawo.commons; | ||
|
||
import com.powsybl.commons.PowsyblException; | ||
import com.univocity.parsers.common.ParsingContext; | ||
import com.univocity.parsers.common.ResultIterator; | ||
import com.univocity.parsers.csv.CsvParser; | ||
import com.univocity.parsers.csv.CsvParserSettings; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.*; | ||
|
||
/** | ||
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>} | ||
*/ | ||
public abstract class AbstractCsvParser<T> { | ||
|
||
protected static final char DEFAULT_SEPARATOR = '|'; | ||
|
||
private final char separator; | ||
|
||
protected AbstractCsvParser(char separator) { | ||
this.separator = separator; | ||
} | ||
|
||
public List<T> parse(Path file) { | ||
if (!Files.exists(file)) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) { | ||
return parse(reader); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
private List<T> parse(BufferedReader reader) { | ||
Objects.requireNonNull(reader); | ||
CsvParserSettings settings = new CsvParserSettings(); | ||
settings.getFormat().setDelimiter(separator); | ||
settings.getFormat().setQuoteEscape('"'); | ||
settings.getFormat().setLineSeparator(System.lineSeparator()); | ||
settings.setMaxColumns(getNbColumns()); | ||
CsvParser csvParser = new CsvParser(settings); | ||
ResultIterator<String[], ParsingContext> iterator = csvParser.iterate(reader).iterator(); | ||
return read(iterator); | ||
} | ||
|
||
protected List<T> read(ResultIterator<String[], ParsingContext> iterator) { | ||
List<T> logs = new ArrayList<>(); | ||
int iLine = 0; | ||
while (iterator.hasNext()) { | ||
iLine++; | ||
String[] tokens = iterator.next(); | ||
if (!hasCorrectNbColumns(tokens.length)) { | ||
throw new PowsyblException("Columns of line " + iLine + " are inconsistent"); | ||
} | ||
createEntry(tokens).ifPresent(logs::add); | ||
} | ||
return logs; | ||
} | ||
|
||
protected abstract Optional<T> createEntry(String[] tokens); | ||
|
||
protected abstract boolean hasCorrectNbColumns(int tokensSize); | ||
|
||
protected abstract int getNbColumns(); | ||
} |
51 changes: 51 additions & 0 deletions
51
commons/src/main/java/com/powsybl/dynawo/commons/CommonReports.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,51 @@ | ||
/** | ||
* Copyright (c) 2023, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
package com.powsybl.dynawo.commons; | ||
|
||
import com.powsybl.commons.reporter.Report; | ||
import com.powsybl.commons.reporter.Reporter; | ||
import com.powsybl.commons.reporter.TypedValue; | ||
import com.powsybl.dynawo.commons.dynawologs.LogEntry; | ||
import com.powsybl.dynawo.commons.timeline.TimelineEntry; | ||
|
||
/** | ||
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>} | ||
*/ | ||
public final class CommonReports { | ||
|
||
private static final String TIME_MS = "timeMsTypedValue"; | ||
private static final String ID = "idTypedValue"; | ||
|
||
private CommonReports() { | ||
} | ||
|
||
public static Reporter createDynawoLogReporter(Reporter reporter) { | ||
return reporter.createSubReporter("dynawoLog", | ||
"Dynawo Log"); | ||
} | ||
|
||
public static void reportTimelineEvent(Reporter reporter, TimelineEntry timelineEntry) { | ||
reporter.report(Report.builder() | ||
.withKey("DynawoTimelineEvent") | ||
.withDefaultMessage("[t=${time}] ${message} on equipment '${identifiableId}'") | ||
.withTypedValue("time", timelineEntry.time(), TIME_MS) | ||
.withTypedValue("identifiableId", timelineEntry.modelName(), ID) | ||
.withValue("message", timelineEntry.message()) | ||
.withSeverity(TypedValue.TRACE_SEVERITY) | ||
.build()); | ||
} | ||
|
||
public static void reportLogEvent(Reporter reporter, LogEntry logEntry) { | ||
reporter.report(Report.builder() | ||
.withKey("DynawoTimelineEvent") | ||
.withDefaultMessage("${message}") | ||
.withValue("message", logEntry.message()) | ||
.withSeverity(logEntry.severity()) | ||
.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
44 changes: 44 additions & 0 deletions
44
commons/src/main/java/com/powsybl/dynawo/commons/dynawologs/CsvLogParser.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,44 @@ | ||
/** | ||
* Copyright (c) 2023, RTE (http://www.rte-france.com/) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
package com.powsybl.dynawo.commons.dynawologs; | ||
|
||
import com.powsybl.dynawo.commons.AbstractCsvParser; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>} | ||
*/ | ||
public final class CsvLogParser extends AbstractCsvParser<LogEntry> { | ||
|
||
private static final int NB_COLUMNS = 4; | ||
|
||
public CsvLogParser() { | ||
this(DEFAULT_SEPARATOR); | ||
} | ||
|
||
public CsvLogParser(char separator) { | ||
super(separator); | ||
} | ||
|
||
@Override | ||
protected Optional<LogEntry> createEntry(String[] tokens) { | ||
return tokens.length == NB_COLUMNS ? LogUtils.createLog(tokens[1], tokens[2] + " " + tokens[3]) | ||
: LogUtils.createLog(tokens[1], tokens[2]); | ||
} | ||
|
||
@Override | ||
protected boolean hasCorrectNbColumns(int tokensSize) { | ||
return tokensSize == NB_COLUMNS - 1 || tokensSize == NB_COLUMNS; | ||
} | ||
|
||
@Override | ||
protected int getNbColumns() { | ||
return NB_COLUMNS; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
commons/src/main/java/com/powsybl/dynawo/commons/dynawologs/LogEntry.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,17 @@ | ||
/** | ||
* Copyright (c) 2023, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
package com.powsybl.dynawo.commons.dynawologs; | ||
|
||
import com.powsybl.commons.reporter.TypedValue; | ||
|
||
/** | ||
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>} | ||
*/ | ||
public record LogEntry(TypedValue severity, String message) { | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
commons/src/main/java/com/powsybl/dynawo/commons/dynawologs/LogUtils.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,57 @@ | ||
/** | ||
* Copyright (c) 2023, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
package com.powsybl.dynawo.commons.dynawologs; | ||
|
||
import com.powsybl.commons.reporter.TypedValue; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>} | ||
*/ | ||
public final class LogUtils { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(LogUtils.class); | ||
private static final String EMPTY_START = "======"; | ||
private static final String EMPTY_BREAKER = "------"; | ||
|
||
private LogUtils() { | ||
} | ||
|
||
public static Optional<LogEntry> createLog(String severity, String message) { | ||
if (severity == null) { | ||
LOGGER.warn("Inconsistent log entry (modelName: '{}', message: '{}')", severity, message); | ||
} else { | ||
if (emptyMessage(message)) { | ||
LOGGER.debug("Empty message, the entry will be skipped : {}", message); | ||
} else { | ||
return convertDynawoLog(severity).map(severityTypedValue -> new LogEntry(severityTypedValue, message)); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private static Optional<TypedValue> convertDynawoLog(String severity) { | ||
return switch (severity) { | ||
case "DEBUG" -> Optional.of(TypedValue.DEBUG_SEVERITY); | ||
case "INFO" -> Optional.of(TypedValue.INFO_SEVERITY); | ||
case "WARN" -> Optional.of(TypedValue.WARN_SEVERITY); | ||
case "ERROR" -> Optional.of(TypedValue.ERROR_SEVERITY); | ||
default -> { | ||
LOGGER.warn("Inconsistent severity entry '{}'", severity); | ||
yield Optional.empty(); | ||
} | ||
}; | ||
} | ||
|
||
private static boolean emptyMessage(String message) { | ||
return message == null || message.startsWith(EMPTY_BREAKER) || message.startsWith(EMPTY_START); | ||
} | ||
} |
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
Oops, something went wrong.