feat: improve linux distribution information get logic (#11171)

* feat: Add PrettyDistro field to dashboard information and update related services and frontend components

* fix: Trim whitespace and parentheses from detected Linux distribution name in GetDistro method

* fix: Correctly trim parentheses from detected Linux distribution name in GetDistro method

* refactor: Simplify Linux distribution detection by removing unnecessary checks and consolidating logic
This commit is contained in:
KOMATA 2025-12-03 14:14:35 +08:00 committed by GitHub
parent 8c32fa3e6c
commit 5d2084fda4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 66 additions and 3 deletions

View file

@ -13,6 +13,7 @@ type DashboardBase struct {
Platform string `json:"platform"`
PlatformFamily string `json:"platformFamily"`
PlatformVersion string `json:"platformVersion"`
PrettyDistro string `json:"prettyDistro"`
KernelArch string `json:"kernelArch"`
KernelVersion string `json:"kernelVersion"`
VirtualizationSystem string `json:"virtualizationSystem"`
@ -49,6 +50,7 @@ type OsInfo struct {
PlatformFamily string `json:"platformFamily"`
KernelArch string `json:"kernelArch"`
KernelVersion string `json:"kernelVersion"`
PrettyDistro string `json:"prettyDistro"`
DiskSize int64 `json:"diskSize"`
}

View file

@ -88,6 +88,7 @@ func (u *DashboardService) LoadOsInfo() (*dto.OsInfo, error) {
baseInfo.PlatformFamily = hostInfo.PlatformFamily
baseInfo.KernelArch = hostInfo.KernelArch
baseInfo.KernelVersion = hostInfo.KernelVersion
baseInfo.PrettyDistro = psutil.HOST.GetDistro()
diskInfo, err := psutil.DISK.GetUsage(global.Dir.BaseDir, false)
if err == nil {
@ -152,6 +153,7 @@ func (u *DashboardService) LoadBaseInfo(ioOption string, netOption string) (*dto
Platform: hostInfo.Platform,
PlatformFamily: hostInfo.PlatformFamily,
PlatformVersion: hostInfo.PlatformVersion,
PrettyDistro: psutil.HOST.GetDistro(),
KernelArch: hostInfo.KernelArch,
KernelVersion: hostInfo.KernelVersion,
VirtualizationSystem: string(ss),

View file

@ -1,6 +1,9 @@
package psutil
import (
"fmt"
"os"
"strings"
"sync"
"time"
@ -13,7 +16,8 @@ type HostInfoState struct {
mu sync.RWMutex
lastSampleTime time.Time
cachedInfo *host.InfoStat
cachedInfo *host.InfoStat
cachedDistro string
}
func (h *HostInfoState) GetHostInfo(forceRefresh bool) (*host.InfoStat, error) {
@ -36,3 +40,52 @@ func (h *HostInfoState) GetHostInfo(forceRefresh bool) (*host.InfoStat, error) {
return hostInfo, nil
}
func (h *HostInfoState) GetDistro() string {
if h.cachedDistro == "" {
h.cachedDistro = detectLinuxDistro()
}
return h.cachedDistro
}
func detectLinuxDistro() string {
distroFiles := []string{
"/etc/os-release",
"/usr/lib/os-release",
}
var targetFile string
for _, f := range distroFiles {
if _, err := os.Stat(f); err == nil {
targetFile = f
break
}
}
if targetFile != "" {
data, err := os.ReadFile(targetFile)
if err == nil {
content := string(data)
for _, line := range strings.Split(content, "\n") {
idx := strings.Index(line, "=")
if idx == -1 {
continue
}
key := line[:idx]
if key == "PRETTY_NAME" {
d := strings.Trim(line[idx+1:], "\"")
if strings.Contains(d, "(") && strings.Contains(d, ")") {
d = d[:strings.LastIndex(d, "(")]
}
return strings.TrimSpace(d)
}
}
}
}
if osInfo, err := host.Info(); err == nil {
return fmt.Sprintf("%s %s", osInfo.Platform, osInfo.PlatformVersion)
}
return "Linux"
}

View file

@ -52,6 +52,7 @@ export namespace Dashboard {
platform: string;
platformFamily: string;
platformVersion: string;
prettyDistro: string;
kernelArch: string;
kernelVersion: string;
virtualizationSystem: string;

View file

@ -203,7 +203,9 @@
<span class="system-label">{{ $t('home.platformVersion') }}</span>
</template>
{{
baseInfo.platformVersion
baseInfo.prettyDistro
? baseInfo.prettyDistro
: baseInfo.platformVersion
? baseInfo.platform + '-' + baseInfo.platformVersion
: baseInfo.platform
}}
@ -393,6 +395,7 @@ const baseInfo = ref<Dashboard.BaseInfo>({
platform: '',
platformFamily: '',
platformVersion: '',
prettyDistro: '',
kernelArch: '',
kernelVersion: '',
virtualizationSystem: '',
@ -660,7 +663,9 @@ const handleCopy = () => {
'\n' +
i18n.global.t('home.platformVersion') +
': ' +
(baseInfo.value.platformVersion
(baseInfo.value.prettyDistro
? baseInfo.value.prettyDistro
: baseInfo.value.platformVersion
? baseInfo.value.platform + '-' + baseInfo.value.platformVersion
: baseInfo.value.platform) +
'\n' +