|
- package rpc
-
- import (
- "context"
- "fmt"
-
- "gitlink.org.cn/cloudream/common/consts/errorcode"
- "gitlink.org.cn/cloudream/common/pkgs/logger"
- "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc"
- corrpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/coordinator"
- jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
- )
-
- func (svc *Service) GetHubConfig(ctx context.Context, msg *corrpc.GetHubConfig) (*corrpc.GetHubConfigResp, *rpc.CodeError) {
- log := logger.WithField("HubID", msg.HubID)
-
- hub, err := svc.db.Hub().GetByID(svc.db.DefCtx(), msg.HubID)
- if err != nil {
- log.Warnf("getting hub: %v", err)
- return nil, rpc.Failed(errorcode.OperationFailed, fmt.Sprintf("getting hub: %v", err))
- }
-
- return corrpc.RespGetHubConfig(hub), nil
- }
-
- func (svc *Service) GetHubs(ctx context.Context, msg *corrpc.GetHubs) (*corrpc.GetHubsResp, *rpc.CodeError) {
- var hubs []*jcstypes.Hub
-
- if msg.HubIDs == nil {
- get, err := svc.db.Hub().GetAllHubs(svc.db.DefCtx())
- if err != nil {
- logger.Warnf("getting all hubs: %s", err.Error())
- return nil, rpc.Failed(errorcode.OperationFailed, "get all hub failed")
- }
- for _, hub := range get {
- h := hub
- hubs = append(hubs, &h)
- }
-
- } else {
- // 可以不用事务
- get, err := svc.db.Hub().BatchGetByID(svc.db.DefCtx(), msg.HubIDs)
- if err != nil {
- logger.Warnf("batch get hubs by id: %s", err.Error())
- return nil, rpc.Failed(errorcode.OperationFailed, fmt.Sprintf("batch get hubs by id: %v", err))
- }
-
- getMp := make(map[jcstypes.HubID]jcstypes.Hub)
- for _, hub := range get {
- getMp[hub.HubID] = hub
- }
-
- for _, id := range msg.HubIDs {
- if hub, ok := getMp[id]; ok {
- h := hub
- hubs = append(hubs, &h)
- } else {
- hubs = append(hubs, nil)
- }
- }
- }
-
- return corrpc.NewGetHubsResp(hubs), nil
- }
-
- func (svc *Service) GetHubConnectivities(ctx context.Context, msg *corrpc.GetHubConnectivities) (*corrpc.GetHubConnectivitiesResp, *rpc.CodeError) {
- cons, err := svc.db.HubConnectivity().BatchGetByFromHub(svc.db.DefCtx(), msg.HubIDs)
- if err != nil {
- logger.Warnf("batch get hub connectivities by from hub: %s", err.Error())
- return nil, rpc.Failed(errorcode.OperationFailed, "batch get hub connectivities by from hub failed")
- }
-
- return corrpc.RespGetHubConnectivities(cons), nil
- }
-
- func (svc *Service) ReportHubConnectivity(ctx context.Context, msg *corrpc.ReportHubConnectivity) (*corrpc.ReportHubConnectivityResp, *rpc.CodeError) {
- err := svc.db.HubConnectivity().BatchUpdateOrCreate(svc.db.DefCtx(), msg.Connecttivities)
- if err != nil {
- logger.Warnf("batch update or create hub connectivities: %v", err)
- return nil, rpc.Failed(errorcode.OperationFailed, "batch update or create hub connectivities: %v", err)
- }
-
- return &corrpc.ReportHubConnectivityResp{}, nil
- }
|