Skip to content

Commit

Permalink
Record: rename 'collector' interface to 'recorder'.
Browse files Browse the repository at this point in the history
  • Loading branch information
lesovsky committed Jan 9, 2021
1 parent d274e4c commit a2ff31a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
26 changes: 13 additions & 13 deletions record/record.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 'pgcenter record' - collects Postgres statistics and write it to file.
// 'pgcenter record' - collects Postgres statistics and record to persistent store.

package record

Expand All @@ -14,7 +14,7 @@ import (

// Config defines config container for configuring 'pgcenter record'.
type Config struct {
Interval time.Duration // Statistics collecting interval
Interval time.Duration // Statistics recording interval
Count int // Number of statistics snapshot to record
OutputFile string // File where statistics will be saved
TruncateFile bool // Truncate a file before beginning
Expand Down Expand Up @@ -42,10 +42,10 @@ func RunMain(dbConfig *postgres.Config, config Config) error {

// app defines 'pgcenter record' runtime dependencies.
type app struct {
config Config
dbConfig *postgres.Config
views view.Views
collector collector
config Config
dbConfig *postgres.Config
views view.Views
recorder recorder
}

// newApp creates new 'pgcenter record' app.
Expand Down Expand Up @@ -78,8 +78,8 @@ func (app *app) setup() error {

app.views = views

// Create tar collector.
app.collector = newTarCollector(tarConfig{
// Create tar recorder.
app.recorder = newTarRecorder(tarConfig{
filename: app.config.OutputFile,
truncate: app.config.TruncateFile,
})
Expand All @@ -98,22 +98,22 @@ func (app *app) record(doQuit chan os.Signal) error {

// record the number of snapshots requested by user (or record continuously until SIGINT will be received)
for i := count; i > 0; i-- {
err := app.collector.open()
err := app.recorder.open()
if err != nil {
return err
}

stats, err := app.collector.collect(app.dbConfig, app.views)
stats, err := app.recorder.collect(app.dbConfig, app.views)
if err != nil {
return err
}

err = app.collector.write(stats)
err = app.recorder.write(stats)
if err != nil {
return err
}

err = app.collector.close()
err = app.recorder.close()
if err != nil {
return err
}
Expand All @@ -124,7 +124,7 @@ func (app *app) record(doQuit chan os.Signal) error {
case <-doQuit:
t.Stop()

err = app.collector.close()
err = app.recorder.close()
if err != nil {
fmt.Println(err)
}
Expand Down
2 changes: 1 addition & 1 deletion record/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_app_setup(t *testing.T) {
for _, v := range app.views {
assert.NotEqual(t, "", v.Query) // view's queries must not be empty (must be created using templates)
}
assert.NotNil(t, app.collector)
assert.NotNil(t, app.recorder)
}

func Test_app_record(t *testing.T) {
Expand Down
26 changes: 13 additions & 13 deletions record/collector.go → record/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,46 @@ import (
"time"
)

// collector defines a way of how to collect and store collected stats.
type collector interface {
// recorder defines a way of how to record and store collected stats.
type recorder interface {
open() error
collect(dbConfig *postgres.Config, views view.Views) (map[string]stat.PGresult, error)
write(map[string]stat.PGresult) error
close() error
}

// tarConfig defines configuration needed for creating tar collector.
// tarConfig defines configuration needed for creating tar recorder.
type tarConfig struct {
filename string
truncate bool
}

// tarCollector implement collector interface.
// tarRecorder implement recorder interface.
// This implementation collects Postgres stats and stores it in .json files packed into .tar archive.
type tarCollector struct {
type tarRecorder struct {
config tarConfig
file *os.File
fileFlags int
writer *tar.Writer
}

// newTarCollector creates new collector.
func newTarCollector(c tarConfig) collector {
// newTarRecorder creates new recorder.
func newTarRecorder(c tarConfig) recorder {
var flags int
if c.truncate {
flags = os.O_CREATE | os.O_RDWR | os.O_TRUNC
} else {
flags = os.O_CREATE | os.O_RDWR
}

return &tarCollector{
return &tarRecorder{
config: c,
fileFlags: flags,
}
}

// open method opens tar archive.
func (c *tarCollector) open() error {
func (c *tarRecorder) open() error {
f, err := os.OpenFile(filepath.Clean(c.config.filename), c.fileFlags, 0640)
if err != nil {
return err
Expand Down Expand Up @@ -91,7 +91,7 @@ func (c *tarCollector) open() error {
}

// collect connects to Postgres, collects and returns stats data.
func (c *tarCollector) collect(dbConfig *postgres.Config, views view.Views) (map[string]stat.PGresult, error) {
func (c *tarRecorder) collect(dbConfig *postgres.Config, views view.Views) (map[string]stat.PGresult, error) {
db, err := postgres.Connect(dbConfig)
if err != nil {
return nil, err
Expand All @@ -113,7 +113,7 @@ func (c *tarCollector) collect(dbConfig *postgres.Config, views view.Views) (map
}

// write accepts stats data and writes it into tar archive.
func (c *tarCollector) write(stats map[string]stat.PGresult) error {
func (c *tarRecorder) write(stats map[string]stat.PGresult) error {
for name, v := range stats {
data, err := json.Marshal(v)
if err != nil {
Expand All @@ -134,8 +134,8 @@ func (c *tarCollector) write(stats map[string]stat.PGresult) error {
return nil
}

// close closes collector's file and tar writer descriptors.
func (c *tarCollector) close() error {
// close closes recorder's file and tar writer descriptors.
func (c *tarRecorder) close() error {
if c.writer != nil {
err := c.writer.Close()
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions record/collector_test.go → record/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ import (
"testing"
)

func Test_tarCollector_open_close(t *testing.T) {
tc := newTarCollector(tarConfig{filename: "/tmp/pgcenter-record-testing.stat.tar", truncate: true})
func Test_tarRecorder_open_close(t *testing.T) {
tc := newTarRecorder(tarConfig{filename: "/tmp/pgcenter-record-testing.stat.tar", truncate: true})
assert.NoError(t, tc.open())
assert.NoError(t, tc.close())

tc = newTarCollector(tarConfig{filename: "/tmp/pgcenter-record-testing.stat.tar", truncate: false})
tc = newTarRecorder(tarConfig{filename: "/tmp/pgcenter-record-testing.stat.tar", truncate: false})
assert.NoError(t, tc.open())
assert.NoError(t, tc.close())
}

func Test_tarCollector_collect(t *testing.T) {
tc := newTarCollector(tarConfig{filename: "/tmp/pgcenter-record-testing.stat.tar"})
func Test_tarRecorder(t *testing.T) {
tc := newTarRecorder(tarConfig{filename: "/tmp/pgcenter-record-testing.stat.tar"})
assert.NoError(t, tc.open())

// create and configure views
Expand All @@ -51,7 +51,7 @@ func Test_tarCollector_collect(t *testing.T) {
assert.NoError(t, tc.close())
}

func Test_tarCollector_write(t *testing.T) {
func Test_tarRecorder_write(t *testing.T) {
stats := map[string]stat.PGresult{
"pgcenter_record_testing": {
Valid: true, Ncols: 2, Nrows: 4, Cols: []string{"col1", "col2"},
Expand All @@ -67,7 +67,7 @@ func Test_tarCollector_write(t *testing.T) {
filename := "/tmp/pgcenter-record-testing.stat.tar"

// Write testdata.
tc := newTarCollector(tarConfig{filename: filename, truncate: true})
tc := newTarRecorder(tarConfig{filename: filename, truncate: true})
assert.NoError(t, tc.open())
assert.NoError(t, tc.write(stats))
assert.NoError(t, tc.close())
Expand Down

0 comments on commit a2ff31a

Please sign in to comment.