import os import sys import json import time import datetime from os p

 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
import os
import sys
import json
import time
import datetime
from os.path import dirname, abspath, join
current_dir = dirname(abspath(__file__))
sys.path.append(dirname(dirname(current_dir)))
from database import redis
from database import psql
placements = []
def calculate_daily_ecpm(position=0):
cursor, result = redis.scan(position, "placement:*", 10000)
for line in result:
placements.append(line)
if cursor != 0:
return calculate_daily_ecpm(cursor)
else:
conn = psql.getconn()
today = datetime.date.today()-datetime.timedelta(days=1)
for key in placements:
try:
if "." not in key:
redis.delete(key)
continue
_, ssp, country, banner_size, device_platform, banner_type, adspace_type, package = key.split(":")
placement = redis.hgetall(key)
daily_revenue = float(placement.get("daily_revenue", 0))
daily_impressions = int(placement.get("daily_impressions", 0))
daily_clicks = int(placement.get("daily_clicks", 0))
daily_expenses = float(placement.get("daily_expenses", 0))
daily_bids = int(placement.get("daily_bids", 0))
daily_volume = int(placement.get("daily_volume", 0))
daily_bidprices = float(placement.get("daily_bidprices", 0))
mode = placement.get("mode", "")
if mode != "working":
mode = "learning"
if not daily_volume:
continue
avg_bid = 0
if daily_bidprices:
avg_bid = daily_bids/daily_bidprices
with conn:
with conn.cursor() as cur:
cur.execute("""
INSERT INTO daily (key, date, package, ssp, country, banner_size,
banner_type, adspace_type, device_platform, impressions,
bids, expenses, revenue, clicks, volume, avg_bid, mode)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """,
[key, today, package, ssp, country, banner_size,
banner_type, adspace_type, device_platform,
daily_impressions, daily_bids, daily_expenses,
daily_revenue, daily_clicks, daily_volume, avg_bid, mode])
redis.hmset(key, {
"daily_revenue": 0,
"daily_impressions": 0,
"daily_clicks": 0,
"daily_expenses": 0,
"daily_bids": 0,
"daily_volume": 0,
"daily_bidprices": 0,
})
except Exception as e:
print(key)
print(e)
def main(**opts):
calculate_daily_ecpm()