mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
add container log struct to models, collectors
This commit is contained in:
@@ -4,7 +4,10 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bcicen/ctop/models"
|
||||||
api "github.com/fsouza/go-dockerclient"
|
api "github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,9 +17,9 @@ type DockerLogs struct {
|
|||||||
done chan bool
|
done chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *DockerLogs) Stream() chan string {
|
func (l *DockerLogs) Stream() chan models.Log {
|
||||||
r, w := io.Pipe()
|
r, w := io.Pipe()
|
||||||
logCh := make(chan string)
|
logCh := make(chan models.Log)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
opts := api.LogsOptions{
|
opts := api.LogsOptions{
|
||||||
@@ -35,7 +38,9 @@ func (l *DockerLogs) Stream() chan string {
|
|||||||
go func() {
|
go func() {
|
||||||
scanner := bufio.NewScanner(r)
|
scanner := bufio.NewScanner(r)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
logCh <- scanner.Text()
|
parts := strings.Split(scanner.Text(), " ")
|
||||||
|
ts := l.parseTime(parts[0])
|
||||||
|
logCh <- models.Log{ts, strings.Join(parts[1:], " ")}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -58,3 +63,12 @@ func (l *DockerLogs) Stream() chan string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *DockerLogs) Stop() { l.done <- true }
|
func (l *DockerLogs) Stop() { l.done <- true }
|
||||||
|
|
||||||
|
func (l *DockerLogs) parseTime(s string) time.Time {
|
||||||
|
ts, err := time.Parse("2006-01-02T15:04:05.000000000Z", s)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed to parse container log: %s", err)
|
||||||
|
ts = time.Now()
|
||||||
|
}
|
||||||
|
return ts
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
var log = logging.Init()
|
var log = logging.Init()
|
||||||
|
|
||||||
type LogCollector interface {
|
type LogCollector interface {
|
||||||
Stream() chan string
|
Stream() chan models.Log
|
||||||
Stop()
|
Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package collector
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/bcicen/ctop/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
const mockLog = "Cura ob pro qui tibi inveni dum qua fit donec amare illic mea, regem falli contexo pro peregrinorum heremo absconditi araneae meminerim deliciosas actionibus facere modico dura sonuerunt psalmi contra rerum, tempus mala anima volebant dura quae o modis."
|
const mockLog = "Cura ob pro qui tibi inveni dum qua fit donec amare illic mea, regem falli contexo pro peregrinorum heremo absconditi araneae meminerim deliciosas actionibus facere modico dura sonuerunt psalmi contra rerum, tempus mala anima volebant dura quae o modis."
|
||||||
@@ -10,15 +12,15 @@ type MockLogs struct {
|
|||||||
done chan bool
|
done chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *MockLogs) Stream() chan string {
|
func (l *MockLogs) Stream() chan models.Log {
|
||||||
logCh := make(chan string)
|
logCh := make(chan models.Log)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-l.done:
|
case <-l.done:
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
logCh <- mockLog
|
logCh <- models.Log{time.Now(), mockLog}
|
||||||
time.Sleep(250 * time.Millisecond)
|
time.Sleep(250 * time.Millisecond)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,20 @@
|
|||||||
package expanded
|
package expanded
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/bcicen/ctop/models"
|
||||||
ui "github.com/gizak/termui"
|
ui "github.com/gizak/termui"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LogLines struct {
|
type LogLines struct {
|
||||||
ts []string
|
ts []time.Time
|
||||||
data []string
|
data []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLogLines(max int) *LogLines {
|
func NewLogLines(max int) *LogLines {
|
||||||
ll := &LogLines{
|
ll := &LogLines{
|
||||||
ts: make([]string, max),
|
ts: make([]time.Time, max),
|
||||||
data: make([]string, max),
|
data: make([]string, max),
|
||||||
}
|
}
|
||||||
return ll
|
return ll
|
||||||
@@ -31,14 +34,14 @@ func (ll *LogLines) getLines(start, end int) []string {
|
|||||||
return ll.data[start:end]
|
return ll.data[start:end]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ll *LogLines) add(s string) {
|
func (ll *LogLines) add(l models.Log) {
|
||||||
if len(ll.data) == cap(ll.data) {
|
if len(ll.data) == cap(ll.data) {
|
||||||
ll.data = append(ll.data[:0], ll.data[1:]...)
|
ll.data = append(ll.data[:0], ll.data[1:]...)
|
||||||
ll.ts = append(ll.ts[:0], ll.ts[1:]...)
|
ll.ts = append(ll.ts[:0], ll.ts[1:]...)
|
||||||
}
|
}
|
||||||
ll.ts = append(ll.ts, "timestamp")
|
ll.ts = append(ll.ts, l.Timestamp)
|
||||||
ll.data = append(ll.data, s)
|
ll.data = append(ll.data, l.Message)
|
||||||
log.Debugf("recorded log line: %v", s)
|
log.Debugf("recorded log line: %v", l)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Logs struct {
|
type Logs struct {
|
||||||
@@ -46,7 +49,7 @@ type Logs struct {
|
|||||||
lines *LogLines
|
lines *LogLines
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLogs(stream chan string) *Logs {
|
func NewLogs(stream chan models.Log) *Logs {
|
||||||
p := ui.NewList()
|
p := ui.NewList()
|
||||||
p.Y = ui.TermHeight() / 2
|
p.Y = ui.TermHeight() / 2
|
||||||
p.X = 0
|
p.X = 0
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Log struct {
|
||||||
|
Timestamp time.Time
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
type Metrics struct {
|
type Metrics struct {
|
||||||
CPUUtil int
|
CPUUtil int
|
||||||
NetTx int64
|
NetTx int64
|
||||||
|
|||||||
Reference in New Issue
Block a user