File size: 7,961 Bytes
69b897d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<template>
  <div class="space-y-1">
    <div class="flex items-center justify-between text-xs">
      <span class="text-gray-500">{{ label }}</span>
      <span v-if="windowState === 'active'" class="font-medium text-gray-700">
        <i class="fas fa-clock mr-1 text-blue-500" />
        {{ formatTime(remainingSeconds) }}
      </span>
      <span v-else-if="windowState === 'expired'" class="font-medium text-orange-600">
        <i class="fas fa-sync-alt mr-1" />
        窗口已过期
      </span>
      <span v-else-if="windowState === 'notStarted'" class="font-medium text-gray-500">
        <i class="fas fa-pause-circle mr-1" />
        窗口未激活
      </span>
      <span v-else class="font-medium text-gray-400"> {{ rateLimitWindow }} 分钟 </span>
    </div>

    <!-- 进度条(仅在有限制时显示) -->
    <div v-if="showProgress" class="space-y-0.5">
      <div v-if="hasRequestLimit" class="space-y-0.5">
        <div class="flex items-center justify-between text-xs">
          <span class="text-gray-400">请求</span>
          <span class="text-gray-600"> {{ currentRequests || 0 }}/{{ requestLimit }} </span>
        </div>
        <div class="h-1 w-full rounded-full bg-gray-200">
          <div
            class="h-1 rounded-full transition-all duration-300"
            :class="getRequestProgressColor()"
            :style="{ width: getRequestProgress() + '%' }"
          />
        </div>
      </div>

      <!-- Token限制(向后兼容) -->
      <div v-if="hasTokenLimit" class="space-y-0.5">
        <div class="flex items-center justify-between text-xs">
          <span class="text-gray-400">Token</span>
          <span class="text-gray-600">
            {{ formatTokenCount(currentTokens || 0) }}/{{ formatTokenCount(tokenLimit) }}
          </span>
        </div>
        <div class="h-1 w-full rounded-full bg-gray-200">
          <div
            class="h-1 rounded-full transition-all duration-300"
            :class="getTokenProgressColor()"
            :style="{ width: getTokenProgress() + '%' }"
          />
        </div>
      </div>

      <!-- 费用限制(新功能) -->
      <div v-if="hasCostLimit" class="space-y-0.5">
        <div class="flex items-center justify-between text-xs">
          <span class="text-gray-400">费用</span>
          <span class="text-gray-600">
            ${{ (currentCost || 0).toFixed(2) }}/${{ costLimit.toFixed(2) }}
          </span>
        </div>
        <div class="h-1 w-full rounded-full bg-gray-200">
          <div
            class="h-1 rounded-full transition-all duration-300"
            :class="getCostProgressColor()"
            :style="{ width: getCostProgress() + '%' }"
          />
        </div>
      </div>
    </div>

    <!-- 额外提示信息 -->
    <div v-if="windowState === 'active' && showTooltip" class="text-xs text-gray-500">
      <i class="fas fa-info-circle mr-1" />
      <span v-if="remainingSeconds < 60">即将重置</span>
      <span v-else-if="remainingSeconds < 300"
        >{{ Math.ceil(remainingSeconds / 60) }} 分钟后重置</span
      >
      <span v-else>{{ formatDetailedTime(remainingSeconds) }}后重置</span>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted, watch } from 'vue'

const props = defineProps({
  label: {
    type: String,
    default: '窗口限制'
  },
  rateLimitWindow: {
    type: Number,
    required: true
  },
  windowStartTime: {
    type: Number,
    default: null
  },
  windowEndTime: {
    type: Number,
    default: null
  },
  windowRemainingSeconds: {
    type: Number,
    default: null
  },
  currentRequests: {
    type: Number,
    default: 0
  },
  requestLimit: {
    type: Number,
    default: 0
  },
  currentTokens: {
    type: Number,
    default: 0
  },
  tokenLimit: {
    type: Number,
    default: 0
  },
  currentCost: {
    type: Number,
    default: 0
  },
  costLimit: {
    type: Number,
    default: 0
  },
  showProgress: {
    type: Boolean,
    default: true
  },
  showTooltip: {
    type: Boolean,
    default: false
  }
})

