Skip to content

Commit

Permalink
better comments
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed May 26, 2019
1 parent 2ad750f commit dc66d42
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/client/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type APIParams struct {
RepeaterStrategy strategy.Interface
}

// DisplayParams customise how records will be showed
// DisplayParams customizes how records will be showed
type DisplayParams struct {
ShowPid bool // include pid
ShowTs bool // include time stamp as "2006-01-02 15:04:05.999999" in given TZ
Expand Down
13 changes: 6 additions & 7 deletions app/core/logentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
)

// LogEntry represents a single event for forwarder, mongo (JSON) and client
// LogEntry represents a single event for forwarder and rest server and client
type LogEntry struct {
ID string `json:"id"`
Host string `json:"host"`
Expand All @@ -20,9 +20,8 @@ type LogEntry struct {
CreatedTs time.Time `json:"cts"`
}

// NewEntry makes entry from log line.
// example:
// Oct 19 15:29:43 host-1 docker/mongo[888]: 2015-10-19T19:29:43 blah blah blah
// NewEntry makes the LogEntry from a log line.
// example: "Oct 19 15:29:43 host-1 docker/mongo[888]: 2015-10-19T19:29:43 blah blah blah"
func NewEntry(line string, tz *time.Location) (entry LogEntry, err error) {

if len(line) < 16 { // 16 is minimal size of "Jan _2 15:04:05" timestamp
Expand All @@ -48,9 +47,9 @@ func NewEntry(line string, tz *time.Location) (entry LogEntry, err error) {
pidElems := strings.Split(containerAndPid, "[")
entry.Container = pidElems[0]
if len(pidElems) > 1 {
pidsStr := strings.TrimSuffix(pidElems[1], ":")
pidsStr = strings.TrimSuffix(pidsStr, "]")
if pid, err := strconv.Atoi(pidsStr); err == nil {
pidStr := strings.TrimSuffix(pidElems[1], ":")
pidStr = strings.TrimSuffix(pidStr, "]")
if pid, err := strconv.Atoi(pidStr); err == nil {
entry.Pid = pid
}
}
Expand Down
4 changes: 3 additions & 1 deletion app/server/file_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ type FileLogger struct {
lock sync.Mutex
}

type WritersFactory func(instance, container string) io.Writer
// WritersFactory is a type for func returning io.Writer for given host and container
type WritersFactory func(host, container string) io.Writer

type dkKey struct {
host string
container string
}

// NewFileLogger creates FileLogger for provided WritersFactory (per host/container) and merged writer
func NewFileLogger(wrf WritersFactory, m io.Writer) *FileLogger {
return &FileLogger{
merged: m,
Expand Down
2 changes: 2 additions & 0 deletions app/server/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (m *Mongo) Publish(records []core.LogEntry) (err error) {
return err
}

// LastPublished returns latest published entry
func (m *Mongo) LastPublished() (entry core.LogEntry, err error) {
var mentry mongoLogEntry
err = m.WithCollection(func(coll *mgo.Collection) error {
Expand All @@ -62,6 +63,7 @@ func (m *Mongo) LastPublished() (entry core.LogEntry, err error) {
return m.makeLogEntry(mentry), err
}

// Find records matching given request
func (m *Mongo) Find(req core.Request) ([]core.LogEntry, error) {

query := m.makeQuery(req)
Expand Down
10 changes: 6 additions & 4 deletions app/server/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"github.com/umputun/dkll/app/core"
)

// RestServer basic rest server to access msgs from mongo
// RestServer is a basic rest server to access msgs from DataService
type RestServer struct {
Port int
DataService DataService
Limit int
Limit int // request limit, i.e. max number of records any single Find can return
Version string
}

Expand Down Expand Up @@ -67,8 +67,8 @@ func (s *RestServer) router() chi.Router {
return router
}

// findCtrl gets Request json from POST body.
// containers,hosts and excludes lists supports regexp in mongo format, i.e. /regex/
// POST /v1/find, body is Request. Returns list of LogEntry
// containers,hosts and excludes lists support regexp in "//", i.e. /regex/
func (s *RestServer) findCtrl(w http.ResponseWriter, r *http.Request) {

req := core.Request{}
Expand All @@ -94,6 +94,8 @@ func (s *RestServer) findCtrl(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, recs)
}

// GET /v1/last
// Returns latest published LogEntry from DataService
func (s *RestServer) lastCtrl(w http.ResponseWriter, r *http.Request) {
last, err := s.DataService.LastPublished()
if err != nil {
Expand Down

0 comments on commit dc66d42

Please sign in to comment.