Răsfoiți Sursa

feat: add script for batch adding test machines

- Introduced a new script to facilitate the bulk addition of test machine data.
- The script generates machine data with unique IDs and random locations.
- Includes error handling and success/failure logging for each machine addition.
- Provides usage instructions for setting the required token environment variable.
yb 2 săptămâni în urmă
părinte
comite
66ee3e2413
3 a modificat fișierele cu 102 adăugiri și 2 ștergeri
  1. 100 0
      scripts/add-test-machines.mjs
  2. 1 1
      src/views/dashboard/index.vue
  3. 1 1
      src/views/machine/index.vue

+ 100 - 0
scripts/add-test-machines.mjs

@@ -0,0 +1,100 @@
+#!/usr/bin/env node
+
+/**
+ * 批量添加测试机器数据
+ *
+ * 使用方法:
+ * 1. 在浏览器中登录系统
+ * 2. 打开开发者工具 -> Application -> Cookies
+ * 3. 复制 token 值
+ * 4. 运行: TOKEN=your_token_here node scripts/add-test-machines.mjs
+ */
+
+const API_BASE = 'https://tg-live-game.pwtk.cc/api'
+const TOKEN = process.env.TOKEN
+
+if (!TOKEN) {
+  console.error('❌ 请设置 TOKEN 环境变量')
+  console.log('\n使用方法:')
+  console.log('  TOKEN=your_token_here node scripts/add-test-machines.mjs')
+  console.log('\n获取 token:')
+  console.log('  1. 登录系统')
+  console.log('  2. 打开开发者工具 -> Application -> Cookies')
+  console.log('  3. 复制 token 值')
+  process.exit(1)
+}
+
+// 位置列表
+const locations = [
+  '北京机房A区', '北京机房B区', '上海数据中心', '深圳机房',
+  '广州机房', '杭州数据中心', '成都机房', '武汉机房',
+  '南京数据中心', '西安机房'
+]
+
+// 生成测试机器数据
+function generateMachines(count) {
+  const machines = []
+  for (let i = 1; i <= count; i++) {
+    const paddedNum = String(i).padStart(3, '0')
+    machines.push({
+      machineId: `TEST_MACHINE_${paddedNum}`,
+      name: `测试机器 ${paddedNum}`,
+      location: locations[Math.floor(Math.random() * locations.length)],
+      description: `这是第 ${i} 台测试机器,用于系统功能验证`
+    })
+  }
+  return machines
+}
+
+// 添加单个机器
+async function addMachine(machine) {
+  const response = await fetch(`${API_BASE}/admin/machines/add`, {
+    method: 'POST',
+    headers: {
+      'Content-Type': 'application/json',
+      'Authorization': `Bearer ${TOKEN}`
+    },
+    body: JSON.stringify(machine)
+  })
+
+  if (!response.ok) {
+    throw new Error(`HTTP ${response.status}: ${response.statusText}`)
+  }
+
+  return response.json()
+}
+
+// 主函数
+async function main() {
+  console.log('🚀 开始添加测试机器数据...\n')
+
+  const machines = generateMachines(30)
+  let successCount = 0
+  let failCount = 0
+
+  for (const machine of machines) {
+    try {
+      const result = await addMachine(machine)
+      if (result.code === 0) {
+        console.log(`✅ ${machine.machineId} - ${machine.name}`)
+        successCount++
+      } else {
+        console.log(`⚠️ ${machine.machineId} - ${result.message || '添加失败'}`)
+        failCount++
+      }
+    } catch (error) {
+      console.log(`❌ ${machine.machineId} - ${error.message}`)
+      failCount++
+    }
+
+    // 添加延迟,避免请求过快
+    await new Promise(resolve => setTimeout(resolve, 100))
+  }
+
+  console.log('\n📊 统计结果:')
+  console.log(`   成功: ${successCount}`)
+  console.log(`   失败: ${failCount}`)
+  console.log(`   总计: ${machines.length}`)
+}
+
+main().catch(console.error)

+ 1 - 1
src/views/dashboard/index.vue

@@ -254,7 +254,7 @@ onMounted(() => {
 <style lang="scss" scoped>
 .dashboard {
   font-family: 'Inter', system-ui, -apple-system, sans-serif;
-
+  padding: 1rem;
   // Page Header
   &__header {
     margin-bottom: 2rem;

+ 1 - 1
src/views/machine/index.vue

@@ -462,7 +462,7 @@ onMounted(() => {
   display: flex;
   flex-direction: column;
   height: 100%;
-  padding: 20px;
+  padding: 1rem;
   box-sizing: border-box;
   overflow: hidden;
 }