Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Provides a tool for generating graphics that show the state machine execution path #1997

Merged
merged 2 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@
*/
package io.seata.saga.statelang.parser.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import io.seata.common.exception.FrameworkErrorCode;
import io.seata.common.exception.FrameworkException;
import io.seata.saga.statelang.domain.ExecutionStatus;
import io.seata.saga.statelang.domain.StateInstance;
import io.seata.saga.statelang.domain.StateMachineInstance;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -211,4 +220,81 @@ private static boolean isBordersCoincided(int xyA, int xyB, int lengthA, int len
int centerPointLength = xyA > xyB ? xyA - xyB : xyB - xyA;
return ((lengthA + lengthB) / 2) > centerPointLength;
}

/**
* Generate tracing graph json
* @param stateMachineInstance
* @return
*/
public static String generateTracingGraphJson(StateMachineInstance stateMachineInstance) {

if (stateMachineInstance == null) {
throw new FrameworkException("StateMachineInstance is not exits",
FrameworkErrorCode.StateMachineInstanceNotExists);
}
String stateMachineJson = stateMachineInstance.getStateMachine().getContent();
if (StringUtils.isEmpty(stateMachineJson)) {
throw new FrameworkException("Cannot get StateMachine Json",
FrameworkErrorCode.ObjectNotExists);
}
JSONObject stateMachineJsonObj = JSON.parseObject(stateMachineJson, Feature.IgnoreAutoType, Feature.OrderedField);
if (!DesignerJsonTransformer.isDesignerJson(stateMachineJsonObj)) {
throw new FrameworkException("StateMachine Json is not generated by Designer",
FrameworkErrorCode.InvalidConfiguration);
}
Map<String, List<StateInstance>> stateInstanceMapGroupByName = new HashMap<>(stateMachineInstance.getStateMap().size());
for (String id : stateMachineInstance.getStateMap().keySet()) {
StateInstance stateInstance = stateMachineInstance.getStateMap().get(id);
List<StateInstance> stateInstanceList = stateInstanceMapGroupByName.get(stateInstance.getName());
if (stateInstanceList == null) {
stateInstanceList = new ArrayList<>();
stateInstanceMapGroupByName.put(stateInstance.getName(), stateInstanceList);
}
stateInstanceList.add(stateInstance);
}
JSONArray nodesArray = stateMachineJsonObj.getJSONArray("nodes");
for (Object nodeObj : nodesArray) {
JSONObject node = (JSONObject) nodeObj;
String stateId = node.getString("stateId");
String stateType = node.getString("stateType");
if ("ServiceTask".equals(stateType)
|| "SubStateMachine".equals(stateType)
|| "Compensation".equals(stateType)) {
node.remove("color");
}
List<StateInstance> stateInstanceList = stateInstanceMapGroupByName.get(stateId);
if (stateInstanceList != null && stateInstanceList.size() > 0) {

StateInstance stateInstance = null;
if (stateInstanceList.size() == 1) {
stateInstance = stateInstanceList.get(0);
} else {
//find out latest stateInstance
for (StateInstance stateInst : stateInstanceList) {

if (stateInstance == null
|| stateInst.getGmtStarted().after(stateInstance.getGmtStarted())) {
stateInstance = stateInst;
}
}
}
node.put("stateInstanceId", stateInstance.getId());
node.put("stateInstanceStatus", stateInstance.getStatus());
if (ExecutionStatus.SU.equals(stateInstance.getStatus())) {
node.put("color", "green");
JSONObject style = new JSONObject();
style.put("fill", "#00D73E");
style.put("lineWidth", 2);
node.put("style", style);
} else {
node.put("color", "red");
JSONObject style = new JSONObject();
style.put("fill", "#FF7777");
style.put("lineWidth", 2);
node.put("style", style);
}
}
}
return stateMachineJsonObj.toString(SerializerFeature.PrettyFormat);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.seata.saga.statelang.domain.DomainConstants;
import io.seata.saga.statelang.domain.ExecutionStatus;
import io.seata.saga.statelang.domain.StateMachineInstance;
import io.seata.saga.statelang.parser.utils.DesignerJsonTransformer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -316,6 +317,10 @@ public void testForwardSubStateMachineWithLayout() throws Exception {

Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus()));

String graphJson = DesignerJsonTransformer.generateTracingGraphJson(inst);
Assertions.assertNotNull(graphJson);
System.out.println(graphJson);

start = System.currentTimeMillis();

paramMap.put("fooThrowException", "false");
Expand All @@ -325,6 +330,10 @@ public void testForwardSubStateMachineWithLayout() throws Exception {
System.out.println("====== XID: " + inst.getId() + " cost :" + cost);

Assertions.assertTrue(ExecutionStatus.SU.equals(inst.getStatus()));

String graphJson2 = DesignerJsonTransformer.generateTracingGraphJson(inst);
Assertions.assertNotNull(graphJson2);
System.out.println(graphJson2);
}

@Test
Expand Down Expand Up @@ -375,6 +384,10 @@ public void testCompensateSubStateMachineWithLayout() throws Exception {

Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus()));

String graphJson = DesignerJsonTransformer.generateTracingGraphJson(inst);
Assertions.assertNotNull(graphJson);
System.out.println(graphJson);

start = System.currentTimeMillis();

inst = stateMachineEngine.compensate(inst.getId(), paramMap);
Expand All @@ -383,6 +396,10 @@ public void testCompensateSubStateMachineWithLayout() throws Exception {
System.out.println("====== XID: " + inst.getId() + " cost :" + cost);

Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getCompensationStatus()));

String graphJson2 = DesignerJsonTransformer.generateTracingGraphJson(inst);
Assertions.assertNotNull(graphJson2);
System.out.println(graphJson2);
}

@Test
Expand Down