add SaveMode for device which can be 0 (All) or a bitmask for what to save. It did not improve performance
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-09-26 10:50:54 +02:00
parent b1fa52e53f
commit 0cd325e8db
5 changed files with 71 additions and 52 deletions
+4 -4
View File
@@ -80,7 +80,7 @@ func Devices(r gin.IRoutes) {
go func() { go func() {
for _, device := range changed { for _, device := range changed {
err := config.DeviceRepository().Save(context.Background(), &device) err := config.DeviceRepository().Save(context.Background(), &device, models.SMState)
if err != nil { if err != nil {
log.Println("Failed to save device for state:", err) log.Println("Failed to save device for state:", err)
continue continue
@@ -109,7 +109,7 @@ func Devices(r gin.IRoutes) {
for i := range devices { for i := range devices {
devices[i].ApplyUpdate(update) devices[i].ApplyUpdate(update)
err := config.DeviceRepository().Save(context.Background(), &devices[i]) err := config.DeviceRepository().Save(context.Background(), &devices[i], models.SMProperties)
if err != nil { if err != nil {
log.Println("Failed to save device for state:", err) log.Println("Failed to save device for state:", err)
continue continue
@@ -145,7 +145,7 @@ func Devices(r gin.IRoutes) {
go func() { go func() {
for _, device := range devices { for _, device := range devices {
err := config.DeviceRepository().Save(context.Background(), &device) err := config.DeviceRepository().Save(context.Background(), &device, models.SMState)
if err != nil { if err != nil {
log.Println("Failed to save device for state:", err) log.Println("Failed to save device for state:", err)
continue continue
@@ -204,7 +204,7 @@ func Devices(r gin.IRoutes) {
device.Tags = append(device.Tags[:index], device.Tags[index+1:]...) device.Tags = append(device.Tags[:index], device.Tags[index+1:]...)
} }
err = config.DeviceRepository().Save(ctxOf(c), device) err = config.DeviceRepository().Save(ctxOf(c), device, models.SMTags)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+1 -1
View File
@@ -157,7 +157,7 @@ func handleEvent(event models.Event) (responses []models.Event) {
wg.Add(1) wg.Add(1)
go func(device models.Device) { go func(device models.Device) {
err := config.DeviceRepository().Save(context.Background(), &device) err := config.DeviceRepository().Save(context.Background(), &device, models.SMState)
if err != nil { if err != nil {
log.Println("Failed to save device for state:", err) log.Println("Failed to save device for state:", err)
} }
+1 -1
View File
@@ -66,7 +66,7 @@ func checkNewDevices() error {
log.Println("Saving new device", driverDevice.InternalID) log.Println("Saving new device", driverDevice.InternalID)
err := config.DeviceRepository().Save(ctx, &driverDevice) err := config.DeviceRepository().Save(ctx, &driverDevice, models.SMState|models.SMProperties|models.SMTags)
if err != nil { if err != nil {
log.Println("Failed to save device:", err) log.Println("Failed to save device:", err)
continue continue
+54 -43
View File
@@ -117,7 +117,7 @@ func (r *DeviceRepo) FetchByReference(ctx context.Context, kind models.Reference
return r.populate(ctx, records) return r.populate(ctx, records)
} }
func (r *DeviceRepo) Save(ctx context.Context, device *models.Device) error { func (r *DeviceRepo) Save(ctx context.Context, device *models.Device, mode models.SaveMode) error {
tx, err := r.DBX.Beginx() tx, err := r.DBX.Beginx()
if err != nil { if err != nil {
return dbErr(err) return dbErr(err)
@@ -149,13 +149,18 @@ func (r *DeviceRepo) Save(ctx context.Context, device *models.Device) error {
} }
// Let's just be lazy for now, optimize later if need be. // Let's just be lazy for now, optimize later if need be.
_, err = tx.ExecContext(ctx, "DELETE FROM device_tag WHERE device_id=?", record.ID) if mode == 0 || mode&models.SMTags != 0 {
if err != nil { _, err = tx.ExecContext(ctx, "DELETE FROM device_tag WHERE device_id=?", record.ID)
return dbErr(err) if err != nil {
return dbErr(err)
}
} }
_, err = tx.ExecContext(ctx, "DELETE FROM device_property WHERE device_id=?", record.ID)
if err != nil { if mode == 0 || mode&models.SMProperties != 0 {
return dbErr(err) _, err = tx.ExecContext(ctx, "DELETE FROM device_property WHERE device_id=?", record.ID)
if err != nil {
return dbErr(err)
}
} }
} else { } else {
res, err := tx.NamedExecContext(ctx, ` res, err := tx.NamedExecContext(ctx, `
@@ -175,51 +180,57 @@ func (r *DeviceRepo) Save(ctx context.Context, device *models.Device) error {
device.ID = int(lastID) device.ID = int(lastID)
} }
for _, tag := range device.Tags { if mode == 0 || mode&models.SMTags != 0 {
_, err := tx.ExecContext(ctx, "INSERT INTO device_tag (device_id, tag_name) VALUES (?, ?)", record.ID, tag) for _, tag := range device.Tags {
if err != nil { _, err := tx.ExecContext(ctx, "INSERT INTO device_tag (device_id, tag_name) VALUES (?, ?)", record.ID, tag)
return dbErr(err) if err != nil {
return dbErr(err)
}
} }
} }
for key, value := range device.UserProperties { if mode == 0 || mode&models.SMProperties != 0 {
_, err := tx.ExecContext(ctx, "INSERT INTO device_property (device_id, prop_key, prop_value, is_user) VALUES (?, ?, ?, 1)", for key, value := range device.UserProperties {
record.ID, key, value, _, err := tx.ExecContext(ctx, "INSERT INTO device_property (device_id, prop_key, prop_value, is_user) VALUES (?, ?, ?, 1)",
) record.ID, key, value,
if err != nil { )
return dbErr(err) if err != nil {
return dbErr(err)
}
}
for key, value := range device.DriverProperties {
j, err := json.Marshal(value)
if err != nil {
// Eh, it'll get filled by the driver anyway
continue
}
_, err = tx.ExecContext(ctx, "INSERT INTO device_property (device_id, prop_key, prop_value, is_user) VALUES (?, ?, ?, 0)",
record.ID, key, string(j),
)
if err != nil {
// Return err here anyway, it might put the tx in a bad state to ignore it.
return dbErr(err)
}
} }
} }
for key, value := range device.DriverProperties { if mode == 0 || mode&models.SMState != 0 {
j, err := json.Marshal(value) _, err = tx.NamedExecContext(ctx, `
if err != nil {
// Eh, it'll get filled by the driver anyway
continue
}
_, err = tx.ExecContext(ctx, "INSERT INTO device_property (device_id, prop_key, prop_value, is_user) VALUES (?, ?, ?, 0)",
record.ID, key, string(j),
)
if err != nil {
// Return err here anyway, it might put the tx in a bad state to ignore it.
return dbErr(err)
}
}
_, err = tx.NamedExecContext(ctx, `
REPLACE INTO device_state(device_id, hue, saturation, kelvin, power, intensity) REPLACE INTO device_state(device_id, hue, saturation, kelvin, power, intensity)
VALUES (:device_id, :hue, :saturation, :kelvin, :power, :intensity) VALUES (:device_id, :hue, :saturation, :kelvin, :power, :intensity)
`, deviceStateRecord{ `, deviceStateRecord{
DeviceID: record.ID, DeviceID: record.ID,
Hue: device.State.Color.Hue, Hue: device.State.Color.Hue,
Saturation: device.State.Color.Saturation, Saturation: device.State.Color.Saturation,
Kelvin: device.State.Color.Kelvin, Kelvin: device.State.Color.Kelvin,
Power: device.State.Power, Power: device.State.Power,
Intensity: device.State.Intensity, Intensity: device.State.Intensity,
}) })
if err != nil { if err != nil {
return dbErr(err) return dbErr(err)
}
} }
return tx.Commit() return tx.Commit()
+11 -3
View File
@@ -20,8 +20,8 @@ type Device struct {
} }
type DeviceUpdate struct { type DeviceUpdate struct {
Icon *string `json:"icon"` Icon *string `json:"icon"`
Name *string `json:"name"` Name *string `json:"name"`
UserProperties map[string]*string `json:"userProperties"` UserProperties map[string]*string `json:"userProperties"`
} }
@@ -46,10 +46,18 @@ type NewDeviceState struct {
type DeviceCapability string type DeviceCapability string
type SaveMode int
const (
SMState SaveMode = 1
SMProperties SaveMode = 2
SMTags SaveMode = 4
)
type DeviceRepository interface { type DeviceRepository interface {
Find(ctx context.Context, id int) (*Device, error) Find(ctx context.Context, id int) (*Device, error)
FetchByReference(ctx context.Context, kind ReferenceKind, value string) ([]Device, error) FetchByReference(ctx context.Context, kind ReferenceKind, value string) ([]Device, error)
Save(ctx context.Context, device *Device) error Save(ctx context.Context, device *Device, mode SaveMode) error
Delete(ctx context.Context, device *Device) error Delete(ctx context.Context, device *Device) error
} }