-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathreports.py
198 lines (169 loc) · 7.61 KB
/
reports.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import copy
import logging
from collections import Counter
import ckan.model as model
import ckan.plugins as p
from ckanext.report import lib
try:
from collections import OrderedDict # from python 2.7
except ImportError:
from sqlalchemy.util import OrderedDict
log = logging.getLogger(__name__)
def openness_report(organization, include_sub_organizations=False):
if organization is None:
return openness_index(include_sub_organizations=include_sub_organizations)
else:
return openness_for_organization(organization=organization, include_sub_organizations=include_sub_organizations)
def openness_index(include_sub_organizations=False):
'''Returns the counts of 5 stars of openness for all organizations.'''
context = {'model': model, 'session': model.Session, 'ignore_auth': True}
total_score_counts = Counter()
counts = {}
# Get all the scores and build up the results by org
orgs = add_progress_bar(model.Session.query(model.Group)
.filter(model.Group.type == 'organization')
.filter(model.Group.state == 'active').all())
for org in orgs:
scores = []
# NB org.packages() misses out many - see:
# http://redmine.dguteam.org.uk/issues/1844
pkgs = model.Session.query(model.Package) \
.filter_by(owner_org=org.id) \
.filter_by(state='active') \
.all()
for pkg in pkgs:
try:
qa = p.toolkit.get_action('qa_package_openness_show')(context, {'id': pkg.id})
except p.toolkit.ObjectNotFound:
log.warning('No QA info for package %s', pkg.name)
return
scores.append(qa['openness_score'])
score_counts = Counter(scores)
total_score_counts += score_counts
counts[org.name] = {
'organization_title': org.title,
'score_counts': score_counts,
}
counts_with_sub_orgs = copy.deepcopy(counts) # new dict
if include_sub_organizations:
for org_name in counts_with_sub_orgs:
org = model.Group.by_name(org_name)
for sub_org_id, sub_org_name, sub_org_title, sub_org_parent_id \
in org.get_children_group_hierarchy(type='organization'):
if sub_org_name not in counts:
# occurs only if there is an organization created since the last loop?
continue
counts_with_sub_orgs[org_name]['score_counts'] += \
counts[sub_org_name]['score_counts']
results = counts_with_sub_orgs
else:
results = counts
table = []
for org_name, org_counts in results.items():
if not org_counts['score_counts']: # Let's skip if there are no counts at all.
continue
total_stars = sum([k*v for k, v in org_counts['score_counts'].items() if k])
num_pkgs_scored = sum([v for k, v in org_counts['score_counts'].items()
if k is not None])
average_stars = round(float(total_stars) / num_pkgs_scored, 1) \
if num_pkgs_scored else 0.0
row = OrderedDict((
('organization_title', results[org_name]['organization_title']),
('organization_name', org_name),
('total_stars', total_stars),
('average_stars', average_stars),
))
row.update(jsonify_counter(org_counts['score_counts']))
table.append(row)
table.sort(key=lambda x: (-x['total_stars'],
-x['average_stars']))
# Get total number of packages & resources
num_packages = model.Session.query(model.Package)\
.filter_by(state='active')\
.count()
return {'table': table,
'total_score_counts': jsonify_counter(total_score_counts),
'num_packages_scored': sum(total_score_counts.values()),
'num_packages': num_packages,
}
def openness_for_organization(organization=None, include_sub_organizations=False):
org = model.Group.get(organization)
if not org:
raise p.toolkit.ObjectNotFound
if not include_sub_organizations:
orgs = [org]
else:
orgs = lib.go_down_tree(org)
context = {'model': model, 'session': model.Session, 'ignore_auth': True}
score_counts = Counter()
rows = []
num_packages = 0
for org in orgs:
# NB org.packages() misses out many - see:
# http://redmine.dguteam.org.uk/issues/1844
pkgs = model.Session.query(model.Package) \
.filter_by(owner_org=org.id) \
.filter_by(state='active') \
.all()
num_packages += len(pkgs)
for pkg in pkgs:
try:
qa = p.toolkit.get_action('qa_package_openness_show')(context, {'id': pkg.id})
except p.toolkit.ObjectNotFound:
log.warning('No QA info for package %s', pkg.name)
return
rows.append(OrderedDict((
('dataset_name', pkg.name),
('dataset_title', pkg.title),
('dataset_notes', lib.dataset_notes(pkg)),
('organization_name', org.name),
('organization_title', org.title),
('openness_score', qa['openness_score']),
('openness_score_reason', qa['openness_score_reason']),
)))
score_counts[qa['openness_score']] += 1
total_stars = sum([k*v for k, v in score_counts.items() if k])
num_pkgs_with_stars = sum([v for k, v in score_counts.items()
if k is not None])
average_stars = round(float(total_stars) / num_pkgs_with_stars, 1) \
if num_pkgs_with_stars else 0.0
return {'table': rows,
'score_counts': jsonify_counter(score_counts),
'total_stars': total_stars,
'average_stars': average_stars,
'num_packages_scored': len(rows),
'num_packages': num_packages,
}
def openness_report_combinations():
for organization in lib.all_organizations(include_none=True):
for include_sub_organizations in (False, True):
yield {'organization': organization,
'include_sub_organizations': include_sub_organizations}
openness_report_info = {
'name': 'openness',
'title': 'Openness (Five Stars)',
'description': 'Datasets graded on Tim Berners-Lee\'s Five Stars of Openness - openly licensed,'
' openly accessible, structured, open format, URIs for entities, linked.',
'option_defaults': OrderedDict((('organization', None),
('include_sub_organizations', False),
)),
'option_combinations': openness_report_combinations,
'generate': openness_report,
'template': 'report/openness.html',
}
def jsonify_counter(counter):
# When counters are stored as JSON, integers become strings. Do the conversion
# here to ensure that when you run the report the first time, you get the same
# response as subsequent times that go through the cache/JSON.
return dict((str(k) if k is not None else k, v) for k, v in counter.items())
def add_progress_bar(iterable, caption=None):
try:
# Add a progress bar, if it is installed
import progressbar
bar = progressbar.ProgressBar(widgets=[
(caption + ' ') if caption else '',
progressbar.Percentage(), ' ',
progressbar.Bar(), ' ', progressbar.ETA()])
return bar(iterable)
except ImportError:
return iterable