Skip to content

Commit

Permalink
Feat timeignore (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
coryhh authored Jul 19, 2023
1 parent d7034bf commit c4c9a9e
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 54 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/maven-core-publish.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will build a package using Maven and then publish it to Apache Maven Central
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#Publishing-using-Apache-Maven

name: Maven Package
name: Maven Core Package

on:
workflow_dispatch:
Expand Down Expand Up @@ -30,13 +30,6 @@ jobs:
- name: Build with Maven
run: mvn -B -ntp clean package -DskipTests=true -Pjar

- name: Publish parent to Apache Maven Central
run: mvn deploy -N -DskipTests=true
env:
MAVEN_USERNAME: ${{ secrets.MAVEN_OSSRH_USERNAME }}
MAVEN_CENTRAL_TOKEN: ${{ secrets.MAVEN_OSSRH_TOKEN }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}

- name: Publish Core to Apache Maven Central
run: mvn deploy -DskipTests=true -pl arex-compare-core -Pjar
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/maven-extension-publish.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will build a package using Maven and then publish it to Apache Maven Central
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#Publishing-using-Apache-Maven

name: Maven Package
name: Maven Extension Package

on:
workflow_dispatch:
Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/maven-parent-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This workflow will build a package using Maven and then publish it to Apache Maven Central
# For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#Publishing-using-Apache-Maven

name: Maven Parent Package

on:
workflow_dispatch:

jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml
server-username: MAVEN_USERNAME # env variable for username in deploy
server-password: MAVEN_CENTRAL_TOKEN # env variable for token in deploy
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import
gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase

- name: Build with Maven
run: mvn -B -ntp clean package -DskipTests=true -Pjar

- name: Publish parent to Apache Maven Central
run: mvn deploy -N -DskipTests=true
env:
MAVEN_USERNAME: ${{ secrets.MAVEN_OSSRH_USERNAME }}
MAVEN_CENTRAL_TOKEN: ${{ secrets.MAVEN_OSSRH_TOKEN }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
2 changes: 1 addition & 1 deletion arex-compare-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>arex-compare-parent</artifactId>
<groupId>com.arextest</groupId>
<version>0.1.21</version>
<version>0.1.22</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.arextest.diff.model.log.LogEntity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.function.Predicate;

/**
Expand All @@ -14,24 +13,29 @@
public class TimePrecisionFilter implements Predicate<LogEntity> {

private static AbstractDataProcessor dataProcessor;

private static SimpleDateFormat parseFormat1
= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
private static SimpleDateFormat parseFormat2
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
private static SimpleDateFormat parseFormat3
= new SimpleDateFormat("HH:mm:ss.SSS");
private static DateTimeFormatter parseFormat1 = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd")
.optionalStart().appendLiteral(' ').optionalEnd()
.appendOptional(DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSS"))
.appendOptional(DateTimeFormatter.ofPattern("HH:mm:ss.SSS"))
.toFormatter();
private static DateTimeFormatter parseFormat2 = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSS"))
.appendOptional(DateTimeFormatter.ofPattern("HH:mm:ss.SSS"))
.toFormatter();
private static DateTimeFormatter parseFormat3 = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd")
.optionalStart().appendLiteral('T').optionalEnd()
.optionalStart().appendLiteral(' ').optionalEnd()
.appendOptional(DateTimeFormatter.ofPattern("HH:mm:ss.SSSXXX"))
.appendOptional(DateTimeFormatter.ofPattern("HH:mm:ss.SSSZ"))
.toFormatter();

private long ignoredTimePrecision;


static {
parseFormat1.setTimeZone(TimeZone.getTimeZone("GMT+8"));
parseFormat2.setTimeZone(TimeZone.getTimeZone("GMT+8"));
parseFormat3.setTimeZone(TimeZone.getTimeZone("GMT+8"));
parseFormat1.setLenient(false);
parseFormat2.setLenient(false);
parseFormat3.setLenient(false);
parseFormat1 = parseFormat1.withZone(ZoneId.of("UTC"));

dataProcessor = new ProcessorChainBuilder()
.addProcessor(new FirstDataProcessor())
Expand Down Expand Up @@ -68,13 +72,13 @@ public boolean test(LogEntity logEntity) {

if ((baseStr.startsWith("0") || baseStr.startsWith("1") || baseStr.startsWith("2")) &&
(testStr.startsWith("0") || testStr.startsWith("1") || testStr.startsWith("2"))) {
Date baseTime = dataProcessor.process(baseStr);
Date testTime = dataProcessor.process(testStr);
Instant baseTime = dataProcessor.process(baseStr);
Instant testTime = dataProcessor.process(testStr);
if (baseTime == null || testTime == null) {
return true;
}

long durationMillis = baseTime.getTime() - testTime.getTime();
long durationMillis = baseTime.toEpochMilli() - testTime.toEpochMilli();
if (Math.abs(durationMillis) <= ignoredTimePrecision) {
return false;
}
Expand All @@ -90,8 +94,8 @@ public void setNextProcessor(AbstractDataProcessor nextProcessor) {
this.nextProcessor = nextProcessor;
}

public Date process(String data) {
Date date = processData(data);
public Instant process(String data) {
Instant date = processData(data);
if (date != null) {
return date;
}
Expand All @@ -101,47 +105,52 @@ public Date process(String data) {
return this.nextProcessor.process(data);
}

protected abstract Date processData(String data);
protected abstract Instant processData(String data);
}

public static class FirstDataProcessor extends AbstractDataProcessor {

@Override
protected Date processData(String data) {
protected Instant processData(String data) {

Date time = null;
Instant instant = null;
try {
time = parseFormat1.parse(data);
} catch (ParseException e) {
ZonedDateTime zdt = ZonedDateTime.parse(data, parseFormat1);
instant = zdt.toInstant();
} catch (Exception e) {
}
return time;
return instant;
}
}

public static class SecondDataProcessor extends AbstractDataProcessor {

@Override
protected Date processData(String data) {
Date time = null;
protected Instant processData(String data) {
Instant instant = null;
try {
time = parseFormat2.parse(data);
} catch (ParseException e) {
LocalTime time = LocalTime.parse(data, parseFormat2);
LocalDate date = LocalDate.ofEpochDay(0);
LocalDateTime dateTime = LocalDateTime.of(date, time);
instant = dateTime.toInstant(ZoneOffset.UTC);
} catch (Exception e) {
}
return time;
return instant;

}
}

public static class ThirdDataProcessor extends AbstractDataProcessor {

@Override
protected Date processData(String data) {
Date time = null;
protected Instant processData(String data) {
Instant instant = null;
try {
time = parseFormat3.parse(data);
} catch (ParseException e) {
ZonedDateTime zdt = ZonedDateTime.parse(data, parseFormat3);
instant = zdt.toInstant();
} catch (Exception e) {
}
return time;

return instant;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public CompareResult jsonCompare(RulesConfig rulesConfig) {
.message("compare successfully")
.msgInfo(baseMsg, testMsg)
.logs(logs)
.processedBaseMsg(processedBaseMsg)
.processedTestMsg(processedTestMsg)
.processedBaseMsg(rulesConfig.isQuickCompare() ? baseMsg : processedBaseMsg)
.processedTestMsg(rulesConfig.isQuickCompare() ? testMsg : processedTestMsg)
.parseNodePaths(parsePaths)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public CompareResult jsonCompare(RulesConfig rulesConfig) {
.message("compare successfully")
.msgInfo(baseMsg, testMsg)
.logs(logs)
.processedBaseMsg(processedBaseMsg)
.processedTestMsg(processedTestMsg)
.processedBaseMsg(rulesConfig.isQuickCompare() ? baseMsg : processedBaseMsg)
.processedTestMsg(rulesConfig.isQuickCompare() ? testMsg : processedTestMsg)
.parseNodePaths(parsePaths)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,32 @@ public void testPrefixFilter() {
CompareResult result = sdk.compare(baseMsg, testMsg);
Assert.assertEquals(result.getLogs().size(), 0);
}

@Test
public void testYearAndNsTimeIgnore() {
CompareSDK sdk = new CompareSDK();
sdk.getGlobalOptions().putNameToLower(true).putNullEqualsEmpty(true);
String baseMsg = "{\"time\":\"2023-06-08 23:30:00.000\"}";
String testMsg = "{\"time\":\"2023-06-08 23:30:00.999999\"}";

CompareOptions compareOptions = CompareOptions.options();
compareOptions.putIgnoredTimePrecision(1000L);

CompareResult result = sdk.compare(baseMsg, testMsg, compareOptions);
Assert.assertEquals(result.getLogs().size(), 0);
}

@Test
public void testNoYearAndNsTimeIgnore() {
CompareSDK sdk = new CompareSDK();
sdk.getGlobalOptions().putNameToLower(true).putNullEqualsEmpty(true);
String baseMsg = "{\"time\":\"2023-06-08 23:30:00.000+08:00\"}";
String testMsg = "{\"time\":\"2023-06-08T23:30:00.100Z\"}";

CompareOptions compareOptions = CompareOptions.options();
compareOptions.putIgnoredTimePrecision(1000L);

CompareResult result = sdk.compare(baseMsg, testMsg, compareOptions);
Assert.assertEquals(result.getLogs().size(), 1);
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.arextest</groupId>
<artifactId>arex-compare-parent</artifactId>
<packaging>pom</packaging>
<version>0.1.21</version>
<version>0.1.22</version>
<modules>
<module>arex-compare-extension</module>
<module>arex-compare-core</module>
Expand Down

0 comments on commit c4c9a9e

Please sign in to comment.