Skip to content

Commit

Permalink
working deletion code.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed May 26, 2022
1 parent 28a3c3e commit 987632d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions webapp/backend/pkg/database/scrutiny_repository_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/analogj/scrutiny/webapp/backend/pkg/models"
"github.com/analogj/scrutiny/webapp/backend/pkg/models/collector"
"gorm.io/gorm/clause"
"time"
)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -74,5 +75,31 @@ func (sr *scrutinyRepository) GetDeviceDetails(ctx context.Context, wwn string)
}

func (sr *scrutinyRepository) DeleteDevice(ctx context.Context, wwn string) error {
if err := sr.gormClient.WithContext(ctx).Where("wwn = ?", wwn).Delete(&models.Device{}).Error; err != nil {
return err
}

//delete data from influxdb.
buckets := []string{
sr.appConfig.GetString("web.influxdb.bucket"),
fmt.Sprintf("%s_weekly", sr.appConfig.GetString("web.influxdb.bucket")),
fmt.Sprintf("%s_monthly", sr.appConfig.GetString("web.influxdb.bucket")),
fmt.Sprintf("%s_yearly", sr.appConfig.GetString("web.influxdb.bucket")),
}

for _, bucket := range buckets {
sr.logger.Infof("Deleting data for %s in bucket: %s", wwn, bucket)
if err := sr.influxClient.DeleteAPI().DeleteWithName(
ctx,
sr.appConfig.GetString("web.influxdb.org"),
bucket,
time.Now().AddDate(-10, 0, 0),
time.Now(),
fmt.Sprintf(`device_wwn="%s"`, wwn),
); err != nil {
return err
}
}

return nil
}

0 comments on commit 987632d

Please sign in to comment.