17
17
18
18
from .check_source import check_source
19
19
20
+
20
21
def run_module ():
21
22
22
23
params = read_params ()
23
24
meta = covidcast .metadata ()
24
25
25
26
complaints = []
26
27
for data_source in params ["sources" ].keys ():
27
- complaints .extend (check_source (data_source , meta , params ["sources" ], params .get ("grace" , 0 )))
28
+ complaints .extend (check_source (data_source , meta ,
29
+ params ["sources" ], params .get ("grace" , 0 )))
28
30
29
31
if len (complaints ) > 0 :
30
32
for complaint in complaints :
@@ -34,11 +36,13 @@ def run_module():
34
36
35
37
sys .exit (1 )
36
38
39
+
37
40
def split_complaints (complaints , n = 49 ):
38
41
"""Yield successive n-sized chunks from complaints list."""
39
42
for i in range (0 , len (complaints ), n ):
40
43
yield complaints [i :i + n ]
41
44
45
+
42
46
def report_complaints (all_complaints , params ):
43
47
"""Post complaints to Slack."""
44
48
if not params ["slack_token" ]:
@@ -59,39 +63,45 @@ def report_complaints(all_complaints, params):
59
63
# You will get a SlackApiError if "ok" is False
60
64
assert False , e .response ["error" ]
61
65
62
-
63
66
def get_maintainers_block (complaints ):
64
67
maintainers = set ()
65
68
for c in complaints :
66
69
maintainers .update (c .maintainers )
67
-
70
+
68
71
maintainers_block = {
69
- "type" : "section" ,
70
- "text" : {
71
- "type" : "mrkdwn" ,
72
- "text" : "Hi, this is Sir Complains-a-Lot. I need to speak to " +
73
- (", " .join ("<@{0}>" .format (m ) for m in maintainers )) + "."
74
- }
75
- }
72
+ "type" : "section" ,
73
+ "text" : {
74
+ "type" : "mrkdwn" ,
75
+ "text" : "Hi, this is Sir Complains-a-Lot. I need to speak to " +
76
+ (", " .join ("<@{0}>" .format (m )
77
+ for m in maintainers )) + "."
78
+ }
79
+ }
76
80
77
81
return maintainers_block
78
82
79
83
80
84
def format_complaints_aggregated_by_source (complaints ):
81
- """Build formatted Slack message for posting to the API, aggregating the
85
+ """Build formatted Slack message for posting to the API, aggregating
82
86
complaints by source to reduce the number of blocks."""
83
87
84
88
blocks = [get_maintainers_block (complaints )]
85
89
86
- def aggregated_message_for_source (x ): return "{complaint} - (last update: {last_updated})" .format (
87
- complaint = x .message , last_updated = x .last_updated .strftime ("%Y-%m-%d" ))
90
+ def message_for_source (complaint ):
91
+ return "{main_text} - (last update: {last_updated})" .format (
92
+ main_text = complaint .message ,
93
+ last_updated = complaint .last_updated .strftime ("%Y-%m-%d" ))
88
94
89
- for source , v in groupby (complaints , key = lambda x : x .data_source ):
90
- for message , complaint_list in groupby (v , key = aggregated_message_for_source ):
95
+ for source , complaints_by_source in groupby (
96
+ complaints , key = lambda x : x .data_source ):
97
+ for message , complaint_list in groupby (
98
+ complaints_by_source , key = message_for_source ):
91
99
signal_and_geo_types = ""
92
100
for complaint in complaint_list :
93
101
signal_and_geo_types += "`{signal}: [{geo_types}]`\n " .format (
94
- signal = complaint .signal , geo_types = ", " .join (complaint .geo_types ))
102
+ signal = complaint .signal ,
103
+ geo_types = ", " .join (complaint .geo_types ))
104
+
95
105
blocks .extend ([
96
106
{
97
107
"type" : "divider"
@@ -100,7 +110,11 @@ def aggregated_message_for_source(x): return "{complaint} - (last update: {last_
100
110
"type" : "section" ,
101
111
"text" : {
102
112
"type" : "mrkdwn" ,
103
- "text" : "*{source_name}* {message_for_group}:\n {signals}" .format (source_name = source .upper (), message_for_group = message , signals = signal_and_geo_types )
113
+ "text" : "*{source_name}* {message}:\n {signals}"
114
+ .format (
115
+ source_name = source .upper (),
116
+ message = message ,
117
+ signals = signal_and_geo_types )
104
118
}
105
119
}
106
120
])
@@ -121,13 +135,12 @@ def format_complaints(complaints):
121
135
for complaint in complaints :
122
136
blocks .append (
123
137
{
124
- "type" : "section" ,
125
- "text" : {
126
- "type" : "mrkdwn" ,
127
- "text" : complaint .to_md ()
128
- }
129
- }
138
+ "type" : "section" ,
139
+ "text" : {
140
+ "type" : "mrkdwn" ,
141
+ "text" : complaint .to_md ()
142
+ }
143
+ }
130
144
)
131
145
132
-
133
146
return blocks
0 commit comments