}
}
@SuppressWarnings("unchecked")
public void updateCpms(Jedis conn, String adId) {
Transaction trans = conn.multi();
trans.hget("type:", adId);
trans.zscore("ad:base_value:", adId);
trans.smembers("terms:" + adId);
List<Object> response = trans.exec();
String type = (String)response.get(0);
Double baseValue = (Double)response.get(1);
Set<String> words = (Set<String>)response.get(2);
String which = "clicks";
Ecpm ecpm = Enum.valueOf(Ecpm.class, type.toUpperCase());
if (Ecpm.CPA.equals(ecpm)) {
which = "actions";
}
trans = conn.multi();
trans.get("type:" + type + ":views:");
trans.get("type:" + type + ':' + which);
response = trans.exec();
String typeViews = (String)response.get(0);
String typeClicks = (String)response.get(1);
AVERAGE_PER_1K.put(ecpm,
1000. *
Integer.valueOf(typeClicks != null ? typeClicks : "1") /
Integer.valueOf(typeViews != null ? typeViews : "1"));
if (Ecpm.CPM.equals(ecpm)) {
return;
}
String viewKey = "views:" + adId;
String clickKey = which + ':' + adId;
trans = conn.multi();
trans.zscore(viewKey, "");
trans.zscore(clickKey, "");
response = trans.exec();
Double adViews = (Double)response.get(0);
Double adClicks = (Double)response.get(1);
double adEcpm = 0;
if (adClicks == null || adClicks < 1){
Double score = conn.zscore("idx:ad:value:", adId);
adEcpm = score != null ? score.doubleValue() : 0;
}else{
adEcpm = toEcpm(
ecpm,
adViews != null ? adViews.doubleValue() : 1,
adClicks != null ? adClicks.doubleValue() : 0,
baseValue);
conn.zadd("idx:ad:value:", adEcpm, adId);
}
for (String word : words) {
trans = conn.multi();
trans.zscore(viewKey, word);
trans.zscore(clickKey, word);
response = trans.exec();
Double views = (Double)response.get(0);
Double clicks = (Double)response.get(1);
if (clicks == null || clicks < 1){
continue;