public static String generateTracingGraphJson()

in saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java [217:286]


    public static String generateTracingGraphJson(StateMachineInstance stateMachineInstance, JsonParser jsonParser) {

        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);
        }

        Map<String, Object> stateMachineJsonObj = jsonParser.parse(stateMachineJson, Map.class, true);
        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 (StateInstance stateInstance : stateMachineInstance.getStateMap().values()) {
            CollectionUtils.computeIfAbsent(stateInstanceMapGroupByName, stateInstance.getName(), key -> new ArrayList<>())
                    .add(stateInstance);
        }
        List<Object> nodesArray = (List<Object>) stateMachineJsonObj.get("nodes");
        for (Object nodeObj : nodesArray) {
            Map<String, Object> node = (Map<String, Object>) nodeObj;
            String stateId = (String) node.get("stateId");
            String stateType = (String) node.get("stateType");
            if ("ServiceTask".equals(stateType)
                    || "SubStateMachine".equals(stateType)
                    || "Compensation".equals(stateType)) {
                node.remove("color");
            }
            List<StateInstance> stateInstanceList = stateInstanceMapGroupByName.get(stateId);
            if (CollectionUtils.isNotEmpty(stateInstanceList)) {
                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");
                    Map<String, Object> style = new LinkedHashMap<>();
                    style.put("fill", "#00D73E");
                    style.put("lineWidth", 2);
                    node.put("style", style);
                } else {
                    node.put("color", "red");
                    Map<String, Object> style = new LinkedHashMap<>();
                    style.put("fill", "#FF7777");
                    style.put("lineWidth", 2);
                    node.put("style", style);
                }
            }
        }

        if (stateMachineJsonObj != null) { /*lgtm[java/useless-null-check]*/
            return jsonParser.toJsonString(stateMachineJsonObj, true);
        }
        return "";
    }