Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: appengine/findit/crash/classifier.py

Issue 1914113002: [Findit] Enable project classifier and component classifier (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Address comments and add tests. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 from collections import namedtuple
6 from collections import OrderedDict
7
8 from common import constants
9 from crash.type_enums import CallStackLanguageType
10 from model.crash.crash_config import CrashConfig
11
12
13 class ClassOccurrencesInfo(object):
Martin Barbella 2016/05/10 06:00:41 Nit: ClassOccurrenceInfo
Sharu Jiang 2016/05/12 23:58:00 Done.
14 """Represents information of a class in results or crash_stack.
15
16 Class information includes the name of the class, a list of indice (index in
17 results list or in the crash_stack) of occurrences."""
18
19 def __init__(self, name, occurrences):
20 self.name = name
21 self.occurrences = occurrences
22
23
24 def DefaultRankFunction(class_info):
25 """Default rank function to rank classes."""
26 # If the top 2 frames are in the same class, give this class highest
27 # priority.
28 if 0 in class_info.occurrences and 1 in class_info.occurrences:
29 return -constants.INFINITY, class_info.occurrences[0]
30
31 return -len(class_info.occurrences), class_info.occurrences[0]
32
33
34 class Classifier(object): # pragma: no cover.
35 """Classifys results or crash stack into a class or a list of classes."""
Martin Barbella 2016/05/10 06:00:41 Nit: classifies
Sharu Jiang 2016/05/12 23:58:00 Done.
36
37 def GetClassFromStackFrame(self, frame):
38 raise NotImplementedError()
39
40 def GetClassFromResult(self, result):
41 raise NotImplementedError()
42
43 def _Classify(self, results, crash_stack, top_n, max_classes,
44 rank_function=DefaultRankFunction):
45 """Classifies a crash to a list of classes, ranked by rank_function.
46
47 Extracts a list of classes from results or crash_stack, rank the classes and
48 returns max_classes number of classes on the top.
49
50 Args:
51 results (list of Result): Culprit results.
52 crash_stack (CallStack): The callstack that caused the crash.
53 top_n (int): Number of top frames to be considered when classifying.
54 max_classes (int): Maximal number of classes to return.
55 rank_function (function): Used to rank classes based on
56 ClassOccurrencesInfos.
57
58 Returns:
59 A list of classes of this crash.
60 """
61 # Extracts the class list from culprit results if possible since it's more
62 # reliable.
63 if results:
64 class_list = map(self.GetClassFromResult, results[:top_n])
65 else:
66 class_list = map(self.GetClassFromStackFrame, crash_stack[:top_n])
67
68 def _GetClassOccurrencesInfos():
69 """Gets ClassOccurrencesInfo list from class_list.
70
71 The ClassOccurrencesInfo list is ordered by the first occurences of each
72 class.
73 """
74 if not class_list:
75 return class_list
76
77 infos = OrderedDict()
78
79 # Get occurences information of each class.
80 for index, class_name in enumerate(class_list):
81 if class_name not in infos:
82 infos[class_name] = ClassOccurrencesInfo(class_name, [index])
83 else:
84 infos[class_name].occurrences.append(index)
85
86 return infos.values()
87
88 class_infos = _GetClassOccurrencesInfos()
89 class_infos = sorted(class_infos, key=rank_function)
90
91 print class_list
Martin Barbella 2016/05/10 06:00:41 Remove before landing
Sharu Jiang 2016/05/12 23:58:00 Done.
92 classes = [info.name for info in class_infos]
93
94 return classes[:max_classes]
95
96 def Classify(self, results, crash_stack):
97 raise NotImplementedError()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698