2022-01-26 16:40:50 +08:00
|
|
|
package report
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/1340691923/xwl_bi/model"
|
|
|
|
"github.com/1340691923/xwl_bi/platform-basic-libs/my_error"
|
|
|
|
model2 "github.com/1340691923/xwl_bi/platform-basic-libs/sinker/model"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Shopify/sarama"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ReportInterface interface {
|
2022-03-08 15:47:38 +08:00
|
|
|
NewReportType(data *ReportTypeData)
|
2022-01-26 16:40:50 +08:00
|
|
|
GetkafkaData() model.KafkaData
|
2022-03-08 15:47:38 +08:00
|
|
|
InflowOfKakfa(marshaler func(v interface{}) ([]byte, error)) (err error)
|
2022-01-26 16:40:50 +08:00
|
|
|
Put()
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserReport struct {
|
|
|
|
kafkaData model.KafkaData
|
|
|
|
}
|
|
|
|
|
|
|
|
var userPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &UserReport{}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var eventPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &EventReport{}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-03-08 15:47:38 +08:00
|
|
|
type ReportTypeData struct {
|
|
|
|
Appid string
|
|
|
|
TableId string
|
|
|
|
Debug string
|
|
|
|
TimeNow string
|
|
|
|
EventName string
|
|
|
|
Ip string
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *UserReport) NewReportType(data *ReportTypeData) {
|
|
|
|
this.kafkaData.APPID = data.Appid
|
|
|
|
this.kafkaData.TableId = data.TableId
|
|
|
|
this.kafkaData.Debug = data.Debug
|
|
|
|
this.kafkaData.ReqData = data.Body
|
|
|
|
this.kafkaData.Ip = data.Ip
|
|
|
|
this.kafkaData.ReportTime = data.TimeNow
|
2022-01-26 16:40:50 +08:00
|
|
|
this.kafkaData.ReportType = model.UserReportType
|
|
|
|
this.kafkaData.EventName = "用户属性"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *UserReport) GetkafkaData() model.KafkaData {
|
|
|
|
return this.kafkaData
|
|
|
|
}
|
|
|
|
|
2022-03-08 15:47:38 +08:00
|
|
|
func (this *UserReport) InflowOfKakfa(marshaler func(v interface{}) ([]byte, error)) (err error) {
|
2022-01-26 16:40:50 +08:00
|
|
|
|
|
|
|
msg := &sarama.ProducerMessage{}
|
|
|
|
msg.Topic = model.GlobConfig.Comm.Kafka.ReportTopicName
|
2022-03-08 15:47:38 +08:00
|
|
|
sendData, _ := marshaler(this.kafkaData)
|
2022-01-26 16:40:50 +08:00
|
|
|
msg.Value = sarama.ByteEncoder(sendData)
|
|
|
|
msg.Timestamp = time.Now()
|
|
|
|
|
|
|
|
return sendMsg(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *UserReport) Put() {
|
|
|
|
userPool.Put(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
type EventReport struct {
|
|
|
|
kafkaData model.KafkaData
|
|
|
|
}
|
|
|
|
|
2022-03-08 15:47:38 +08:00
|
|
|
func (this *EventReport) NewReportType(data *ReportTypeData) {
|
|
|
|
this.kafkaData.APPID = data.Appid
|
|
|
|
this.kafkaData.TableId = data.TableId
|
|
|
|
this.kafkaData.Debug = data.Debug
|
|
|
|
this.kafkaData.ReqData = data.Body
|
|
|
|
this.kafkaData.ReportTime = data.TimeNow
|
2022-01-26 16:40:50 +08:00
|
|
|
this.kafkaData.ReportType = model.EventReportType
|
2022-03-08 15:47:38 +08:00
|
|
|
this.kafkaData.EventName = data.EventName
|
|
|
|
this.kafkaData.Ip = data.Ip
|
2022-01-26 16:40:50 +08:00
|
|
|
}
|
|
|
|
|
2022-03-08 15:47:38 +08:00
|
|
|
func (this *EventReport) InflowOfKakfa(marshaler func(v interface{}) ([]byte, error)) (err error) {
|
|
|
|
|
2022-01-26 16:40:50 +08:00
|
|
|
msg := &sarama.ProducerMessage{}
|
|
|
|
msg.Topic = model.GlobConfig.Comm.Kafka.ReportTopicName
|
2022-03-08 15:47:38 +08:00
|
|
|
sendData, _ := marshaler(this.kafkaData)
|
2022-01-26 16:40:50 +08:00
|
|
|
|
|
|
|
msg.Value = sarama.ByteEncoder(sendData)
|
|
|
|
msg.Timestamp = time.Now()
|
|
|
|
|
|
|
|
return sendMsg(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *EventReport) GetkafkaData() model.KafkaData {
|
|
|
|
return this.kafkaData
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *EventReport) Put() {
|
|
|
|
eventPool.Put(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEventReport() ReportInterface {
|
|
|
|
return eventPool.Get().(*EventReport)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserReport() ReportInterface {
|
|
|
|
return userPool.Get().(*UserReport)
|
|
|
|
}
|
|
|
|
|
|
|
|
var duckMap = map[string]func() ReportInterface{
|
|
|
|
model2.ReportUserProperties: NewUserReport,
|
|
|
|
model2.ReportEventProperties: NewEventReport,
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetReportDuck(typ string) (reportInterface ReportInterface, err error) {
|
|
|
|
var ok bool
|
|
|
|
var fn func() ReportInterface
|
|
|
|
if fn, ok = duckMap[typ]; !ok {
|
|
|
|
err = my_error.NewBusiness(ERROR_TABLE, ReportTypeErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reportInterface = fn()
|
|
|
|
return
|
|
|
|
}
|