package com.lanyuanxiaoyao.service.scheduler.quartz.distribute.strategy;

import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnQueue;
import com.lanyuanxiaoyao.service.forest.service.YarnService;

/**
 * Yarn 队列剩余资源限制
 *
 * @author ZhangJiacheng
 * @date 2023-06-08
 */
public class YarnQueueUsedLimit implements AvailableStrategy {
    private final YarnService yarnService;
    private final String cluster;
    private final String queue;
    private final Double limit;

    public YarnQueueUsedLimit(YarnService yarnService, String cluster, String queue, Double limit) {
        this.yarnService = yarnService;
        this.cluster = cluster;
        this.queue = queue;
        this.limit = limit;
    }

    private double queueUsed(String cluster, String queue) {
        YarnQueue queueInfo = yarnService.queueDetail(cluster, queue);
        return queueInfo.getAbsoluteUsedCapacity() * 1.0 / queueInfo.getAbsoluteMaxCapacity();
    }

    @Override
    public boolean available() {
        return queueUsed(cluster, queue) < limit;
    }
}
