diff --git a/api/datastore/internal/datastoreutil/metrics.go b/api/datastore/internal/datastoreutil/metrics.go index cf1c8afbf..796e4dd02 100644 --- a/api/datastore/internal/datastoreutil/metrics.go +++ b/api/datastore/internal/datastoreutil/metrics.go @@ -118,5 +118,23 @@ func (m *metricds) DeleteLog(ctx context.Context, appName, callID string) error return m.ds.DeleteLog(ctx, appName, callID) } +func (m *metricds) BatchDeleteLogs(ctx context.Context, appName string) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "ds_batch_delete_logs") + defer span.Finish() + return m.ds.BatchDeleteLogs(ctx, appName) +} + +func (m *metricds) BatchDeleteCalls(ctx context.Context, appName string) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "ds_batch_delete_calls") + defer span.Finish() + return m.ds.BatchDeleteCalls(ctx, appName) +} + +func (m *metricds) BatchDeleteRoutes(ctx context.Context, appName string) error { + span, ctx := opentracing.StartSpanFromContext(ctx, "ds_batch_delete_routes") + defer span.Finish() + return m.ds.BatchDeleteRoutes(ctx, appName) +} + // instant & no context ;) func (m *metricds) GetDatabase() *sqlx.DB { return m.ds.GetDatabase() } diff --git a/api/datastore/internal/datastoreutil/validator.go b/api/datastore/internal/datastoreutil/validator.go index c08728820..a5bbec3e0 100644 --- a/api/datastore/internal/datastoreutil/validator.go +++ b/api/datastore/internal/datastoreutil/validator.go @@ -142,6 +142,18 @@ func (v *validator) DeleteLog(ctx context.Context, appName, callID string) error return v.Datastore.DeleteLog(ctx, appName, callID) } +func (v *validator) BatchDeleteLogs(ctx context.Context, appName string) error { + return v.Datastore.BatchDeleteLogs(ctx, appName) +} + +func (v *validator) BatchDeleteCalls(ctx context.Context, appName string) error { + return v.Datastore.BatchDeleteCalls(ctx, appName) +} + +func (v *validator) BatchDeleteRoutes(ctx context.Context, appName string) error { + return v.Datastore.BatchDeleteRoutes(ctx, appName) +} + // GetDatabase returns the underlying sqlx database implementation func (v *validator) GetDatabase() *sqlx.DB { return v.Datastore.GetDatabase() diff --git a/api/datastore/mock.go b/api/datastore/mock.go index ef5403832..31b4e137f 100644 --- a/api/datastore/mock.go +++ b/api/datastore/mock.go @@ -157,6 +157,28 @@ func (m *mock) GetCalls(ctx context.Context, filter *models.CallFilter) ([]*mode return m.Calls, nil } +func (m *mock) BatchDeleteCalls(ctx context.Context, appName string) error { + newCalls := []*models.Call{} + for _, c := range m.Calls { + if c.AppName != appName { + newCalls = append(newCalls, c) + } + } + m.Calls = newCalls + return nil +} + +func (m *mock) BatchDeleteRoutes(ctx context.Context, appName string) error { + newRoutes := []*models.Route{} + for _, c := range m.Routes { + if c.AppName != appName { + newRoutes = append(newRoutes, c) + } + } + m.Routes = newRoutes + return nil +} + // GetDatabase returns nil here since shouldn't really be used func (m *mock) GetDatabase() *sqlx.DB { return nil diff --git a/api/datastore/sql/sql.go b/api/datastore/sql/sql.go index c80320514..c2e61325f 100644 --- a/api/datastore/sql/sql.go +++ b/api/datastore/sql/sql.go @@ -621,6 +621,24 @@ func (ds *sqlStore) DeleteLog(ctx context.Context, appName, callID string) error return err } +func (ds *sqlStore) BatchDeleteLogs(ctx context.Context, appName string) error { + query := ds.db.Rebind(`DELETE FROM logs WHERE app_name=?`) + _, err := ds.db.ExecContext(ctx, query, appName) + return err +} + +func (ds *sqlStore) BatchDeleteCalls(ctx context.Context, appName string) error { + query := ds.db.Rebind(`DELETE FROM calls WHERE app_name=?`) + _, err := ds.db.ExecContext(ctx, query, appName) + return err +} + +func (ds *sqlStore) BatchDeleteRoutes(ctx context.Context, appName string) error { + query := ds.db.Rebind(`DELETE FROM routes WHERE app_name=?`) + _, err := ds.db.ExecContext(ctx, query, appName) + return err +} + // TODO scrap for sqlx scanx ?? some things aren't perfect (e.g. config is a json string) type RowScanner interface { Scan(dest ...interface{}) error diff --git a/api/logs/mock.go b/api/logs/mock.go index 0a1f298c4..cda74c9c4 100644 --- a/api/logs/mock.go +++ b/api/logs/mock.go @@ -45,3 +45,12 @@ func (m *mock) DeleteLog(ctx context.Context, appName, callID string) error { delete(m.Logs, callID) return nil } + +func (m *mock) BatchDeleteLogs(ctx context.Context, appName string) error { + for _, log := range m.Logs { + if log.AppName == appName { + m.DeleteLog(ctx, appName, log.CallID) + } + } + return nil +} diff --git a/api/models/call.go b/api/models/call.go index eef0bd3be..8a33cb3d6 100644 --- a/api/models/call.go +++ b/api/models/call.go @@ -23,8 +23,9 @@ const ( var possibleStatuses = [...]string{"delayed", "queued", "running", "success", "error", "cancelled"} type CallLog struct { - CallID string `json:"call_id"` - Log string `json:"log"` + CallID string `json:"call_id"` + Log string `json:"log"` + AppName string `json:"app_name"` } // Call is a representation of a specific invocation of a route. diff --git a/api/models/datastore.go b/api/models/datastore.go index b4e1ed798..3268619b3 100644 --- a/api/models/datastore.go +++ b/api/models/datastore.go @@ -68,6 +68,8 @@ type Datastore interface { // calls exist, an empty list and a nil error are returned. GetCalls(ctx context.Context, filter *CallFilter) ([]*Call, error) + BatchDeleteCalls(ctx context.Context, appName string) error + BatchDeleteRoutes(ctx context.Context, appName string) error // Implement LogStore methods for convenience LogStore diff --git a/api/models/logs.go b/api/models/logs.go index a4cc7da88..0ab0268db 100644 --- a/api/models/logs.go +++ b/api/models/logs.go @@ -20,4 +20,6 @@ type LogStore interface { // DeleteLog will remove the log at callID, it will not return an error if // the log does not exist before removal. DeleteLog(ctx context.Context, appName, callID string) error + + BatchDeleteLogs(ctx context.Context, appName string) error }