goploy/model/PublishTraceModel.go

328 lines
8.5 KiB
Go
Raw Normal View History

2022-04-09 21:53:37 +08:00
// Copyright 2022 The Goploy Authors. All rights reserved.
// Use of this source code is governed by a GPLv3-style
// license that can be found in the LICENSE file.
2020-08-04 14:28:25 +08:00
package model
2021-01-08 20:22:35 +08:00
import (
2022-02-24 09:14:50 +08:00
"fmt"
2021-01-08 20:22:35 +08:00
sq "github.com/Masterminds/squirrel"
)
2020-08-04 14:28:25 +08:00
const publishTraceTable = "`publish_trace`"
2020-08-15 13:38:06 +08:00
// PublishTrace -
2020-08-04 14:28:25 +08:00
type PublishTrace struct {
ID int64 `json:"id"`
Token string `json:"token"`
2022-02-24 09:14:50 +08:00
NamespaceID int64 `json:"namespaceId"`
2020-08-04 14:28:25 +08:00
ProjectID int64 `json:"projectId"`
ProjectName string `json:"projectName"`
Detail string `json:"detail"`
State int `json:"state"`
PublisherID int64 `json:"publisherId"`
PublisherName string `json:"publisherName"`
Type int `json:"type"`
Ext string `json:"ext"`
PublishState int `json:"publishState"`
InsertTime string `json:"insertTime"`
UpdateTime string `json:"updateTime"`
}
2020-08-15 13:38:06 +08:00
// PublishTraces -
2020-08-04 14:28:25 +08:00
type PublishTraces []PublishTrace
2020-08-15 13:38:06 +08:00
// publish trace state
2020-08-04 14:28:25 +08:00
const (
2022-05-13 19:29:06 +08:00
QUEUE = 0
2020-08-04 14:28:25 +08:00
BeforePull = 1
Pull = 2
AfterPull = 3
BeforeDeploy = 4
Deploy = 5
AfterDeploy = 6
)
2022-02-24 09:14:50 +08:00
func (pt PublishTrace) GetList(page, limit uint64) (PublishTraces, error) {
builder := sq.
Select(
fmt.Sprintf("%s.token", publishTraceTable),
fmt.Sprintf("min(%s.project_id)", publishTraceTable),
fmt.Sprintf("min(%s.project_name)", publishTraceTable),
fmt.Sprintf("min(%s.publisher_id)", publishTraceTable),
fmt.Sprintf("min(%s.publisher_name)", publishTraceTable),
fmt.Sprintf("min(%s.state)", publishTraceTable),
fmt.Sprintf("IFNULL(GROUP_CONCAT(IF(%s.state = 0, %[1]s.detail, NULL)), '') as detail", publishTraceTable),
fmt.Sprintf("min(%s.insert_time) as insert_time", publishTraceTable),
).
From(publishTraceTable).
GroupBy("token")
if pt.NamespaceID > 0 {
builder = builder.
Join(fmt.Sprintf("%s ON %[1]s.id = %s.project_id", projectTable, publishTraceTable)).
Where(sq.Eq{projectTable + ".namespace_id": pt.NamespaceID})
}
if pt.PublisherName != "" {
builder = builder.Where(sq.Eq{publishTraceTable + ".publisher_name": pt.PublisherName})
}
if pt.ProjectName != "" {
builder = builder.Where(sq.Eq{publishTraceTable + ".project_name": pt.ProjectName})
}
2020-08-04 14:28:25 +08:00
2022-02-24 09:14:50 +08:00
rows, err := builder.RunWith(DB).
OrderBy("insert_time DESC").
Limit(limit).
Offset((page - 1) * limit).
Query()
2020-08-04 14:28:25 +08:00
if err != nil {
2022-02-24 09:14:50 +08:00
return nil, err
2020-08-04 14:28:25 +08:00
}
2022-02-24 09:14:50 +08:00
publishTraces := PublishTraces{}
for rows.Next() {
var publishTrace PublishTrace
2020-08-04 14:28:25 +08:00
2022-02-24 09:14:50 +08:00
if err := rows.Scan(
&publishTrace.Token,
&publishTrace.ProjectID,
&publishTrace.ProjectName,
&publishTrace.PublisherID,
&publishTrace.PublisherName,
&publishTrace.State,
&publishTrace.Detail,
&publishTrace.InsertTime); err != nil {
return nil, err
}
publishTraces = append(publishTraces, publishTrace)
}
return publishTraces, nil
}
func (pt PublishTrace) GetTotal() (int64, error) {
var total int64
builder := sq.Select("COUNT(distinct token) AS count").
From(publishTraceTable)
if pt.NamespaceID > 0 {
builder = builder.
Join(fmt.Sprintf("%s ON %[1]s.id = %s.project_id", projectTable, publishTraceTable)).
Where(sq.Eq{projectTable + ".namespace_id": pt.NamespaceID})
}
if pt.PublisherName != "" {
builder = builder.Where(sq.Eq{publishTraceTable + ".publisher_name": pt.PublisherName})
}
if pt.ProjectName != "" {
builder = builder.Where(sq.Eq{publishTraceTable + ".project_name": pt.ProjectName})
}
err := builder.RunWith(DB).
QueryRow().
Scan(&total)
if err != nil {
return 0, err
}
return total, nil
2020-08-04 14:28:25 +08:00
}
func (pt PublishTrace) GetListByToken() (PublishTraces, error) {
rows, err := sq.
2020-12-23 17:38:39 +08:00
Select(
"id",
"token",
"project_id",
"project_name",
"if(state = 0,detail, '') as detail",
"state",
"publisher_id",
"publisher_name",
"type",
"ext",
"insert_time",
"update_time").
2020-08-04 14:28:25 +08:00
From(publishTraceTable).
Where(sq.Eq{"token": pt.Token}).
RunWith(DB).
Query()
if err != nil {
return nil, err
}
publishTraces := PublishTraces{}
for rows.Next() {
var publishTrace PublishTrace
if err := rows.Scan(
&publishTrace.ID,
&publishTrace.Token,
&publishTrace.ProjectID,
&publishTrace.ProjectName,
&publishTrace.Detail,
&publishTrace.State,
&publishTrace.PublisherID,
&publishTrace.PublisherName,
&publishTrace.Type,
&publishTrace.Ext,
&publishTrace.InsertTime,
&publishTrace.UpdateTime); err != nil {
return nil, err
}
publishTraces = append(publishTraces, publishTrace)
}
return publishTraces, nil
}
2021-01-08 20:22:35 +08:00
func (pt PublishTrace) GetPreview(
branch string,
commit string,
filename string,
commitDate []string,
deployDate []string,
pagination Pagination,
) (PublishTraces, Pagination, error) {
2020-08-04 14:28:25 +08:00
builder := sq.
2022-05-13 19:29:06 +08:00
Select(
"token",
"MIN(publisher_name) publisher_name",
"MIN(state) publish_state",
"GROUP_CONCAT(IF(type = 2 and ext != '', JSON_EXTRACT(ext, '$.commit') , '') SEPARATOR '') as ext",
"MIN(update_time) update_time",
).
From(publishTraceTable)
2020-08-04 14:28:25 +08:00
if pt.ProjectID != 0 {
builder = builder.Where(sq.Eq{"project_id": pt.ProjectID})
}
if pt.PublisherID != 0 {
builder = builder.Where(sq.Eq{"publisher_id": pt.PublisherID})
}
2021-01-08 20:22:35 +08:00
if branch != "" {
builder = builder.Where(sq.Like{"ext": "%" + branch + "%"})
}
if commit != "" {
builder = builder.Where(sq.Like{"ext": "%" + commit + "%"})
}
if filename != "" {
builder = builder.Where(sq.Like{"ext": "%" + filename + "%"})
}
if len(commitDate) > 1 {
builder = builder.Where(`substring(ext, POSITION('"timestamp":' IN ext) + 12, 10) between ? and ?`, commitDate[0], commitDate[1])
}
if len(deployDate) > 1 {
builder = builder.Where("insert_time between ? and ?", deployDate[0], deployDate[1])
}
2020-08-04 14:28:25 +08:00
if pt.PublishState != -1 {
builder = builder.Having(sq.Eq{"publish_state": pt.PublishState})
}
rows, err := builder.RunWith(DB).
2022-05-13 19:29:06 +08:00
GroupBy("token").
2020-08-04 14:28:25 +08:00
OrderBy("update_time DESC").
Limit(pagination.Rows).
Offset((pagination.Page - 1) * pagination.Rows).
Query()
if err != nil {
return nil, pagination, err
}
publishTraces := PublishTraces{}
for rows.Next() {
var publishTrace PublishTrace
if err := rows.Scan(
&publishTrace.Token,
&publishTrace.PublisherName,
2022-05-13 19:29:06 +08:00
&publishTrace.PublishState,
2020-08-04 14:28:25 +08:00
&publishTrace.Ext,
2022-05-13 19:29:06 +08:00
&publishTrace.UpdateTime); err != nil {
2020-08-04 14:28:25 +08:00
return nil, pagination, err
}
publishTraces = append(publishTraces, publishTrace)
}
builder = sq.
Select("COUNT(*) AS count").
From(publishTraceTable).
Where(sq.Eq{"type": Pull})
if pt.ProjectID != 0 {
builder = builder.Where(sq.Eq{"project_id": pt.ProjectID})
}
if pt.PublisherID != 0 {
builder = builder.Where(sq.Eq{"publisher_id": pt.PublisherID})
}
2021-01-08 20:22:35 +08:00
if branch != "" {
builder = builder.Where(sq.Like{"ext": "%" + branch + "%"})
}
if commit != "" {
builder = builder.Where(sq.Like{"ext": "%" + commit + "%"})
}
if filename != "" {
builder = builder.Where(sq.Like{"ext": "%" + filename + "%"})
}
if len(commitDate) > 1 {
builder = builder.Where(`substring(ext, POSITION('"timestamp":' IN ext) + 12, 10) between ? and ?`, commitDate[0], commitDate[1])
}
if len(deployDate) > 1 {
builder = builder.Where("insert_time between ? and ?", deployDate[0], deployDate[1])
}
2020-08-04 14:28:25 +08:00
if pt.PublishState == 0 {
builder = builder.Where("EXISTS (SELECT id FROM " + publishTraceTable + " AS pt where pt.state = 0 AND pt.token = publish_trace.token)")
} else if pt.PublishState == 1 {
builder = builder.Where("! EXISTS (SELECT id FROM " + publishTraceTable + " AS pt where pt.state = 0 AND pt.token = publish_trace.token)")
}
err = builder.RunWith(DB).
QueryRow().
Scan(&pagination.Total)
if err != nil {
2021-05-22 20:38:04 +08:00
return publishTraces, pagination, err
2020-08-04 14:28:25 +08:00
}
return publishTraces, pagination, nil
}
2020-12-23 17:38:39 +08:00
// GetDetail return detail value by id
func (pt PublishTrace) GetDetail() (string, error) {
var detail string
err := sq.
Select("detail").
From(publishTraceTable).
Where(sq.Eq{"id": pt.ID}).
RunWith(DB).
QueryRow().
Scan(&detail)
if err != nil {
return detail, err
}
return detail, nil
}
2021-02-12 23:05:06 +08:00
2022-02-24 09:14:50 +08:00
// AddRow return LastInsertId
func (pt PublishTrace) AddRow() (int64, error) {
result, err := sq.
Insert(publishTraceTable).
Columns("token", "project_id", "project_name", "detail", "state", "publisher_id", "publisher_name", "type", "ext").
Values(pt.Token, pt.ProjectID, pt.ProjectName, pt.Detail, pt.State, pt.PublisherID, pt.PublisherName, pt.Type, pt.Ext).
RunWith(DB).
Exec()
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
return id, err
}
2021-02-12 23:05:06 +08:00
// EditUpdateTimeByToken -
func (pt PublishTrace) EditUpdateTimeByToken() error {
_, err := sq.
Update(publishTraceTable).
SetMap(sq.Eq{
"update_time": pt.UpdateTime,
}).
Where(sq.Eq{"token": pt.Token}).
RunWith(DB).
Exec()
return err
}