2018-03-14 14:12:26 +08:00
|
|
|
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
|
2017-11-16 16:30:22 +08:00
|
|
|
// RAINBOND, Application Management Platform
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2017-11-16 16:30:22 +08:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version. For any non-GPL usage of Rainbond,
|
|
|
|
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
|
|
|
|
// must be obtained first.
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2017-11-16 16:30:22 +08:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2018-03-14 14:33:31 +08:00
|
|
|
|
2017-11-16 16:30:22 +08:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package dao
|
|
|
|
|
|
|
|
import (
|
2018-01-05 10:40:21 +08:00
|
|
|
"encoding/json"
|
2017-12-12 19:04:44 +08:00
|
|
|
"time"
|
|
|
|
|
2018-01-05 10:40:21 +08:00
|
|
|
"github.com/Sirupsen/logrus"
|
2018-04-24 16:44:59 +08:00
|
|
|
"github.com/goodrain/rainbond/db/model"
|
2017-11-16 16:30:22 +08:00
|
|
|
"github.com/jinzhu/gorm"
|
2017-12-12 19:04:44 +08:00
|
|
|
)
|
2017-11-16 16:30:22 +08:00
|
|
|
|
|
|
|
//AddModel AddModel
|
|
|
|
func (c *EventDaoImpl) AddModel(mo model.Interface) error {
|
|
|
|
result := mo.(*model.ServiceEvent)
|
2017-11-16 17:22:08 +08:00
|
|
|
var oldResult model.ServiceEvent
|
2017-11-16 16:30:22 +08:00
|
|
|
if ok := c.DB.Where("event_id=?", result.EventID).Find(&oldResult).RecordNotFound(); ok {
|
|
|
|
if err := c.DB.Create(result).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-22 19:28:56 +08:00
|
|
|
logrus.Infoln("event result is exist")
|
|
|
|
return c.UpdateModel(mo)
|
2017-11-16 16:30:22 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//UpdateModel UpdateModel
|
|
|
|
func (c *EventDaoImpl) UpdateModel(mo model.Interface) error {
|
|
|
|
result := mo.(*model.ServiceEvent)
|
2017-11-17 09:49:52 +08:00
|
|
|
|
2017-11-16 16:30:22 +08:00
|
|
|
var oldResult model.ServiceEvent
|
|
|
|
if ok := c.DB.Where("event_id=?", result.EventID).Find(&oldResult).RecordNotFound(); !ok {
|
2017-12-12 19:04:44 +08:00
|
|
|
finalUpdateEvent(result, &oldResult)
|
|
|
|
oldB, _ := json.Marshal(oldResult)
|
|
|
|
logrus.Infof("update event to %s", string(oldB))
|
2017-12-28 10:18:52 +08:00
|
|
|
if err := c.DB.Save(&oldResult).Error; err != nil {
|
2017-11-16 16:30:22 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func finalUpdateEvent(target *model.ServiceEvent, old *model.ServiceEvent) {
|
2017-12-12 19:04:44 +08:00
|
|
|
if target.CodeVersion != "" {
|
|
|
|
old.CodeVersion = target.CodeVersion
|
2017-11-22 19:16:20 +08:00
|
|
|
}
|
2017-12-12 19:04:44 +08:00
|
|
|
if target.OptType != "" {
|
|
|
|
old.OptType = target.OptType
|
2017-11-22 19:28:56 +08:00
|
|
|
}
|
|
|
|
|
2017-12-12 19:04:44 +08:00
|
|
|
if target.Status != "" {
|
|
|
|
old.Status = target.Status
|
2017-11-22 19:16:20 +08:00
|
|
|
}
|
2017-12-12 19:04:44 +08:00
|
|
|
if target.Message != "" {
|
|
|
|
old.Message = target.Message
|
2017-11-22 19:16:20 +08:00
|
|
|
}
|
|
|
|
old.FinalStatus = "complete"
|
2017-12-12 19:04:44 +08:00
|
|
|
if target.FinalStatus != "" {
|
|
|
|
old.FinalStatus = target.FinalStatus
|
2017-11-22 19:16:20 +08:00
|
|
|
}
|
2017-11-16 16:30:22 +08:00
|
|
|
|
2017-11-22 19:16:20 +08:00
|
|
|
old.EndTime = time.Now().String()
|
2017-12-12 19:04:44 +08:00
|
|
|
if old.Status == "failure" && old.OptType == "callback" {
|
2017-11-22 19:16:20 +08:00
|
|
|
old.DeployVersion = old.OldDeployVersion
|
2017-11-16 16:30:22 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-12 19:04:44 +08:00
|
|
|
|
|
|
|
//EventDaoImpl EventLogMessageDaoImpl
|
2017-11-16 16:30:22 +08:00
|
|
|
type EventDaoImpl struct {
|
|
|
|
DB *gorm.DB
|
|
|
|
}
|
|
|
|
|
2017-12-12 19:04:44 +08:00
|
|
|
//GetEventByEventID get event log message
|
2017-11-16 16:30:22 +08:00
|
|
|
func (c *EventDaoImpl) GetEventByEventID(eventID string) (*model.ServiceEvent, error) {
|
|
|
|
var result model.ServiceEvent
|
|
|
|
if err := c.DB.Where("event_id=?", eventID).Find(&result).Error; err != nil {
|
2018-02-06 14:59:21 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetEventByEventIDs get event info
|
|
|
|
func (c *EventDaoImpl) GetEventByEventIDs(eventIDs []string) ([]*model.ServiceEvent, error) {
|
|
|
|
var result []*model.ServiceEvent
|
|
|
|
if err := c.DB.Where("event_id in (?)", eventIDs).Find(&result).Error; err != nil {
|
2017-11-16 16:30:22 +08:00
|
|
|
if err == gorm.ErrRecordNotFound {
|
2018-02-06 14:59:21 +08:00
|
|
|
return nil, nil
|
2017-11-16 16:30:22 +08:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-06 14:59:21 +08:00
|
|
|
return result, nil
|
2017-11-16 16:30:22 +08:00
|
|
|
}
|
|
|
|
|
2017-12-12 19:04:44 +08:00
|
|
|
//GetEventByServiceID get event log message
|
2017-11-16 16:30:22 +08:00
|
|
|
func (c *EventDaoImpl) GetEventByServiceID(serviceID string) ([]*model.ServiceEvent, error) {
|
|
|
|
var result []*model.ServiceEvent
|
|
|
|
if err := c.DB.Where("service_id=?", serviceID).Find(&result).Order("start_time DESC").Error; err != nil {
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
//return messageRaw, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2018-05-11 13:44:15 +08:00
|
|
|
|
2018-06-14 14:32:36 +08:00
|
|
|
//GetEventByServiceID delete event log
|
|
|
|
func (c *EventDaoImpl) DelEventByServiceID(serviceID string) (error) {
|
|
|
|
var result *model.ServiceEvent
|
|
|
|
if err := c.DB.Where("service_id=?", serviceID).Delete(&result).Error; err != nil {
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
//return messageRaw, nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-11 13:44:15 +08:00
|
|
|
//NotificationEventDaoImpl NotificationEventDaoImpl
|
|
|
|
type NotificationEventDaoImpl struct {
|
|
|
|
DB *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
//AddModel AddModel
|
|
|
|
func (c *NotificationEventDaoImpl) AddModel(mo model.Interface) error {
|
|
|
|
result := mo.(*model.NotificationEvent)
|
|
|
|
result.LastTime = time.Now()
|
|
|
|
result.FirstTime = time.Now()
|
|
|
|
var oldResult model.NotificationEvent
|
|
|
|
if ok := c.DB.Where("hash=?", result.Hash).Find(&oldResult).RecordNotFound(); ok {
|
|
|
|
if err := c.DB.Create(result).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Infoln("event result is exist")
|
|
|
|
return c.UpdateModel(mo)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//UpdateModel UpdateModel
|
|
|
|
func (c *NotificationEventDaoImpl) UpdateModel(mo model.Interface) error {
|
|
|
|
result := mo.(*model.NotificationEvent)
|
|
|
|
var oldResult model.NotificationEvent
|
|
|
|
if ok := c.DB.Where("hash=?", result.Hash).Find(&oldResult).RecordNotFound(); !ok {
|
|
|
|
result.FirstTime = oldResult.FirstTime
|
|
|
|
result.ID = oldResult.ID
|
|
|
|
}
|
|
|
|
return c.DB.Save(result).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetNotificationEventByKind GetNotificationEventByKind
|
|
|
|
func (c *NotificationEventDaoImpl) GetNotificationEventByKind(kind, kindID string) ([]*model.NotificationEvent, error) {
|
|
|
|
var result []*model.NotificationEvent
|
|
|
|
if err := c.DB.Where("kind=? and kind_id=?", kind, kindID).Find(&result).Order("last_time DESC").Error; err != nil {
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetNotificationEventByTime GetNotificationEventByTime
|
|
|
|
func (c *NotificationEventDaoImpl) GetNotificationEventByTime(start, end time.Time) ([]*model.NotificationEvent, error) {
|
|
|
|
var result []*model.NotificationEvent
|
|
|
|
if !start.IsZero() && !end.IsZero() {
|
|
|
|
if err := c.DB.Where("last_time>? and last_time<?", start, end).Find(&result).Order("last_time DESC").Error; err != nil {
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
if err := c.DB.Where("last_time<?", time.Now()).Find(&result).Order("last_time DESC").Error; err != nil {
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetNotificationEventNotHandle GetNotificationEventNotHandle
|
|
|
|
func (c *NotificationEventDaoImpl) GetNotificationEventNotHandle() ([]*model.NotificationEvent, error) {
|
|
|
|
var result []*model.NotificationEvent
|
|
|
|
if err := c.DB.Where("is_handle=?", false).Find(&result).Order("last_time DESC").Error; err != nil {
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetNotificationEventByHash GetNotificationEventByHash
|
|
|
|
func (c *NotificationEventDaoImpl) GetNotificationEventByHash(hash string) (*model.NotificationEvent, error) {
|
|
|
|
var result model.NotificationEvent
|
|
|
|
if err := c.DB.Where("hash=?", hash).Find(&result).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|