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

Delta Between Two Patch Sets: sitescripts/reports/utils.py

Issue 8625042: Reports - user usefullness (Closed)
Left Patch Set: Reports - user usefulness Created Oct. 19, 2012, 12:54 p.m.
Right Patch Set: Reports - user usefulness Created Oct. 29, 2012, 12:35 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/reports/template/report.html ('k') | sitescripts/reports/web/updateReport.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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, memoize, cached, get_template, sendMai l 8 from sitescripts.utils import get_config, cached, get_template, sendMail
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 'email' in reportData el se None 62 contact = getUserId(reportData.get('email', None)) if reportData.get('email', None) else 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 114 @cached(3600)
115 def getUserUsefulnessScore(contact): 115 def getUserUsefulnessScore(contact):
116 if contact == None: 116 if contact == None:
117 return 0 117 return 0
118 118
119 cursor = get_db().cursor() 119 cursor = get_db().cursor()
120 # source from http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
120 executeQuery(cursor, 121 executeQuery(cursor,
121 '''SELECT ((positive + 1.9208) / (positive + negative) 122 '''SELECT ((positive + 1.9208) / (positive + negative)
122 - 1.96 * SQRT((positive * negative) / (positive + negati ve) + 0.9604) / (positive + negative)) 123 - 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 / (1 + 3.8416 / (positive + negative)) AS score FROM #PF X#users WHERE id = %s''',
124 (contact)) 125 (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() 126 score = 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 if score == None:
127 return 0 128 return 0
128 129
129 if user[0] == None: # no score yet 130 if score[0] == None: # no score yet
130 return 0.3 131 return 0.3
131 else: 132 else:
132 return user[0] 133 return 4 * score[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
134 def updateUserUsefulness(contact, newusefulness, oldusefulness): 135 def updateUserUsefulness(contact, newusefulness, oldusefulness):
135 new = int(newusefulness) 136 new = int(newusefulness)
136 old = int(oldusefulness) 137 old = int(oldusefulness)
137 if new == old: 138 if new == old:
138 return 139 return
139 positive = 0 140 positive = 0
140 negative = 0 141 negative = 0
141 if old > 0: 142 if old > 0:
142 positive -= 1 143 positive -= 1
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 dbpasswd = get_config().get('reports', 'dbpassword') 217 dbpasswd = get_config().get('reports', 'dbpassword')
217 if os.name == 'nt': 218 if os.name == 'nt':
218 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8', named_pipe=True) 219 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8', named_pipe=True)
219 else: 220 else:
220 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8') 221 return MySQLdb.connect(user=dbuser, passwd=dbpasswd, db=database, use_unicod e=True, charset='utf8')
221 222
222 def executeQuery(cursor, query, args=None): 223 def executeQuery(cursor, query, args=None):
223 tablePrefix = get_config().get('reports', 'dbprefix') 224 tablePrefix = get_config().get('reports', 'dbprefix')
224 query = re.sub(r'#PFX#', tablePrefix, query) 225 query = re.sub(r'#PFX#', tablePrefix, query)
225 cursor.execute(query, args) 226 cursor.execute(query, args)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld