1
wanshenmean
2026-03-16 689dd676fc0efb31236d989334122590b7198d61
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
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { InstanceListItem } from '../types'
import * as api from '../api'
 
export const useInstancesStore = defineStore('instances', () => {
  // 状态
  const instances = ref<InstanceListItem[]>([])
  const loading = ref(false)
  const error = ref<string | null>(null)
  const autoRefreshEnabled = ref(true)
  let refreshTimer: number | null = null
 
  // 计算属性
  const runningCount = computed(() =>
    instances.value.filter(i => i.status === 'Running').length
  )
 
  const stoppedCount = computed(() =>
    instances.value.filter(i => i.status === 'Stopped').length
  )
 
  const errorCount = computed(() =>
    instances.value.filter(i => i.status === 'Error').length
  )
 
  // 方法
  async function loadInstances() {
    loading.value = true
    error.value = null
    try {
      instances.value = await api.getAllInstances()
    } catch (err) {
      error.value = '加载实例列表失败'
      console.error(err)
    } finally {
      loading.value = false
    }
  }
 
  async function startInstance(id: string) {
    try {
      await api.startInstance(id)
      await loadInstances()
    } catch (err) {
      console.error('启动实例失败:', err)
      throw err
    }
  }
 
  async function stopInstance(id: string) {
    try {
      await api.stopInstance(id)
      await loadInstances()
    } catch (err) {
      console.error('停止实例失败:', err)
      throw err
    }
  }
 
  async function deleteInstance(id: string) {
    try {
      await api.deleteInstance(id)
      await loadInstances()
    } catch (err) {
      console.error('删除实例失败:', err)
      throw err
    }
  }
 
  function startAutoRefresh(interval: number = 2000) {
    stopAutoRefresh()
    if (autoRefreshEnabled.value) {
      refreshTimer = window.setInterval(() => {
        loadInstances()
      }, interval)
    }
  }
 
  function stopAutoRefresh() {
    if (refreshTimer !== null) {
      clearInterval(refreshTimer)
      refreshTimer = null
    }
  }
 
  function setAutoRefreshEnabled(enabled: boolean) {
    autoRefreshEnabled.value = enabled
    if (!enabled) {
      stopAutoRefresh()
    }
  }
 
  // 获取单个实例
  function getInstance(id: string) {
    return instances.value.find(i => i.instanceId === id)
  }
 
  return {
    // 状态
    instances,
    loading,
    error,
    autoRefreshEnabled,
    // 计算属性
    runningCount,
    stoppedCount,
    errorCount,
    // 方法
    loadInstances,
    startInstance,
    stopInstance,
    deleteInstance,
    startAutoRefresh,
    stopAutoRefresh,
    setAutoRefreshEnabled,
    getInstance
  }
})