123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- package http
- import (
- "fmt"
- "go-common/app/admin/main/apm/model/app"
- "go-common/library/ecode"
- "go-common/library/log"
- "strconv"
- "strings"
- bm "go-common/library/net/http/blademaster"
- )
- func appList(c *bm.Context) {
- var err error
- v := new(struct {
- AppID string `form:"app_id"`
- Pn int `form:"pn" default:"1"`
- Ps int `form:"ps" default:"20"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- var (
- aa []*app.App
- count int
- lk = "%" + v.AppID + "%"
- )
- if v.AppID != "" {
- err = apmSvc.DB.Where("app_id LIKE ?", lk).Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
- } else {
- err = apmSvc.DB.Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
- }
- if err != nil {
- log.Error("apmSvc.AppList error(%v)", err)
- c.JSON(nil, err)
- return
- }
- if v.AppID != "" {
- err = apmSvc.DB.Where("app_id LIKE ?", lk).Model(&app.App{}).Count(&count).Error
- } else {
- err = apmSvc.DB.Model(&app.App{}).Count(&count).Error
- }
- if err != nil {
- log.Error("apmSvc.AppList count error(%v)", err)
- c.JSON(nil, err)
- return
- }
- data := &Paper{
- Pn: v.Pn,
- Ps: v.Ps,
- Items: aa,
- Total: count,
- }
- c.JSON(data, nil)
- }
- func appAdd(c *bm.Context) {
- var err error
- username := name(c)
- v := new(struct {
- AppTreeID int64 `form:"app_tree_id" validate:"required"`
- AppID string `form:"app_id" validate:"required"`
- Limit int64 `form:"limit"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- if err = apmSvc.AppAdd(c, username, v.AppTreeID, v.AppID, v.Limit); err != nil {
- log.Error("apmSvc.appAdd error(%v)", err)
- c.JSON(nil, err)
- return
- }
- c.JSON(nil, err)
- }
- func appEdit(c *bm.Context) {
- var err error
- v := new(struct {
- ID int `form:"id" validate:"required"`
- Limit int `form:"limit" validate:"required"`
- })
- if err = c.Bind(v); err != nil {
- return
- }
- a := &app.App{}
- if err = apmSvc.DB.Where("id = ?", v.ID).Find(a).Error; err != nil {
- log.Error("apmSvc.appEdit find(%d) error(%v)", v.ID, err)
- c.JSON(nil, err)
- return
- }
- ups := map[string]interface{}{
- "limit": v.Limit,
- }
- if err = apmSvc.DB.Model(&app.App{}).Where("id = ?", v.ID).Updates(ups).Error; err != nil {
- log.Error("apmSvc.appEdit updates error(%v)", err)
- c.JSON(nil, err)
- return
- }
- sqlLog := &map[string]interface{}{
- "SQLType": "update",
- "Where": "id = ?",
- "Value1": v.ID,
- "Update": ups,
- "Old": a,
- }
- username := name(c)
- apmSvc.SendLog(*c, username, 0, 2, int64(v.ID), "apmSvc.appEdit", sqlLog)
- c.JSON(nil, err)
- }
- func appDelete(c *bm.Context) {
- v := new(struct {
- ID int `form:"id" validate:"required"`
- })
- username := name(c)
- var err error
- if err = c.Bind(v); err != nil {
- return
- }
- a := &app.App{}
- if err = apmSvc.DB.Where("id = ?", v.ID).Find(a).Error; err != nil {
- log.Error("apmSvc.appDelete find(%d) error(%v)", v.ID, err)
- c.JSON(nil, err)
- return
- }
- if err = apmSvc.DB.Delete(a).Error; err != nil {
- log.Error("apmSvc.appDelete delete(%d) error(%v)", v.ID, err)
- c.JSON(nil, err)
- return
- }
- apmSvc.SendLog(*c, username, 0, 3, int64(v.ID), "apmSvc.appDelete", a)
- c.JSON(nil, err)
- }
- func appAuthList(c *bm.Context) {
- v := new(struct {
- AppID string `form:"app_id"`
- ServiceID string `form:"service_id"`
- Pn int `form:"pn" default:"1"`
- Ps int `form:"ps" default:"20"`
- })
- var err error
- if err = c.Bind(v); err != nil {
- return
- }
- var (
- aa []*app.Auth
- count int
- )
- if v.AppID != "" && v.ServiceID != "" {
- err = apmSvc.DB.Where("app_id=? and service_id=?", v.AppID, v.ServiceID).Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
- } else if v.AppID != "" {
- err = apmSvc.DB.Where("app_id=?", v.AppID).Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
- } else if v.ServiceID != "" {
- err = apmSvc.DB.Where("service_id=?", v.ServiceID).Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
- } else {
- err = apmSvc.DB.Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
- }
- if err != nil {
- log.Error("apmSvc.appAuthList error(%v)", err)
- c.JSON(nil, err)
- return
- }
- if v.AppID != "" {
- err = apmSvc.DB.Where("app_id=?", v.AppID).Model(&app.Auth{}).Count(&count).Error
- } else {
- err = apmSvc.DB.Model(&app.Auth{}).Count(&count).Error
- }
- if err != nil {
- log.Error("apmSvc.appAuthList count error(%v)", err)
- c.JSON(nil, err)
- return
- }
- data := &Paper{
- Pn: v.Pn,
- Ps: v.Ps,
- Items: aa,
- Total: count,
- }
- c.JSON(data, nil)
- }
- func appAuthAdd(c *bm.Context) {
- v := new(struct {
- AppTreeID int64 `form:"app_tree_id" validate:"required"`
- AppID string `form:"app_id" validate:"required"`
- ServiceTreeID int64 `form:"service_tree_id" validate:"required"`
- ServiceID string `form:"service_id" validate:"required"`
- RPCMethod string `form:"rpc_method"`
- HTTPMethod string `form:"http_method"`
- Quota int64 `form:"quota"`
- })
- var err error
- if err = c.Bind(v); err != nil {
- return
- }
- if v.ServiceTreeID == v.AppTreeID {
- log.Error("apmSvc.appAuthAdd service_tree_id=app_tree_id error(%v)", v.ServiceTreeID)
- c.JSON(nil, ecode.RequestErr)
- return
- }
- cnt := 0
- if err = apmSvc.DB.Model(&app.Auth{}).Where("service_tree_id=? and app_tree_id=?", v.ServiceTreeID, v.AppTreeID).Count(&cnt).Error; err != nil {
- log.Error("apmSvc.appAuthAdd count error(%v)", err)
- c.JSON(nil, ecode.RequestErr)
- return
- }
- if cnt > 0 {
- log.Error("apmSvc.appAuthAdd count (%v)", cnt)
- c.JSON(nil, ecode.RequestErr)
- return
- }
- a := &app.Auth{
- AppTreeID: v.AppTreeID,
- AppID: v.AppID,
- ServiceTreeID: v.ServiceTreeID,
- ServiceID: v.ServiceID,
- RPCMethod: v.RPCMethod,
- HTTPMethod: v.HTTPMethod,
- Quota: v.Quota,
- }
- if err = apmSvc.DB.Create(a).Error; err != nil {
- log.Error("apmSvc.appAuthAdd create error(%v)", err)
- c.JSON(nil, err)
- return
- }
- username := name(c)
- apmSvc.SendLog(*c, username, 0, 1, a.ID, "apmSvc.appAuthAdd", a)
- c.JSON(nil, err)
- }
- func appAuthEdit(c *bm.Context) {
- v := new(struct {
- ID int `form:"id" validate:"required"`
- RPCMethod string `form:"rpc_method"`
- HTTPMethod string `form:"http_method"`
- Quota int `form:"quota" validate:"required"`
- })
- var err error
- if err = c.Bind(v); err != nil {
- return
- }
- a := &app.Auth{}
- if err = apmSvc.DB.Where("id = ?", v.ID).Find(a).Error; err != nil {
- log.Error("apmSvc.appAuthEdit find(%d) error(%v)", v.ID, err)
- c.JSON(nil, err)
- return
- }
- ups := map[string]interface{}{
- "rpc_method": v.RPCMethod,
- "http_method": v.HTTPMethod,
- "quota": v.Quota,
- }
- if err = apmSvc.DB.Model(&app.Auth{}).Where("id = ?", v.ID).Updates(ups).Error; err != nil {
- log.Error("apmSvc.appAuthEdit updates error(%v)", err)
- c.JSON(nil, err)
- return
- }
- sqlLog := &map[string]interface{}{
- "SQLType": "update",
- "Where": "id = ?",
- "Value1": v.ID,
- "Update": ups,
- "Old": a,
- }
- username := name(c)
- apmSvc.SendLog(*c, username, 0, 2, int64(v.ID), "apmSvc.appAuthEdit", sqlLog)
- c.JSON(nil, err)
- }
- func appAuthDelete(c *bm.Context) {
- v := new(struct {
- ID int `form:"id" validate:"required"`
- })
- var err error
- if err = c.Bind(v); err != nil {
- return
- }
- a := &app.Auth{}
- if err = apmSvc.DB.Where("id = ?", v.ID).Find(a).Error; err != nil {
- log.Error("apmSvc.appAuthDelete find(%d) error(%v)", v.ID, err)
- c.JSON(nil, err)
- return
- }
- if err = apmSvc.DB.Delete(a).Error; err != nil {
- log.Error("apmSvc.appAuthDelete delete(%d) error(%v)", v.ID, err)
- c.JSON(nil, err)
- return
- }
- username := name(c)
- apmSvc.SendLog(*c, username, 0, 3, int64(v.ID), "apmSvc.appAuthDelete", a)
- c.JSON(nil, err)
- }
- func appTree(c *bm.Context) {
- v := new(struct {
- Bu string `form:"bu"`
- Team string `form:"team"`
- Name string `form:"name"`
- })
- var err error
- if err = c.Bind(v); err != nil {
- return
- }
- username := name(c)
- trees, err := apmSvc.Trees(c, username, c.Request.Header.Get("Cookie"))
- if err != nil {
- log.Error("apmSvc.appTree error(%v)", err)
- c.JSON(nil, err)
- return
- }
- var data []string
- temp := make(map[string]string)
- if v.Bu == "" && v.Team == "" {
- for _, val := range trees {
- nameArr := strings.Split(val.Path, ".")
- newName := nameArr[1]
- if f := temp[newName]; f == "" {
- data = append(data, newName)
- temp[newName] = newName
- }
- }
- } else if v.Bu != "" && v.Team != "" && v.Name != "" {
- for _, val := range trees {
- nameArr := strings.Split(val.Path, ".")
- if v.Bu == nameArr[1] && v.Team == nameArr[2] && v.Name == nameArr[3] {
- data = append(data, strconv.Itoa(val.TreeID))
- }
- }
- } else if v.Bu != "" && v.Team != "" {
- for _, val := range trees {
- nameArr := strings.Split(val.Path, ".")
- if v.Bu == nameArr[1] && v.Team == nameArr[2] {
- newName := nameArr[1] + "." + nameArr[2] + "." + nameArr[3]
- if f := temp[newName]; f == "" {
- data = append(data, nameArr[3])
- temp[newName] = newName
- }
- }
- }
- } else if v.Bu != "" {
- for _, val := range trees {
- nameArr := strings.Split(val.Path, ".")
- if v.Bu == nameArr[1] {
- newName := v.Bu + "." + nameArr[2]
- if f := temp[newName]; f == "" {
- data = append(data, nameArr[2])
- temp[newName] = newName
- }
- }
- }
- }
- c.JSON(data, nil)
- }
- func appCallerSearch(c *bm.Context) {
- v := new(struct {
- AppID string `form:"app_id"`
- Pn int `form:"pn" default:"1"`
- Ps int `form:"ps" default:"20"`
- })
- var err error
- if err = c.Bind(v); err != nil {
- return
- }
- var (
- aa []*app.Auth
- count int
- lk = "%" + v.AppID + "%"
- )
- if v.AppID != "" {
- err = apmSvc.DB.Where("app_id LIKE ?", lk).Group("app_tree_id").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
- } else {
- err = apmSvc.DB.Group("app_tree_id").Offset((v.Pn - 1) * v.Ps).Limit(v.Ps).Find(&aa).Error
- }
- if err != nil {
- log.Error("apmSvc.appCallerSearch error(%v)", err)
- c.JSON(nil, err)
- return
- }
- if v.AppID != "" {
- err = apmSvc.DB.Select("count(distinct(app_tree_id))").Where("app_id LIKE ?", lk).Model(&app.Auth{}).Count(&count).Error
- } else {
- err = apmSvc.DB.Select("count(distinct(app_tree_id))").Model(&app.Auth{}).Count(&count).Error
- }
- if err != nil {
- log.Error("apmSvc.appCallerSearch count error(%v)", err)
- c.JSON(nil, err)
- return
- }
- type result struct {
- AppTreeID int64 `gorm:"column:app_tree_id" json:"app_tree_id"`
- AppID string `gorm:"column:app_id" json:"app_id"`
- Services []*app.Auth
- }
- var results []*result
- if count > 0 {
- var in []int64
- auth := []*app.Auth{}
- for _, val := range aa {
- in = append(in, val.AppTreeID)
- fmt.Printf("apptreeid=%v", val.AppTreeID)
- }
- apmSvc.DB.Where("app_tree_id in (?)", in).Find(&auth)
- for _, vv := range aa {
- rs := new(result)
- rs.AppTreeID = vv.AppTreeID
- rs.AppID = vv.AppID
- for _, vvv := range auth {
- if vv.AppTreeID == vvv.AppTreeID {
- rs.Services = append(rs.Services, vvv)
- }
- }
- results = append(results, rs)
- }
- }
- data := &Paper{
- Pn: v.Pn,
- Ps: v.Ps,
- Items: results,
- Total: count,
- }
- c.JSON(data, nil)
- }
|