Skip to content

Commit 4a72c9e

Browse files
authored
Merge pull request AnalogJ#754 from Berry-95/491-FEAT-Allow-disks-to-be-archived
Fixes 491 [FEAT] Allow disks to be hidden/archived
2 parents e46ab73 + 3e11583 commit 4a72c9e

26 files changed

+406
-33
lines changed

docs/dbdiagram.io.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SQLite Table(s)
33

44
Table Device {
5+
Archived bool
56
//GORM attributes, see: http://gorm.io/docs/conventions.html
67
CreatedAt time
78
UpdatedAt time

webapp/backend/pkg/database/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type DeviceRepo interface {
2020
UpdateDevice(ctx context.Context, wwn string, collectorSmartData collector.SmartInfo) (models.Device, error)
2121
UpdateDeviceStatus(ctx context.Context, wwn string, status pkg.DeviceStatus) (models.Device, error)
2222
GetDeviceDetails(ctx context.Context, wwn string) (models.Device, error)
23+
UpdateDeviceArchived(ctx context.Context, wwn string, archived bool) error
2324
DeleteDevice(ctx context.Context, wwn string) error
2425

2526
SaveSmartAttributes(ctx context.Context, wwn string, collectorSmartData collector.SmartInfo) (measurements.Smart, error)

webapp/backend/pkg/database/migrations/m20220509170100/device.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66
)
77

8+
// Deprecated: m20220509170100.Device is deprecated, only used by db migrations
89
type Device struct {
910
//GORM attributes, see: http://gorm.io/docs/conventions.html
1011
CreatedAt time.Time
@@ -14,9 +15,9 @@ type Device struct {
1415
WWN string `json:"wwn" gorm:"primary_key"`
1516

1617
DeviceName string `json:"device_name"`
17-
DeviceUUID string `json:"device_uuid"`
18-
DeviceSerialID string `json:"device_serial_id"`
19-
DeviceLabel string `json:"device_label"`
18+
DeviceUUID string `json:"device_uuid"`
19+
DeviceSerialID string `json:"device_serial_id"`
20+
DeviceLabel string `json:"device_label"`
2021

2122
Manufacturer string `json:"manufacturer"`
2223
ModelName string `json:"model_name"`
@@ -38,4 +39,3 @@ type Device struct {
3839
// Data set by Scrutiny
3940
DeviceStatus pkg.DeviceStatus `json:"device_status"`
4041
}
41-
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package m20250221084400
2+
3+
import (
4+
"github.com/analogj/scrutiny/webapp/backend/pkg"
5+
"time"
6+
)
7+
8+
type Device struct {
9+
Archived bool `json:"archived"`
10+
//GORM attributes, see: http://gorm.io/docs/conventions.html
11+
CreatedAt time.Time
12+
UpdatedAt time.Time
13+
DeletedAt *time.Time
14+
15+
WWN string `json:"wwn" gorm:"primary_key"`
16+
17+
DeviceName string `json:"device_name"`
18+
DeviceUUID string `json:"device_uuid"`
19+
DeviceSerialID string `json:"device_serial_id"`
20+
DeviceLabel string `json:"device_label"`
21+
22+
Manufacturer string `json:"manufacturer"`
23+
ModelName string `json:"model_name"`
24+
InterfaceType string `json:"interface_type"`
25+
InterfaceSpeed string `json:"interface_speed"`
26+
SerialNumber string `json:"serial_number"`
27+
Firmware string `json:"firmware"`
28+
RotationSpeed int `json:"rotational_speed"`
29+
Capacity int64 `json:"capacity"`
30+
FormFactor string `json:"form_factor"`
31+
SmartSupport bool `json:"smart_support"`
32+
DeviceProtocol string `json:"device_protocol"` //protocol determines which smart attribute types are available (ATA, NVMe, SCSI)
33+
DeviceType string `json:"device_type"` //device type is used for querying with -d/t flag, should only be used by collector.
34+
35+
// User provided metadata
36+
Label string `json:"label"`
37+
HostId string `json:"host_id"`
38+
39+
// Data set by Scrutiny
40+
DeviceStatus pkg.DeviceStatus `json:"device_status"`
41+
}

webapp/backend/pkg/database/mock/mock_database.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp/backend/pkg/database/scrutiny_repository_device.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// Device
1515
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1616

17-
//insert device into DB (and update specified columns if device is already registered)
17+
// insert device into DB (and update specified columns if device is already registered)
1818
// update device fields that may change: (DeviceType, HostID)
1919
func (sr *scrutinyRepository) RegisterDevice(ctx context.Context, dev models.Device) error {
2020
if err := sr.gormClient.WithContext(ctx).Clauses(clause.OnConflict{
@@ -51,7 +51,7 @@ func (sr *scrutinyRepository) UpdateDevice(ctx context.Context, wwn string, coll
5151
return device, sr.gormClient.Model(&device).Updates(device).Error
5252
}
5353

54-
//Update Device Status
54+
// Update Device Status
5555
func (sr *scrutinyRepository) UpdateDeviceStatus(ctx context.Context, wwn string, status pkg.DeviceStatus) (models.Device, error) {
5656
var device models.Device
5757
if err := sr.gormClient.WithContext(ctx).Where("wwn = ?", wwn).First(&device).Error; err != nil {
@@ -74,6 +74,16 @@ func (sr *scrutinyRepository) GetDeviceDetails(ctx context.Context, wwn string)
7474
return device, nil
7575
}
7676

77+
// Update Device Archived State
78+
func (sr *scrutinyRepository) UpdateDeviceArchived(ctx context.Context, wwn string, archived bool) error {
79+
var device models.Device
80+
if err := sr.gormClient.WithContext(ctx).Where("wwn = ?", wwn).First(&device).Error; err != nil {
81+
return fmt.Errorf("Could not get device from DB: %v", err)
82+
}
83+
84+
return sr.gormClient.Model(&device).Where("wwn = ?", wwn).Update("archived", archived).Error
85+
}
86+
7787
func (sr *scrutinyRepository) DeleteDevice(ctx context.Context, wwn string) error {
7888
if err := sr.gormClient.WithContext(ctx).Where("wwn = ?", wwn).Delete(&models.Device{}).Error; err != nil {
7989
return err

webapp/backend/pkg/database/scrutiny_repository_migrations.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/analogj/scrutiny/webapp/backend/pkg/database/migrations/m20220503120000"
1313
"github.com/analogj/scrutiny/webapp/backend/pkg/database/migrations/m20220509170100"
1414
"github.com/analogj/scrutiny/webapp/backend/pkg/database/migrations/m20220716214900"
15+
"github.com/analogj/scrutiny/webapp/backend/pkg/database/migrations/m20250221084400"
1516
"github.com/analogj/scrutiny/webapp/backend/pkg/models"
1617
"github.com/analogj/scrutiny/webapp/backend/pkg/models/collector"
1718
"github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements"
@@ -399,6 +400,15 @@ func (sr *scrutinyRepository) Migrate(ctx context.Context) error {
399400
return tx.Create(&defaultSettings).Error
400401
},
401402
},
403+
{
404+
ID: "m20250221084400", // add archived to device data
405+
Migrate: func(tx *gorm.DB) error {
406+
407+
//migrate the device database.
408+
// adding column (archived)
409+
return tx.AutoMigrate(m20250221084400.Device{})
410+
},
411+
},
402412
})
403413

404414
if err := m.Migrate(); err != nil {

webapp/backend/pkg/models/device.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type DeviceWrapper struct {
1414

1515
type Device struct {
1616
//GORM attributes, see: http://gorm.io/docs/conventions.html
17+
Archived bool `json:"archived"`
1718
CreatedAt time.Time
1819
UpdatedAt time.Time
1920
DeletedAt *time.Time
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package handler
2+
3+
import (
4+
"github.com/analogj/scrutiny/webapp/backend/pkg/database"
5+
"github.com/gin-gonic/gin"
6+
"github.com/sirupsen/logrus"
7+
"net/http"
8+
)
9+
10+
func ArchiveDevice(c *gin.Context) {
11+
logger := c.MustGet("LOGGER").(*logrus.Entry)
12+
deviceRepo := c.MustGet("DEVICE_REPOSITORY").(database.DeviceRepo)
13+
14+
err := deviceRepo.UpdateDeviceArchived(c, c.Param("wwn"), true)
15+
if err != nil {
16+
logger.Errorln("An error occurred while archiving device", err)
17+
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
18+
return
19+
}
20+
21+
c.JSON(http.StatusOK, gin.H{"success": true})
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package handler
2+
3+
import (
4+
"github.com/analogj/scrutiny/webapp/backend/pkg/database"
5+
"github.com/gin-gonic/gin"
6+
"github.com/sirupsen/logrus"
7+
"net/http"
8+
)
9+
10+
func UnarchiveDevice(c *gin.Context) {
11+
logger := c.MustGet("LOGGER").(*logrus.Entry)
12+
deviceRepo := c.MustGet("DEVICE_REPOSITORY").(database.DeviceRepo)
13+
14+
err := deviceRepo.UpdateDeviceArchived(c, c.Param("wwn"), false)
15+
if err != nil {
16+
logger.Errorln("An error occurred while unarchiving device", err)
17+
c.JSON(http.StatusInternalServerError, gin.H{"success": false})
18+
return
19+
}
20+
21+
c.JSON(http.StatusOK, gin.H{"success": true})
22+
}

0 commit comments

Comments
 (0)