Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"hook_url.go",
"mysql_event_log.go",
"mysql_hook_url.go",
"mysql_url_event.go",
],
importpath = "go-common/app/admin/ep/tapd/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/ep/tapd/conf:go_default_library",
"//app/admin/ep/tapd/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/orm:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/sync/pipeline/fanout:go_default_library",
"//vendor/github.com/jinzhu/gorm:go_default_library",
"//vendor/gopkg.in/gomail.v2:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,92 @@
package dao
import (
"bytes"
"context"
"encoding/json"
"net/http"
"time"
"go-common/app/admin/ep/tapd/conf"
"go-common/library/cache/memcache"
"go-common/library/database/orm"
"go-common/library/log"
xhttp "go-common/library/net/http/blademaster"
"go-common/library/sync/pipeline/fanout"
"github.com/jinzhu/gorm"
"gopkg.in/gomail.v2"
)
// Dao dao.
type Dao struct {
c *conf.Config
httpClient *xhttp.Client
db *gorm.DB
email *gomail.Dialer
mc *memcache.Pool
cache *fanout.Fanout
expire int32
}
// New init mysql db.
func New(c *conf.Config) *Dao {
return &Dao{
c: c,
httpClient: xhttp.NewClient(c.HTTPClient),
db: orm.NewMySQL(c.ORM),
email: gomail.NewDialer(c.Mail.Host, c.Mail.Port, c.Mail.Username, c.Mail.Password),
mc: memcache.NewPool(c.Memcache.Config),
cache: fanout.New("cache", fanout.Worker(5), fanout.Buffer(10240)),
expire: int32(time.Duration(c.Memcache.Expire) / time.Second),
}
}
// Close close the resource.
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
if d.mc != nil {
d.mc.Close()
}
}
// Ping verify server is ok.
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.db.DB().Ping(); err != nil {
log.Info("dao.cloudDB.Ping() error(%v)", err)
}
return
}
// tokenCacheSave The err does not need to return, because this method is irrelevant.
func (d *Dao) tokenCacheSave(c context.Context, cacheItem *memcache.Item) {
var f = func(c context.Context) {
var (
conn = d.mc.Get(c)
err error
)
defer conn.Close()
if err = conn.Set(cacheItem); err != nil {
log.Error("AddCache conn.Set(%s) error(%v)", cacheItem.Key, err)
}
}
if err := d.cache.Do(c, f); err != nil {
log.Error("Token cache save err(%v)", err)
}
}
func (d *Dao) newRequest(method, url string, v interface{}) (req *http.Request, err error) {
body := &bytes.Buffer{}
if method != http.MethodGet {
if err = json.NewEncoder(body).Encode(v); err != nil {
log.Error("json encode value(%s) err(?) ", v, err)
return
}
}
if req, err = http.NewRequest(method, url, body); err != nil {
log.Error("http new request url(?) err(?)", url, err)
}
return
}

View File

@@ -0,0 +1,169 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"reflect"
"strings"
"sync"
"go-common/app/admin/ep/tapd/model"
"go-common/library/cache/memcache"
"go-common/library/log"
)
var rwmLock = new(sync.RWMutex)
// CallHookUrl Call Hook Url.
func (d *Dao) CallHookUrl(c context.Context, URL string, body interface{}) (err error) {
var (
req *http.Request
res = make(map[string]interface{})
)
if req, err = d.newRequest(http.MethodPost, URL, body); err != nil {
log.Error("d.CallHookUrl url(%s) err(%v)", URL, err)
return
}
if err = d.httpClient.Do(c, req, &res); err != nil {
log.Error("d.CallHookUrl url(%s) err(%v)", URL, err)
}
return
}
// CallHookUrlAsForm Call Hook Url As Form.
func (d *Dao) CallHookUrlAsForm(c context.Context, URL string, body map[string]interface{}) (err error) {
var (
res = make(map[string]interface{})
req *http.Request
)
data := make(url.Values)
for mapKey := range body {
typeKind := reflect.TypeOf(body[mapKey]).Kind()
switch typeKind {
case reflect.Int:
data[mapKey] = []string{fmt.Sprintf("%d", body[mapKey].(int))}
case reflect.Int64:
data[mapKey] = []string{fmt.Sprintf("%d", body[mapKey].(int64))}
case reflect.Float64:
data[mapKey] = []string{fmt.Sprintf("%.0f", body[mapKey].(float64))}
case reflect.String:
data[mapKey] = []string{body[mapKey].(string)}
default:
data[mapKey] = []string{fmt.Sprint(body[mapKey])}
}
}
if req, err = http.NewRequest(http.MethodPost, URL, strings.NewReader(data.Encode())); err != nil {
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
err = d.httpClient.Do(c, req, &res)
return
}
// SaveEnableHookURLToCache Save Enable Hook URL To Cache.
func (d *Dao) SaveEnableHookURLToCache() (eventMap map[string][]*model.HookUrl, err error) {
rwmLock.Lock()
defer rwmLock.Unlock()
var urlEvents []*model.UrlEvent
eventMap = make(map[string][]*model.HookUrl)
// 获取有效event
if urlEvents, err = d.QueryURLEventByStatus(model.HookEventStatusEnable); err != nil {
return
}
//倒查url 过滤不存在或无效状态
for _, urlEvent := range urlEvents {
var hookURL *model.HookUrl
if hookURL, err = d.QueryHookURLByID(urlEvent.UrlID); err != nil || hookURL.ID == 0 || hookURL.Status != model.HookURLStatusEnable {
continue
}
eventMap[urlEvent.Event] = append(eventMap[urlEvent.Event], hookURL)
}
for eventMapKey := range eventMap {
item := &memcache.Item{Key: eventMapKey, Object: eventMap[eventMapKey], Flags: memcache.FlagJSON, Expiration: d.expire}
d.tokenCacheSave(context.Background(), item)
}
return
}
// GetEnableHookURLFromCacheAndSaveIfNot Get Enable Hook URL From Cache and save if not.
func (d *Dao) GetEnableHookURLFromCacheAndSaveIfNot(event model.Event) (hookURLs []*model.HookUrl, err error) {
var (
conn = d.mc.Get(context.Background())
item *memcache.Item
)
rwmLock.RLock()
defer rwmLock.RUnlock()
defer conn.Close()
if item, err = conn.Get(string(event)); err == nil {
if err = json.Unmarshal(item.Value, &hookURLs); err != nil {
log.Error("GetEnableHookURLFromCache json parse error(%v)", err)
}
return
}
var eventMap map[string][]*model.HookUrl
if eventMap, err = d.SaveEnableHookURLToCache(); err != nil {
return
}
hookURLs = eventMap[string(event)]
return
}
// GetEnableHookURLFromCache Get Enable Hook URL From Cache.
func (d *Dao) GetEnableHookURLFromCache(event model.Event) (hookURLs []*model.HookUrl, err error) {
var (
conn = d.mc.Get(context.Background())
item *memcache.Item
)
rwmLock.RLock()
defer rwmLock.RUnlock()
defer conn.Close()
if item, err = conn.Get(string(event)); err == nil {
if err = json.Unmarshal(item.Value, &hookURLs); err != nil {
log.Error("GetEnableHookURLFromCache json parse error(%v)", err)
}
return
}
return
}
// GetEnableHookURLFromDB Get Enable Hook URL From DB.
func (d *Dao) GetEnableHookURLFromDB(event model.Event) (hookURLs []*model.HookUrl, err error) {
var urlEvents []*model.UrlEvent
if urlEvents, err = d.QueryURLEventByEventAndStatus(string(event), model.HookEventStatusEnable); err != nil {
return
}
for _, urlEvent := range urlEvents {
var hookURL *model.HookUrl
if hookURL, err = d.QueryHookURLByID(urlEvent.UrlID); err != nil {
return
}
hookURLs = append(hookURLs, hookURL)
}
return
}

View File

@@ -0,0 +1,37 @@
package dao
import "go-common/app/admin/ep/tapd/model"
// AddEventLog Add Event Log.
func (d *Dao) AddEventLog(eventLog *model.EventLog) error {
return d.db.Create(eventLog).Error
}
// UpdateEventLog Update Event Log.
func (d *Dao) UpdateEventLog(eventLog *model.EventLog) error {
return d.db.Model(&model.EventLog{}).Where("id=?", eventLog.ID).Update(eventLog).Error
}
//FindEventLogs Find Event Logs.
func (d *Dao) FindEventLogs(req *model.QueryEventLogReq) (total int64, eventLogs []*model.EventLog, err error) {
gDB := d.db.Model(&model.EventLog{})
if req.WorkspaceID > 0 {
gDB = gDB.Where("workspace_id=?", req.WorkspaceID)
}
if req.EventID > 0 {
gDB = gDB.Where("event_id=?", req.EventID)
}
if string(req.Event) != "" {
gDB = gDB.Where("event like ?", string(req.Event)+_wildcards)
}
if err = gDB.Count(&total).Error; err != nil {
return
}
err = gDB.Order("ctime desc").Offset((req.PageNum - 1) * req.PageSize).Limit(req.PageSize).Find(&eventLogs).Error
return
}

View File

@@ -0,0 +1,120 @@
package dao
import (
"go-common/app/admin/ep/tapd/model"
"go-common/library/ecode"
)
const _wildcards = "%"
// AddHookURL Add Hook URL.
func (d *Dao) AddHookURL(hookURL *model.HookUrl) error {
return d.db.Create(hookURL).Error
}
// UpdateHookURL Update Hook URL.
func (d *Dao) UpdateHookURL(hookURL *model.HookUrl) error {
return d.db.Model(&model.HookUrl{}).Where("id=?", hookURL.ID).Update(hookURL).Error
}
// QueryHookURLByID Query Hook URL By ID.
func (d *Dao) QueryHookURLByID(id int64) (hookURL *model.HookUrl, err error) {
hookURL = &model.HookUrl{}
err = d.db.Model(&model.HookUrl{}).Where("id = ?", id).First(hookURL).Error
if err == ecode.NothingFound {
err = nil
}
return
}
// AddHookURLandEvent Add Hook URL and Event.
func (d *Dao) AddHookURLandEvent(hookURL *model.HookUrl, urlEvents []*model.UrlEvent) (err error) {
tx := d.db.Begin()
if err = tx.Error; err != nil {
return
}
if err = tx.Create(hookURL).Error; err != nil {
tx.Rollback()
return
}
for _, urlEvent := range urlEvents {
urlEvent.UrlID = hookURL.ID
if err = tx.Create(urlEvent).Error; err != nil {
tx.Rollback()
return
}
}
if err = tx.Commit().Error; err != nil {
tx.Rollback()
}
return
}
// UpdateHookURLandEvent Update Hook URL and Event.
func (d *Dao) UpdateHookURLandEvent(hookURL *model.HookUrl, urlEvents []*model.UrlEvent) (err error) {
tx := d.db.Begin()
if err = tx.Error; err != nil {
return
}
if err = tx.Model(model.HookUrl{}).Where("id=?", hookURL.ID).
Updates(map[string]interface{}{"url": hookURL.URL, "workspace_id": hookURL.WorkspaceID, "status": hookURL.Status, "update_by": hookURL.UpdateBy}).
Error; err != nil {
tx.Rollback()
return
}
for _, urlEvent := range urlEvents {
if urlEvent.ID != 0 {
//update
if err = tx.Model(model.UrlEvent{}).Where("id=?", urlEvent.ID).Update(urlEvent).Error; err != nil {
tx.Rollback()
return
}
} else {
//add
if err = tx.Create(urlEvent).Error; err != nil {
tx.Rollback()
return
}
}
}
if err = tx.Commit().Error; err != nil {
tx.Rollback()
}
return
}
//FindHookURLs Find Hook URLs.
func (d *Dao) FindHookURLs(req *model.QueryHookURLReq) (total int64, hookURLs []*model.HookUrl, err error) {
gDB := d.db.Model(&model.HookUrl{})
if req.ID > 0 {
gDB = gDB.Where("id=?", req.ID)
}
if req.Status > 0 {
gDB = gDB.Where("status=?", req.Status)
}
if req.UpdateBy != "" {
gDB = gDB.Where("update_by=?", req.UpdateBy)
}
if req.URL != "" {
gDB = gDB.Where("url like ?", req.URL+_wildcards)
}
if err = gDB.Count(&total).Error; err != nil {
return
}
err = gDB.Order("ctime desc").Offset((req.PageNum - 1) * req.PageSize).Limit(req.PageSize).Find(&hookURLs).Error
return
}

View File

@@ -0,0 +1,52 @@
package dao
import (
"go-common/app/admin/ep/tapd/model"
"go-common/library/ecode"
)
// AddURLEvent Add URL Event.
func (d *Dao) AddURLEvent(urlEvent *model.UrlEvent) error {
return d.db.Create(urlEvent).Error
}
// QueryURLEventByUrlAndEvent Query URL Event By Url And Event.
func (d *Dao) QueryURLEventByUrlAndEvent(urlID int64, eventType string) (urlEvents []*model.UrlEvent, err error) {
err = d.db.Model(&model.HookUrl{}).Where("url_id = ? and event = ?", urlID, eventType).Find(&urlEvents).Error
if err == ecode.NothingFound {
err = nil
}
return
}
// QueryURLEventByUrl Query URL Event By Url.
func (d *Dao) QueryURLEventByUrl(urlID int64) (urlEvents []*model.UrlEvent, err error) {
err = d.db.Model(&model.HookUrl{}).Where("url_id = ?", urlID).Find(&urlEvents).Error
if err == ecode.NothingFound {
err = nil
}
return
}
// QueryURLEventByEventAndStatus Query URL Event By Event and status.
func (d *Dao) QueryURLEventByEventAndStatus(event string, status int) (urlEvents []*model.UrlEvent, err error) {
err = d.db.Model(&model.HookUrl{}).Where("event = ? and status = ?", event, status).Find(&urlEvents).Error
if err == ecode.NothingFound {
err = nil
}
return
}
// QueryURLEventByStatus Query URL Event By Status.
func (d *Dao) QueryURLEventByStatus(status int) (urlEvents []*model.UrlEvent, err error) {
err = d.db.Model(&model.HookUrl{}).Where("status = ?", status).Find(&urlEvents).Error
if err == ecode.NothingFound {
err = nil
}
return
}
// UpdateURLEventStatus Update URL Event status.
func (d *Dao) UpdateURLEventStatus(id int64, status int) error {
return d.db.Model(&model.UrlEvent{}).Where("id=?", id).Update("status", status).Error
}