123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- package service
- import (
- "errors"
- "fmt"
- "github.com/go-sql-driver/mysql"
- omodels "go-admin/app/observe/models"
- "time"
- "encoding/json"
- "github.com/go-admin-team/go-admin-core/sdk/service"
- "gorm.io/gorm"
- "go-admin/app/admin/models"
- "go-admin/app/admin/service/dto"
- "go-admin/common/actions"
- cDto "go-admin/common/dto"
- "go-admin/utils"
- )
- type OtApps struct {
- service.Service
- }
- // GetPage 获取OtApps列表
- func (e *OtApps) GetPage(c *dto.OtAppsGetPageReq, p *actions.DataPermission, list *[]models.OtApps, count *int64) error {
- var err error
- var data models.OtApps
- err = e.Orm.Model(&data).
- Scopes(
- cDto.MakeCondition(c.GetNeedSearch()),
- cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
- actions.Permission(data.TableName(), p),
- ).
- Find(list).Limit(-1).Offset(-1).
- Count(count).Error
- if err != nil {
- e.Log.Errorf("OtAppsService GetPage error:%s \r\n", err)
- return err
- }
- return nil
- }
- // Get 获取OtApps对象
- func (e *OtApps) Get(d *dto.OtAppsGetReq, p *actions.DataPermission, model *models.OtApps) error {
- var data models.OtApps
- err := e.Orm.Model(&data).
- Scopes(
- actions.Permission(data.TableName(), p),
- ).
- First(model, d.GetId()).Error
- if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
- err = errors.New("查看对象不存在或无权查看")
- e.Log.Errorf("Service GetOtApps error:%s \r\n", err)
- return err
- }
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- return err
- }
- return nil
- }
- // Insert 创建OtApps对象
- func (e *OtApps) Insert(c *dto.OtAppsInsertReq) error {
- var err error
- var data models.OtApps
- c.Generate(&data)
- svcAppNames := make([]omodels.DataServiceAppName, 0, len(c.ServiceNames))
- for _, serviceName := range c.ServiceNames {
- svcAppNames = append(svcAppNames, omodels.DataServiceAppName{
- CreateAt: time.Now(),
- ServiceName: serviceName,
- AppName: c.Alias,
- })
- }
- if errTrans := e.Orm.Transaction(func(tx *gorm.DB) error {
- err = tx.Create(&data).Error
- if err != nil {
- e.Log.Errorf("OtAppsService Insert error:%s \r\n", err)
- return fmt.Errorf("insert otapp failed: %w", err)
- }
- if len(svcAppNames) == 0 {
- return nil
- }
- if errDel := tx.Delete(&omodels.DataServiceAppName{}, "app_name=?", c.Alias).Error; errDel != nil {
- return fmt.Errorf("删除appname关系失败:%w", errDel)
- }
- if errBatch := tx.CreateInBatches(&svcAppNames, len(svcAppNames)).Error; errBatch != nil {
- var mysqlErr *mysql.MySQLError
- if errors.As(errBatch, &mysqlErr) {
- switch mysqlErr.Number {
- case 1062: // MySQL code for duplicate entry
- return fmt.Errorf("一个service只能属于一个appname:%w", errBatch)
- default:
- return fmt.Errorf("写入data_service_appname出错:%w", errBatch)
- }
- }
- return fmt.Errorf("写入data_service_appname出错:%w", errBatch)
- }
- return nil
- }); errTrans != nil {
- return errTrans
- }
- return nil
- }
- // Update 修改OtApps对象
- func (e *OtApps) Update(c *dto.OtAppsUpdateReq, p *actions.DataPermission) error {
- var err error
- var data = models.OtApps{}
- e.Orm.Scopes(
- actions.Permission(data.TableName(), p),
- ).First(&data, c.GetId())
- c.Generate(&data)
- svcAppNames := make([]omodels.DataServiceAppName, 0, len(c.ServiceNames))
- for _, serviceName := range c.ServiceNames {
- svcAppNames = append(svcAppNames, omodels.DataServiceAppName{
- CreateAt: time.Now(),
- ServiceName: serviceName,
- AppName: c.Alias,
- })
- }
- if errTrans := e.Orm.Transaction(func(tx *gorm.DB) error {
- db := tx.Save(&data)
- if err = db.Error; err != nil {
- e.Log.Errorf("OtAppsService Save error:%s \r\n", err)
- return err
- }
- if db.RowsAffected == 0 {
- return errors.New("无权更新该数据")
- }
- if errDel := tx.Delete(&omodels.DataServiceAppName{}, "app_name=?", c.Alias).Error; errDel != nil {
- return fmt.Errorf("删除appname关系失败:%w", errDel)
- }
- if len(svcAppNames) == 0 {
- return nil
- }
- if errBatch := tx.CreateInBatches(&svcAppNames, len(svcAppNames)).Error; errBatch != nil {
- var mysqlErr *mysql.MySQLError
- if errors.As(errBatch, &mysqlErr) {
- switch mysqlErr.Number {
- case 1062: // MySQL code for duplicate entry
- return fmt.Errorf("一个service只能属于一个appname:%w", errBatch)
- default:
- return fmt.Errorf("写入data_service_appname出错:%w", errBatch)
- }
- }
- return fmt.Errorf("写入data_service_appname出错:%w", errBatch)
- }
- return nil
- }); errTrans != nil {
- return errTrans
- }
- return nil
- }
- // Remove 删除OtApps
- func (e *OtApps) Remove(d *dto.OtAppsDeleteReq, p *actions.DataPermission) error {
- var data models.OtApps
- db := e.Orm.Model(&data).
- Scopes(
- actions.Permission(data.TableName(), p),
- ).Delete(&data, d.GetId())
- if err := db.Error; err != nil {
- e.Log.Errorf("Service RemoveOtApps error:%s \r\n", err)
- return err
- }
- if db.RowsAffected == 0 {
- return errors.New("无权删除该数据")
- }
- return nil
- }
- func (e *OtApps) CreateDashBoard(ad *OtAppDashboard, op *OtPlugins, d *models.OtApps) error {
- // GetPlugin
- plugin := new(models.OtPlugins)
- plugin.Alias = "grafana"
- var err error
- err = op.GetPluginByAlias(plugin)
- if err != nil {
- e.Log.Error("Get Plugin %s error: %s \r\n", plugin.Alias, err)
- return err
- }
- configMap := make(map[string]string)
- err = json.Unmarshal([]byte(plugin.Config), &configMap)
- if err != nil {
- e.Log.Errorf("unmarshal error: %s \r\n", err)
- return err
- }
- // 请求grafana API,create
- grafanaApi := fmt.Sprintf("%s/api/dashboards/db", plugin.Url)
- e.Log.Debugf("create grafan dashboard: %s, token: %s", grafanaApi, configMap["admin"])
- g := utils.GrafanaViewer{
- Url: plugin.Url,
- Token: configMap["admin"],
- }
- gresp := utils.GrDashBoard{
- Title: fmt.Sprintf("%s-viewer", d.Alias),
- Tags: []string{d.Alias, d.Name},
- TimeZone: "browser",
- SchemaVersion: 16,
- Refresh: "1m",
- }
- gdbr := utils.GrDashBoardReq{
- Dashboard: &gresp,
- Overwrite: false,
- }
- err = g.CreateDashBoard(&gdbr, &gresp)
- if err != nil {
- e.Log.Errorf("CreateDashBoard error: %s \r\n", err.Error())
- return err
- }
- //TODO: 写入关系
- c := new(dto.OtAppDashboardInsertReq)
- c.AppId = int64(d.Id)
- c.DashboardUid = gresp.Url
- err = ad.Insert(c)
- if err != nil {
- e.Log.Errorf("CreateDashBoard insert app_dashboard error: %s \r\n", err.Error())
- return err
- }
- return nil
- }
|