Coverage for apps/report/extractor.py : 94%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from .report_fields import LABELS, CAMEL_CASES
4def _numeral(value, ignore=False):
5 """
6 Change to integer/float
7 """
8 try:
9 try:
10 return int(value)
11 except ValueError:
12 return float(value)
13 except ValueError as e:
14 if not ignore: 14 ↛ 15line 14 didn't jump to line 15, because the condition on line 14 was never true
15 raise e
16 return value
19def _percentage(value):
20 """
21 Change percentage text to integer/float
22 """
23 return _numeral(value.replace(' ', '').replace('%', ''))
26def generate_hierarchy(obj, data):
27 result = {}
29 result['name'] = obj['name'] if 'name' in obj else LABELS[obj['key']]
30 result['key'] = obj['key']
32 children = obj.get('children')
33 if children:
34 result['children'] = []
35 result['size'] = 0
36 for child in children:
37 child_result = generate_hierarchy(child, data)
38 result['children'].append(child_result)
39 result['size'] += child_result['size']
40 elif obj.get('key'): 40 ↛ 43line 40 didn't jump to line 43, because the condition on line 40 was never false
41 result['size'] = _numeral(data[obj['key']])
43 return result
46def extract_rc_data(data):
47 fields = (
48 '@PlannedRC', '@TotalRC', '@Sponsored', '@Available', '@TotalHold', '@TotalDeath',
49 '@TotalMale', '@TotalFemale', '@TotalLeft',
50 )
51 return {
52 CAMEL_CASES[field]: _numeral(data[field])
53 for field in fields
54 }
57def extract_health_nutrition(data):
58 fields = (
59 ('@HealthSatisfactory', 'good'),
60 ('@HealthNotSatisfactory', 'bad'),
61 ('@Below5Child', 'normal'),
62 ('@NotFollowingGrowthCurve', 'bad'),
63 ('@MUACSevereMalnutrition', 'bad'),
64 ('@MUACModerateMalnutrition', 'bad'),
65 ('@MUACPartiallyImmunized', 'normal'),
67 # TODO: Remove this from data also
68 ('@NotParticipatingHealthNutriActivities', 'normal'),
69 ('@NotVarifiedHealthGrowthCard', 'bad'),
70 )
71 return [
72 {
73 'name': LABELS[_d[0]],
74 'value': _numeral(data.get(_d[0])),
75 'type': _d[1], 'key': _d[0],
76 } for _d in fields
77 ]
80def extract_rc_pie_chart(data):
81 fields = [
82 {
83 'key': '@PlannedRC',
84 },
85 {
86 'key': '@TotalRC',
87 'children': [
88 {
89 'key': '@Sponsored',
90 'children': [
91 {
92 'key': '@SponsoredMale',
93 },
94 {
95 'key': '@SponsoredFemale',
96 },
97 ],
98 },
99 {
100 'key': '@Available',
101 'children': [
102 {
103 'key': '@AvailableMale',
104 },
105 {
106 'key': '@AvailableFemale',
107 },
108 ],
109 },
110 ],
111 },
112 {
113 'key': '@TotalHold',
114 },
115 ]
117 return generate_hierarchy({
118 'name': 'RC Supply',
119 'key': 'rc_supply',
120 'children': fields,
121 }, data)
124def extract_education(data):
125 fields = [
126 {
127 'key': '@PrimarySchoolAge',
128 'children': [
129 {
130 'key': '@PrimarySchoolAgeFormal',
131 },
132 {
133 'key': '@PrimarySchoolAgeNonFormal',
134 },
135 {
136 'key': '@PrimarySchoolAgeNoEducation',
137 },
138 ],
139 },
141 {
142 'key': '@SecondarySchoolAge',
143 'children': [
144 {
145 'key': '@SecondarySchoolAgeFormal',
146 },
147 {
148 'key': '@SecondarySchoolAgeNonFormal',
149 },
150 {
151 'key': '@SecondarySchoolAgeVocational',
152 },
153 {
154 'key': '@SecondarySchoolAgeNoEducation',
155 },
156 ],
157 },
158 ]
160 return generate_hierarchy({
161 'name': 'Education',
162 'key': 'education',
163 'children': fields,
164 }, data)
167def extract_child_monitoring(data):
168 fields = (
169 '@NotSighted30Days',
170 '@NotSighted60Days',
171 '@NotSighted90Days',
172 '@VisitCompleted',
173 '@SponsorVisitCompleted',
174 )
175 return [
176 {
177 'name': LABELS[field],
178 'value': _numeral(data.get(field)),
179 'key': field,
180 } for field in fields
181 ]
184def extract_correspondence(data):
185 def normalize(_d, fields):
186 return {
187 CAMEL_CASES[field]: _numeral(_d[field], ignore=True)
188 for field in fields
189 }
191 fields = (
192 '@TypeName',
193 '@Initial',
194 '@Received',
195 '@Closed',
196 '@PendingCurrent',
197 '@PendingOverDue',
198 '@Textbox370',
199 )
200 return [normalize(datum, fields) for datum in data]
203def extract_data(data):
204 report = data['Report']
205 tablix2 = report['Tablix2']
206 correspondence_data = report['Tablix3']['Details_Collection']['Details']
208 _report = {
209 'reportDate': tablix2['@Textbox55'].split(':', 1)[1].strip(),
210 'rcData': extract_rc_data(tablix2),
211 'rcPieChart': extract_rc_pie_chart(tablix2),
213 'healthNutrition': extract_health_nutrition(tablix2),
214 'education': extract_education(tablix2),
215 'childMonitoring': extract_child_monitoring(tablix2),
216 'correspondences': extract_correspondence(correspondence_data),
217 }
219 return _report