Skip to content

Commit

Permalink
Merge pull request #4 from kamatama41/update-190715
Browse files Browse the repository at this point in the history
Update 190715
  • Loading branch information
kamatama41 authored Jul 16, 2019
2 parents 5cb8112 + aef072d commit 4933c6e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
74 changes: 48 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ var (
limit = flag.Int("l", 1000, "Number of logs fetched at once.")
fromStr = flag.String("from", "", "Minimum timestamp for requested logs, should be an ISO-8601 string.")
toStr = flag.String("to", "", "Maximum timestamp for requested logs, should be an ISO-8601 string.")
version = flag.Bool("V", false, "Show version of taildog")
version = flag.Bool("version", false, "Show version of taildog.")

apiKey = getEnv("DD_API_KEY")
appKey = getEnv("DD_APP_KEY")
)

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)
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

var VERSION = "0.1.2"
var VERSION = "0.2.0"

0 comments on commit 4933c6e

Please sign in to comment.