Skip to content

Commit

Permalink
rename some variables in web
Browse files Browse the repository at this point in the history
  • Loading branch information
sunface committed Jan 25, 2019
1 parent b49e6f2 commit 8bf9b7d
Show file tree
Hide file tree
Showing 20 changed files with 930 additions and 133 deletions.
95 changes: 57 additions & 38 deletions web/service/api_stats.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
package service

import (
"fmt"
"log"
"net/http"

"github.com/labstack/echo"
"github.com/mafanr/g"
"github.com/mafanr/g/utils"
"github.com/mafanr/vgo/web/misc"
"go.uber.org/zap"
)

type ApiStat struct {
URL string `json:"url"`
MaxElapsed int `json:"max_elapsed"`
MinElapsed int `json:"min_elapsed"`
Count int `json:"count"`
AverageElapsed int `json:"average_elapsed"`
ErrorCount int `json:"error_count"`
API string `json:"api"`
MaxElapsed int `json:"max_elapsed"`
MinElapsed int `json:"min_elapsed"`
Count int `json:"count"`
AverageElapsed float64 `json:"average_elapsed"`
ErrorCount int `json:"error_count"`
}

// type ApiStats []*ApiStat

// func (a ApiStats) Len() int { // 重写 Len() 方法
// return len(a)
// }
// func (a ApiStats) Swap(i, j int) { // 重写 Swap() 方法
// a[i], a[j] = a[j], a[i]
// }
// func (a ApiStats) Less(i, j int) bool { // 重写 Less() 方法, 从大到小排序
// return a[j].AverageElapsed < a[i].AverageElapsed
// }

