File size: 3,767 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
#!/usr/bin/env node

/**
 * Redis 键调试工具
 * 用于查看 Redis 中存储的所有键和数据结构
 */

const redis = require('../src/models/redis')
const logger = require('../src/utils/logger')

async function debugRedisKeys() {
  try {
    logger.info('🔄 Connecting to Redis...')
    await redis.connect()
    logger.success('✅ Connected to Redis')

    // 获取所有键
    const allKeys = await redis.client.keys('*')
    logger.info(`\n📊 Total keys in Redis: ${allKeys.length}\n`)

    // 按类型分组
    const keysByType = {
      apiKeys: [],
      claudeAccounts: [],
      geminiAccounts: [],
      admins: [],
      sessions: [],
      usage: [],
      other: []
    }

    // 分类键
    for (const key of allKeys) {
      if (key.startsWith('apikey:')) {
        keysByType.apiKeys.push(key)
      } else if (key.startsWith('claude_account:')) {
        keysByType.claudeAccounts.push(key)
      } else if (key.startsWith('gemini_account:')) {
        keysByType.geminiAccounts.push(key)
      } else if (key.startsWith('admin:') || key.startsWith('admin_username:')) {
        keysByType.admins.push(key)
      } else if (key.startsWith('session:')) {
        keysByType.sessions.push(key)
      } else if (
        key.includes('usage') ||
        key.includes('rate_limit') ||
        key.includes('concurrency')
      ) {
        keysByType.usage.push(key)
      } else {
        keysByType.other.push(key)
      }
    }

    // 显示分类结果
    console.log('='.repeat(60))
    console.log('📂 Keys by Category:')
    console.log('='.repeat(60))
    console.log(`API Keys: ${keysByType.apiKeys.length}`)
    console.log(`Claude Accounts: ${keysByType.claudeAccounts.length}`)
    console.log(`Gemini Accounts: ${keysByType.geminiAccounts.length}`)
    console.log(`Admins: ${keysByType.admins.length}`)
    console.log(`Sessions: ${keysByType.sessions.length}`)
    console.log(`Usage/Rate Limit: ${keysByType.usage.length}`)
    console.log(`Other: ${keysByType.other.length}`)
    console.log('='.repeat(60))

    // 详细显示每个类别的键
    if (keysByType.apiKeys.length > 0) {
      console.log('\n🔑 API Keys:')
      for (const key of keysByType.apiKeys.slice(0, 5)) {
        console.log(`  - ${key}`)
      }
      if (keysByType.apiKeys.length > 5) {
        console.log(`  ... and ${keysByType.apiKeys.length - 5} more`)
      }
    }

    if (keysByType.claudeAccounts.length > 0) {
      console.log('\n🤖 Claude Accounts:')
      for (const key of keysByType.claudeAccounts) {
        console.log(`  - ${key}`)
      }
    }

    if (keysByType.geminiAccounts.length > 0) {
      console.log('\n💎 Gemini Accounts:')
      for (const key of keysByType.geminiAccounts) {
        console.log(`  - ${key}`)
      }
    }

    if (keysByType.other.length > 0) {
      console.log('\n❓ Other Keys:')
      for (const key of keysByType.other.slice(0, 10)) {
        console.log(`  - ${key}`)
      }
      if (keysByType.other.length > 10) {
        console.log(`  ... and ${keysByType.other.length - 10} more`)
      }
    }

    // 检查数据类型
    console.log(`\n${'='.repeat(60)}`)
    console.log('🔍 Checking Data Types:')
    console.log('='.repeat(60))

    // 随机检查几个键的类型
    const sampleKeys = allKeys.slice(0, Math.min(10, allKeys.length))
    for (const key of sampleKeys) {
      const type = await redis.client.type(key)
      console.log(`${key} => ${type}`)
    }
  } catch (error) {
    logger.error('💥 Debug failed:', error)
  } finally {
    await redis.disconnect()
    logger.info('👋 Disconnected from Redis')
  }
}

// 运行调试
debugRedisKeys().catch((error) => {
  logger.error('💥 Unexpected error:', error)
  process.exit(1)
})