Implementing batch deletes for calls, logs and routes

Partially-Closes: #302
This commit is contained in:
Denis Makogon
2017-09-11 11:41:03 +03:00
parent 774d53662f
commit 3e190342fb
8 changed files with 86 additions and 2 deletions

View File

@@ -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() }

View File

@@ -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()

View File

@@ -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

View File

@@ -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

View File

@@ -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
}

View File

@@ -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.

View File

@@ -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

View File

@@ -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
}