usr bin env python This module provides routines to calculate rating o

  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
#!/usr/bin/env python
"""
This module provides routines to calculate rating of users in tripster.ru project.
Main publick functions:
calc_all_cities_rating()
calc_cities_per_continent_rating()
calc_cities_per_country_rating()
calc_all_countries_rating()
calc_countries_per_continent_rating()
"""
from settings import settings
from lib.models import engine
import types_
cursor = engine.connect().connection.cursor()
def userid_sql(userid):
"""
Build sql expression for where clause.
"""
if userid is None:
where = ''
else:
where = ' AND user = %d' % userid
return where
def clear_rating(type, userid=None):
"""
Delete all records with given type from ratings table.
"""
cursor = engine.connect().connection.cursor()
where = userid_sql(userid)
cursor.execute("""DELETE FROM ratings
WHERE type = %%s %s""" % where, (type))
def insert_rating(user_id, position, count, type, object=None):
"""
Create record in ratings table.
"""
cursor.execute("""INSERT INTO ratings
SET user = %s, position = %s,
count = %s, type= %s, object=%s""",
(user_id, position, count, type, object))
def calc_all_cities_rating(userid=None):
"""
Calculate rating of visited cities for each user.
"""
cursor.execute("""SELECT u.id, COUNT(*) as count
FROM users u
LEFT JOIN cities_in_users ciu ON ciu.user = u.id
WHERE u.fraud_user = 0
GROUP BY u.id
ORDER BY count DESC""")
clear_rating(types_.RATING_ALL_CITIES, userid)
for position, row in enumerate(cursor.fetchall()):
iter_userid, count = row
if userid and userid != iter_userid:
continue
insert_rating(iter_userid, position + 1, count, types_.RATING_ALL_CITIES)
def calc_cities_per_continent_rating(userid=None):
"""
Calculate rating of visited cities on each continent for each user
"""
cursor.execute("""SELECT u.id, countries.continent, COUNT(*) AS count
FROM users u
LEFT JOIN cities_in_users ciu ON ciu.user = u.id
LEFT JOIN cities ON cities.id = ciu.city
LEFT JOIN countries ON countries.id = cities.country
WHERE countries.continent != 'RU'
AND u.fraud_user = 0
GROUP BY u.id, countries.continent
ORDER BY count DESC""")
clear_rating(types_.RATING_CITIES_PER_CONTINENT)
for position, row in enumerate(cursor.fetchall()):
iter_userid, continent, count = row
if userid and userid != iter_userid:
continue
insert_rating(iter_userid, position + 1, count,
types_.RATING_CITIES_PER_CONTINENT, continent)
def calc_cities_per_country_rating(userid=None):
"""
Calculate rating of visited cities in each country for each user
"""
cursor.execute("""SELECT u.id, countries.id, COUNT(*) AS count
FROM users u
LEFT JOIN cities_in_users ciu ON ciu.user = u.id
LEFT JOIN cities ON cities.id = ciu.city
LEFT JOIN countries ON countries.id = cities.country
WHERE u.fraud_user = 0
GROUP BY u.id, countries.id
ORDER BY count DESC""")
clear_rating(types_.RATING_CITIES_PER_COUNTRY)
for position, row in enumerate(cursor.fetchall()):
iter_userid, country, count = row
if userid and userid != iter_userid:
continue
insert_rating(iter_userid, position + 1, count,
types_.RATING_CITIES_PER_COUNTRY, country)
def calc_all_countries_rating(userid=None):
"""
Calculate rating of visited countries for each user.
"""
cursor.execute("""SELECT u.id, COUNT(DISTINCT countries.id) as count
FROM users u
LEFT JOIN cities_in_users ciu ON ciu.user = u.id
LEFT JOIN cities ON cities.id = ciu.city
LEFT JOIN countries ON countries.id = cities.country
WHERE u.fraud_user = 0
GROUP BY u.id
ORDER BY count DESC""")
clear_rating(types_.RATING_ALL_COUNTRIES)
for position, row in enumerate(cursor.fetchall()):
iter_userid, count = row
if userid and userid != iter_userid:
continue
insert_rating(iter_userid, position + 1, count, types_.RATING_ALL_COUNTRIES)
def calc_countries_per_continent_rating(userid=None):
"""
Calculate rating of visited countries on each continent for each user
"""
cursor.execute("""SELECT u.id, countries.continent, COUNT(DISTINCT countries.id) AS count
FROM users u
LEFT JOIN cities_in_users ciu ON ciu.user = u.id
LEFT JOIN cities ON cities.id = ciu.city
LEFT JOIN countries ON countries.id = cities.country
WHERE u.fraud_user = 0
GROUP BY u.id, countries.continent
ORDER BY count DESC""")
clear_rating(types_.RATING_COUNTRIES_PER_CONTINENT)
for position, row in enumerate(cursor.fetchall()):
iter_userid, continent, count = row
if userid and userid != iter_userid:
continue
insert_rating(iter_userid, position + 1, count,
types_.RATING_COUNTRIES_PER_CONTINENT, continent)
def update(login=None):
"""
Rebuild all ratings.
"""
if login is None:
userid = None
else:
cursor.execute("SELECT id FROM users WHERE login = %s", [login])
userid = cursor.fetchone()[0]
print 'Cities rating'
calc_all_cities_rating(userid)
print 'Cities per continent rating'
calc_cities_per_continent_rating(userid)
print 'Cities per country rating'
calc_cities_per_country_rating(userid)
print 'Countries rating'
calc_all_countries_rating(userid)
print 'Countries per continet rating'
calc_countries_per_continent_rating(userid)
cursor.execute("COMMIT")
if __name__ == '__main__':
print 'Calculating ratings...'
update()