-
Notifications
You must be signed in to change notification settings - Fork 560
/
Copy pathDfpDataCacheJob.scala
179 lines (139 loc) · 6.22 KB
/
DfpDataCacheJob.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
package dfp
import common.dfp._
import common.GuLogging
import org.joda.time.DateTime
import play.api.libs.json.Json.{toJson, _}
import tools.Store
import scala.concurrent.{ExecutionContext, Future}
class DfpDataCacheJob(
adUnitAgent: AdUnitAgent,
customFieldAgent: CustomFieldAgent,
customTargetingAgent: CustomTargetingAgent,
placementAgent: PlacementAgent,
dfpApi: DfpApi,
) extends GuLogging {
case class LineItemLoadSummary(validLineItems: Seq[GuLineItem], invalidLineItems: Seq[GuLineItem])
def run()(implicit executionContext: ExecutionContext): Future[Unit] =
Future {
log.info("Refreshing data cache")
val start = System.currentTimeMillis
val data = loadLineItems()
val sponsorshipLineItemIds = dfpApi.readSponsorshipLineItemIds()
val currentLineItems = loadCurrentLineItems()
val duration = System.currentTimeMillis - start
log.info(s"Loading DFP data took $duration ms")
write(data)
Store.putNonRefreshableLineItemIds(sponsorshipLineItemIds)
writeLiveBlogTopSponsorships(currentLineItems)
writeSurveySponsorships(currentLineItems)
}
/*
for initialization and total refresh of data,
so would be used for first read and for emergency data update.
*/
def refreshAllDfpData()(implicit executionContext: ExecutionContext): Unit = {
for {
_ <- adUnitAgent.refresh()
_ <- customFieldAgent.refresh()
_ <- customTargetingAgent.refresh()
_ <- placementAgent.refresh()
} {
loadLineItems()
}
}
private def loadCurrentLineItems(): DfpDataExtractor = {
val currentLineItems = dfpApi.readCurrentLineItems
val loadSummary = LineItemLoadSummary(
validLineItems = currentLineItems.validItems,
invalidLineItems = currentLineItems.invalidItems,
)
DfpDataExtractor(loadSummary.validLineItems, loadSummary.invalidLineItems)
}
private def loadLineItems(): DfpDataExtractor = {
def fetchCachedLineItems(): DfpLineItems = {
val lineItemReport = Store.getDfpLineItemsReport()
DfpLineItems(validItems = lineItemReport.lineItems, invalidItems = lineItemReport.invalidLineItems)
}
val start = System.currentTimeMillis
val loadSummary = loadLineItems(
fetchCachedLineItems(),
dfpApi.readLineItemsModifiedSince,
dfpApi.readCurrentLineItems,
)
val loadDuration = System.currentTimeMillis - start
log.info(s"Loading line items took $loadDuration ms")
DfpDataExtractor(loadSummary.validLineItems, loadSummary.invalidLineItems)
}
def report(ids: Iterable[Long]): String = if (ids.isEmpty) "None" else ids.mkString(", ")
def loadLineItems(
cachedLineItems: => DfpLineItems,
lineItemsModifiedSince: DateTime => DfpLineItems,
allActiveLineItems: => DfpLineItems,
): LineItemLoadSummary = {
// If the cache is empty, run a full query to generate a complete LineItemLoadSummary, using allActiveLineItems.
if (cachedLineItems.validItems.isEmpty) {
// Create a full summary object from scratch, using a query that collects all line items from dfp.
LineItemLoadSummary(
validLineItems = allActiveLineItems.validItems,
invalidLineItems = allActiveLineItems.invalidItems,
)
} else {
// Calculate the most recent modified timestamp of the existing cache items,
// and find line items modified since that timestamp.
val threshold = cachedLineItems.validItems.map(_.lastModified).maxBy(_.getMillis)
val recentlyModified = lineItemsModifiedSince(threshold)
// Update existing items with a patch of new items.
def updateCachedContent(existingItems: Seq[GuLineItem], newItems: Seq[GuLineItem]): Seq[GuLineItem] = {
// Create a combined map of all the line items, preferring newer items over old ones (equality is based on id).
val updatedLineItemMap = GuLineItem.asMap(existingItems) ++ GuLineItem.asMap(newItems)
// These are the existing, cached keys.
val existingKeys = existingItems.map(_.id).toSet
val (active, inactive) = newItems partition (Seq("READY", "DELIVERING", "DELIVERY_EXTENDED") contains _.status)
val activeKeys = active.map(_.id).toSet
val inactiveKeys = inactive.map(_.id).toSet
val added = activeKeys -- existingKeys
val modified = activeKeys intersect existingKeys
val removed = inactiveKeys intersect existingKeys
// New cache contents.
val updatedKeys = existingKeys ++ added -- removed
log.info(s"Cached line item count was ${cachedLineItems.validItems.size}")
log.info(s"Last modified time of cached line items: $threshold")
log.info(s"Added: ${report(added)}")
log.info(s"Modified: ${report(modified)}")
log.info(s"Removed: ${report(inactiveKeys)}")
log.info(s"Cached line item count now ${updatedKeys.size}")
updatedKeys.toSeq.sorted.map(updatedLineItemMap)
}
LineItemLoadSummary(
validLineItems = updateCachedContent(cachedLineItems.validItems, recentlyModified.validItems),
invalidLineItems = updateCachedContent(cachedLineItems.invalidItems, recentlyModified.invalidItems),
)
}
}
private def write(data: DfpDataExtractor): Unit = {
if (data.hasValidLineItems) {
val now = printLondonTime(DateTime.now())
val pageSkinSponsorships = data.pageSkinSponsorships
Store.putDfpPageSkinAdUnits(stringify(toJson(PageSkinSponsorshipReport(now, pageSkinSponsorships))))
Store.putDfpLineItemsReport(stringify(toJson(LineItemReport(now, data.lineItems, data.invalidLineItems))))
}
}
private def writeLiveBlogTopSponsorships(data: DfpDataExtractor): Unit = {
if (data.hasValidLineItems) {
val now = printLondonTime(DateTime.now())
val sponsorships = data.liveBlogTopSponsorships
Store.putLiveBlogTopSponsorships(
stringify(toJson(LiveBlogTopSponsorshipReport(Some(now), sponsorships))),
)
}
}
private def writeSurveySponsorships(data: DfpDataExtractor): Unit = {
if (data.hasValidLineItems) {
val now = printLondonTime(DateTime.now())
val sponsorships = data.surveySponsorships
Store.putSurveySponsorships(
stringify(toJson(SurveySponsorshipReport(Some(now), sponsorships))),
)
}
}
}