// 响应式数据
const remainingSeconds = ref(props.windowRemainingSeconds)
let intervalId = null

// 计算属性
const windowState = computed(() => {
  if (props.windowStartTime === null) {
    return 'notStarted' // 窗口未开始
  }
  if (remainingSeconds.value === 0) {
    return 'expired' // 窗口已过期
  }
  if (remainingSeconds.value > 0) {
    return 'active' // 窗口活跃中
  }
  return 'unknown'
})

const hasRequestLimit = computed(() => props.requestLimit > 0)
const hasTokenLimit = computed(() => props.tokenLimit > 0)
const hasCostLimit = computed(() => props.costLimit > 0)

// 方法
const formatTime = (seconds) => {
  if (seconds === null || seconds === undefined) return '--:--'

  const hours = Math.floor(seconds / 3600)
  const minutes = Math.floor((seconds % 3600) / 60)
  const secs = seconds % 60

  if (hours > 0) {
    return `${hours}h ${minutes}m`
  } else if (minutes > 0) {
    return `${minutes}m ${secs}s`
  } else {
    return `${secs}s`
  }
}

const formatDetailedTime = (seconds) => {
  const hours = Math.floor(seconds / 3600)
  const minutes = Math.floor((seconds % 3600) / 60)

  if (hours > 0) {
    return `${hours}小时${minutes}分钟`
  } else {
    return `${minutes}分钟`
  }
}

const formatTokenCount = (count) => {
  if (count >= 1000000) {
    return (count / 1000000).toFixed(1) + 'M'
  } else if (count >= 1000) {
    return (count / 1000).toFixed(1) + 'K'
  }
  return count.toString()
}

const getRequestProgress = () => {
  if (!props.requestLimit || props.requestLimit === 0) return 0
  const percentage = ((props.currentRequests || 0) / props.requestLimit) * 100
  return Math.min(percentage, 100)
}

const getRequestProgressColor = () => {
  const progress = getRequestProgress()
  if (progress >= 100) return 'bg-red-500'
  if (progress >= 80) return 'bg-yellow-500'
  return 'bg-blue-500'
}

const getTokenProgress = () => {
  if (!props.tokenLimit || props.tokenLimit === 0) return 0
  const percentage = ((props.currentTokens || 0) / props.tokenLimit) * 100
  return Math.min(percentage, 100)
}

const getTokenProgressColor = () => {
  const progress = getTokenProgress()
  if (progress >= 100) return 'bg-red-500'
  if (progress >= 80) return 'bg-yellow-500'
  return 'bg-purple-500'
}

const getCostProgress = () => {
  if (!props.costLimit || props.costLimit === 0) return 0
  const percentage = ((props.currentCost || 0) / props.costLimit) * 100
  return Math.min(percentage, 100)
}

const getCostProgressColor = () => {
  const progress = getCostProgress()
  if (progress >= 100) return 'bg-red-500'
  if (progress >= 80) return 'bg-yellow-500'
  return 'bg-green-500'
}

// 更新倒计时
const updateCountdown = () => {
  if (props.windowEndTime && remainingSeconds.value > 0) {
    const now = Date.now()
    const remaining = Math.max(0, Math.floor((props.windowEndTime - now) / 1000))
    remainingSeconds.value = remaining

    if (remaining === 0) {
      // 窗口已过期,停止倒计时
      if (intervalId) {
        clearInterval(intervalId)
        intervalId = null
      }
    }
  }
}

// 监听props变化
watch(
  () => props.windowRemainingSeconds,
  (newVal) => {
    remainingSeconds.value = newVal
  }
)

watch(
  () => props.windowEndTime,
  (newVal) => {
    if (newVal) {
      // 重新计算剩余时间
      updateCountdown()

      // 如果窗口活跃且没有定时器,启动定时器
      if (!intervalId && remainingSeconds.value > 0) {
        intervalId = setInterval(updateCountdown, 1000)
      }
    }
  }
)

// 生命周期钩子
onMounted(() => {
  if (props.windowEndTime && remainingSeconds.value > 0) {
    // 立即更新一次
    updateCountdown()
    // 启动定时器
    intervalId = setInterval(updateCountdown, 1000)
  }
})

onUnmounted(() => {
  if (intervalId) {
    clearInterval(intervalId)
  }
})
</script>