Skip to content

Commit 6879cad

Browse files
authored
Merge pull request #9 from grove693/feature/small-refactorings
Created TransactionType enum + added fluent methods on Transaction object
2 parents 8f3d424 + c37172c commit 6879cad

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/main/java/dev/inspector/agent/model/Transaction.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Transaction extends Context implements Transportable {
1616

1717
private static Logger LOGGER = LoggerFactory.getLogger(Transaction.class);
1818
private String model = "transaction";
19-
private String type = "request";
19+
private TransactionType type;
2020
private String name;
2121
private String hash = System.currentTimeMillis() + "" + (int) (Math.random() * 100);
2222
private User user;
@@ -28,23 +28,38 @@ public class Transaction extends Context implements Transportable {
2828
public Transaction(String name) {
2929
this.name = name;
3030
this.timestamp = TimesUtils.getTimestamp();
31+
type = TransactionType.GENERAL;
3132
}
32-
public void withUser(User user){
33+
public Transaction withUser(User user){
3334
this.user = user;
35+
36+
return this;
37+
}
38+
39+
public Transaction withType(TransactionType type) {
40+
this.type = type;
41+
42+
return this;
3443
}
3544

36-
public void setResult(String result){
45+
public Transaction setResult(String result){
3746
this.result = result;
47+
48+
return this;
3849
}
3950

40-
public void end(){
51+
public Transaction end(){
4152
BigDecimal end = TimesUtils.getTimestamp();
4253
this.end(end.subtract(this.timestamp).multiply(BigDecimal.valueOf(1000)));
54+
55+
return this;
4356
}
4457

45-
public void end(BigDecimal duration){
58+
public Transaction end(BigDecimal duration){
4659
this.durationInMillis = duration.longValue();
4760
this.memoryPeak = this.getMemoryPeak();
61+
62+
return this;
4863
}
4964

5065
public static long getMemoryPeak() {
@@ -75,7 +90,7 @@ public JSONObject toTransport() {
7590
.put("model", this.model)
7691
.put("hash", this.hash)
7792
.put("name", this.name)
78-
.put("type", this.type)
93+
.put("type", this.type.getType())
7994
.put("timestamp", this.timestamp)
8095
.put("duration", this.durationInMillis)
8196
.put("result", this.result)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dev.inspector.agent.model;
2+
3+
public enum TransactionType {
4+
5+
GENERAL("transaction"),
6+
REQUEST("request"),
7+
SCHEDULER("scheduler");
8+
9+
private final String type;
10+
11+
TransactionType(String type) {
12+
this.type = type;
13+
}
14+
15+
public String getType() {
16+
return type;
17+
}
18+
}

0 commit comments

Comments
 (0)