Skip to content

Commit

Permalink
- Change time to update config object
Browse files Browse the repository at this point in the history
- Add a filter to remove duplicate logs and set the next "to" as the same time of the last log.
  • Loading branch information
kamatama41 committed Jul 16, 2019
1 parent c279e4e commit aef072d
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ var (
)

type config struct {
query string
from myTime
to myTime
limit int
tmpl *template.Template
nextLogId string
follow bool
query string
from myTime
to myTime
limit int
tmpl *template.Template
follow bool
lastInfo *logsInfo
}

type logsInfo struct {
Expand Down Expand Up @@ -76,22 +76,29 @@ func main() {
log.Fatal(err)
}

cfg, err = showLogs(cfg)
logs, err := showLogs(cfg)
if err != nil {
log.Fatal(err)
}

for cfg.follow {
time.Sleep(time.Duration(*interval) * time.Second)

cfg, err = showLogs(cfg)
err = cfg.update(logs)
if err != nil {
log.Fatal(err)
}

logs, err = showLogs(cfg)
if err != nil {
log.Fatal(err)
}
}
}

func showLogs(cfg *config) (*config, error) {
func showLogs(cfg *config) (*logsInfo, error) {
//println("$$$ " + cfg.String() + " ::: " + time.Now().String())

reqBody := map[string]interface{}{
"query": cfg.query,
"limit": cfg.limit,
Expand All @@ -101,8 +108,10 @@ func showLogs(cfg *config) (*config, error) {
},
"sort": "asc",
}
if cfg.nextLogId != "" {
reqBody["startAt"] = cfg.nextLogId

lastInfo := cfg.lastInfo
if lastInfo.NextLogId != "" {
reqBody["startAt"] = lastInfo.NextLogId
}

reqBodyJson, err := json.Marshal(reqBody)
Expand Down Expand Up @@ -141,15 +150,28 @@ func showLogs(cfg *config) (*config, error) {
return nil, err
}

// Remove duplicate logs
var logsToDisplay []logInfo
FilterLoop:
for _, l := range logsInfo.Logs {
if lastInfo != nil {
for _, lastLog := range lastInfo.Logs {
if l.Id == lastLog.Id {
continue FilterLoop
}
}
logsToDisplay = append(logsToDisplay, l)
}
}

for _, l := range logsToDisplay {
err := cfg.tmpl.Execute(os.Stdout, l.Content)
if err != nil {
return nil, err
}
}

err = cfg.update(logsInfo)
return cfg, err
return logsInfo, err
}

func getEnv(key string) string {
Expand Down Expand Up @@ -214,18 +236,19 @@ func newConfig() (*config, error) {
}

return &config{
query: *query,
from: from,
to: to,
limit: *limit,
follow: follow,
tmpl: tmpl,
query: *query,
from: from,
to: to,
limit: *limit,
follow: follow,
tmpl: tmpl,
lastInfo: &logsInfo{},
}, nil
}

func (cfg *config) update(info *logsInfo) error {
cfg.nextLogId = info.NextLogId
if cfg.nextLogId != "" {
cfg.lastInfo = info
if info.NextLogId != "" {
// There are remaining logs (keep last condition)
return nil
}
Expand All @@ -236,14 +259,13 @@ func (cfg *config) update(info *logsInfo) error {
if err != nil {
return err
}
// (Timestamp of the last log) + 1ms, to avoid to show duplicate logs
cfg.from = t.Add(time.Duration(1 * time.Millisecond))
cfg.from = t
}
cfg.to = now()

return nil
}

func (cfg *config) String() string {
return fmt.Sprintf("%s %s %s", cfg.from.Format(time.RFC3339Nano), cfg.to.Format(time.RFC3339Nano), cfg.nextLogId)
return fmt.Sprintf("%s %s %s", cfg.from.Format(time.RFC3339Nano), cfg.to.Format(time.RFC3339Nano), cfg.lastInfo.NextLogId)
}

0 comments on commit aef072d

Please sign in to comment.