作者 tangxvhui

更新,推送每日评估数据到字库

... ... @@ -4,6 +4,7 @@ import (
"time"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/pushdata"
"github.com/beego/beego/v2/server/web"
serviceNodeTask "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/node_task"
... ... @@ -23,6 +24,7 @@ func main() {
go notify.RunTaskSmsNotify()
//go notify.AppMessageRun()
go consumer.Run()
pushdata.PushData()
web.Run()
}
... ...
package pushdata
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
)
type FieldName struct {
Name string `json:"name"`
}
type RespData struct {
Code int `json:"code"`
Code int `json:"code"`
Msg string `json:"msg"`
}
func (resp *RespData) IsOk() bool {
return resp.Code == 0
}
type ReqCreateTable struct {
Name string `json:"name"`
Fields []FieldName `json:"fields"`
Data []map[string]string `json:"data"`
type Client struct {
Host string
AppSecret string // 调试用 3Oo4dG64X0
AppKey string // 调试用 GnAmG4jybB
}
type ReqAppendData struct {
Name string `json:"name"`
Fields []FieldName `json:"fields"`
Data []map[string]string `json:"data"`
func NewClient() *Client {
return &Client{
Host: "http://character-library-metadata-bastion-test.fjmaimaimai.com",
AppSecret: "3Oo4dG64X0",
AppKey: "GnAmG4jybB",
}
}
type AppTable interface {
AppTableName() string // 应用表名称
AppTableField() []FieldName // 应用字段
func (c *Client) useHeader() (h http.Header) {
token := jwt.New(jwt.SigningMethodHS256)
tk, err := token.SignedString([]byte(c.AppSecret))
if err != nil {
fmt.Printf("err:%s \n", err)
}
h = http.Header{}
h.Add("x-mmm-accesstoken", tk)
h.Add("x-mmm-appkey", c.AppKey)
return
}
type GenData interface {
DataForAppend() (ReqAppendData, error) //生成需要追加的数据
type ReqAppendData struct {
Name string `json:"name"` // 应用表名称
Fields []FieldName `json:"fields"` // 应用字段
Data []map[string]string `json:"data"` // 数据
}
type AppTableFile interface {
AppTable
GenData
// AppendData 追加应用表数据
func (c *Client) AppendData(reqData ReqAppendData) error {
apiUrl := c.Host + `/api/app-table-file/append-data`
bodyData, err := json.Marshal(reqData)
if err != nil {
return fmt.Errorf("推送数据到字库,追加应用表数据:%s", err)
}
req, err := http.NewRequest(http.MethodPost, apiUrl, bytes.NewReader(bodyData))
if err != nil {
return fmt.Errorf("推送数据到字库,追加应用表数据:%s", err)
}
req.Header = c.useHeader()
req.Header.Add("Content-Type", "application/json")
httpClient := http.DefaultClient
httpClient.Timeout = 60 * time.Second
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("推送数据到字库,追加应用表数据:%s", err)
}
defer resp.Body.Close()
var respData RespData
j := json.NewDecoder(resp.Body)
err = j.Decode(&respData)
if err != nil {
return fmt.Errorf("推送数据到字库,追加应用表数据:%s", err)
}
if !respData.IsOk() {
return fmt.Errorf("推送数据到字库,追加应用表数据响应:%s", respData.Msg)
}
return nil
}
type Client struct {
entry []AppTableFile
host string
type ReqSearchTable struct {
Name string `json:"name"`
}
// RegistEntry 注册数据推送项
func (c *Client) RegistEntry(item AppTableFile) {
c.entry = append(c.entry, item)
// SearchTable 查询应用数据表
func (c *Client) SearchTable(name string) {
apiUrl := `/api/app-table-file/list`
_ = apiUrl
}
// CreateTable 创建应用表
func (c *Client) CreateTable() {
type ReqCreateTable struct {
Name string `json:"name"` // 应用表名称
Fields []FieldName `json:"fields"` // 应用字段
Data []map[string]string `json:"data"`
}
// AppendData 追加应用表数据
func (c *Client) AppendData() {
// CreateTable 创建应用表
func (c *Client) CreateTable() {
apiUrl := `/api/app-table-file/create`
_ = apiUrl
}
... ...
package pushdata
import (
"encoding/json"
"testing"
"time"
)
func TestToken(t *testing.T) {
c := NewClient()
h := c.useHeader()
t.Logf("%v", h)
}
func TestField(t *testing.T) {
item := StaffAssessItem{}
fields := item.AppTableField()
bt, _ := json.Marshal(fields)
t.Log(string(bt))
}
func TestPushData(t *testing.T) {
nowTime := time.Unix(1688572800, 0)
err := SendDataStaffAssess(nowTime)
if err != nil {
t.Error(err)
}
}
... ...
package pushdata
import (
"time"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
)
func PushData() {
nowTime := time.Now()
y, m, d := nowTime.Date()
t1 := time.Date(y, m, d, 10, 0, 0, 0, time.Local) //今天的10点
interval := t1.Sub(nowTime)
if interval < 0 {
interval = (24 * time.Hour) + interval
}
timer := time.NewTimer(interval)
for {
<-timer.C
nowTime = time.Now()
err := SendDataStaffAssess(nowTime)
if err != nil {
log.Logger.Error("发送每日评估任务")
}
timer.Reset(24 * time.Hour)
}
}
... ...
package pushdata
import (
"fmt"
"time"
)
... ... @@ -19,7 +20,7 @@ type StaffAssessItem struct {
}
func (t *StaffAssessItem) AppTableName() string {
return "每日绩效评估"
return "人员绩效评估"
}
func (t *StaffAssessItem) AppTableField() []FieldName {
... ... @@ -55,7 +56,7 @@ func (t *StaffAssessItem) MapData() (data map[string]string) {
return
}
func (t *StaffAssessItem) DataForAppend() (data ReqAppendData, err error) {
func (t *StaffAssessItem) DataForAppend(nowTime time.Time) (data ReqAppendData, err error) {
sqlStr := `select
staff_assess.target_user ->>'account' as target_user_account,
staff_assess.target_user ->>'userName' as target_user,
... ... @@ -72,7 +73,6 @@ func (t *StaffAssessItem) DataForAppend() (data ReqAppendData, err error) {
join staff_assess_content on staff_assess.id =staff_assess_content.staff_assess_id
where 1=1
and staff_assess.end_time between to_timestamp(?) and to_timestamp(?) `
nowTime := time.Now()
yesterday := nowTime.AddDate(0, 0, -1)
y, m, d := yesterday.Local().Date()
yesterdayZero := time.Date(y, m, d, 0, 0, 0, 0, time.Local)
... ... @@ -86,10 +86,21 @@ func (t *StaffAssessItem) DataForAppend() (data ReqAppendData, err error) {
data = ReqAppendData{
Name: dataList[0].AppTableName(),
Fields: dataList[0].AppTableField(),
Data: make([]map[string]string, len(dataList)),
Data: make([]map[string]string, 0, len(dataList)),
}
for i := range dataList {
data.Data = append(data.Data, dataList[i].MapData())
}
return
}
func SendDataStaffAssess(nowTime time.Time) error {
c := NewClient()
entry := StaffAssessItem{}
data, err := entry.DataForAppend(nowTime)
if err != nil {
return fmt.Errorf("获取每日评估的数据 %s", err)
}
err = c.AppendData(data)
return err
}
... ...