[ADD] Add a new event notification interface

This commit is contained in:
zhoujunhao 2018-08-07 16:34:31 +08:00
parent 0e4a0e5d0a
commit 6d4010ea64
5 changed files with 92 additions and 1 deletions

View File

@ -249,6 +249,8 @@ func (v2 *V2) appRouter() chi.Router {
func (v2 *V2) notificationEventRouter() chi.Router {
r := chi.NewRouter()
r.Get("/", controller.GetNotificationEvents)
r.Get("/1", controller.GetNotificationEvents1)
r.Get("/2", controller.GetNotificationEvents2)
r.Put("/{hash}", controller.HandleNotificationEvent)
r.Get("/{hash}", controller.GetNotificationEvent)
return r

View File

@ -112,6 +112,46 @@ func GetNotificationEvents(w http.ResponseWriter, r *http.Request) {
httputil.ReturnSuccess(r, w, res)
}
func GetNotificationEvents1(w http.ResponseWriter, r *http.Request) {
var startTime, endTime time.Time
start := r.FormValue("start")
end := r.FormValue("end")
fmt.Println("======>form",start,end)
if si, err := strconv.Atoi(start); err == nil {
startTime = time.Unix(int64(si), 0)
}
if ei, err := strconv.Atoi(end); err == nil {
endTime = time.Unix(int64(ei), 0)
}
fmt.Println("=====>",startTime,endTime)
res, err := db.GetManager().NotificationEventDao().GetNotificationEventGrouping(startTime, endTime)
if err != nil {
httputil.ReturnError(r, w, 500, err.Error())
return
}
httputil.ReturnSuccess(r, w, res)
}
func GetNotificationEvents2(w http.ResponseWriter, r *http.Request) {
var startTime, endTime time.Time
start := r.FormValue("start")
end := r.FormValue("end")
fmt.Println("======>form",start,end)
if si, err := strconv.Atoi(start); err == nil {
startTime = time.Unix(int64(si), 0)
}
if ei, err := strconv.Atoi(end); err == nil {
endTime = time.Unix(int64(ei), 0)
}
fmt.Println("=====>",startTime,endTime)
res, err := db.GetManager().NotificationEventDao().GetNotificationEventGrouping2(startTime, endTime)
if err != nil {
httputil.ReturnError(r, w, 500, err.Error())
return
}
httputil.ReturnSuccess(r, w, res)
}
//Handle Handle
// swagger:parameters handlenotify
type Handle struct {

View File

@ -73,7 +73,7 @@ type AbnormalInfo struct {
func (a AbnormalInfo) Hash() string {
hash := sha256.New()
hash.Write([]byte(a.ServiceID + a.ServiceAlias + a.PodName + a.ContainerName))
return string(hash.Sum(nil))
return fmt.Sprintf("%x", hash.Sum(nil))
}
func (a AbnormalInfo) String() string {
return fmt.Sprintf("ServiceID: %s;ServiceAlias: %s;PodName: %s ; ContainerName: %s; Reason: %s; Message: %s",

View File

@ -384,6 +384,8 @@ type NotificationEventDao interface {
GetNotificationEventByKind(kind, kindID string) ([]*model.NotificationEvent, error)
GetNotificationEventByTime(start, end time.Time) ([]*model.NotificationEvent, error)
GetNotificationEventNotHandle() ([]*model.NotificationEvent, error)
GetNotificationEventGrouping(start, end time.Time) ([]*model.NotificationEvent, error)
GetNotificationEventGrouping2(start, end time.Time) ([]*model.NotificationEvent, error)
}
//AppBackupDao group app backup history

View File

@ -196,9 +196,56 @@ func (c *NotificationEventDaoImpl) GetNotificationEventByTime(start, end time.Ti
}
return nil, err
}
c.DB.Table("notification_event").Select("*").Group("kind_id").Order("last_time DESC").Limit(1).Scan(&result)
c.DB.Where("last_time>? and last_time<?", start, end).Group("kind_id").Order("last_time DESC").Limit(1).Find(&result)
return result, nil
}
func (c *NotificationEventDaoImpl) GetNotificationEventGrouping(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).Group("kind_id").Order("last_time DESC").Limit(1).Find(&result).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return result, nil
}
return nil, err
}
return result, nil
}
if err := c.DB.Where("last_time>? and last_time<?", start, end).Group("kind_id").Order("last_time DESC").Limit(1).Find(&result).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return result, nil
}
return nil, err
}
return result, nil
}
func (c *NotificationEventDaoImpl) GetNotificationEventGrouping2(start, end time.Time) ([]*model.NotificationEvent, error) {
var result []*model.NotificationEvent
if !start.IsZero() && !end.IsZero() {
if err := c.DB.Table("notification_event").Select("*").Where("last_time>? and last_time<?", start, end).Group("kind_id").Order("last_time DESC").Limit(1).Scan(&result).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return result, nil
}
return nil, err
}
return result, nil
}
if err := c.DB.Table("notification_event").Select("*").Where("last_time>? and last_time<?", start, end).Group("kind_id").Order("last_time DESC").Limit(1).Scan(&result).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