Skip to content

Commit

Permalink
[#noissue] Apply Java8 Date API
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Apr 15, 2024
1 parent af0bb1c commit f6cacca
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import java.time.Period;
import java.util.Objects;

/**
Expand Down Expand Up @@ -62,7 +63,8 @@ public ApplicationAgentHostList getApplicationHostInfo (
int maxLimit = Math.min(MAX_PAGING_LIMIT, limit);
durationDays = ObjectUtils.defaultIfNull(durationDays, AgentInfoService.NO_DURATION);

return agentInfoService.getApplicationAgentHostList(offset, maxLimit, durationDays);
Period durationDaysPeriod = Period.ofDays(durationDays);
return agentInfoService.getApplicationAgentHostList(offset, maxLimit, durationDaysPeriod);
}

@RequestMapping(value = "/isAvailableApplicationName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.navercorp.pinpoint.web.vo.tree.ApplicationAgentHostList;
import com.navercorp.pinpoint.web.vo.tree.SortByAgentInfo;

import java.time.Period;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -50,7 +51,7 @@ public interface AgentInfoService {
AgentsMapByHost getAgentsListByApplicationName(AgentStatusFilter agentStatusFilter, AgentInfoFilter agentInfoPredicate, String applicationName, Range range, SortByAgentInfo.Rules sortBy);
AgentsMapByHost getAgentsListByApplicationName(AgentStatusFilter agentStatusFilter, String applicationName, Range range, SortByAgentInfo.Rules sortBy);

ApplicationAgentHostList getApplicationAgentHostList(int offset, int limit, int durationDays);
ApplicationAgentHostList getApplicationAgentHostList(int offset, int limit, Period durationDays);

Set<AgentAndStatus> getAgentsByApplicationName(String applicationName, long timestamp);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.navercorp.pinpoint.web.filter.agent.AgentEventFilter;
import com.navercorp.pinpoint.web.hyperlink.HyperLinkFactory;
import com.navercorp.pinpoint.web.service.stat.AgentWarningStatService;
import com.navercorp.pinpoint.web.util.DateTimeUtils;
import com.navercorp.pinpoint.web.vo.AgentEvent;
import com.navercorp.pinpoint.web.vo.Application;
import com.navercorp.pinpoint.web.vo.agent.AgentAndStatus;
Expand Down Expand Up @@ -55,8 +56,8 @@
import org.springframework.util.CollectionUtils;

import java.time.Instant;
import java.time.Period;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
Expand Down Expand Up @@ -175,18 +176,19 @@ public AgentsMapByHost getAgentsListByApplicationName(AgentStatusFilter agentSta
}

@Override
public ApplicationAgentHostList getApplicationAgentHostList(int offset, int limit, int durationDays) {
public ApplicationAgentHostList getApplicationAgentHostList(int offset, int limit, Period durationDays) {
if (offset <= 0) {
throw new IllegalArgumentException("offset must be greater than 0");
}
if (limit <= 0) {
throw new IllegalArgumentException("limit must be greater than 0");
}
Objects.requireNonNull(durationDays, "durationDays");

return getApplicationAgentHostList0(offset, limit, durationDays);
}

private ApplicationAgentHostList getApplicationAgentHostList0(int offset, int limit, int durationDays) {
private ApplicationAgentHostList getApplicationAgentHostList0(int offset, int limit, Period durationDays) {
List<String> applicationNameList = getApplicationNameList(applicationIndexDao.selectAllApplicationNames());
if (offset > applicationNameList.size()) {
ApplicationAgentHostList.Builder builder = newBuilder(offset, offset, applicationNameList.size());
Expand All @@ -213,25 +215,23 @@ private ApplicationAgentHostList.Builder newBuilder(int offset, int endIndex, in
return ApplicationAgentHostList.newBuilder(offset, endIndex, totalApplications);
}

private List<String> getAgentIdList(String applicationName, int durationDays) {
private List<String> getAgentIdList(String applicationName, Period durationDays) {
List<String> agentIds = this.applicationIndexDao.selectAgentIds(applicationName);
if (CollectionUtils.isEmpty(agentIds)) {
return Collections.emptyList();
}

if (durationDays <= 0) {
if (durationDays.isNegative()) {
return agentIds;
}

List<String> activeAgentIdList = new ArrayList<>();

Range fastRange = Range.newRange(TimeUnit.HOURS, 1, System.currentTimeMillis());
Instant now = DateTimeUtils.epochMilli();
Range fastRange = Range.newRange(TimeUnit.HOURS, 1, now.toEpochMilli());

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, durationDays * -1);
final long fromTimestamp = cal.getTimeInMillis();
Instant from = now.minus(durationDays);
final long fromTimestamp = from.toEpochMilli();
Range queryRange = Range.between(fromTimestamp, fastRange.getFrom() + 1);

List<String> activeAgentIdList = new ArrayList<>();
for (String agentId : agentIds) {
// FIXME This needs to be done with a more accurate information.
// If at any time a non-java agent is introduced, or an agent that does not collect jvm data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public final class DateTimeUtils {
private DateTimeUtils() {
}

/**
* Returns the current time in milliseconds.
* precision : milliseconds
* @return the current time in milliseconds
*/
public static Instant epochMilli() {
return Instant.ofEpochMilli(System.currentTimeMillis());
}

public static long timestampToStartOfDay(long epochMilli) {
// Java 8 date-time: get start of day from ZonedDateTime
// https://stackoverflow.com/questions/29143910/java-8-date-time-get-start-of-day-from-zoneddatetime
Expand Down

0 comments on commit f6cacca

Please sign in to comment.