-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathDfpApi.scala
200 lines (159 loc) · 6.69 KB
/
DfpApi.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package dfp
// StatementBuilder query language is PQL defined here:
// https://developers.google.com/ad-manager/api/pqlreference
import com.google.api.ads.admanager.axis.utils.v202405.StatementBuilder
import com.google.api.ads.admanager.axis.v202405._
import com.madgag.scala.collection.decorators.MapDecorator
import common.GuLogging
import common.dfp._
import org.joda.time.DateTime
case class DfpLineItems(validItems: Seq[GuLineItem], invalidItems: Seq[GuLineItem])
class DfpApi(dataMapper: DataMapper, dataValidation: DataValidation) extends GuLogging {
import dfp.DfpApi._
private def readLineItems(
stmtBuilder: StatementBuilder,
postFilter: LineItem => Boolean = _ => true,
): DfpLineItems = {
val lineItems = withDfpSession(session => {
session
.lineItems(stmtBuilder)
.filter(postFilter)
.map(dfpLineItem => {
(dataMapper.toGuLineItem(session)(dfpLineItem), dfpLineItem)
})
})
// Note that this will call getTargeting on each
// item, potentially making one API call per lineitem.
val validatedLineItems = lineItems
.groupBy(Function.tupled(dataValidation.isGuLineItemValid))
.mapV(_.map(_._1))
DfpLineItems(
validItems = validatedLineItems.getOrElse(true, Nil),
invalidItems = validatedLineItems.getOrElse(false, Nil),
)
}
def getAllOrders: Seq[GuOrder] = {
val stmtBuilder = new StatementBuilder()
withDfpSession(_.orders(stmtBuilder).map(dataMapper.toGuOrder))
}
def getAllAdvertisers: Seq[GuAdvertiser] = {
val stmtBuilder = new StatementBuilder()
.where("type = :type")
.withBindVariableValue("type", CompanyType.ADVERTISER.toString)
.orderBy("id ASC")
withDfpSession(_.companies(stmtBuilder).map(dataMapper.toGuAdvertiser))
}
def readCurrentLineItems: DfpLineItems = {
val stmtBuilder = new StatementBuilder()
.where("status = :readyStatus OR status = :deliveringStatus")
.withBindVariableValue("readyStatus", ComputedStatus.READY.toString)
.withBindVariableValue("deliveringStatus", ComputedStatus.DELIVERING.toString)
.orderBy("id ASC")
readLineItems(stmtBuilder)
}
def readLineItemsModifiedSince(threshold: DateTime): DfpLineItems = {
val stmtBuilder = new StatementBuilder()
.where("lastModifiedDateTime > :threshold")
.withBindVariableValue("threshold", threshold.getMillis)
readLineItems(stmtBuilder)
}
def readSponsorshipLineItemIds(): Seq[Long] = {
// The advertiser ID for "Amazon Transparent Ad Marketplace"
val amazonAdvertiserId = 4751525411L
val stmtBuilder = new StatementBuilder()
.where(
"(status = :readyStatus OR status = :deliveringStatus) AND lineItemType = :sponsorshipType AND advertiserId != :amazonAdvertiserId",
)
.withBindVariableValue("readyStatus", ComputedStatus.READY.toString)
.withBindVariableValue("deliveringStatus", ComputedStatus.DELIVERING.toString)
.withBindVariableValue("sponsorshipType", LineItemType.SPONSORSHIP.toString)
.withBindVariableValue("amazonAdvertiserId", amazonAdvertiserId.toString)
.orderBy("id ASC")
// Lets avoid Prebid lineitems
val IsPrebid = "(?i).*?prebid.*".r
val lineItems = readLineItems(
stmtBuilder,
lineItem => {
lineItem.getName match {
case IsPrebid() => false
case _ => true
}
},
)
(lineItems.validItems.map(_.id) ++ lineItems.invalidItems.map(_.id)).sorted
}
def readActiveCreativeTemplates(): Seq[GuCreativeTemplate] = {
val stmtBuilder = new StatementBuilder()
.where("status = :active and type = :userDefined")
.withBindVariableValue("active", CreativeTemplateStatus._ACTIVE)
.withBindVariableValue("userDefined", CreativeTemplateType._USER_DEFINED)
withDfpSession {
_.creativeTemplates(stmtBuilder) map dataMapper.toGuCreativeTemplate filterNot (_.isForApps)
}
}
def readTemplateCreativesModifiedSince(threshold: DateTime): Seq[GuCreative] = {
val stmtBuilder = new StatementBuilder()
.where("lastModifiedDateTime > :threshold")
.withBindVariableValue("threshold", threshold.getMillis)
withDfpSession {
_.creatives.get(stmtBuilder) collect { case creative: TemplateCreative =>
creative
} map dataMapper.toGuTemplateCreative
}
}
private def readDescendantAdUnits(rootName: String, stmtBuilder: StatementBuilder): Seq[GuAdUnit] = {
withDfpSession { session =>
session.adUnits(stmtBuilder) filter { adUnit =>
def isRoot(path: Array[AdUnitParent]) = path.length == 1 && adUnit.getName == rootName
def isDescendant(path: Array[AdUnitParent]) = path.length > 1 && path(1).getName == rootName
Option(adUnit.getParentPath) exists { path => isRoot(path) || isDescendant(path) }
} map dataMapper.toGuAdUnit sortBy (_.id)
}
}
def readActiveAdUnits(rootName: String): Seq[GuAdUnit] = {
val stmtBuilder = new StatementBuilder()
.where("status = :status")
.withBindVariableValue("status", InventoryStatus._ACTIVE)
readDescendantAdUnits(rootName, stmtBuilder)
}
def readSpecialAdUnits(rootName: String): Seq[(String, String)] = {
val statementBuilder = new StatementBuilder()
.where("status = :status")
.where("explicitlyTargeted = :targeting")
.withBindVariableValue("status", InventoryStatus._ACTIVE)
.withBindVariableValue("targeting", true)
readDescendantAdUnits(rootName, statementBuilder) map { adUnit =>
(adUnit.id, adUnit.path.mkString("/"))
} sortBy (_._2)
}
def getCreativeIds(lineItemId: Long): Seq[Long] = {
val stmtBuilder = new StatementBuilder()
.where("status = :status AND lineItemId = :lineItemId")
.withBindVariableValue("status", LineItemCreativeAssociationStatus._ACTIVE)
.withBindVariableValue("lineItemId", lineItemId)
withDfpSession { session =>
session.lineItemCreativeAssociations.get(stmtBuilder) map (id => Long2long(id.getCreativeId))
}
}
def getPreviewUrl(lineItemId: Long, creativeId: Long, url: String): Option[String] =
for {
session <- SessionWrapper()
previewUrl <- session.lineItemCreativeAssociations.getPreviewUrl(lineItemId, creativeId, url)
} yield previewUrl
def getReportQuery(reportId: Long): Option[ReportQuery] =
for {
session <- SessionWrapper()
query <- session.getReportQuery(reportId)
} yield query
def runReportJob(report: ReportQuery): Seq[String] = {
withDfpSession { session =>
session.runReportJob(report)
}
}
}
object DfpApi {
def withDfpSession[T](block: SessionWrapper => Seq[T]): Seq[T] = {
val results = for (session <- SessionWrapper()) yield block(session)
results getOrElse Nil
}
}