Skip to content

Commit

Permalink
fix: monitoring error resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
1jeongg committed Oct 13, 2024
1 parent 9507147 commit e23b03a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public RouterResponse.MonitorResult getMetric(Metric metric, Router router, Long
return getResult(monitoringQuery);
}

public RouterResponse.MonitorResult getMetricInRangeTime(Metric metric, Router router, Long nowTime, Long targetTime) {
public RouterResponse.MonitorResult getMetricInRangeTime(Metric metric, Router router, Long time) {
String expr = getExpression(metric, router);
RouterResponse.MonitoringQueries monitoringQuery = getMonitoringQueryInRange(expr, nowTime, targetTime);
RouterResponse.MonitoringQueries monitoringQuery = getMonitoringQuery(expr, time);
return getResult(monitoringQuery);
}

Expand Down Expand Up @@ -102,6 +102,7 @@ private RouterResponse.MonitorResult getResult(RouterResponse.MonitoringQueries
return new ServerException(BaseException.GRAFANA_REQUEST_FAIL);
}))
.bodyToMono(RouterResponse.MonitorResult.class)
.doOnSuccess(result -> log.debug(result.toString()))
.block());
} catch(Exception e) {
log.debug(e.getMessage());
Expand All @@ -120,17 +121,6 @@ private RouterResponse.MonitoringQueries getMonitoringQuery(String expr, Long ti
);
}

private RouterResponse.MonitoringQueries getMonitoringQueryInRange(String expr, Long nowTime, Long targetTime) {
RouterResponse.Datasource datasource = new RouterResponse.Datasource("prometheus", grafanaDatasourceUID);
RouterResponse.MonitoringQuery query = getQuery(expr, datasource);

return new RouterResponse.MonitoringQueries(
List.of(query),
nowTime.toString(),
targetTime.toString()
);
}

private static RouterResponse.MonitoringQuery getQuery(String expr, RouterResponse.Datasource datasource) {
return new RouterResponse.MonitoringQuery(
datasource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void checkRouterNonExistInDB(RouterRequest.CreateDTO requestDTO) {
}

public void checkRouterPermission(Router router, User user) {
if (!user.getCompany().equals(router.getCompany())) {
if (!user.getCompany().getId().equals(router.getCompany().getId())) {
throw new ForbiddenException(BaseException.ROUTER_PERMISSION_DENIED);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import com.wasin.wasin.repository.UserJPARepository;
import com.wasin.wasin.service.MonitoringService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
Expand All @@ -43,6 +43,7 @@ public MonitorResponse.FindById monitorById(Long routerId, Long metricId, Long t

monitoringValidation.checkRouterExistInGroup(router, user);
RouterResponse.MonitorResult results = monitoringApiUtil.getMetric(metric, router, time);
log.debug(results.toString());

return monitoringMapper.resultToDTO(results, metricId, time);
}
Expand All @@ -56,11 +57,9 @@ public MonitorResponse.FindMultiple monitorMultiple(Long metricId, Long time, Us
Company company = user.getCompany();
List<Router> routerList = routerJPARepository.findAllRouterByCompanyId(company.getId());

long toTime = LocalDateTime.now().atZone(ZoneOffset.UTC).toInstant().toEpochMilli() - 60 * 60 * 1000 * 9;
long fromTime = toTime - time * 60 * 1000;

Long finalTime = time;
List<RouterResponse.MonitorResult> results = routerList.stream().map(router ->
monitoringApiUtil.getMetricInRangeTime(metric, router, fromTime, toTime)
monitoringApiUtil.getMetricInRangeTime(metric, router, finalTime)
).toList();

return monitoringMapper.resultToMultipleDTO(results, metricId, time);
Expand Down

0 comments on commit e23b03a

Please sign in to comment.