#!/bin/sh

# 定义合法的任务状态
VALID_STATES="ALL,NEW,NEW_SAVING,SUBMITTED,ACCEPTED,RUNNING,FINISHED,FAILED,KILLED"

# 检查是否传入参数
if [ $# -lt 1 ]; then
    echo "Usage: $0 <appStates>"
    echo "Example: $0 FINISHED"
    echo "Valid states: $VALID_STATES"
    exit 1
fi

# 获取任务状态作为参数
APP_STATES=$1

# 检查输入状态是否合法
if ! echo "$VALID_STATES" | grep -qw "$APP_STATES"; then
    echo "Error: Invalid appStates '$APP_STATES'."
    echo "Valid states: $VALID_STATES"
    exit 1
fi

# 获取指定状态的任务列表
yarn app -list -appStates "$APP_STATES" 2>/dev/null | grep '61' | awk '{print $1}' | while read -r app_id; do
    # 获取任务详情
    details=$(yarn application -status "$app_id" 2>/dev/null | grep -E "Application-Id|Application-Name|Start-Time|Finish-Time")
    
    # 提取信息
    app_id=$(echo "$details" | grep "Application-Id" | awk -F ": " '{print $2}')
    app_name=$(echo "$details" | grep "Application-Name" | awk -F ": " '{print $2}')
    start_time_ms=$(echo "$details" | grep "Start-Time" | awk -F ": " '{print $2}')
    finish_time_ms=$(echo "$details" | grep "Finish-Time" | awk -F ": " '{print $2}')
    
    # 将毫秒级时间戳转换为秒级，并格式化为日期时间
    start_time=$(date -d @$((start_time_ms / 1000)) +'%Y-%m-%d %H:%M:%S')
    finish_time=$(date -d @$((finish_time_ms / 1000)) +'%Y-%m-%d %H:%M:%S')

    # 显示任务信息
    echo -e "Application-Id: $app_id\tApplication-Name: $app_name\tStart-Time: $start_time\tFinish-Time: $finish_time"
done