fix: event and component relevancy (#1414)

Co-authored-by: 曲源成 <quyc@goodrain.com>
This commit is contained in:
Quyc 2022-11-03 20:24:28 +08:00 committed by GitHub
parent 1c325998d9
commit f449183889
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 76 additions and 39 deletions

View File

@ -321,7 +321,7 @@ func (e *EventLogStruct) MyTeamsEvents(w http.ResponseWriter, r *http.Request) {
Events[i].EndTime = strings.Replace(Events[i].EndTime[0:19]+"+08:00", " ", "T", 1)
}
MyTeamsEvent.ServiceEvent = event
buildList, err := handler.GetServiceManager().ListVersionInfo(event.TargetID, true)
buildVersion, err := handler.GetServiceManager().EventBuildVersion(event.TargetID, event.BuildVersion)
if err != nil {
if err.Error() == "error getting service by uuid: record not found" {
logrus.Debugf("ServiceID:%v record not found", event.TargetID)
@ -330,11 +330,7 @@ func (e *EventLogStruct) MyTeamsEvents(w http.ResponseWriter, r *http.Request) {
httputil.ReturnError(r, w, 500, "get ListVersionInfo error")
return
}
if buildList == nil{
logrus.Info("service not found build list")
continue
}
MyTeamsEvent.BuildList = buildList
MyTeamsEvent.BuildList = buildVersion
res = append(res, MyTeamsEvent)
}
httputil.ReturnList(r, w, total, page, res)

View File

@ -360,7 +360,7 @@ func (t *TenantStruct) BuildService(w http.ResponseWriter, r *http.Request) {
func (t *TenantStruct) BuildList(w http.ResponseWriter, r *http.Request) {
serviceID := r.Context().Value(ctxutil.ContextKey("service_id")).(string)
resp, err := handler.GetServiceManager().ListVersionInfo(serviceID, false)
resp, err := handler.GetServiceManager().ListVersionInfo(serviceID)
if err != nil {
logrus.Error("get version info error", err.Error())

View File

@ -2356,7 +2356,7 @@ func (s *ServiceAction) GetServiceDeployInfo(tenantID, serviceID string) (*pb.De
}
// ListVersionInfo lists version info
func (s *ServiceAction) ListVersionInfo(serviceID string, showCurrentBuildInfo bool) (*api_model.BuildListRespVO, error) {
func (s *ServiceAction) ListVersionInfo(serviceID string) (*api_model.BuildListRespVO, error) {
versionInfos, err := db.GetManager().VersionInfoDao().GetAllVersionByServiceID(serviceID)
if err != nil && err != gorm.ErrRecordNotFound {
logrus.Errorf("error getting all version by service id: %v", err)
@ -2384,18 +2384,6 @@ func (s *ServiceAction) ListVersionInfo(serviceID string, showCurrentBuildInfo b
bv.ImageTag = image.GetTag()
}
}
if showCurrentBuildInfo {
for _, bversion := range bversions {
if bversion.BuildVersion == svc.DeployVersion {
result := &api_model.BuildListRespVO{
DeployVersion: svc.DeployVersion,
List: bversion,
}
return result, nil
}
continue
}
}
result := &api_model.BuildListRespVO{
DeployVersion: svc.DeployVersion,
List: bversions,
@ -2403,6 +2391,29 @@ func (s *ServiceAction) ListVersionInfo(serviceID string, showCurrentBuildInfo b
return result, nil
}
// EventBuildVersion -
func (s *ServiceAction) EventBuildVersion(serviceID, buildVersion string) (*api_model.BuildListRespVO, error) {
versionInfo, err := db.GetManager().VersionInfoDao().GetVersionByDeployVersion(buildVersion, serviceID)
if err != nil && err != gorm.ErrRecordNotFound {
logrus.Errorf("error getting all version by service id: %v", err)
return nil, fmt.Errorf("error getting all version by service id: %v", err)
}
b, err := json.Marshal(versionInfo)
if err != nil {
return nil, fmt.Errorf("error marshaling version infos: %v", err)
}
var bversion *api_model.BuildVersion
if err := json.Unmarshal(b, &bversion); err != nil {
return nil, fmt.Errorf("error unmarshaling version infos: %v", err)
}
result := &api_model.BuildListRespVO{
DeployVersion: buildVersion,
List: bversion,
}
return result, nil
}
// AddAutoscalerRule -
func (s *ServiceAction) AddAutoscalerRule(req *api_model.AutoscalerRuleReq) error {
tx := db.GetManager().Begin()

View File

@ -80,7 +80,8 @@ type ServiceHandler interface {
ServiceCheck(*api_model.ServiceCheckStruct) (string, string, *util.APIHandleError)
GetServiceCheckInfo(uuid string) (*exector.ServiceCheckResult, *util.APIHandleError)
GetServiceDeployInfo(tenantID, serviceID string) (*pb.DeployInfo, *util.APIHandleError)
ListVersionInfo(serviceID string, showCurrentBuildInfo bool) (*api_model.BuildListRespVO, error)
ListVersionInfo(serviceID string) (*api_model.BuildListRespVO, error)
EventBuildVersion(serviceID, buildVersion string) (*api_model.BuildListRespVO, error)
AddAutoscalerRule(req *api_model.AutoscalerRuleReq) error
UpdAutoscalerRule(req *api_model.AutoscalerRuleReq) error

View File

@ -236,11 +236,15 @@ func (w *resWriter) WriteHeader(statusCode int) {
// WrapEL wrap eventlog, handle event log before and after process
func WrapEL(f http.HandlerFunc, target, optType string, synType int) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var serviceKind string
var (
serviceKind string
buildVersion string
)
serviceObj := r.Context().Value(ctxutil.ContextKey("service"))
if serviceObj != nil {
service := serviceObj.(*dbmodel.TenantServices)
serviceKind = service.Kind
buildVersion = service.DeployVersion
}
if r.Method != "GET" {
@ -287,7 +291,7 @@ func WrapEL(f http.HandlerFunc, target, optType string, synType int) http.Handle
tenantID := r.Context().Value(ctxutil.ContextKey("tenant_id")).(string)
var ctx context.Context
event, err := util.CreateEvent(target, optType, targetID, tenantID, string(body), operator, synType)
event, err := util.CreateEvent(target, optType, targetID, tenantID, string(body), operator, buildVersion, synType)
if err != nil {
logrus.Error("create event error : ", err)
httputil.ReturnError(r, w, 500, "操作失败")

View File

@ -77,7 +77,7 @@ func checkTimeout(event *dbmodel.ServiceEvent) bool {
}
// CreateEvent save event
func CreateEvent(target, optType, targetID, tenantID, reqBody, userName string, synType int) (*dbmodel.ServiceEvent, error) {
func CreateEvent(target, optType, targetID, tenantID, reqBody, userName, buildVersion string, synType int) (*dbmodel.ServiceEvent, error) {
if len(reqBody) > 1024 {
reqBody = reqBody[0:1024]
}
@ -91,6 +91,7 @@ func CreateEvent(target, optType, targetID, tenantID, reqBody, userName string,
StartTime: time.Now().Format(time.RFC3339),
SynType: synType,
OptType: optType,
BuildVersion: buildVersion,
}
err := db.GetManager().ServiceEventDao().AddModel(&event)
return &event, err

View File

@ -317,6 +317,10 @@ func (e *exectorManager) buildFromImage(task *pb.TaskMessage) {
logrus.Errorf("Update app service deploy version failure %s, service %s do not auto upgrade", err.Error(), i.ServiceID)
break
}
if err := e.UpdateServiceEvent(i.EventID, i.DeployVersion); err != nil {
logrus.Errorf("Update service event deploy version failure %s, service %s do not auto upgrade", err.Error(), i.ServiceID)
return
}
err = e.sendAction(i.TenantID, i.ServiceID, i.EventID, i.DeployVersion, i.Action, configs, i.Logger)
if err != nil {
i.Logger.Error("Send upgrade action failed", map[string]string{"step": "callback", "status": "failure"})
@ -378,6 +382,10 @@ func (e *exectorManager) buildFromSourceCode(task *pb.TaskMessage) {
logrus.Errorf("Update app service deploy version failure %s, service %s do not auto upgrade", err.Error(), i.ServiceID)
return
}
if err := e.UpdateServiceEvent(i.EventID, i.DeployVersion); err != nil {
logrus.Errorf("Update service event deploy version failure %s, service %s do not auto upgrade", err.Error(), i.ServiceID)
return
}
err = e.sendAction(i.TenantID, i.ServiceID, i.EventID, i.DeployVersion, i.Action, configs, i.Logger)
if err != nil {
i.Logger.Error("Send upgrade action failed", map[string]string{"step": "callback", "status": "failure"})
@ -423,6 +431,10 @@ func (e *exectorManager) buildFromMarketSlug(task *pb.TaskMessage) {
logrus.Errorf("Update app service deploy version failure %s, service %s do not auto upgrade", err.Error(), i.ServiceID)
break
}
if err := e.UpdateServiceEvent(i.EventID, i.DeployVersion); err != nil {
logrus.Errorf("Update service event deploy version failure %s, service %s do not auto upgrade", err.Error(), i.ServiceID)
return
}
err = e.sendAction(i.TenantID, i.ServiceID, i.EventID, i.DeployVersion, i.Action, i.Configs, i.Logger)
if err != nil {
i.Logger.Error("Send upgrade action failed", map[string]string{"step": "callback", "status": "failure"})
@ -458,6 +470,7 @@ func (e *exectorManager) sendAction(tenantID, serviceID, eventID, newVersion, ac
TargetID: serviceID,
UserName: "",
SynType: dbmodel.ASYNEVENTTYPE,
BuildVersion: newVersion,
}
if err := db.GetManager().ServiceEventDao().AddModel(event); err != nil {
logrus.Errorf("create upgrade event failure %s, service %s do not auto upgrade", err.Error(), serviceID)
@ -606,3 +619,7 @@ func (e *exectorManager) GetCurrentConcurrentTask() float64 {
func (e *exectorManager) UpdateDeployVersion(serviceID, newVersion string) error {
return db.GetManager().TenantServiceDao().UpdateDeployVersion(serviceID, newVersion)
}
func (e *exectorManager) UpdateServiceEvent(eventID, deployVersion string) error {
return db.GetManager().ServiceEventDao().UpdateBuildVersion(eventID, deployVersion)
}

View File

@ -435,6 +435,7 @@ type EventDao interface {
UpdateReason(eventID string, reason string) error
SetEventStatus(ctx context.Context, status model.EventStatus) error
UpdateInBatch(events []*model.ServiceEvent) error
UpdateBuildVersion(eventID, deployVersion string) error
}
//VersionInfoDao VersionInfoDao

View File

@ -78,21 +78,22 @@ var EventStatusFailure EventStatus = "failure"
//ServiceEvent event struct
type ServiceEvent struct {
Model
EventID string `gorm:"column:event_id;size:40"`
TenantID string `gorm:"column:tenant_id;size:40;index:tenant_id"`
ServiceID string `gorm:"column:service_id;size:40;index:service_id"`
Target string `gorm:"column:target;size:40"`
TargetID string `gorm:"column:target_id;size:255;index:target_id"`
RequestBody string `gorm:"column:request_body;size:1024"`
UserName string `gorm:"column:user_name;size:40"`
StartTime string `gorm:"column:start_time;size:40"`
EndTime string `gorm:"column:end_time;size:40"`
OptType string `gorm:"column:opt_type;size:40"`
SynType int `gorm:"column:syn_type;size:1"`
Status string `gorm:"column:status;size:40"`
FinalStatus string `gorm:"column:final_status;size:40"`
Message string `gorm:"column:message"`
Reason string `gorm:"column:reason"`
EventID string `gorm:"column:event_id;size:40"`
TenantID string `gorm:"column:tenant_id;size:40;index:tenant_id"`
ServiceID string `gorm:"column:service_id;size:40;index:service_id"`
Target string `gorm:"column:target;size:40"`
TargetID string `gorm:"column:target_id;size:255;index:target_id"`
RequestBody string `gorm:"column:request_body;size:1024"`
UserName string `gorm:"column:user_name;size:40"`
StartTime string `gorm:"column:start_time;size:40"`
EndTime string `gorm:"column:end_time;size:40"`
OptType string `gorm:"column:opt_type;size:40"`
SynType int `gorm:"column:syn_type;size:1"`
Status string `gorm:"column:status;size:40"`
FinalStatus string `gorm:"column:final_status;size:40"`
Message string `gorm:"column:message"`
Reason string `gorm:"column:reason"`
BuildVersion string `gorm:"column:build_version;size:40" json:"build_version"`
}
//TableName 表名

View File

@ -135,6 +135,11 @@ func (c *EventDaoImpl) UpdateInBatch(events []*model.ServiceEvent) error {
return nil
}
// UpdateBuildVersion update BuildVersion.
func (c *EventDaoImpl) UpdateBuildVersion(eventID string, deployVersion string) error {
return c.DB.Model(&model.ServiceEvent{}).Where("event_id=?", eventID).UpdateColumn("build_version", deployVersion).Error
}
//GetEventByServiceID get event log message
func (c *EventDaoImpl) GetEventByServiceID(serviceID string) ([]*model.ServiceEvent, error) {
var result []*model.ServiceEvent