Skip to content

Commit

Permalink
[#noissue] Add agentId parameter to apdexScore api
Browse files Browse the repository at this point in the history
  • Loading branch information
donghun-cho committed Feb 27, 2024
1 parent 034ddbb commit 586364c
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,34 @@ public ApdexScore getApdexScore(
return apdexScoreService.selectApdexScoreData(application, range);
}

@GetMapping(value = "/getApdexScore", params = {"agentId"})
public ApdexScore getApdexScore(
@RequestParam("applicationName") @NotBlank String applicationName,
@RequestParam("serviceTypeCode") Short serviceTypeCode,
@RequestParam("agentId") @NotBlank String agentId,
@RequestParam("from") @PositiveOrZero long from,
@RequestParam("to") @PositiveOrZero long to) {
final Range range = Range.between(from, to);

Check warning on line 67 in web/src/main/java/com/navercorp/pinpoint/web/controller/ApdexScoreController.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/controller/ApdexScoreController.java#L67

Added line #L67 was not covered by tests

Application application = applicationFactory.createApplication(applicationName, serviceTypeCode);

Check warning on line 69 in web/src/main/java/com/navercorp/pinpoint/web/controller/ApdexScoreController.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/controller/ApdexScoreController.java#L69

Added line #L69 was not covered by tests

return apdexScoreService.selectApdexScoreData(application, agentId, range);

Check warning on line 71 in web/src/main/java/com/navercorp/pinpoint/web/controller/ApdexScoreController.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/controller/ApdexScoreController.java#L71

Added line #L71 was not covered by tests
}

@GetMapping(value = "/getApdexScore", params = {"agentId", "serviceTypeName"})
public ApdexScore getApdexScore(
@RequestParam("applicationName") @NotBlank String applicationName,
@RequestParam("serviceTypeName") @NotBlank String serviceTypeName,
@RequestParam("agentId") @NotBlank String agentId,
@RequestParam("from") @PositiveOrZero long from,
@RequestParam("to") @PositiveOrZero long to) {
final Range range = Range.between(from, to);

Check warning on line 81 in web/src/main/java/com/navercorp/pinpoint/web/controller/ApdexScoreController.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/controller/ApdexScoreController.java#L81

Added line #L81 was not covered by tests

Application application = applicationFactory.createApplicationByTypeName(applicationName, serviceTypeName);

Check warning on line 83 in web/src/main/java/com/navercorp/pinpoint/web/controller/ApdexScoreController.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/controller/ApdexScoreController.java#L83

Added line #L83 was not covered by tests

return apdexScoreService.selectApdexScoreData(application, agentId, range);

Check warning on line 85 in web/src/main/java/com/navercorp/pinpoint/web/controller/ApdexScoreController.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/controller/ApdexScoreController.java#L85

Added line #L85 was not covered by tests
}

@GetMapping(value = "/getApplicationStat/apdexScore/chart")
public StatChart<?> getApplicationApdexScoreChart(
@RequestParam("applicationId") @NotBlank String applicationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface ApdexScoreService {

ApdexScore selectApdexScoreData(Application application, Range range);

ApdexScore selectApdexScoreData(Application application, String agentId, Range range);

StatChart selectApplicationChart(Application application, Range range, TimeWindow timeWindow);

StatChart selectAgentChart(Application application, Range range, TimeWindow timeWindow, String agentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ public ApdexScoreServiceImpl(MapResponseDao mapResponseDao) {
this.mapResponseDao = Objects.requireNonNull(mapResponseDao, "mapResponseDao");
}

private AgentTimeHistogram createAgentTimeHistogram(Application application, Range range, TimeWindow timeWindow, List<ResponseTime> responseHistogramList) {
AgentTimeHistogramBuilder builder = new AgentTimeHistogramBuilder(application, range, timeWindow);
AgentTimeHistogram timeHistogram = builder.build(responseHistogramList);
return timeHistogram;
}

@Override
public ApdexScore selectApdexScoreData(Application application, Range range) {
ServiceType applicationServiceType = application.getServiceType();
Expand All @@ -55,6 +49,21 @@ public ApdexScore selectApdexScoreData(Application application, Range range) {
}
}

@Override
public ApdexScore selectApdexScoreData(Application application, String agentId, Range range) {
ServiceType applicationServiceType = application.getServiceType();

if (applicationServiceType.isWas()) {
List<ResponseTime> responseTimeList = mapResponseDao.selectResponseTime(application, range);
Histogram agentHistogram = createAgentHistogram(responseTimeList, agentId, applicationServiceType);

return ApdexScore.newApdexScore(agentHistogram);
} else {
logger.debug("application service type isWas:{}", applicationServiceType.isWas());
return ApdexScore.newApdexScore(new Histogram(applicationServiceType));

Check warning on line 63 in web/src/main/java/com/navercorp/pinpoint/web/service/ApdexScoreServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/service/ApdexScoreServiceImpl.java#L62-L63

Added lines #L62 - L63 were not covered by tests
}
}

private Histogram createApplicationHistogram(List<ResponseTime> responseHistogram, ServiceType applicationServiceType) {
final Histogram applicationHistogram = new Histogram(applicationServiceType);
for (ResponseTime responseTime : responseHistogram) {
Expand All @@ -64,6 +73,17 @@ private Histogram createApplicationHistogram(List<ResponseTime> responseHistogra
return applicationHistogram;
}

private Histogram createAgentHistogram(List<ResponseTime> responseHistogram, String agentId, ServiceType applicationServiceType) {
final Histogram agentHistogram = new Histogram(applicationServiceType);
for (ResponseTime responseTime : responseHistogram) {
Histogram histogram = responseTime.findHistogram(agentId);
if (histogram != null) {
agentHistogram.add(histogram);
}
}
return agentHistogram;
}

@Override
public StatChart selectApplicationChart(Application application, Range range, TimeWindow timeWindow) {
List<ResponseTime> responseTimeList = mapResponseDao.selectResponseTime(application, range);
Expand All @@ -82,4 +102,10 @@ public StatChart selectAgentChart(Application application, Range range, TimeWind
List<SampledApdexScore> sampledPoints = timeHistogram.getSampledAgentApdexScoreList(agentId);
return new AgentApdexScoreChart(timeWindow, sampledPoints);
}

private AgentTimeHistogram createAgentTimeHistogram(Application application, Range range, TimeWindow timeWindow, List<ResponseTime> responseHistogramList) {
AgentTimeHistogramBuilder builder = new AgentTimeHistogramBuilder(application, range, timeWindow);
AgentTimeHistogram timeHistogram = builder.build(responseHistogramList);
return timeHistogram;

Check warning on line 109 in web/src/main/java/com/navercorp/pinpoint/web/service/ApdexScoreServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

web/src/main/java/com/navercorp/pinpoint/web/service/ApdexScoreServiceImpl.java#L107-L109

Added lines #L107 - L109 were not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.navercorp.pinpoint.web.service;

import com.navercorp.pinpoint.common.server.util.time.Range;
import com.navercorp.pinpoint.common.trace.HistogramSchema;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.web.applicationmap.dao.MapResponseDao;
import com.navercorp.pinpoint.web.applicationmap.histogram.ApdexScore;
import com.navercorp.pinpoint.web.applicationmap.histogram.Histogram;
import com.navercorp.pinpoint.web.vo.Application;
import com.navercorp.pinpoint.web.vo.ResponseTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ApdexScoreServiceImplTest {
private static final long MINUTES_MILLIS = 60 * 1000;

private final Application testApplication = new Application("testApplication", ServiceType.STAND_ALONE);
private final List<String> agentIdList = List.of("agentId1", "agentId2", "agentId3");

private ApdexScoreServiceImpl apdexScoreService;
private Range testRange;

@BeforeEach
public void mockResponseDao() {
long endTimestamp = (System.currentTimeMillis() / MINUTES_MILLIS) * MINUTES_MILLIS;
testRange = Range.newRange(TimeUnit.MINUTES, 5, endTimestamp);

List<ResponseTime> responseTimeList = new ArrayList<>(5);
for (int i = 0; i < 5; i++) {
responseTimeList.add(createResponseTime(endTimestamp - (MINUTES_MILLIS * i)));
}

MapResponseDao mapResponseDao = mock(MapResponseDao.class);
when(mapResponseDao.selectResponseTime(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(Collections.emptyList());
when(mapResponseDao.selectResponseTime(ArgumentMatchers.eq(testApplication), ArgumentMatchers.any())).thenReturn(responseTimeList);

apdexScoreService = new ApdexScoreServiceImpl(mapResponseDao);
}

private ResponseTime createResponseTime(long timeStamp) {
ResponseTime responseTime = new ResponseTime(testApplication.getName(), testApplication.getServiceType(), timeStamp);
for (String agentId : agentIdList) {
responseTime.addResponseTime(agentId, createTestHistogram(1, 2, 3, 4, 5));
}
return responseTime;
}

private Histogram createTestHistogram(long fast, long normal, long slow, long verySlow, long error) {
Histogram histogram = new Histogram(ServiceType.TEST);
HistogramSchema schema = histogram.getHistogramSchema();

histogram.addCallCount(schema.getFastSlot().getSlotTime(), fast);
histogram.addCallCount(schema.getNormalSlot().getSlotTime(), normal);
histogram.addCallCount(schema.getSlowSlot().getSlotTime(), slow);
histogram.addCallCount(schema.getVerySlowSlot().getSlotTime(), verySlow);
histogram.addCallCount(schema.getErrorSlot().getSlotTime(), error);
return histogram;
}

@Test
public void selectApplicationApdexScoreData() {
ApdexScore apdexScore = apdexScoreService.selectApdexScoreData(testApplication, testRange);

assertThat(apdexScore.getApdexScore()).isGreaterThan(0);
}

@Test
public void selectNonWasApplicationApexScoreData() {
ApdexScore apdexScore = apdexScoreService.selectApdexScoreData(new Application("nonWas", ServiceType.USER), testRange);

assertThat(apdexScore.getApdexScore()).isEqualTo(0);
}

@Test
public void selectNonExistingApplicationApexScoreData() {
ApdexScore apdexScore = apdexScoreService.selectApdexScoreData(new Application("nonExisting", ServiceType.STAND_ALONE), testRange);

assertThat(apdexScore.getApdexScore()).isEqualTo(0);
}

@Test
public void selectAgentApexScoreData() {
ApdexScore apdexScore = apdexScoreService.selectApdexScoreData(testApplication, agentIdList.get(0), testRange);

assertThat(apdexScore.getApdexScore()).isGreaterThan(0);
}

@Test
public void selectNonExistingAgentApexScoreData() {
ApdexScore apdexScore = apdexScoreService.selectApdexScoreData(testApplication, "nonExistingAgentId", testRange);

assertThat(apdexScore.getApdexScore()).isEqualTo(0);
}
}

0 comments on commit 586364c

Please sign in to comment.