Commit 3fe4ee2d authored by jingbo.wang's avatar jingbo.wang

总表 OK

parent 84b776f3
...@@ -82,7 +82,7 @@ func GetServiceNameListFromDb() []string { ...@@ -82,7 +82,7 @@ func GetServiceNameListFromDb() []string {
} }
//从db拉取数据 //从db拉取数据
func GetDataFromDb(startTime time.Time, endTime time.Time) *ServiceMap { func GetDataFromDb(startTime time.Time, endTime time.Time) ServiceMap {
serviceNameList := GetServiceNameListFromDb() serviceNameList := GetServiceNameListFromDb()
serviceMap := NewServiceMap() serviceMap := NewServiceMap()
...@@ -95,7 +95,7 @@ func GetDataFromDb(startTime time.Time, endTime time.Time) *ServiceMap { ...@@ -95,7 +95,7 @@ func GetDataFromDb(startTime time.Time, endTime time.Time) *ServiceMap {
for _, v := range resp.Results { for _, v := range resp.Results {
for _, row := range v.Series { for _, row := range v.Series {
traceList := rowToTraceList(row) traceList := rowToTraceList(serviceName, row)
for _, trace := range traceList { for _, trace := range traceList {
serviceMap.AddTrace(serviceName, trace.Path, trace) serviceMap.AddTrace(serviceName, trace.Path, trace)
} }
...@@ -106,7 +106,7 @@ func GetDataFromDb(startTime time.Time, endTime time.Time) *ServiceMap { ...@@ -106,7 +106,7 @@ func GetDataFromDb(startTime time.Time, endTime time.Time) *ServiceMap {
return serviceMap return serviceMap
} }
func rowToTraceList(row models.Row) []*Trace { func rowToTraceList(serviceName string, row models.Row) []*Trace {
//[time duration host msg path sys_name traceId] //[time duration host msg path sys_name traceId]
rtn := make([]*Trace, 0) rtn := make([]*Trace, 0)
durationIndex, ok := getModRowKeyIdx("duration", row) durationIndex, ok := getModRowKeyIdx("duration", row)
...@@ -165,7 +165,7 @@ func rowToTraceList(row models.Row) []*Trace { ...@@ -165,7 +165,7 @@ func rowToTraceList(row models.Row) []*Trace {
continue continue
} }
rtn = append(rtn, NewTrace(id, host, path, duration, timePoint)) rtn = append(rtn, NewTrace(id, serviceName, host, path, duration, timePoint))
} }
return rtn return rtn
......
...@@ -14,4 +14,15 @@ func TestGetDataFromDb(t *testing.T) { ...@@ -14,4 +14,15 @@ func TestGetDataFromDb(t *testing.T) {
start := end.AddDate(0, 0, -10) start := end.AddDate(0, 0, -10)
sm := GetDataFromDb(start, end) sm := GetDataFromDb(start, end)
t.Log(sm.GetAverageTime(), sm.GetCount()) t.Log(sm.GetAverageTime(), sm.GetCount())
tl := NewTraceList("")
for _, v := range sm {
if trace, ok := v.GetMaxMediaTrace(); ok {
tl.AddTrace(trace)
}
}
list := tl.GetTraceList()
for _, v := range list {
t.Log(v.Duration, v.ServiceName, v.Id, v.Path)
}
} }
...@@ -28,6 +28,17 @@ func (s *Service) DelPath(path string) { ...@@ -28,6 +28,17 @@ func (s *Service) DelPath(path string) {
delete(s.pathMap, path) delete(s.pathMap, path)
} }
//获取最大中位值的trace
func (s *Service) GetMaxMediaTrace() (*Trace, bool) {
tl := NewTraceList("")
for _, path := range s.pathMap {
if trace, ok := path.GetMediaTrace(); ok {
tl.AddTrace(trace)
}
}
return tl.GetMediaTrace()
}
//求服务的平均响应时间 //求服务的平均响应时间
func (s *Service) GetAverageTime() time.Duration { func (s *Service) GetAverageTime() time.Duration {
count := s.GetCount() count := s.GetCount()
......
...@@ -2,40 +2,36 @@ package report_form ...@@ -2,40 +2,36 @@ package report_form
import "time" import "time"
type ServiceMap struct { type ServiceMap map[string]*Service
serviceMap map[string]*Service
}
func NewServiceMap() *ServiceMap { func NewServiceMap() ServiceMap {
return &ServiceMap{ return make(map[string]*Service)
serviceMap: make(map[string]*Service),
}
} }
//添加trace //添加trace
func (sm *ServiceMap) AddTrace(service string, path string, trace *Trace) { func (sm ServiceMap) AddTrace(service string, path string, trace *Trace) {
if _, ok := sm.serviceMap[service]; !ok { if _, ok := sm[service]; !ok {
sm.serviceMap[service] = NewService(service) sm[service] = NewService(service)
} }
sm.serviceMap[service].AddTrace(path, trace) sm[service].AddTrace(path, trace)
} }
//获取service //获取service
func (sm *ServiceMap) GetService(service string) (*Service, bool) { func (sm ServiceMap) GetService(service string) (*Service, bool) {
s, ok := sm.serviceMap[service] s, ok := sm[service]
return s, ok return s, ok
} }
//求服务的平均响应时间 //求服务的平均响应时间
func (sm *ServiceMap) GetAverageTime() time.Duration { func (sm ServiceMap) GetAverageTime() time.Duration {
count := sm.GetCount() count := sm.GetCount()
if count == 0 { if count == 0 {
return 0 return 0
} }
sum := time.Duration(0) sum := time.Duration(0)
for _, v := range sm.serviceMap { for _, v := range sm {
for _, vv := range v.pathMap { for _, vv := range v.pathMap {
for _, vvv := range vv.traceList { for _, vvv := range vv.traceList {
sum += vvv.Duration sum += vvv.Duration
...@@ -47,9 +43,9 @@ func (sm *ServiceMap) GetAverageTime() time.Duration { ...@@ -47,9 +43,9 @@ func (sm *ServiceMap) GetAverageTime() time.Duration {
} }
//求服务的访问量 //求服务的访问量
func (sm *ServiceMap) GetCount() int { func (sm ServiceMap) GetCount() int {
sum := 0 sum := 0
for _, v := range sm.serviceMap { for _, v := range sm {
sum += v.GetCount() sum += v.GetCount()
} }
return sum return sum
......
...@@ -12,7 +12,7 @@ func TestNewServiceMap(t *testing.T) { ...@@ -12,7 +12,7 @@ func TestNewServiceMap(t *testing.T) {
host = "1.1.1.1" host = "1.1.1.1"
) )
sm := NewServiceMap() sm := NewServiceMap()
sm.AddTrace(service, path, NewTrace("1", host, path, 1, time.Now())) sm.AddTrace(service, path, NewTrace("1", service, host, path, 1, time.Now()))
s, ok := sm.GetService(service) s, ok := sm.GetService(service)
if !ok { if !ok {
t.Error("can not found service:", service) t.Error("can not found service:", service)
......
...@@ -8,13 +8,14 @@ import ( ...@@ -8,13 +8,14 @@ import (
func TestNewService(t *testing.T) { func TestNewService(t *testing.T) {
const ( const (
path = "/hello" path = "/hello"
host = "1.1.1.1" host = "1.1.1.1"
serviceName = "aaaa"
) )
s := NewService("aaaa") s := NewService(serviceName)
for i := 1; i <= 10; i++ { for i := 1; i <= 10; i++ {
s.AddTrace(path, NewTrace(strconv.Itoa(i), host, path, time.Duration(i), time.Now().Add(time.Duration(i)))) s.AddTrace(path, NewTrace(strconv.Itoa(i), serviceName, host, path, time.Duration(i), time.Now().Add(time.Duration(i))))
} }
t.Log(s.GetAverageTime()) t.Log(s.GetAverageTime())
......
...@@ -7,20 +7,22 @@ import ( ...@@ -7,20 +7,22 @@ import (
) )
type Trace struct { type Trace struct {
Id string Id string
Host string ServiceName string
Path string Host string
Duration time.Duration Path string
TimePoint time.Time Duration time.Duration
TimePoint time.Time
} }
func NewTrace(id string, host string, path string, duration time.Duration, timePoint time.Time) *Trace { func NewTrace(id string, serviceName string, host string, path string, duration time.Duration, timePoint time.Time) *Trace {
return &Trace{ return &Trace{
Id: id, Id: id,
Host: host, ServiceName: serviceName,
Path: path, Host: host,
Duration: duration, Path: path,
TimePoint: timePoint, Duration: duration,
TimePoint: timePoint,
} }
} }
......
...@@ -8,12 +8,13 @@ import ( ...@@ -8,12 +8,13 @@ import (
func TestNewTraceList(t *testing.T) { func TestNewTraceList(t *testing.T) {
const ( const (
path = "/hello" path = "/hello"
host = "1.1.1.1" host = "1.1.1.1"
serviceName = "aaaa"
) )
tl := NewTraceList(path) tl := NewTraceList(path)
for i := 1; i <= 10; i++ { for i := 1; i <= 10; i++ {
tl.AddTrace(NewTrace(strconv.Itoa(i), host, path, time.Duration(i), time.Now().Add(time.Duration(i)))) tl.AddTrace(NewTrace(strconv.Itoa(i), serviceName, host, path, time.Duration(i), time.Now().Add(time.Duration(i))))
} }
t.Log(tl.GetCount()) t.Log(tl.GetCount())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment