Skip to content

Commit

Permalink
#1402 Homogenize AgentInfo JSON serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Xylus committed Jan 7, 2016
1 parent c51234b commit c544979
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 92 deletions.
Expand Up @@ -193,8 +193,7 @@ public AgentStatus getAgentStatus(String agentId, long timestamp) {
}
AgentLifeCycleBo agentLifeCycleBo = this.agentLifeCycleDao.getAgentLifeCycle(agentId, timestamp);
if (agentLifeCycleBo == null) {
AgentStatus agentStatus = new AgentStatus();
agentStatus.setAgentId(agentId);
AgentStatus agentStatus = new AgentStatus(agentId);
agentStatus.setState(AgentLifeCycleState.UNKNOWN);
return agentStatus;
} else {
Expand Down
@@ -0,0 +1,93 @@
/*
* Copyright 2016 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.web.view;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.common.util.AgentLifeCycleState;
import com.navercorp.pinpoint.web.applicationmap.link.LinkInfo;
import com.navercorp.pinpoint.web.applicationmap.link.MatcherGroup;
import com.navercorp.pinpoint.web.vo.AgentInfo;
import com.navercorp.pinpoint.web.vo.AgentStatus;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;

/**
* @author HyunGil Jeong
*/
public class AgentInfoSerializer extends JsonSerializer<AgentInfo> {

@Autowired(required = false)
private List<MatcherGroup> matcherGroupList;

@Autowired
private ServiceTypeRegistryService serviceTypeRegistryService;

@Override
public void serialize(AgentInfo agentInfo, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeStartObject();

jgen.writeStringField("applicationName", agentInfo.getApplicationName());
jgen.writeStringField("agentId", agentInfo.getAgentId());
jgen.writeNumberField("startTimestamp", agentInfo.getStartTimestamp());
jgen.writeStringField("hostName", agentInfo.getHostName());
jgen.writeStringField("ip", agentInfo.getIp());
jgen.writeStringField("ports", agentInfo.getPorts());
final ServiceType serviceType = serviceTypeRegistryService.findServiceType(agentInfo.getServiceTypeCode());
jgen.writeStringField("serviceType", serviceType.getDesc());
jgen.writeNumberField("pid", agentInfo.getPid());
jgen.writeStringField("vmVersion", agentInfo.getVmVersion());
jgen.writeStringField("agentVersion", agentInfo.getAgentVersion());
jgen.writeObjectField("serverMetaData", agentInfo.getServerMetaData());

AgentStatus status = agentInfo.getStatus();
if (status == null) {
status = new AgentStatus(agentInfo.getAgentId());
status.setState(AgentLifeCycleState.UNKNOWN);
}
jgen.writeObjectField("status", status);

jgen.writeNumberField("initialStartTimestamp", agentInfo.getInitialStartTimestamp());

if (matcherGroupList != null) {
jgen.writeFieldName("linkList");
jgen.writeStartArray();

for (MatcherGroup matcherGroup : matcherGroupList) {
if (matcherGroup.ismatchingType(agentInfo)) {
LinkInfo linkInfo = matcherGroup.makeLinkInfo(agentInfo);
jgen.writeStartObject();
jgen.writeStringField("linkName", linkInfo.getLinkName());
jgen.writeStringField("linkURL", linkInfo.getLinkUrl());
jgen.writeStringField("linkType", linkInfo.getLinktype());
jgen.writeEndObject();
}
}

jgen.writeEndArray();
}

jgen.writeEndObject();
}

}
Expand Up @@ -20,19 +20,11 @@
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.common.util.AgentLifeCycleState;
import com.navercorp.pinpoint.web.applicationmap.link.LinkInfo;
import com.navercorp.pinpoint.web.applicationmap.link.MatcherGroup;
import com.navercorp.pinpoint.web.vo.AgentInfo;
import com.navercorp.pinpoint.web.vo.AgentStatus;
import com.navercorp.pinpoint.web.vo.ApplicationAgentList;

/**
Expand All @@ -41,12 +33,6 @@
*/
public class ApplicationAgentListSerializer extends JsonSerializer<ApplicationAgentList> {

@Autowired(required = false)
private List<MatcherGroup> matcherGroupList;

@Autowired
private ServiceTypeRegistryService serviceTypeRegistryService;

@Override
public void serialize(ApplicationAgentList applicationAgentList, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeStartObject();
Expand All @@ -63,52 +49,7 @@ public void serialize(ApplicationAgentList applicationAgentList, JsonGenerator j
private void writeAgentList(JsonGenerator jgen, List<AgentInfo> agentList) throws IOException {
jgen.writeStartArray();
for (AgentInfo agentInfo : agentList) {
jgen.writeStartObject();
jgen.writeStringField("applicationName", agentInfo.getApplicationName());
jgen.writeStringField("agentId", agentInfo.getAgentId());
jgen.writeNumberField("startTime", agentInfo.getStartTimestamp());
jgen.writeStringField("hostName", agentInfo.getHostName());
jgen.writeStringField("ip", agentInfo.getIp());
jgen.writeStringField("ports", agentInfo.getPorts());

final ServiceType serviceType = serviceTypeRegistryService.findServiceType(agentInfo.getServiceTypeCode());
jgen.writeStringField("serviceType", serviceType.getDesc());
jgen.writeNumberField("pid", agentInfo.getPid());
jgen.writeStringField("vmVersion", agentInfo.getVmVersion());
jgen.writeStringField("agentVersion", agentInfo.getAgentVersion());
jgen.writeObjectField("serverMetaData", agentInfo.getServerMetaData());

AgentStatus agentStatus = agentInfo.getStatus();
if (agentStatus == null) {
jgen.writeNumberField("endTimeStamp", 0);
jgen.writeStringField("endStatus", AgentLifeCycleState.UNKNOWN.getDesc());
} else {
jgen.writeNumberField("endTimeStamp", agentStatus.getEventTimestamp());
jgen.writeStringField("endStatus", agentStatus.getState().getDesc());
}
jgen.writeObjectField("status", agentStatus);

jgen.writeNumberField("initialStartTime", agentInfo.getInitialStartTimestamp());

if (matcherGroupList != null) {
jgen.writeFieldName("linkList");
jgen.writeStartArray();

for (MatcherGroup matcherGroup : matcherGroupList) {
if (matcherGroup.ismatchingType(agentInfo)) {
LinkInfo linkInfo = matcherGroup.makeLinkInfo(agentInfo);
jgen.writeStartObject();
jgen.writeStringField("linkName", linkInfo.getLinkName());
jgen.writeStringField("linkURL", linkInfo.getLinkUrl());
jgen.writeStringField("linkType", linkInfo.getLinktype());
jgen.writeEndObject();
}
}

jgen.writeEndArray();
}

jgen.writeEndObject();
jgen.writeObject(agentInfo);
}
jgen.writeEndArray();
}
Expand Down
11 changes: 5 additions & 6 deletions web/src/main/java/com/navercorp/pinpoint/web/vo/AgentInfo.java
Expand Up @@ -20,12 +20,15 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.navercorp.pinpoint.common.bo.AgentInfoBo;
import com.navercorp.pinpoint.common.bo.ServerMetaDataBo;
import com.navercorp.pinpoint.web.view.AgentInfoSerializer;

/**
* @author HyunGil Jeong
*/
@JsonSerialize(using = AgentInfoSerializer.class)
public class AgentInfo {

public static final Comparator<AgentInfo> AGENT_NAME_ASC_COMPARATOR = new Comparator<AgentInfo>() {
Expand All @@ -48,11 +51,7 @@ public int compare(AgentInfo lhs, AgentInfo rhs) {
private String vmVersion;
private String agentVersion;
private ServerMetaDataBo serverMetaData;

@JsonInclude(Include.NON_DEFAULT)
private long initialStartTimestamp;

@JsonInclude(Include.NON_NULL)
private AgentStatus status;

public AgentInfo() {
Expand Down Expand Up @@ -135,11 +134,11 @@ public int getPid() {
public void setPid(int pid) {
this.pid = pid;
}

public String getVmVersion() {
return vmVersion;
}

public void setVmVersion(String vmVersion) {
this.vmVersion = vmVersion;
}
Expand Down
29 changes: 5 additions & 24 deletions web/src/main/java/com/navercorp/pinpoint/web/vo/AgentStatus.java
Expand Up @@ -30,23 +30,19 @@
*/
public class AgentStatus {

private String agentId;
private final String agentId;

@JsonInclude(Include.NON_DEFAULT)
private long startTimestamp;

@JsonInclude(Include.NON_DEFAULT)
private long eventTimestamp;

@JsonSerialize(using = AgentLifeCycleStateSerializer.class)
private AgentLifeCycleState state;
private AgentLifeCycleState state = AgentLifeCycleState.UNKNOWN;

public AgentStatus() {
public AgentStatus(String agentId) {
this.agentId = agentId;
}

public AgentStatus(AgentLifeCycleBo agentLifeCycleBo) {
this.agentId = agentLifeCycleBo.getAgentId();
this.startTimestamp = agentLifeCycleBo.getStartTimestamp();
this.eventTimestamp = agentLifeCycleBo.getEventTimestamp();
this.state = agentLifeCycleBo.getAgentLifeCycleState();
}
Expand All @@ -55,18 +51,6 @@ public String getAgentId() {
return agentId;
}

public void setAgentId(String agentId) {
this.agentId = agentId;
}

public long getStartTimestamp() {
return startTimestamp;
}

public void setStartTimestamp(long startTimestamp) {
this.startTimestamp = startTimestamp;
}

public long getEventTimestamp() {
return eventTimestamp;
}
Expand All @@ -89,7 +73,6 @@ public int hashCode() {
int result = 1;
result = prime * result + ((agentId == null) ? 0 : agentId.hashCode());
result = prime * result + (int)(eventTimestamp ^ (eventTimestamp >>> 32));
result = prime * result + (int)(startTimestamp ^ (startTimestamp >>> 32));
result = prime * result + ((state == null) ? 0 : state.hashCode());
return result;
}
Expand All @@ -110,15 +93,13 @@ public boolean equals(Object obj) {
return false;
if (eventTimestamp != other.eventTimestamp)
return false;
if (startTimestamp != other.startTimestamp)
return false;
if (state != other.state)
return false;
return true;
}

@Override
public String toString() {
return "AgentStatus [agentId=" + agentId + ", startTimestamp=" + startTimestamp + ", eventTimestamp=" + eventTimestamp + ", state=" + state + "]";
return "AgentStatus [agentId=" + agentId + ", eventTimestamp=" + eventTimestamp + ", state=" + state + "]";
}
}

0 comments on commit c544979

Please sign in to comment.