Skip to content

Commit

Permalink
feat: add timepicker to dashboard (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 authored Sep 1, 2024
1 parent ed79c15 commit 09f67cd
Show file tree
Hide file tree
Showing 18 changed files with 714 additions and 606 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ require (
github.com/lufia/iostat v1.2.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/mattn/go-xmlrpc v0.0.3 // indirect
github.com/mdlayher/ethtool v0.1.0 // indirect
github.com/mdlayher/genetlink v1.3.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mattn/go-xmlrpc v0.0.3 h1:Y6WEMLEsqs3RviBrAa1/7qmbGB7DVD3brZIbqMbQdGY=
github.com/mattn/go-xmlrpc v0.0.3/go.mod h1:mqc2dz7tP5x5BKlCahN/n+hs7OSZKJkS9JsHNBRlrxA=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var RootFlags = []cli.Flag{
&cli.StringFlag{
Name: "lt,listen_type",
Value: "raw",
Usage: "监听类型,可选项有 raw,ws,wss,mwss",
Usage: "监听类型,可选项有 raw,ws,wss",
EnvVars: []string{"EHCO_LISTEN_TYPE"},
Destination: (*string)(&ListenType),
Required: false,
Expand All @@ -45,7 +45,7 @@ var RootFlags = []cli.Flag{
&cli.StringFlag{
Name: "tt,transport_type",
Value: "raw",
Usage: "传输类型,可选选有 raw,ws,wss,mwss",
Usage: "传输类型,可选选有 raw,ws,wss",
EnvVars: []string{"EHCO_TRANSPORT_TYPE"},
Destination: (*string)(&TransportType),
},
Expand Down
42 changes: 34 additions & 8 deletions internal/cmgr/cmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package cmgr

import (
"context"
"os"
"path/filepath"
"sort"
"sync"
"time"

"github.com/Ehco1996/ehco/internal/cmgr/ms"
"github.com/Ehco1996/ehco/internal/conn"
"github.com/Ehco1996/ehco/pkg/metric_reader"
"go.uber.org/zap"
Expand Down Expand Up @@ -35,7 +38,8 @@ type Cmgr interface {
// Start starts the connection manager.
Start(ctx context.Context, errCH chan error)

QueryNodeMetrics(ctx context.Context, req *QueryNodeMetricsReq) ([]metric_reader.NodeMetrics, error)
// Metrics related
QueryNodeMetrics(ctx context.Context, req *ms.QueryNodeMetricsReq) (*ms.QueryNodeMetricsResp, error)
}

type cmgrImpl struct {
Expand All @@ -47,11 +51,11 @@ type cmgrImpl struct {
activeConnectionsMap map[string][]conn.RelayConn
closedConnectionsMap map[string][]conn.RelayConn

ms *ms.MetricsStore
mr metric_reader.Reader
ms *MetricsStore
}

func NewCmgr(cfg *Config) Cmgr {
func NewCmgr(cfg *Config) (Cmgr, error) {
cmgr := &cmgrImpl{
cfg: cfg,
l: zap.S().Named("cmgr"),
Expand All @@ -60,12 +64,16 @@ func NewCmgr(cfg *Config) Cmgr {
}
if cfg.NeedMetrics() {
cmgr.mr = metric_reader.NewReader(cfg.MetricsURL)
// 当前只能存储 24h 的 metrics,之后再优化
bufSize := 60 * 60 * 24 / cfg.SyncInterval
cmgr.l.Infof("metrics buffer size: %d", bufSize)
cmgr.ms = NewMetricsStore(bufSize, time.Hour*24)

homeDir, _ := os.UserHomeDir()
dbPath := filepath.Join(homeDir, ".ehco", "metrics.db")
ms, err := ms.NewMetricsStore(dbPath)
if err != nil {
return nil, err
}
cmgr.ms = ms
}
return cmgr
return cmgr, nil
}

func (cm *cmgrImpl) ListConnections(connType string, page, pageSize int) []conn.RelayConn {
Expand Down Expand Up @@ -192,3 +200,21 @@ func (cm *cmgrImpl) Start(ctx context.Context, errCH chan error) {
}
}
}

func (cm *cmgrImpl) QueryNodeMetrics(ctx context.Context, req *ms.QueryNodeMetricsReq) (*ms.QueryNodeMetricsResp, error) {
num := -1 // default to return all metrics
if req.Latest {
m, err := cm.mr.ReadOnce(ctx)
if err != nil {
return nil, err
}
if err := cm.ms.AddNodeMetric(m); err != nil {
return nil, err
}
num = 1
}

startTime := time.Unix(req.StartTimestamp, 0)
endTime := time.Unix(req.EndTimestamp, 0)
return cm.ms.QueryNodeMetric(startTime, endTime, num)
}
2 changes: 0 additions & 2 deletions internal/cmgr/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package cmgr

var DummyConfig = &Config{}

type Config struct {
SyncURL string
MetricsURL string
Expand Down
180 changes: 0 additions & 180 deletions internal/cmgr/metric_man.go

This file was deleted.

Loading

0 comments on commit 09f67cd

Please sign in to comment.