// 单个应用下,所有接口的统计信息
func (web *Web) apiStats(c echo.Context) error {
appName := c.FormValue("app_name")
Expand All @@ -32,11 +45,12 @@ func (web *Web) apiStats(c echo.Context) error {
}

// 读取相应数据,按照时间填到对应的桶中
q := `SELECT url,max_elapsed,min_elapsed,average_elapsed,count,err_count FROM rpc_stats WHERE app_name = ? and input_date > ? and input_date < ? `
q := `SELECT api,max_elapsed,min_elapsed,average_elapsed,count,err_count FROM api_stats WHERE app_name = ? and input_date > ? and input_date < ? `
iter := web.Cql.Query(q, appName, start.Unix(), end.Unix()).Iter()

// apps := make(map[string]*AppStat)
var maxElapsed, minElapsed, count, aElapsed, errCount int
var maxElapsed, minElapsed, count, errCount int
var aElapsed float64
var url string
ass := make(map[string]*ApiStat)
for iter.Scan(&url, &maxElapsed, &minElapsed, &aElapsed, &count, &errCount) {
Expand All @@ -56,7 +70,7 @@ func (web *Web) apiStats(c echo.Context) error {
as.Count += count
as.ErrorCount += errCount
// 平均 = 过去的平均 * 过去总次数 + 最新的平均 * 最新的次数/ (过去总次数 + 最新次数)
as.AverageElapsed = (as.AverageElapsed*as.Count + aElapsed*count) / (as.Count + count)
as.AverageElapsed, _ = utils.DecimalPrecision((as.AverageElapsed*float64(as.Count) + aElapsed*float64(count)) / float64((as.Count + count)))
}
}

Expand All @@ -77,16 +91,19 @@ func (web *Web) apiStats(c echo.Context) error {
}

type ApiMethod struct {
ID int `json:"-"`
Name string `json:"name"`
ServiceType int `json:"service_type"`
RatioElapsed int `json:"ratio_elapsed"`
Elapsed int `json:"elapsed"`
MaxElapsed int `json:"max_elapsed"`
MinElapsed int `json:"min_elapsed"`
Count int `json:"count"`
AverageElapsed int `json:"average_elapsed"`
ErrorCount int `json:"error_count"`
ID int `json:"-"`
API string `json:"api"`
ServiceType int `json:"service_type"`
RatioElapsed int `json:"ratio_elapsed"`
Elapsed int `json:"elapsed"`
MaxElapsed int `json:"max_elapsed"`
MinElapsed int `json:"min_elapsed"`
Count int `json:"count"`
AverageElapsed float64 `json:"average_elapsed"`
ErrorCount int `json:"error_count"`

Line int `json:"line"`
Method string `json:"method"`
}

func (web *Web) apiDetail(c echo.Context) error {
Expand All @@ -100,7 +117,7 @@ func (web *Web) apiDetail(c echo.Context) error {
}

appName := c.FormValue("app_name")
url := c.FormValue("url")
url := c.FormValue("api")

if appName == "" || url == "" {
return c.JSON(http.StatusOK, g.Result{
Expand All @@ -110,16 +127,17 @@ func (web *Web) apiDetail(c echo.Context) error {
})
}

q := `SELECT api_id,ser_type,elapsed,max_elapsed,min_elapsed,average_elapsed,count,err_count FROM rpc_details_stats WHERE app_name = ? and url=? and input_date > ? and input_date < ? `
q := `SELECT api_id,ser_type,elapsed,max_elapsed,min_elapsed,average_elapsed,count,err_count FROM api_details_stats WHERE app_name = ? and api = ? and input_date > ? and input_date < ? `
iter := web.Cql.Query(q, appName, url, start.Unix(), end.Unix()).Iter()

var apiID, serType, elapsed, maxE, minE, averageE, count, errCount int
var apiID, serType, elapsed, maxE, minE, count, errCount int
var averageE float64
var totalElapsed int
ad := make(map[int]*ApiMethod)
for iter.Scan(&apiID, &serType, &elapsed, &maxE, &minE, &averageE, &count, &errCount) {
am, ok := ad[apiID]
if !ok {
ad[apiID] = &ApiMethod{apiID, "", serType, 0, elapsed, maxE, minE, count, averageE, errCount}
ad[apiID] = &ApiMethod{apiID, url, serType, 0, elapsed, maxE, minE, count, averageE, errCount, 0, ""}
} else {
am.Elapsed += elapsed
// 取最大值
Expand All @@ -134,33 +152,34 @@ func (web *Web) apiDetail(c echo.Context) error {
am.Count += count
am.ErrorCount += errCount
// 平均 = 过去的平均 * 过去总次数 + 最新的平均 * 最新的次数/ (过去总次数 + 最新次数)
am.AverageElapsed = (am.AverageElapsed*am.Count + averageE*count) / (am.Count + count)
am.AverageElapsed, _ = utils.DecimalPrecision((am.AverageElapsed*float64(am.Count) + averageE*float64(count)) / float64((am.Count + count)))
}

totalElapsed += elapsed
}

ads := make([]*ApiMethod, 0, len(ad))
for _, am := range ad {
// 计算耗时占比
am.RatioElapsed = am.Elapsed * 100 / totalElapsed
// 通过apiID 获取api name
q := `SELECT api_info,line,type FROM app_apis WHERE app_name = ? and api_id=?`
q := web.Cql.Query(`SELECT api_info,line FROM app_apis WHERE app_name = ? and api_id=?`, appName, am.ID)
var apiInfo string
var line, tp int
err := web.Cql.Query(q, appName, am.ID).Scan(&apiInfo, &line, &tp)
fmt.Println(err, line, tp, apiInfo)
var line int
err := q.Scan(&apiInfo, &line)
if err != nil {
g.L.Info("access database error", zap.Error(err), zap.String("query", q.String()))
continue
}

am.Line = line
am.Method = apiInfo

ads = append(ads, am)
}

return c.JSON(http.StatusOK, g.Result{
Status: http.StatusOK,
Data: []string{},
Data: ads,
})
}

// CREATE TABLE IF NOT EXISTS app_apis (
// app_name text,
// api_id int,
// api_info text,
// line int,
// type int,
// PRIMARY KEY (app_name, api_id)
35 changes: 29 additions & 6 deletions web/service/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package service

import (
"encoding/json"
"fmt"
"log"
"math"
"net/http"
Expand Down Expand Up @@ -37,7 +36,7 @@ func (web *Web) appList(c echo.Context) error {
// if web.cache.appList == nil || now.Sub(web.cache.appListUpdate).Seconds() > CacheUpdateIntv {
// 取过去6分钟的数据
start := now.Unix() - 450
q := `SELECT app_name,total_elapsed,count,err_count,satisfaction,tolerate FROM rpc_stats WHERE input_date > ? and input_date < ? `
q := `SELECT app_name,total_elapsed,count,err_count,satisfaction,tolerate FROM api_stats WHERE input_date > ? and input_date < ? `
iter := web.Cql.Query(q, start, now.Unix()).Iter()

apps := make(map[string]*AppStat)
Expand Down Expand Up @@ -71,7 +70,6 @@ func (web *Web) appList(c echo.Context) error {
app.Apdex, _ = utils.DecimalPrecision((app.satisfaction + app.tolerate/2) / float64(app.Count))
napps = append(napps, app)
}
fmt.Println("query from database:", napps)

if err := iter.Close(); err != nil {
log.Println("close iter error:", err, web.Cql.Closed())
Expand Down Expand Up @@ -111,7 +109,7 @@ func (web *Web) appListWithSetting(c echo.Context) error {
var tElapsed, errCount, satisfaction, tolerate int

if appShow == 1 {
q = web.Cql.Query(`SELECT app_name,total_elapsed,count,err_count,satisfaction,tolerate FROM rpc_stats WHERE input_date > ? and input_date < ? `, start, now.Unix())
q = web.Cql.Query(`SELECT app_name,total_elapsed,count,err_count,satisfaction,tolerate FROM api_stats WHERE input_date > ? and input_date < ? `, start, now.Unix())
iter := q.Iter()

for iter.Scan(&appName, &tElapsed, &count, &errCount, &satisfaction, &tolerate) {
Expand Down Expand Up @@ -139,7 +137,7 @@ func (web *Web) appListWithSetting(c echo.Context) error {
}
} else {
for _, an := range appNames {
err := web.Cql.Query(`SELECT app_name,total_elapsed,count,err_count,satisfaction,tolerate FROM rpc_stats WHERE app_name =? and input_date > ? and input_date < ? `, an, start, now.Unix()).Scan(&appName, &tElapsed, &count, &errCount, &satisfaction, &tolerate)
err := web.Cql.Query(`SELECT app_name,total_elapsed,count,err_count,satisfaction,tolerate FROM api_stats WHERE app_name =? and input_date > ? and input_date < ? `, an, start, now.Unix()).Scan(&appName, &tElapsed, &count, &errCount, &satisfaction, &tolerate)
if err != nil {
log.Println("select app stats error:", err)
}
Expand Down Expand Up @@ -233,7 +231,7 @@ func (web *Web) appDash(c echo.Context) error {
}

// 读取相应数据,按照时间填到对应的桶中
q := `SELECT total_elapsed,count,err_count,satisfaction,tolerate,input_date FROM rpc_stats WHERE app_name = ? and input_date > ? and input_date < ? `
q := `SELECT total_elapsed,count,err_count,satisfaction,tolerate,input_date FROM api_stats WHERE app_name = ? and input_date > ? and input_date < ? `
iter := web.Cql.Query(q, appName, start.Unix(), end.Unix()).Iter()

// apps := make(map[string]*AppStat)
Expand Down Expand Up @@ -397,3 +395,28 @@ func (web *Web) userAppSetting(user string) (int, []string) {

return appShow, appNames
}

func (web *Web) appApis(c echo.Context) error {
appName := c.FormValue("app_name")
if appName == "" {
return c.JSON(http.StatusOK, g.Result{
Status: http.StatusBadRequest,
ErrCode: g.ParamInvalidC,
Message: g.ParamInvalidE,
})
}

q := `SELECT url FROM app_urls WHERE app_name=?`
iter := web.Cql.Query(q, appName).Iter()

var url string
urls := make([]string, 0)
for iter.Scan(&url) {
urls = append(urls, url)
}

return c.JSON(http.StatusOK, g.Result{
Status: http.StatusOK,
Data: urls,
})
}
89 changes: 89 additions & 0 deletions web/service/method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package service

import (
"net/http"

"github.com/labstack/echo"
"github.com/mafanr/g"
"github.com/mafanr/g/utils"
"github.com/mafanr/vgo/web/misc"
"go.uber.org/zap"
)

func (web *Web) appMethods(c echo.Context) error {
start, end, err := misc.StartEndDate(c)
if err != nil {
return c.JSON(http.StatusOK, g.Result{
Status: http.StatusOK,
ErrCode: g.ParamInvalidC,
Message: "日期参数不合法",
})
}

appName := c.FormValue("app_name")

if appName == "" {
return c.JSON(http.StatusOK, g.Result{
Status: http.StatusBadRequest,
ErrCode: g.ParamInvalidC,
Message: g.ParamInvalidE,
})
}

q := `SELECT api_id,api,ser_type,elapsed,max_elapsed,min_elapsed,average_elapsed,count,err_count FROM api_details_stats WHERE app_name = ? and input_date > ? and input_date < ? `
iter := web.Cql.Query(q, appName, start.Unix(), end.Unix()).Iter()

var apiID, serType, elapsed, maxE, minE, count, errCount int
var averageE float64
var totalElapsed int
var url string
ad := make(map[int]*ApiMethod)
for iter.Scan(&apiID, &url, &serType, &elapsed, &maxE, &minE, &averageE, &count, &errCount) {
am, ok := ad[apiID]
if !ok {
ad[apiID] = &ApiMethod{apiID, url, serType, 0, elapsed, maxE, minE, count, averageE, errCount, 0, ""}
} else {
am.Elapsed += elapsed
// 取最大值
if maxE > am.MaxElapsed {
am.MaxElapsed = maxE
}
// 取最小值
if minE < am.MinElapsed {
am.MinElapsed = minE
}

am.Count += count
am.ErrorCount += errCount
// 平均 = 过去的平均 * 过去总次数 + 最新的平均 * 最新的次数/ (过去总次数 + 最新次数)
am.AverageElapsed, _ = utils.DecimalPrecision((am.AverageElapsed*float64(am.Count) + averageE*float64(count)) / float64((am.Count + count)))
}

totalElapsed += elapsed
}

ads := make([]*ApiMethod, 0, len(ad))
for _, am := range ad {
// 计算耗时占比
am.RatioElapsed = am.Elapsed * 100 / totalElapsed
// 通过apiID 获取api name
q := web.Cql.Query(`SELECT api_info,line FROM app_apis WHERE app_name = ? and api_id=?`, appName, am.ID)
var apiInfo string
var line int
err := q.Scan(&apiInfo, &line)
if err != nil {
g.L.Info("access database error", zap.Error(err), zap.String("query", q.String()))
continue
}

am.Line = line
am.Method = apiInfo

ads = append(ads, am)
}

return c.JSON(http.StatusOK, g.Result{
Status: http.StatusOK,
Data: ads,
})
}
13 changes: 12 additions & 1 deletion web/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,27 @@ func (s *Web) Start() error {
e.GET("/apm/web/appNames", s.appNames)
e.GET("/apm/web/appNamesWithSetting", s.appNamesWithSetting, s.checkLogin)

// 查询APP底下的所有API
e.GET("/apm/web/appApis", s.appApis)

//应用接口统计
e.GET("/apm/web/apiStats", s.apiStats)
//获取指定接口的详细方法统计
e.GET("/apm/web/apiDetail", s.apiDetail)

// 应用Method统计
e.GET("/apm/web/appMethods", s.appMethods)

// 数据库统计
e.GET("/apm/web/sqlStats", s.sqlStats)

//查询所有服务器名
e.GET("/apm/web/agentList", s.agentList, s.checkLogin)

e.GET("/apm/web/serviceMap", queryServiceMap, s.checkLogin)
e.GET("/apm/web/traces", queryTraces, s.checkLogin)

// 链路查询
e.GET("/apm/web/queryTraces", queryTraces, s.checkLogin)
e.GET("/apm/web/trace", queryTrace, s.checkLogin)

// 告警平台
Expand Down
Loading

0 comments on commit 8bf9b7d

Please sign in to comment.