Skip to content

Commit 721b7ad

Browse files
committed
better parser for log line accepting values in double quotes or apostrophes
1 parent a2faa7f commit 721b7ad

File tree

1 file changed

+36
-1
lines changed
  • metaacct/src/main/java/cz/cesnet/meta/accounting/server/util

1 file changed

+36
-1
lines changed

metaacct/src/main/java/cz/cesnet/meta/accounting/server/util/PBSReader.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.Arrays;
1616
import java.util.Date;
1717
import java.util.List;
18+
import java.util.regex.Matcher;
19+
import java.util.regex.Pattern;
1820

1921
public class PBSReader {
2022

@@ -38,6 +40,9 @@ public static List<PBSRecord> readPBSFile(InputStream is, Date limitForStartedJo
3840
continue;
3941
}
4042
PBSRecord record = new PBSRecord();
43+
/*
44+
Formát řádku logu je timestamp;type;job_id;message následující kód tedy parsuje tyto 4 části.
45+
*/
4146
String[] entryParts = line.split(";");
4247

4348
switch (entryParts[1]) {
@@ -76,7 +81,11 @@ public static List<PBSRecord> readPBSFile(InputStream is, Date limitForStartedJo
7681

7782
record.setIdString(entryParts[2]);
7883

79-
String[] messageParts = entryParts[3].split(" ");
84+
/*
85+
Formát message je definován jako položky oddělené mezerou, pokud hodnota obsahuje mezeru, je v uvozovkách.
86+
Ale prakticky se objevuje i hodnota v apostrofech obsahující JSON včetně uvozovek a mezer.
87+
*/
88+
String[] messageParts = parseMessageParts(entryParts[3]);
8089
PBSMessage pbsMessage = new PBSMessage();
8190

8291

@@ -260,4 +269,30 @@ private static void applyFixForTorque(PBSMessage pbsMessage, int usedCPUs) {
260269
private static String substringAfter(String s,char ch) {
261270
return s.substring(s.indexOf(ch) + 1);
262271
}
272+
273+
// Vegenerováno Gemini - Regex logika:
274+
// 1. ([^=\s]+) -> Key
275+
// 2. '([^']*)' -> Value v apostrofech (bere vše, včetně " a mezer, vhodné pro JSON)
276+
// 3. "([^"]*)" -> Value v uvozovkách
277+
// 4. ([^\s"']+) -> Value bez oddělovačů (nesmí obsahovat ' ani ")
278+
private static final Pattern PATTERN = Pattern.compile("([^=\\s]+)=(?:'([^']*)'|\"([^\"]*)\"|([^\\s\"']+))");
279+
280+
private static String[] parseMessageParts(String line) {
281+
List<String> list = new ArrayList<>();
282+
Matcher matcher = PATTERN.matcher(line);
283+
284+
while (matcher.find()) {
285+
String key = matcher.group(1); // Key
286+
String value;
287+
if (matcher.group(2) != null) {
288+
value = matcher.group(2); // Single quoted (JSON)
289+
} else if (matcher.group(3) != null) {
290+
value = matcher.group(3); // Double quoted
291+
} else {
292+
value = matcher.group(4); // Unquoted
293+
}
294+
list.add(key+"="+value);
295+
}
296+
return list.toArray(new String[0]);
297+
}
263298
}

0 commit comments

Comments
 (0)