package com.skr.mdm.aop;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author nfq
 * @date 2020/9/15 11:48
 */
@Component
@Aspect
@Slf4j
public class PerformanceMonitoring {

    private static final long TIMEOUT = 3000;

    /**
     * API性能监控
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.skr.mdm..*.controller..*.*(..))")
    public Object apiMonitoring(ProceedingJoinPoint point) throws Throwable {
        TimeInterval timer = DateUtil.timer();
        Object proceed = point.proceed();
        if (timer.interval() > TIMEOUT) {
            log.warn("---------------执行:{}, {}-----------耗时:{} 秒", point.getTarget().getClass(), point.getSignature().getName(), timer.intervalSecond());
        }
        return proceed;
    }
}
