package org.skywalking.apm.collector.ui.service;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.List;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
import org.skywalking.apm.collector.ui.cache.ServiceNameCache;
import org.skywalking.apm.collector.ui.dao.ISegmentDAO;
import org.skywalking.apm.network.proto.KeyWithStringValue;
import org.skywalking.apm.network.proto.LogMessage;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.network.proto.TraceSegmentObject;

/**
 * @author pengys5
 */
public class SpanService {

    public JsonObject load(String segmentId, int spanId) {
        ISegmentDAO segmentDAO = (ISegmentDAO)DAOContainer.INSTANCE.get(ISegmentDAO.class.getName());
        TraceSegmentObject segmentObject = segmentDAO.load(segmentId);

        JsonObject spanJson = new JsonObject();
        List<SpanObject> spans = segmentObject.getSpansList();
        for (SpanObject spanObject : spans) {
            if (spanId == spanObject.getSpanId()) {
                String operationName = spanObject.getOperationName();
                if (spanObject.getOperationNameId() != 0) {
                    String serviceName = ServiceNameCache.get(spanObject.getOperationNameId());
                    if (StringUtils.isNotEmpty(serviceName)) {
                        operationName = serviceName.split(Const.ID_SPLIT)[1];
                    } else {
                        operationName = Const.EMPTY_STRING;
                    }
                }
                spanJson.addProperty("operationName", operationName);
                spanJson.addProperty("startTime", spanObject.getStartTime());
                spanJson.addProperty("endTime", spanObject.getEndTime());

                JsonArray logsArray = new JsonArray();
                List<LogMessage> logs = spanObject.getLogsList();
                for (LogMessage logMessage : logs) {
                    JsonObject logJson = new JsonObject();
                    logJson.addProperty("time", logMessage.getTime());

                    JsonArray logInfoArray = new JsonArray();
                    for (KeyWithStringValue value : logMessage.getDataList()) {
                        JsonObject valueJson = new JsonObject();
                        valueJson.addProperty("key", value.getKey());
                        valueJson.addProperty("value", value.getValue());
                        logInfoArray.add(valueJson);
                    }
                    logJson.add("logInfo", logInfoArray);
                    logsArray.add(logJson);
                }
                spanJson.add("logMessage", logsArray);

                JsonArray tagsArray = new JsonArray();
                for (KeyWithStringValue tagValue : spanObject.getTagsList()) {
                    JsonObject tagJson = new JsonObject();
                    tagJson.addProperty("key", tagValue.getKey());
                    tagJson.addProperty("value", tagValue.getValue());
                    tagsArray.add(tagJson);
                }
                spanJson.add("tags", tagsArray);
            }
        }

        return spanJson;
    }
}
@@ -309,16 +309,79 @@
 tainer;%0A
+import org.skywalking.apm.collector.ui.cache.ApplicationCache;%0A
 import o
@@ -431,24 +431,24 @@
 eNameCache;%0A
-
 import org.s
@@ -714,16 +714,84 @@
 tObject;
+%0Aimport org.skywalking.apm.network.trace.component.ComponentsDefine;
 %0A%0A/**%0A *
@@ -1652,97 +1652,8 @@
 1%5D;%0A
-                    %7D else %7B%0A                        operationName = Const.EMPTY_STRING;%0A
     
@@ -2865,32 +2865,1192 @@
 ew JsonArray();%0A
+%0A                JsonObject spanTypeJson = new JsonObject();%0A                spanTypeJson.addProperty(%22key%22, %22span type%22);%0A                spanTypeJson.addProperty(%22value%22, spanObject.getSpanType().name());%0A                tagsArray.add(spanTypeJson);%0A%0A                JsonObject componentJson = new JsonObject();%0A                componentJson.addProperty(%22key%22, %22component%22);%0A                if (spanObject.getComponentId() == 0) %7B%0A                    componentJson.addProperty(%22value%22, spanObject.getComponent());%0A                %7D else %7B%0A                    componentJson.addProperty(%22value%22, ComponentsDefine.getInstance().getComponentName(spanObject.getComponentId()));%0A                %7D%0A                tagsArray.add(componentJson);%0A%0A                JsonObject peerJson = new JsonObject();%0A                peerJson.addProperty(%22key%22, %22peer%22);%0A                if (spanObject.getPeerId() == 0) %7B%0A                    peerJson.addProperty(%22value%22, spanObject.getPeer());%0A                %7D else %7B%0A                    peerJson.addProperty(%22value%22, ApplicationCache.getForUI(spanObject.getPeerId()));%0A                %7D%0A                tagsArray.add(peerJson);%0A%0A
                 
@@ -4345,32 +4345,32 @@
 y.add(tagJson);%0A
-
                 
@@ -4363,32 +4363,272 @@
               %7D%0A
+%0A                JsonObject isErrorJson = new JsonObject();%0A                isErrorJson.addProperty(%22key%22, %22is error%22);%0A                isErrorJson.addProperty(%22value%22, spanObject.getIsError());%0A                tagsArray.add(isErrorJson);%0A%0A
                 

Add some property into tag array.