Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/reports/utils.py

Issue 8625042: Reports - user usefullness (Closed)
Patch Set: Reports - user usefulness Created Oct. 19, 2012, 12:54 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This Source Code is subject to the terms of the Mozilla Public License 3 # This Source Code is subject to the terms of the Mozilla Public License
4 # version 2.0 (the "License"). You can obtain a copy of the License at 4 # version 2.0 (the "License"). You can obtain a copy of the License at
5 # http://mozilla.org/MPL/2.0/. 5 # http://mozilla.org/MPL/2.0/.
6 6
7 import hashlib, hmac, base64, MySQLdb, os, re, marshal, subprocess 7 import hashlib, hmac, base64, MySQLdb, os, re, marshal, subprocess
8 from sitescripts.utils import get_config, cached, get_template, sendMail 8 from sitescripts.utils import get_config, memoize, cached, get_template, sendMai l
9 9
10 def getReportSubscriptions(guid): 10 def getReportSubscriptions(guid):
11 cursor = get_db().cursor(MySQLdb.cursors.DictCursor) 11 cursor = get_db().cursor(MySQLdb.cursors.DictCursor)
12 executeQuery(cursor, 12 executeQuery(cursor,
13 '''SELECT url, hasmatches FROM #PFX#sublists INNER JOIN 13 '''SELECT url, hasmatches FROM #PFX#sublists INNER JOIN
14 #PFX#subscriptions ON (#PFX#sublists.list = #PFX#subscriptions.id) 14 #PFX#subscriptions ON (#PFX#sublists.list = #PFX#subscriptions.id)
15 WHERE report = %s''', 15 WHERE report = %s''',
16 (guid)) 16 (guid))
17 rows = cursor.fetchall() 17 rows = cursor.fetchall()
18 cursor.close() 18 cursor.close()
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 cursor = get_db().cursor() 52 cursor = get_db().cursor()
53 screenshot = reportData.get('screenshot', None) 53 screenshot = reportData.get('screenshot', None)
54 if screenshot != None: 54 if screenshot != None:
55 reportData['hasscreenshot'] = 2 if reportData.get('screenshotEdited', False) else 1 55 reportData['hasscreenshot'] = 2 if reportData.get('screenshotEdited', False) else 1
56 try: 56 try:
57 saveScreenshot(guid, screenshot) 57 saveScreenshot(guid, screenshot)
58 except (TypeError, UnicodeEncodeError): 58 except (TypeError, UnicodeEncodeError):
59 reportData['hasscreenshot'] = 0 59 reportData['hasscreenshot'] = 0
60 del reportData['screenshot'] 60 del reportData['screenshot']
61 knownIssues = len(reportData.get('knownIssues', [])) 61 knownIssues = len(reportData.get('knownIssues', []))
62 contact = getUserId(reportData.get('email', None)) if reportData.get('email', None) else None 62 contact = getUserId(reportData.get('email', None)) if 'email' in reportData el se None
Wladimir Palant 2012/10/22 13:32:52 If 'email' in reportData is True then it means tha
Andrey Novikov 2012/10/23 14:15:29 Done.
63 dumpstr = marshal.dumps(reportData) 63 dumpstr = marshal.dumps(reportData)
64 64
65 if contact != None and isNew: 65 if contact != None and isNew:
66 executeQuery(cursor, 66 executeQuery(cursor,
67 '''INSERT INTO #PFX#users (id, reports) VALUES (%s, 1) ON DUPLIC ATE KEY UPDATE reports = reports + 1''', 67 '''INSERT INTO #PFX#users (id, reports) VALUES (%s, 1) ON DUPLIC ATE KEY UPDATE reports = reports + 1''',
68 (contact)) 68 (contact))
69 executeQuery(cursor, 69 executeQuery(cursor,
70 '''INSERT INTO #PFX#reports (guid, type, ctime, site, comment, sta tus, contact, hasscreenshot, knownissues, dump) 70 '''INSERT INTO #PFX#reports (guid, type, ctime, site, comment, sta tus, contact, hasscreenshot, knownissues, dump)
71 VALUES (%(guid)s, %(type)s, FROM_UNIXTIME(%(ctime)s), %(site)s, %(comment)s, %(status)s, %(contact)s, 71 VALUES (%(guid)s, %(type)s, FROM_UNIXTIME(%(ctime)s), %(site)s, %(comment)s, %(status)s, %(contact)s,
72 %(hasscreenshot)s, %(knownissues)s, %(dump)s) ON DUPLICATE KEY 72 %(hasscreenshot)s, %(knownissues)s, %(dump)s) ON DUPLICATE KEY
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 '''DELETE FROM #PFX#reports WHERE guid = %s''', 104 '''DELETE FROM #PFX#reports WHERE guid = %s''',
105 (guid)) 105 (guid))
106 get_db().commit() 106 get_db().commit()
107 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.html') 107 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.html')
108 if os.path.isfile(file): 108 if os.path.isfile(file):
109 os.remove(file) 109 os.remove(file)
110 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.png') 110 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.png')
111 if os.path.isfile(file): 111 if os.path.isfile(file):
112 os.remove(file) 112 os.remove(file)
113 113
114 @memoize
115 def getUserUsefulnessScore(contact):
116 if contact == None:
117 return 0
118
119 cursor = get_db().cursor()
120 executeQuery(cursor,
121 '''SELECT ((positive + 1.9208) / (positive + negative)
122 - 1.96 * SQRT((positive * negative) / (positive + negati ve) + 0.9604) / (positive + negative))
123 / (1 + 3.8416 / (positive + negative)) AS score FROM #PF X#users WHERE id = %s''',
124 (contact))
Wladimir Palant 2012/10/22 13:32:52 Please add a link to the source of this formula.
Andrey Novikov 2012/10/23 14:15:29 Done.
125 user = cursor.fetchone()
Wladimir Palant 2012/10/22 13:32:52 You are selecting the score, not the user - please
Andrey Novikov 2012/10/23 14:15:29 Done.
126 if user == None:
127 return 0
128
129 if user[0] == None: # no score yet
130 return 0.3
131 else:
132 return user[0]
Wladimir Palant 2012/10/22 13:32:52 How about returning |4 * user[0]| here? We want th
Andrey Novikov 2012/10/23 14:15:29 Done.
133
134 def updateUserUsefulness(contact, newusefulness, oldusefulness):
135 new = int(newusefulness)
136 old = int(oldusefulness)
137 if new == old:
138 return
139 positive = 0
140 negative = 0
141 if old > 0:
142 positive -= 1
143 elif old < 0:
144 negative -= 1
145 if new > 0:
146 positive += 1
147 elif new < 0:
148 negative += 1
149 cursor = get_db().cursor()
150 executeQuery(cursor,
151 '''UPDATE #PFX#users SET negative = negative + %s, positive = posi tive + %s WHERE id = %s''',
152 (negative, positive, contact))
153 get_db().commit()
154
114 def saveScreenshot(guid, screenshot): 155 def saveScreenshot(guid, screenshot):
115 prefix = 'data:image/png;base64,' 156 prefix = 'data:image/png;base64,'
116 if not screenshot.startswith(prefix): 157 if not screenshot.startswith(prefix):
117 raise TypeError('Screenshot is not a PNG image') 158 raise TypeError('Screenshot is not a PNG image')
118 data = base64.b64decode(screenshot[len(prefix):]) 159 data = base64.b64decode(screenshot[len(prefix):])
119 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.png') 160 file = os.path.join(get_config().get('reports', 'dataPath'), guid[0], guid[1], guid[2], guid[3], guid + '.png')
120 dir = os.path.dirname(file) 161 dir = os.path.dirname(file)
121 if not os.path.exists(dir): 162 if not os.path.exists(dir):
122 os.makedirs(dir) 163 os.makedirs(dir)
123 f = open(file, 'wb') 164 f = open(file, 'wb')
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 dbpasswd = get_config().get('reports', 'dbpassword') 216 dbpasswd = get_config().get('reports', 'dbpassword')
176 if os.name == 'nt': 217 if os.name == 'nt':
177 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8', named_pipe=True) 218 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8', named_pipe=True)
178 else: 219 else:
179 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8') 220 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8')
180 221
181 def executeQuery(cursor, query, args=None): 222 def executeQuery(cursor, query, args=None):
182 tablePrefix = get_config().get('reports', 'dbprefix') 223 tablePrefix = get_config().get('reports', 'dbprefix')
183 query = re.sub(r'#PFX#', tablePrefix, query) 224 query = re.sub(r'#PFX#', tablePrefix, query)
184 cursor.execute(query, args) 225 cursor.execute(query, args)
OLDNEW

Powered by Google App Engine
This is Rietveld