def GetTestScoreAndDisplayValue(self, test_key, raw_scores): """Get a normalized score (0 to 100) and a value to output to the display. Args: test_key: a key for a test_set test. raw_scores: a dict of raw_scores indexed by test keys. Returns: score, display_value # score is from 0 to 100. # display_value is the text for the cell. """ test = self.GetTest(test_key) group_members = test.group_members if len(group_members): if raw_scores is None: return 0, '' abbrevs = set() total_score = 0 n_scored = 0 for member in group_members: snippet = ecmascript_snippets.with_name(member.key) member_median = raw_scores.get(member.key) if member_median is not None: score, display = self.GetTestScoreAndDisplayValue( member.key, raw_scores) if ecmascript_snippets.ABBREV in snippet: abbrev = snippet.get( ecmascript_snippets.ABBREV).get(display) if abbrev: abbrevs.add(abbrev) total_score += score n_scored += 1 avg_score = (n_scored and int(total_score / n_scored)) or 0 abbrevs = list(abbrevs) abbrevs.sort() return avg_score, ', '.join(abbrevs) snippet = ecmascript_snippets.with_name(test_key) median = raw_scores[test_key] if median is None: return 0, '' # TODO(mikesamuel): a confidence metric around the results. int_median = int(round(median)) display = '?' values = snippet[ecmascript_snippets.VALUES] if int_median >= 0 and int_median < len(values): display = values[int_median] return rate_display(display, snippet.get(ecmascript_snippets.GOOD)), display
def GetTestScoreAndDisplayValue(self, test_key, raw_scores): """Get a normalized score (0 to 100) and a value to output to the display. Args: test_key: a key for a test_set test. raw_scores: a dict of raw_scores indexed by test keys. Returns: score, display_value # score is from 0 to 100. # display_value is the text for the cell. """ test = self.GetTest(test_key) group_members = test.group_members if len(group_members): if raw_scores is None: return 0, '' abbrevs = set() total_score = 0 n_scored = 0 for member in group_members: snippet = ecmascript_snippets.with_name(member.key) member_median = raw_scores.get(member.key) if member_median is not None: score, display = self.GetTestScoreAndDisplayValue( member.key, raw_scores) if ecmascript_snippets.ABBREV in snippet: abbrev = snippet.get(ecmascript_snippets.ABBREV).get(display) if abbrev: abbrevs.add(abbrev) total_score += score n_scored += 1 avg_score = (n_scored and int(total_score / n_scored)) or 0 abbrevs = list(abbrevs) abbrevs.sort() return avg_score, ', '.join(abbrevs) snippet = ecmascript_snippets.with_name(test_key) median = raw_scores[test_key] if median is None: return 0, '' # TODO(mikesamuel): a confidence metric around the results. int_median = int(round(median)) display = '?' values = snippet[ecmascript_snippets.VALUES] if int_median >= 0 and int_median < len(values): display = values[int_median] return rate_display(display, snippet.get(ecmascript_snippets.GOOD)), display
def Json(request): def html(s): return re.sub(r'<', '<', re.sub('>', '>', re.sub(r'&', '&', s))) def help_page(msg): return ( '<title>%(msg)s</title>' '<h1>%(msg)s</h1>' 'Serve JSON mapping code snippets to results.\n' '<p>The JSON is the intersection of the (key, value) pairs accross all' ' user agents requested, so if Firefox 3 was requested then only' ' key/value pairs that are present in both Firefox 3.0, 3.1, etc.' ' will be present.\n' '<p>The extra <code>userAgent</code> key maps to the user agents' ' requested so that the output is self-describing.\n' '\n' '<p>CGI params<dl>\n' ' <dt><code>ot</code></dt>\n' ' <dd>The output mime-type</dd>\n' ' <dt><code>ua</code></dt>\n' ' <dd>The user agents requested</dd>\n' '</dl>\n' 'E.g.,\n' ' <li><code>ua=Firefox+3.5</code>\n' ' <li><code>ua=MSIE+6.0</code>\n' ' <li><code>ot=application%%2Fjson</code>\n' ' <li><code>ot=text%%2Fplain</code>\n' '</ul>' ) % { 'msg': html(msg) } if request.method != 'GET' and request.method != 'HEAD': return http.HttpResponseBadRequest( help_page('Bad method "%s"' % request.method), mimetype='text/html') user_agent_string = None out_type = 'text/plain' for key, value in request.GET.iteritems(): if key == u'ua': user_agent_string = value elif key == u'ot' and value in ('text/plain', 'application/json'): out_type = value else: return http.HttpResponseBadRequest( help_page('Unknown CGI param "%s"' % key), mimetype='text/html') raise Exception() if user_agent_string is None: return http.HttpResponseBadRequest( help_page('Please specify useragent'), mimetype='text/html') user_agent_strings = user_agent_string.split(',') test_set = all_test_sets.GetTestSet(CATEGORY) test_keys = [t.key for t in test_set.tests] stats_data = result_stats.CategoryStatsManager.GetStats( test_set=test_set, browsers=user_agent_strings, test_keys=test_keys) del stats_data['total_runs'] # Keep only those items that are common across all the user agents requested. combined = None for browser_stats in stats_data.values(): stats = browser_stats['results'] if combined is None: combined = dict([(k, v.get('display')) for (k, v) in stats.iteritems() if k in ecmascript_snippets.SNIPPET_NAMES]) else: old_combined = combined combined = dict([(k, v.get('display')) for (k, v) in stats.iteritems() if (k in old_combined and old_combined.get(k) == v.get('display'))]) if combined is None: combined = {} result = [(ecmascript_snippets.with_name(k)[ecmascript_snippets.CODE], v) for (k, v) in combined.iteritems() if v is not None] result.append(('*userAgent*', json.to_json(stats_data.keys()))) def check_json_value(v): if v == 'throw': return '{ "throw": true }' return v response = http.HttpResponse(mimetype=out_type) response.write('{%s}' % ',\n'.join( [('%s:%s' % (json.to_json(k), check_json_value(v))) for (k, v) in result])) return response