Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/hub/systems/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type System struct {
agentVersion semver.Version // Agent version
updateTicker *time.Ticker // Ticker for updating the system
detailsFetched atomic.Bool // True if static system details have been fetched and saved
syncName atomic.Bool // True if display name should be kept in sync with hostname
smartFetching atomic.Bool // True if SMART devices are currently being fetched
smartInterval time.Duration // Interval for periodic SMART data updates
}
Expand Down Expand Up @@ -124,8 +125,8 @@ func (sys *System) update() error {
options := common.DataRequestOptions{
CacheTimeMs: uint16(interval),
}
// fetch system details if not already fetched
if !sys.detailsFetched.Load() {
// fetch system details if not already fetched or if sync_name is enabled
if !sys.detailsFetched.Load() || sys.syncName.Load() {
options.IncludeDetails = true
}

Expand Down Expand Up @@ -236,6 +237,10 @@ func (sys *System) createRecords(data *system.CombinedData) (*core.Record, error
if err := createSystemDetailsRecord(txApp, data.Details, sys.Id); err != nil {
return err
}
// sync display name with hostname if enabled
if systemRecord.GetBool("sync_name") {
systemRecord.Set("name", data.Details.Hostname)
}
}

// update system record (do this last because it triggers alerts and we need above records to be inserted first)
Expand Down
2 changes: 2 additions & 0 deletions internal/hub/systems/system_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (sm *SystemManager) onRecordAfterUpdateSuccess(e *core.RecordEvent) error {
if ok {
prevStatus = system.Status
system.Status = newStatus
system.syncName.Store(e.Record.GetBool("sync_name"))
}

switch newStatus {
Expand Down Expand Up @@ -296,6 +297,7 @@ func (sm *SystemManager) AddRecord(record *core.Record, system *System) (err err
system.Status = record.GetString("status")
system.Host = record.GetString("host")
system.Port = record.GetString("port")
system.syncName.Store(record.GetBool("sync_name"))

return sm.AddSystem(system)
}
Expand Down
29 changes: 29 additions & 0 deletions internal/migrations/1_systems_add_sync_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package migrations

import (
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
)

func init() {
m.Register(func(app core.App) error {
collection, err := app.FindCollectionByNameOrId("systems")
if err != nil {
return err
}
if collection.Fields.GetByName("sync_name") != nil {
return nil
}
collection.Fields.Add(&core.BoolField{
Name: "sync_name",
})
return app.Save(collection)
}, func(app core.App) error {
collection, err := app.FindCollectionByNameOrId("systems")
if err != nil {
return err
}
collection.Fields.RemoveByName("sync_name")
return app.Save(collection)
})
}
16 changes: 15 additions & 1 deletion internal/site/src/components/add-system.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getPagePath } from "@nanostores/router"
import { ChevronDownIcon, ExternalLinkIcon } from "lucide-react"
import { memo, useEffect, useRef, useState } from "react"
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import {
Dialog,
DialogContent,
Expand Down Expand Up @@ -71,6 +72,7 @@ export const SystemDialog = ({ setOpen, system }: { setOpen: (open: boolean) =>
const isUnixSocket = hostValue.startsWith("/")
const [tab, setTab] = useBrowserStorage("as-tab", "docker")
const [token, setToken] = useState(system?.token ?? "")
const [syncName, setSyncName] = useState(system?.sync_name ?? false)

useEffect(() => {
;(async () => {
Expand All @@ -96,6 +98,7 @@ export const SystemDialog = ({ setOpen, system }: { setOpen: (open: boolean) =>
const formData = new FormData(e.target as HTMLFormElement)
const data = Object.fromEntries(formData) as Record<string, any>
data.users = pb.authStore.record!.id
data.sync_name = syncName
try {
setOpen(false)
if (system) {
Expand Down Expand Up @@ -180,7 +183,18 @@ export const SystemDialog = ({ setOpen, system }: { setOpen: (open: boolean) =>
<Label htmlFor="name" className="xs:text-end">
<Trans>Name</Trans>
</Label>
<Input id="name" name="name" defaultValue={system?.name} required />
<Input id="name" name="name" defaultValue={system?.name} required disabled={syncName} />
<span />
<div className="flex items-center gap-2">
<Checkbox
id="sync_name"
checked={syncName}
onCheckedChange={(checked) => setSyncName(checked === true)}
/>
<Label htmlFor="sync_name" className="font-normal cursor-pointer">
<Trans>Sync name with hostname</Trans>
</Label>
</div>
<Label htmlFor="host" className="xs:text-end">
<Trans>Host / IP</Trans>
</Label>
Expand Down
1 change: 1 addition & 0 deletions internal/site/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface SystemRecord extends RecordModel {
info: SystemInfo
v: string
updated: string
sync_name: boolean
}

export interface SystemInfo {
Expand Down