Skip to content

Commit ea7102e

Browse files
committed
feat: implement device-wise notification mute/unmute (upstream PR AnalogJ#822)
2 parents f954cc8 + 925e86d commit ea7102e

File tree

20 files changed

+207
-17
lines changed

20 files changed

+207
-17
lines changed

webapp/backend/pkg/database/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type DeviceRepo interface {
2121
UpdateDeviceStatus(ctx context.Context, wwn string, status pkg.DeviceStatus) (models.Device, error)
2222
GetDeviceDetails(ctx context.Context, wwn string) (models.Device, error)
2323
UpdateDeviceArchived(ctx context.Context, wwn string, archived bool) error
24+
UpdateDeviceMuted(ctx context.Context, wwn string, archived bool) error
2425
DeleteDevice(ctx context.Context, wwn string) error
2526

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
)
77

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

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ func (sr *scrutinyRepository) UpdateDeviceArchived(ctx context.Context, wwn stri
8484
return sr.gormClient.Model(&device).Where("wwn = ?", wwn).Update("archived", archived).Error
8585
}
8686

87+
// Update Device Muted State
88+
func (sr *scrutinyRepository) UpdateDeviceMuted(ctx context.Context, wwn string, muted bool) error {
89+
var device models.Device
90+
if err := sr.gormClient.WithContext(ctx).Where("wwn = ?", wwn).First(&device).Error; err != nil {
91+
return fmt.Errorf("Could not get device from DB: %v", err)
92+
}
93+
94+
return sr.gormClient.Model(&device).Where("wwn = ?", wwn).Update("muted", muted).Error
95+
}
96+
8797
func (sr *scrutinyRepository) DeleteDevice(ctx context.Context, wwn string) error {
8898
if err := sr.gormClient.WithContext(ctx).Where("wwn = ?", wwn).Delete(&models.Device{}).Error; err != nil {
8999
return err

webapp/backend/pkg/database/scrutiny_repository_migrations.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/analogj/scrutiny/webapp/backend/pkg/database/migrations/m20220509170100"
1414
"github.com/analogj/scrutiny/webapp/backend/pkg/database/migrations/m20220716214900"
1515
"github.com/analogj/scrutiny/webapp/backend/pkg/database/migrations/m20250221084400"
16+
"github.com/analogj/scrutiny/webapp/backend/pkg/database/migrations/m20251108044508"
1617
"github.com/analogj/scrutiny/webapp/backend/pkg/models"
1718
"github.com/analogj/scrutiny/webapp/backend/pkg/models/collector"
1819
"github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements"
@@ -424,6 +425,14 @@ func (sr *scrutinyRepository) Migrate(ctx context.Context) error {
424425
return tx.Create(&defaultSettings).Error
425426
},
426427
},
428+
{
429+
ID: "m20251108044508", // add muted to device data
430+
Migrate: func(tx *gorm.DB) error {
431+
//migrate the device database.
432+
// adding column (muted)
433+
return tx.AutoMigrate(m20251108044508.Device{})
434+
},
435+
},
427436
})
428437

429438
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
@@ -15,6 +15,7 @@ type DeviceWrapper struct {
1515
type Device struct {
1616
//GORM attributes, see: http://gorm.io/docs/conventions.html
1717
Archived bool `json:"archived"`
18+
Muted bool `json:"muted"`
1819
CreatedAt time.Time
1920
UpdatedAt time.Time
2021
DeletedAt *time.Time

webapp/backend/pkg/notify/notify.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ func ShouldNotify(logger logrus.FieldLogger, device models.Device, smartAttrs me
3838
return false
3939
}
4040

41+
// If the device is muted, skip notification regardless of status
42+
if device.Muted {
43+
return false
44+
}
45+
4146
//TODO: cannot check for warning notifyLevel yet.
4247

4348
// setup constants for comparison
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 MuteDevice(c *gin.Context) {
11+
logger := c.MustGet("LOGGER").(*logrus.Entry)
12+
deviceRepo := c.MustGet("DEVICE_REPOSITORY").(database.DeviceRepo)
13+
14+
err := deviceRepo.UpdateDeviceMuted(c, c.Param("wwn"), true)
15+
if err != nil {
16+
logger.Errorln("An error occurred while muting 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 UnmuteDevice(c *gin.Context) {
11+
logger := c.MustGet("LOGGER").(*logrus.Entry)
12+
deviceRepo := c.MustGet("DEVICE_REPOSITORY").(database.DeviceRepo)
13+
14+
err := deviceRepo.UpdateDeviceMuted(c, c.Param("wwn"), false)
15+
if err != nil {
16+
logger.Errorln("An error occurred while muting 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)