bi/platform-basic-libs/service/report/report_interface.go

125 lines
3.0 KiB
Go
Raw Normal View History

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"
2022-03-08 17:56:07 +08:00
jsoniter "github.com/json-iterator/go"
2022-01-26 16:40:50 +08:00
"time"
)
type ReportInterface interface {
2022-03-08 17:56:07 +08:00
NewReportType(appid, tableId, debug, timeNow, eventName, ip string, body []byte)
2022-01-26 16:40:50 +08:00
GetkafkaData() model.KafkaData
2022-03-08 17:56:07 +08:00
InflowOfKakfa() (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 17:56:07 +08:00
func (this *UserReport) NewReportType(appid, tableId, debug, timeNow, eventName, ip string, body []byte) {
this.kafkaData.APPID = appid
this.kafkaData.TableId = tableId
this.kafkaData.Debug = debug
this.kafkaData.ReqData = body
this.kafkaData.Ip = ip
this.kafkaData.ReportTime = 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 17:56:07 +08:00
func (this *UserReport) InflowOfKakfa() (err error) {
2022-01-26 16:40:50 +08:00
2022-03-08 17:56:07 +08:00
var json = jsoniter.ConfigCompatibleWithStandardLibrary
2022-01-26 16:40:50 +08:00
msg := &sarama.ProducerMessage{}
msg.Topic = model.GlobConfig.Comm.Kafka.ReportTopicName
2022-03-08 17:56:07 +08:00
sendData, _ := json.Marshal(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 17:56:07 +08:00
func (this *EventReport) NewReportType(appid, tableId, debug, timeNow, eventName, ip string, body []byte) {
this.kafkaData.APPID = appid
this.kafkaData.TableId = tableId
this.kafkaData.Debug = debug
this.kafkaData.ReqData = body
this.kafkaData.ReportTime = timeNow
2022-01-26 16:40:50 +08:00
this.kafkaData.ReportType = model.EventReportType
2022-03-08 17:56:07 +08:00
this.kafkaData.EventName = eventName
this.kafkaData.Ip = ip
2022-01-26 16:40:50 +08:00
}
2022-03-08 17:56:07 +08:00
func (this *EventReport) InflowOfKakfa() (err error) {
var json = jsoniter.ConfigCompatibleWithStandardLibrary
2022-01-26 16:40:50 +08:00
msg := &sarama.ProducerMessage{}
msg.Topic = model.GlobConfig.Comm.Kafka.ReportTopicName
2022-03-08 17:56:07 +08:00
sendData, _ := json.Marshal(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
}