-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharding_visitor.py
95 lines (69 loc) · 2.43 KB
/
sharding_visitor.py
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
from visitor import Visitor
class ShardingVisitor(Visitor):
def __init__(self, factory):
super().__init__()
self.visitors = dict()
self.factory = factory
def visit(self, submit):
key = self.build_key(submit)
if key not in self.visitors:
self.visitors[key] = self.factory.create(key)
self.visitors[key].visit(submit)
def _enum_visitors(self):
result = list(self.visitors.items())
try:
result.sort(key=lambda p: self._comparable_key(p[0]))
except Exception:
result.sort()
return result
def comparable_key(self, key):
return int(key)
def _comparable_key(self, key):
try:
return self.comparable_key(key)
except Exception:
return key
def get_stat_data(self):
result = []
for key, visitor in self._enum_visitors():
result.append((key, visitor.get_stat_data()))
return result
def pretty_key(self, key):
return str(key)
def pretty_print(self):
result = ''
for key, visitor in self._enum_visitors():
child_result = visitor.pretty_print()
if child_result in ['', None]:
continue
result += '\n' + self.pretty_key(key) + ':\n\t' + child_result.replace('\n', '\n\t')
return result
def build_key(self, submit):
return str(submit)
class ShardingByProblemVisitor(ShardingVisitor):
def build_key(self, submit):
return submit.problem_id
def comparable_key(self, key):
return int(key[1])
def pretty_key(self, key):
return 'Problem #{}'.format(key[1])
class ShardingByContestVisitor(ShardingVisitor):
def build_key(self, submit):
return submit.problem_id[0]
def pretty_key(self, key):
return 'Contest #{}'.format(key)
class ShardingByUserVisitor(ShardingVisitor):
def build_key(self, submit):
return submit.user_id
def pretty_key(self, key):
return 'User #{}'.format(key)
class ShardingByLangVisitor(ShardingVisitor):
def build_key(self, submit):
return submit.lang_id
def pretty_key(self, key):
return 'Lang #{}'.format(key)
class ShardingByScoringVisitor(ShardingVisitor):
def build_key(self, submit):
return 'ACM' if submit.scoring == 'ACM' else 'kirov'
def pretty_key(self, key):
return 'Scoring type: {}'.format(key)