File size: 2,857 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
const axios = require('axios')

const BASE_URL = 'http://localhost:3312'

// 你需要替换为一个有效的 API Key
const API_KEY = 'cr_your_api_key_here'

async function testWindowRemaining() {
  try {
    console.log('🔍 测试时间窗口剩余时间功能...\n')

    // 第一步:获取 API Key ID
    console.log('1. 获取 API Key ID...')
    const idResponse = await axios.post(`${BASE_URL}/api-stats/api/get-key-id`, {
      apiKey: API_KEY
    })

    if (!idResponse.data.success) {
      throw new Error('Failed to get API Key ID')
    }

    const apiId = idResponse.data.data.id
    console.log(`   ✅ API Key ID: ${apiId}\n`)

    // 第二步:查询统计数据
    console.log('2. 查询统计数据(包含时间窗口信息)...')
    const statsResponse = await axios.post(`${BASE_URL}/api-stats/api/user-stats`, {
      apiId
    })

    if (!statsResponse.data.success) {
      throw new Error('Failed to get stats data')
    }

    const stats = statsResponse.data.data
    console.log(`   ✅ 成功获取统计数据\n`)

    // 第三步:检查时间窗口信息
    console.log('3. 时间窗口信息:')
    console.log(`   - 窗口时长: ${stats.limits.rateLimitWindow} 分钟`)
    console.log(`   - 请求限制: ${stats.limits.rateLimitRequests || '无限制'}`)
    console.log(`   - Token限制: ${stats.limits.tokenLimit || '无限制'}`)
    console.log(`   - 当前请求数: ${stats.limits.currentWindowRequests}`)
    console.log(`   - 当前Token数: ${stats.limits.currentWindowTokens}`)

    if (stats.limits.windowStartTime) {
      const startTime = new Date(stats.limits.windowStartTime)
      const endTime = new Date(stats.limits.windowEndTime)

      console.log(`\n   ⏰ 时间窗口状态:`)
      console.log(`   - 窗口开始时间: ${startTime.toLocaleString()}`)
      console.log(`   - 窗口结束时间: ${endTime.toLocaleString()}`)
      console.log(`   - 剩余时间: ${stats.limits.windowRemainingSeconds} 秒`)

      if (stats.limits.windowRemainingSeconds > 0) {
        const minutes = Math.floor(stats.limits.windowRemainingSeconds / 60)
        const seconds = stats.limits.windowRemainingSeconds % 60
        console.log(`   - 格式化剩余时间: ${minutes}${seconds}秒`)
        console.log(`   - 窗口状态: 🟢 活跃中`)
      } else {
        console.log(`   - 窗口状态: 🔴 已过期(下次请求时重置)`)
      }
    } else {
      console.log(`\n   ⏰ 时间窗口状态: ⚪ 未启动(还没有任何请求)`)
    }

    console.log('\n✅ 测试完成!时间窗口剩余时间功能正常工作。')
  } catch (error) {
    console.error('❌ 测试失败:', error.message)
    if (error.response) {
      console.error('   响应数据:', error.response.data)
    }
    process.exit(1)
  }
}

// 运行测试
testWindowRemaining()