-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
253 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package inserter | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
"github.com/edwvee/dbatcher/internal/table" | ||
jsoniter "github.com/json-iterator/go" | ||
) | ||
|
||
type InsertErrorLogger struct { | ||
w io.Writer | ||
prettyPrint bool | ||
mut sync.Mutex | ||
} | ||
|
||
type insertErrorLoggerData struct { | ||
TimeStamp int64 `json:"timestamp"` | ||
TimeStampString string `json:"timestamp_string"` | ||
Error string `json:"error"` | ||
Table string `json:"table"` | ||
Fields string `json:"string"` | ||
Rows [][]interface{} `json:"rows"` | ||
} | ||
|
||
func NewInsertErrorLoggerFromConfig(config InsertErrorLoggerConfig) (*InsertErrorLogger, error) { | ||
if config.Path == "" { | ||
return NewInsertErrorLogger(nil, false), nil | ||
} | ||
f, err := os.OpenFile(config.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewInsertErrorLogger(f, config.PrettyPrint), nil | ||
} | ||
|
||
func NewInsertErrorLogger(w io.Writer, prettyPrint bool) *InsertErrorLogger { | ||
return &InsertErrorLogger{w: w, prettyPrint: prettyPrint} | ||
} | ||
|
||
func (l *InsertErrorLogger) Log(insertError error, t *table.Table) error { | ||
if l.w == nil { | ||
return nil | ||
} | ||
|
||
data := l.MakeData(insertError, t) | ||
|
||
l.mut.Lock() | ||
defer l.mut.Unlock() | ||
encoder := jsoniter.NewEncoder(l.w) | ||
if l.prettyPrint { | ||
encoder.SetIndent("", " ") | ||
} | ||
err := encoder.Encode(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (l *InsertErrorLogger) MakeData(insertError error, t *table.Table) insertErrorLoggerData { | ||
now := time.Now() | ||
data := insertErrorLoggerData{ | ||
TimeStamp: now.Unix(), | ||
TimeStampString: now.String(), | ||
Error: insertError.Error(), | ||
Table: t.GetTableName(), | ||
Fields: t.GetFields(), | ||
Rows: make([][]interface{}, 0, t.GetRowsLen()), | ||
} | ||
for row := t.GetNextRow(); row != nil; row = t.GetNextRow() { | ||
data.Rows = append(data.Rows, row) | ||
} | ||
|
||
return data | ||
} | ||
|
||
func (l *InsertErrorLogger) Close() error { | ||
if c, ok := l.w.(io.Closer); ok { | ||
return c.Close() | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package inserter | ||
|
||
type InsertErrorLoggerConfig struct { | ||
Path string `toml:"path"` | ||
PrettyPrint bool `toml:"pretty_print"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package inserter | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"github.com/edwvee/dbatcher/internal/table" | ||
jsoniter "github.com/json-iterator/go" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func TestNewInsertErrorLoggerFromConfig(t *testing.T) { | ||
config := InsertErrorLoggerConfig{"test_path.log", true} | ||
logger, err := NewInsertErrorLoggerFromConfig(config) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
err = logger.Close() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
err = os.Remove(config.Path) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestInsertErrorLoggerLog(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
logger := NewInsertErrorLogger(buf, false) | ||
tableName := "database.table" | ||
fields := "field1,field2,field3" | ||
table := table.NewTable(table.NewSignature(tableName, fields)) | ||
data := []byte("[[\"test\",0,2.4],[\"test_test\",17,4.4]]") | ||
table.AppendRows(data) | ||
errorMessage := "test error" | ||
err := logger.Log(errors.New(errorMessage), table) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var result insertErrorLoggerData | ||
resultData := buf.Bytes() | ||
err = jsoniter.Unmarshal(resultData, &result) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if result.Error != errorMessage { | ||
t.Errorf("wrong error message: got %s, want %s", result.Error, errorMessage) | ||
} | ||
if result.Table != tableName { | ||
t.Errorf("wrong table name: got %s, want %s", result.Table, tableName) | ||
} | ||
if result.Fields != fields { | ||
t.Errorf("wrong table fields: got %s, want %s", result.Fields, fields) | ||
} | ||
marshalledRows, err := jsoniter.Marshal(result.Rows) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(marshalledRows) != string(data) { | ||
t.Errorf("wrong rows after json marshalling: got %s, want %s", marshalledRows, data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.