402 lines
19 KiB
Go
402 lines
19 KiB
Go
|
package adjust
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"sandc/pkg/utils"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type CallbackInfo struct {
|
||
|
ActivityKind string `json:"activity_kind"`
|
||
|
AdgroupName string `json:"adgroup_name"`
|
||
|
Adid string `json:"adid"`
|
||
|
AndroidId string `json:"android_id"`
|
||
|
AndroidIdMd5 string `json:"android_id_md5"`
|
||
|
AppId string `json:"app_id"`
|
||
|
AppName string `json:"app_name"`
|
||
|
AppNameDashboard string `json:"app_name_dashboard"`
|
||
|
AppToken string `json:"app_token"`
|
||
|
AppVersion string `json:"app_version"`
|
||
|
AppVersionRaw string `json:"app_version_raw"`
|
||
|
AppVersionShort string `json:"app_version_short"`
|
||
|
AttStatus string `json:"att_status"`
|
||
|
AttributionExpiresAt int64 `json:"attribution_expires_at"`
|
||
|
CallbackTtl string `json:"callback_ttl"`
|
||
|
CampaignName string `json:"campaign_name"`
|
||
|
City string `json:"city"`
|
||
|
ClickTime string `json:"click_time"`
|
||
|
ClickTimeHour string `json:"click_time_hour"`
|
||
|
ConnectionType string `json:"connection_type"`
|
||
|
ConversionDuration string `json:"conversion_duration"`
|
||
|
Country string `json:"country"`
|
||
|
CountrySubdivision string `json:"country_subdivision"`
|
||
|
CpuType string `json:"cpu_type"`
|
||
|
CreatedAt int64 `json:"created_at"`
|
||
|
CreatedAtHour int64 `json:"created_at_hour"`
|
||
|
CreatedAtMilli int64 `json:"created_at_milli"`
|
||
|
CreativeName string `json:"creative_name"`
|
||
|
Currency string `json:"currency"`
|
||
|
DbmCampaignType string `json:"dbm_campaign_type"`
|
||
|
DbmCreativeId string `json:"dbm_creative_id"`
|
||
|
DbmExchangeId string `json:"dbm_exchange_id"`
|
||
|
DbmExternalCustomerId string `json:"dbm_external_customer_id"`
|
||
|
DbmInsertionOrderId string `json:"dbm_insertion_order_id"`
|
||
|
DbmLineItemId string `json:"dbm_line_item_id"`
|
||
|
DbmLineItemName string `json:"dbm_line_item_name"`
|
||
|
DcmCampaignType string `json:"dcm_campaign_type"`
|
||
|
DcmCreativeId string `json:"dcm_creative_id"`
|
||
|
DcmExternalCustomerId string `json:"dcm_external_customer_id"`
|
||
|
DcmPlacementId string `json:"dcm_placement_id"`
|
||
|
DcmPlacementName string `json:"dcm_placement_name"`
|
||
|
DcmSiteId string `json:"dcm_site_id"`
|
||
|
Deeplink string `json:"deeplink"`
|
||
|
DeviceAtlasId string `json:"device_atlas_id"`
|
||
|
DeviceManufacturer string `json:"device_manufacturer"`
|
||
|
DeviceModel string `json:"device_model"`
|
||
|
DeviceName string `json:"device_name"`
|
||
|
DeviceType string `json:"device_type"`
|
||
|
EngagementTime string `json:"engagement_time"`
|
||
|
EngagementTimeHour string `json:"engagement_time_hour"`
|
||
|
Environment string `json:"environment"`
|
||
|
Event string `json:"event"`
|
||
|
EventCostId string `json:"event_cost_id"`
|
||
|
EventName string `json:"event_name"`
|
||
|
ExternalDeviceIdMd5 string `json:"external_device_id_md5"`
|
||
|
FbDeeplinkAccountId string `json:"fb_deeplink_account_id"`
|
||
|
FbDeeplinkAdId string `json:"fb_deeplink_ad_id"`
|
||
|
FbDeeplinkAdgroupId string `json:"fb_deeplink_adgroup_id"`
|
||
|
FbDeeplinkCampaignGroupId string `json:"fb_deeplink_campaign_group_id"`
|
||
|
FbDeeplinkCampaignId string `json:"fb_deeplink_campaign_id"`
|
||
|
FbInstallReferrer string `json:"fb_install_referrer"`
|
||
|
FbInstallReferrerAccountId string `json:"fb_install_referrer_account_id"`
|
||
|
FbInstallReferrerAdId string `json:"fb_install_referrer_ad_id"`
|
||
|
FbInstallReferrerAdObjectiveName string `json:"fb_install_referrer_ad_objective_name"`
|
||
|
FbInstallReferrerAdgroupId string `json:"fb_install_referrer_adgroup_id"`
|
||
|
FbInstallReferrerAdgroupName string `json:"fb_install_referrer_adgroup_name"`
|
||
|
FbInstallReferrerCampaignGroupId string `json:"fb_install_referrer_campaign_group_id"`
|
||
|
FbInstallReferrerCampaignGroupName string `json:"fb_install_referrer_campaign_group_name"`
|
||
|
FbInstallReferrerCampaignId string `json:"fb_install_referrer_campaign_id"`
|
||
|
FbInstallReferrerCampaignName string `json:"fb_install_referrer_campaign_name"`
|
||
|
FbInstallReferrerPublisherPlatform string `json:"fb_install_referrer_publisher_platform"`
|
||
|
FireAdid string `json:"fire_adid"`
|
||
|
FirstTracker string `json:"first_tracker"`
|
||
|
FirstTrackerName string `json:"first_tracker_name"`
|
||
|
Gbraid string `json:"gbraid"`
|
||
|
Gclid string `json:"gclid"`
|
||
|
GmpProductType string `json:"gmp_product_type"`
|
||
|
GoogleAdsAdType string `json:"google_ads_ad_type"`
|
||
|
GoogleAdsAdgroupId string `json:"google_ads_adgroup_id"`
|
||
|
GoogleAdsAdgroupName string `json:"google_ads_adgroup_name"`
|
||
|
GoogleAdsCampaignId string `json:"google_ads_campaign_id"`
|
||
|
GoogleAdsCampaignName string `json:"google_ads_campaign_name"`
|
||
|
GoogleAdsCampaignType string `json:"google_ads_campaign_type"`
|
||
|
GoogleAdsCreativeId string `json:"google_ads_creative_id"`
|
||
|
GoogleAdsEngagementType string `json:"google_ads_engagement_type"`
|
||
|
GoogleAdsExternalCustomerId string `json:"google_ads_external_customer_id"`
|
||
|
GoogleAdsKeyword string `json:"google_ads_keyword"`
|
||
|
GoogleAdsMatchtype string `json:"google_ads_matchtype"`
|
||
|
GoogleAdsNetworkSubtype string `json:"google_ads_network_subtype"`
|
||
|
GoogleAdsNetworkType string `json:"google_ads_network_type"`
|
||
|
GoogleAdsPlacement string `json:"google_ads_placement"`
|
||
|
GoogleAdsVideoId string `json:"google_ads_video_id"`
|
||
|
GoogleAppSetId string `json:"google_app_set_id"`
|
||
|
GpsAdid string `json:"gps_adid"`
|
||
|
GpsAdidMd5 string `json:"gps_adid_md5"`
|
||
|
HardwareName string `json:"hardware_name"`
|
||
|
IadAdId string `json:"iad_ad_id"`
|
||
|
IadConversionType string `json:"iad_conversion_type"`
|
||
|
IadCountryOrRegion string `json:"iad_country_or_region"`
|
||
|
IadKeyword string `json:"iad_keyword"`
|
||
|
IadKeywordId string `json:"iad_keyword_id"`
|
||
|
IadOrgId string `json:"iad_org_id"`
|
||
|
Idfa string `json:"idfa"`
|
||
|
IdfaAndroidId string `json:"idfa_android_id"`
|
||
|
IdfaGpsAdid string `json:"idfa_gps_adid"`
|
||
|
IdfaGpsAdidFireAdid string `json:"idfa_gps_adid_fire_adid"`
|
||
|
IdfaMd5 string `json:"idfa_md5"`
|
||
|
IdfaMd5Hex string `json:"idfa_md5_hex"`
|
||
|
IdfaUpper string `json:"idfa_upper"`
|
||
|
Idfv string `json:"idfv"`
|
||
|
IdfvGoogleAppSetId string `json:"idfv_google_app_set_id"`
|
||
|
ImpressionBased string `json:"impression_based"`
|
||
|
ImpressionTime string `json:"impression_time"`
|
||
|
ImpressionTimeHour string `json:"impression_time_hour"`
|
||
|
InstalledAt int64 `json:"installed_at"`
|
||
|
InstalledAtHour int64 `json:"installed_at_hour"`
|
||
|
IpAddress string `json:"ip_address"`
|
||
|
IsImported string `json:"is_imported"`
|
||
|
IsOrganic string `json:"is_organic"`
|
||
|
IsReattributed string `json:"is_reattributed"`
|
||
|
IsS2s string `json:"is_s2s"`
|
||
|
Isp string `json:"isp"`
|
||
|
Label string `json:"label"`
|
||
|
Language string `json:"language"`
|
||
|
LastFallbackTime string `json:"last_fallback_time"`
|
||
|
LastFallbackTimeHour string `json:"last_fallback_time_hour"`
|
||
|
LastSessionTime string `json:"last_session_time"`
|
||
|
LastTracker string `json:"last_tracker"`
|
||
|
LastTrackerName string `json:"last_tracker_name"`
|
||
|
LifetimeSessionCount string `json:"lifetime_session_count"`
|
||
|
MacMd5 string `json:"mac_md5"`
|
||
|
MacSha1 string `json:"mac_sha1"`
|
||
|
MatchType string `json:"match_type"`
|
||
|
Mcc string `json:"mcc"`
|
||
|
Mnc string `json:"mnc"`
|
||
|
NetworkName string `json:"network_name"`
|
||
|
NetworkType string `json:"network_type"`
|
||
|
Nonce string `json:"nonce"`
|
||
|
Oaid string `json:"oaid"`
|
||
|
OaidMd5 string `json:"oaid_md5"`
|
||
|
OsName string `json:"os_name"`
|
||
|
OsVersion string `json:"os_version"`
|
||
|
PartnerParameters string `json:"partner_parameters"`
|
||
|
Platform string `json:"platform"`
|
||
|
PostalCode string `json:"postal_code"`
|
||
|
PredictedValue string `json:"predicted_value"`
|
||
|
PredictionLabel string `json:"prediction_label"`
|
||
|
PredictionPeriodDays string `json:"prediction_period_days"`
|
||
|
ProxyIpAddress string `json:"proxy_ip_address"`
|
||
|
PublisherParameters string `json:"publisher_parameters"`
|
||
|
PushToken string `json:"push_token"`
|
||
|
Random string `json:"random"`
|
||
|
RandomUserId string `json:"random_user_id"`
|
||
|
ReattributedAt int64 `json:"reattributed_at"`
|
||
|
ReattributedAtHour int64 `json:"reattributed_at_hour"`
|
||
|
ReceivedAt int64 `json:"received_at"`
|
||
|
Referrer string `json:"referrer"`
|
||
|
Reftag string `json:"reftag"`
|
||
|
Region string `json:"region"`
|
||
|
ReinstalledAt int64 `json:"reinstalled_at"`
|
||
|
ReportingCurrency string `json:"reporting_currency"`
|
||
|
ReportingRevenue string `json:"reporting_revenue"`
|
||
|
Revenue string `json:"revenue"`
|
||
|
RevenueCny string `json:"revenue_cny"`
|
||
|
RevenueCnyCents string `json:"revenue_cny_cents"`
|
||
|
RevenueFloat string `json:"revenue_float"`
|
||
|
RevenueUsd string `json:"revenue_usd"`
|
||
|
RevenueUsdCents string `json:"revenue_usd_cents"`
|
||
|
Rida string `json:"rida"`
|
||
|
RokuContentId string `json:"roku_content_id"`
|
||
|
RokuPlacementType string `json:"roku_placement_type"`
|
||
|
SanEngagementTimes string `json:"san_engagement_times"`
|
||
|
SdkVersion string `json:"sdk_version"`
|
||
|
SessionCount string `json:"session_count"`
|
||
|
SimSlotIds string `json:"sim_slot_ids"`
|
||
|
SkCoarseConversionValue string `json:"sk_coarse_conversion_value"`
|
||
|
SkConversionValue string `json:"sk_conversion_value"`
|
||
|
SkConversionValueDevice string `json:"sk_conversion_value_device"`
|
||
|
SkConversionValueDeviceTime string `json:"sk_conversion_value_device_time"`
|
||
|
SkConversionValueStatus string `json:"sk_conversion_value_status"`
|
||
|
SkConversionValueTime string `json:"sk_conversion_value_time"`
|
||
|
SkCvWindowExpiration string `json:"sk_cv_window_expiration"`
|
||
|
SkRegisteredAt int64 `json:"sk_registered_at"`
|
||
|
SkTimerExpiration string `json:"sk_timer_expiration"`
|
||
|
SnapchatAdId string `json:"snapchat_ad_id"`
|
||
|
Store string `json:"store"`
|
||
|
ThirdPartySharingDisabled string `json:"third_party_sharing_disabled"`
|
||
|
Tifa string `json:"tifa"`
|
||
|
TimeSpent string `json:"time_spent"`
|
||
|
Timezone string `json:"timezone"`
|
||
|
Tracker string `json:"tracker"`
|
||
|
TrackerName string `json:"tracker_name"`
|
||
|
TrackingEnabled string `json:"tracking_enabled"`
|
||
|
TrackingLimited string `json:"tracking_limited"`
|
||
|
TweetId string `json:"tweet_id"`
|
||
|
TwitterLineItemId string `json:"twitter_line_item_id"`
|
||
|
UninstalledAt int64 `json:"uninstalled_at"`
|
||
|
UserAgent string `json:"user_agent"`
|
||
|
Vida string `json:"vida"`
|
||
|
VizioContentId string `json:"vizio_content_id"`
|
||
|
VizioPlacementType string `json:"vizio_placement_type"`
|
||
|
Wbraid string `json:"wbraid"`
|
||
|
WebUuid string `json:"web_uuid"`
|
||
|
WinAdid string `json:"win_adid"`
|
||
|
WinHwid string `json:"win_hwid"`
|
||
|
WinNaid string `json:"win_naid"`
|
||
|
WinUdid string `json:"win_udid"`
|
||
|
WithinCallbackTtl string `json:"within_callback_ttl"`
|
||
|
YahooCreativeId string `json:"yahoo_creative_id"`
|
||
|
YahooCreativeName string `json:"yahoo_creative_name"`
|
||
|
YahooSiteId string `json:"yahoo_site_id"`
|
||
|
AdImpressionsCount int64 `json:"ad_impressions_count,omitempty"` // 时间
|
||
|
AdMediationPlatform string `json:"ad_mediation_platform,omitempty"`
|
||
|
AdRevenueNetwork string `json:"ad_revenue_network,omitempty"`
|
||
|
AdRevenuePayload string `json:"ad_revenue_payload,omitempty"`
|
||
|
AdRevenuePlacement string `json:"ad_revenue_placement,omitempty"`
|
||
|
AdRevenueUnit string `json:"ad_revenue_unit,omitempty"`
|
||
|
}
|
||
|
|
||
|
type AdjustExportFilter struct {
|
||
|
Filename string
|
||
|
Tokens []string
|
||
|
Event string
|
||
|
Fields []string
|
||
|
CreatedAtFrom time.Time
|
||
|
CreatedAtTo time.Time
|
||
|
Location *time.Location //当前时期
|
||
|
Countries []string
|
||
|
Channels []string
|
||
|
InstallTimeFrom *time.Time
|
||
|
InstallTimeTo *time.Time
|
||
|
InstallHour *int
|
||
|
SumFields []string
|
||
|
Conditons map[string][]string
|
||
|
ConditionsNeq map[string][]string
|
||
|
CsvHeaders []string //csv头部
|
||
|
Offset int
|
||
|
}
|
||
|
|
||
|
type AdjustCondition struct {
|
||
|
Key int64 `json:"key"`
|
||
|
Value string `json:"value"`
|
||
|
}
|
||
|
|
||
|
type CallBackData struct {
|
||
|
Token string
|
||
|
Content []string
|
||
|
MapKeyData map[string]interface{}
|
||
|
}
|
||
|
|
||
|
type AdjustEventType string
|
||
|
|
||
|
const (
|
||
|
AdjustEventTypeInstall AdjustEventType = "install"
|
||
|
AdjustEventTypeGlobal AdjustEventType = "global"
|
||
|
AdjustEventTypeAdRevenue AdjustEventType = "ad_revenue"
|
||
|
)
|
||
|
|
||
|
func (d AdjustEventType) String() string {
|
||
|
return string(d)
|
||
|
}
|
||
|
|
||
|
func CheckAdjustQueueCondition(filter *AdjustExportFilter, info *CallbackInfo) (*CallBackData, bool) {
|
||
|
//判断token
|
||
|
if !InSlice(info.AppToken, filter.Tokens) {
|
||
|
return nil, false
|
||
|
}
|
||
|
|
||
|
//判断事件
|
||
|
if filter.Event == AdjustEventTypeGlobal.String() {
|
||
|
if info.ActivityKind == AdjustEventTypeAdRevenue.String() {
|
||
|
return nil, false
|
||
|
}
|
||
|
} else {
|
||
|
if info.ActivityKind != filter.Event {
|
||
|
return nil, false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//判断创建时间 @todo
|
||
|
fromTime, _ := time.ParseInLocation(time.DateTime, fmt.Sprintf("%s 00:00:00", filter.CreatedAtFrom.In(time.Local).Format(time.DateOnly)), filter.Location)
|
||
|
toTime, _ := time.ParseInLocation(time.DateTime, fmt.Sprintf("%s 23:59:59", filter.CreatedAtTo.In(time.Local).Format(time.DateOnly)), filter.Location)
|
||
|
if info.CreatedAt < fromTime.Unix() || info.CreatedAt > toTime.Unix() {
|
||
|
return nil, false
|
||
|
}
|
||
|
|
||
|
//判断国家
|
||
|
if len(filter.Countries) > 0 {
|
||
|
if !InSlice(info.Country, filter.Countries) {
|
||
|
return nil, false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//判断渠道
|
||
|
if len(filter.Channels) > 0 {
|
||
|
if !InSlice(info.NetworkName, filter.Channels) {
|
||
|
return nil, false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//判断安装时间
|
||
|
if filter.InstallTimeFrom != nil && filter.InstallTimeTo != nil {
|
||
|
installFormTime, _ := time.ParseInLocation(time.DateTime, fmt.Sprintf("%s 00:00:00", filter.InstallTimeFrom.In(time.Local).Format(time.DateOnly)), filter.Location)
|
||
|
installToTime, _ := time.ParseInLocation(time.DateTime, fmt.Sprintf("%s 23:59:59", filter.InstallTimeTo.In(time.Local).Format(time.DateOnly)), filter.Location)
|
||
|
if info.InstalledAt < installFormTime.Unix() || info.InstalledAt > installToTime.Unix() {
|
||
|
return nil, false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//判断小时
|
||
|
if filter.InstallHour != nil {
|
||
|
installHour := *filter.InstallHour
|
||
|
if info.CreatedAt-info.InstalledAt > 3600*int64(installHour) {
|
||
|
return nil, false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mapKeyData := utils.StructToMapJson(*info)
|
||
|
//判断自定义条件等于,仅支持string
|
||
|
if len(filter.Conditons) > 0 {
|
||
|
for k, filterValue := range filter.Conditons {
|
||
|
//如果自定义字段存在,判断值是否符合
|
||
|
if mapVal, ok := mapKeyData[k].(string); ok {
|
||
|
if !InSlice(mapVal, filterValue) {
|
||
|
return nil, false
|
||
|
}
|
||
|
} else {
|
||
|
return nil, false
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//判断自定义条件不等于,仅支持string
|
||
|
if len(filter.ConditionsNeq) > 0 {
|
||
|
for k, filterValue := range filter.ConditionsNeq {
|
||
|
//如果自定义字段存在,判断值是否符合
|
||
|
if mapVal, ok := mapKeyData[k].(string); ok {
|
||
|
if utils.InSlice("NULL", filterValue) {
|
||
|
if mapVal == "" {
|
||
|
return nil, false
|
||
|
}
|
||
|
} else {
|
||
|
// 存在值则跳过数据
|
||
|
if InSlice(mapVal, filterValue) {
|
||
|
return nil, false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
return nil, false
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//获取筛选字段的值
|
||
|
content := make([]string, 0)
|
||
|
for _, field := range filter.Fields {
|
||
|
if mapVal, ok := mapKeyData[field]; ok {
|
||
|
content = append(content, fmt.Sprintf("%v", mapVal))
|
||
|
}
|
||
|
|
||
|
//单独处理date
|
||
|
if field == "date" {
|
||
|
rowDate := time.Unix(info.CreatedAt, 0)
|
||
|
rowDateString := rowDate.In(filter.Location).Format(time.DateOnly)
|
||
|
content = append(content, rowDateString)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//根据自定义字段返回数据
|
||
|
data := &CallBackData{
|
||
|
Token: info.AppToken,
|
||
|
Content: content,
|
||
|
MapKeyData: mapKeyData,
|
||
|
}
|
||
|
|
||
|
return data, true
|
||
|
}
|
||
|
|
||
|
func InSlice(str string, slice []string) bool {
|
||
|
for _, v := range slice {
|
||
|
if formatString(v) == formatString(str) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func formatString(str string) string {
|
||
|
return strings.TrimSpace(strings.ToLower(str))
|
||
|
}
|