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

总表 OK

parent 84b776f3
......@@ -82,7 +82,7 @@ func GetServiceNameListFromDb() []string {
}
//从db拉取数据
func GetDataFromDb(startTime time.Time, endTime time.Time) *ServiceMap {
func GetDataFromDb(startTime time.Time, endTime time.Time) ServiceMap {
serviceNameList := GetServiceNameListFromDb()
serviceMap := NewServiceMap()
......@@ -95,7 +95,7 @@ func GetDataFromDb(startTime time.Time, endTime time.Time) *ServiceMap {
for _, v := range resp.Results {
for _, row := range v.Series {
traceList := rowToTraceList(row)
traceList := rowToTraceList(serviceName, row)
for _, trace := range traceList {
serviceMap.AddTrace(serviceName, trace.Path, trace)
}
......@@ -106,7 +106,7 @@ func GetDataFromDb(startTime time.Time, endTime time.Time) *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]
rtn := make([]*Trace, 0)
durationIndex, ok := getModRowKeyIdx("duration", row)
......@@ -165,7 +165,7 @@ func rowToTraceList(row models.Row) []*Trace {
continue
}
rtn = append(rtn, NewTrace(id, host, path, duration, timePoint))
rtn = append(rtn, NewTrace(id, serviceName, host, path, duration, timePoint))
}
return rtn
......
......@@ -14,4 +14,15 @@ func TestGetDataFromDb(t *testing.T) {
start := end.AddDate(0, 0, -10)
sm := GetDataFromDb(start, end)
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) {
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 {
count := s.GetCount()
......
......@@ -2,40 +2,36 @@ package report_form
import "time"
type ServiceMap struct {
serviceMap map[string]*Service
}
type ServiceMap map[string]*Service
func NewServiceMap() *ServiceMap {
return &ServiceMap{
serviceMap: make(map[string]*Service),
}
func NewServiceMap() ServiceMap {
return make(map[string]*Service)
}
//添加trace
func (sm *ServiceMap) AddTrace(service string, path string, trace *Trace) {
if _, ok := sm.serviceMap[service]; !ok {
sm.serviceMap[service] = NewService(service)
func (sm ServiceMap) AddTrace(service string, path string, trace *Trace) {
if _, ok := sm[service]; !ok {
sm[service] = NewService(service)
}
sm.serviceMap[service].AddTrace(path, trace)
sm[service].AddTrace(path, trace)
}
//获取service
func (sm *ServiceMap) GetService(service string) (*Service, bool) {
s, ok := sm.serviceMap[service]
func (sm ServiceMap) GetService(service string) (*Service, bool) {
s, ok := sm[service]
return s, ok
}
//求服务的平均响应时间
func (sm *ServiceMap) GetAverageTime() time.Duration {
func (sm ServiceMap) GetAverageTime() time.Duration {
count := sm.GetCount()
if count == 0 {
return 0
}
sum := time.Duration(0)
for _, v := range sm.serviceMap {
for _, v := range sm {
for _, vv := range v.pathMap {
for _, vvv := range vv.traceList {
sum += vvv.Duration
......@@ -47,9 +43,9 @@ func (sm *ServiceMap) GetAverageTime() time.Duration {
}
//求服务的访问量
func (sm *ServiceMap) GetCount() int {
func (sm ServiceMap) GetCount() int {
sum := 0
for _, v := range sm.serviceMap {
for _, v := range sm {
sum += v.GetCount()
}
return sum
......
......@@ -12,7 +12,7 @@ func TestNewServiceMap(t *testing.T) {
host = "1.1.1.1"
)
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)
if !ok {
t.Error("can not found service:", service)
......
......@@ -8,13 +8,14 @@ import (
func TestNewService(t *testing.T) {
const (
path = "/hello"
host = "1.1.1.1"
path = "/hello"
host = "1.1.1.1"
serviceName = "aaaa"
)
s := NewService("aaaa")
s := NewService(serviceName)
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())
......
......@@ -7,20 +7,22 @@ import (
)
type Trace struct {
Id string
Host string
Path string
Duration time.Duration
TimePoint time.Time
Id string
ServiceName string
Host string
Path string
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{
Id: id,
Host: host,
Path: path,
Duration: duration,
TimePoint: timePoint,
Id: id,
ServiceName: serviceName,
Host: host,
Path: path,
Duration: duration,
TimePoint: timePoint,
}
}
......
......@@ -8,12 +8,13 @@ import (
func TestNewTraceList(t *testing.T) {
const (
path = "/hello"
host = "1.1.1.1"
path = "/hello"
host = "1.1.1.1"
serviceName = "aaaa"
)
tl := NewTraceList(path)
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())
......
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