-
Notifications
You must be signed in to change notification settings - Fork 3
/
duplicate-packages-organization.py
279 lines (226 loc) · 9 KB
/
duplicate-packages-organization.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from __future__ import absolute_import
import argparse
import csv
import itertools
import logging
import logging.config
import os
import sys
import time
from datetime import datetime
from dedupe.ckan_api import CkanApiClient
class OrgDuplicateLog(object):
# Order matters here for the report
fieldnames = [
"name", # Organization Name
"number_datasets_duplicated", # Duplicate identifiers (CKAN ID)
"total_duplicate_count", # Sum of all duplicates
"total_datasets", # Number of datasets in org
"percent_duplicate", # Amount of org datasets are dupes
]
def __init__(self, filename=None, run_id=None):
if not run_id:
run_id = datetime.now().strftime("%Y%m%d%H%M%S")
if not filename:
filename = "org-duplicates-%s.csv" % run_id
log.info("Opening duplicate package report for writing filename=%s", filename)
self.__f = open(filename, mode="w")
self.log = csv.DictWriter(self.__f, fieldnames=OrgDuplicateLog.fieldnames)
self.log.writeheader()
def add(self, org_results):
log.debug(
"Recording organization counts=%s", org_results.get("organization_name")
)
self.log.writerow(
{
"name": org_results.get("name"),
"number_datasets_duplicated": org_results.get(
"number_datasets_duplicated"
),
"total_duplicate_count": org_results.get("total_duplicate_count"),
"total_datasets": org_results.get("total_datasets"),
"percent_duplicate": org_results.get("percent_duplicate"),
}
)
# Persist the write to disk
self.__f.flush()
os.fsync(self.__f.fileno())
class HarvestDuplicateLog(object):
# Order matters here for the report
fieldnames = [
"harvest_name", # Harvest Source Name
"org_name", # Organization Name
"number_datasets_duplicated", # Duplicate identifiers (CKAN ID)
"total_duplicate_count", # Sum of all duplicates
"total_datasets", # Number of datasets in org
"percent_duplicate", # Amount of org datasets are dupes
]
def __init__(self, filename=None, run_id=None):
if not run_id:
run_id = datetime.now().strftime("%Y%m%d%H%M%S")
if not filename:
filename = "harvest-duplicates-%s.csv" % run_id
log.info("Opening duplicate package report for writing filename=%s", filename)
self.__f = open(filename, mode="w")
self.log = csv.DictWriter(self.__f, fieldnames=HarvestDuplicateLog.fieldnames)
self.log.writeheader()
def add(self, harvest_results):
log.debug(
"Recording organization counts=%s", harvest_results.get("organization_name")
)
self.log.writerow(
{
"harvest_name": harvest_results.get("harvest_name"),
"org_name": harvest_results.get("org_name"),
"number_datasets_duplicated": harvest_results.get(
"number_datasets_duplicated"
),
"total_duplicate_count": harvest_results.get("total_duplicate_count"),
"total_datasets": harvest_results.get("total_datasets"),
"percent_duplicate": harvest_results.get("percent_duplicate"),
}
)
# Persist the write to disk
self.__f.flush()
os.fsync(self.__f.fileno())
logging.basicConfig(
stream=sys.stdout, format="%(asctime)s [%(name)s] %(levelname)s: %(message)s"
)
log = logging.getLogger("dedupe")
log.setLevel(logging.INFO)
# Define module-level context for signal handling
stopped = False
deduper = None
def get_org_list(ckan):
log.debug("Fetching organizations...")
organizations_list = ckan.get_organizations()
log.debug("Found organizations count=%d", len(organizations_list))
return organizations_list
def get_harvest_sources(ckan):
log.debug("Fetching organizations...")
harvest_sources_list = ckan.get_harvest_sources()
log.debug("Found organizations count=%d", len(harvest_sources_list))
return harvest_sources_list
def cleanup(signum, frame):
global deduper, stopped
log.warning("Stopping any in-progress dedupers...")
stopped = True
deduper.stop()
def run():
"""
This code for getting the list of organizations and duplicate duplicate data sets
"""
global deduper, stopped
parser = argparse.ArgumentParser(
description="Detects and removes duplicate packages on "
"data.gov. By default, duplicates are detected but not "
"actually removed."
)
parser.add_argument(
"--api-url",
default="https://catalog.data.gov",
help="The API base URL to query",
)
parser.add_argument(
"--debug", action="store_true", help="Include debug output from urllib3."
)
parser.add_argument(
"--run-id",
default=datetime.now().strftime("%Y%m%d%H%M%S"),
help="An identifier for a single run of the deduplication script.",
)
parser.add_argument(
"--verbose", "-v", action="store_true", help="Include verbose log output."
)
parser.add_argument(
"organization_name",
nargs="*",
help="Names of the organizations to deduplicate.",
)
parser.add_argument(
"--harvest_sources", action="store_true", help="Get counts by harvest source"
)
args = parser.parse_args()
if args.verbose:
log.setLevel(logging.DEBUG)
if args.debug:
logging.getLogger("urllib3").setLevel(logging.DEBUG)
log.info("run_id=%s", args.run_id)
ckan_api = CkanApiClient(args.api_url, "None", identifier_type="identifier")
ckan_geo_api = CkanApiClient(args.api_url, "None", identifier_type="guid")
log.info("Using api=%s", args.api_url)
if args.harvest_sources:
# Get and organize by harvest source
harvest_log = HarvestDuplicateLog(run_id=args.run_id)
harvest_sources = get_harvest_sources(ckan_api)
log.info("Checking %d harvest sources for duplicates", len(harvest_sources))
# Loop over the harvest sources one at a time
count = itertools.count(start=1)
for s in harvest_sources:
if stopped:
break
log.info("Checking harvest source=%s", s["title"])
total = ckan_api.get_harvest_source_count(s["title"])
json_duplicates = ckan_api.get_duplicate_identifiers_source(
s["title"], False, full_count=True
)
geo_duplicates = ckan_geo_api.get_duplicate_identifiers_source(
s["title"], False, full_count=True
)
duplicates = {**json_duplicates, **geo_duplicates}
count = 0
for dupe_cnt in duplicates.values():
count += dupe_cnt - 1
harvest_source_overview = {
"harvest_name": s["title"],
"org_name": s["organization"]["title"],
"number_datasets_duplicated": len(duplicates),
"total_duplicate_count": count,
"total_datasets": total,
"percent_duplicate": round(
float(count) / (total if total > 0 else 1) * 100, 2
),
}
harvest_log.add(harvest_source_overview)
time.sleep(1)
else:
# Get and organize by org
org_log = OrgDuplicateLog(run_id=args.run_id)
if args.organization_name:
org_list = args.organization_name
else:
# get all organizations that have datajson harvester
org_list = get_org_list(ckan_api)
log.info("Checking %d organizations for duplicates", len(org_list))
# Loop over the organizations one at a time
count = itertools.count(start=1)
for organization in org_list:
if stopped:
break
log.info("Checking org=%s", organization)
total = ckan_api.get_organization_count(organization)
json_duplicates = ckan_api.get_duplicate_identifiers(
organization, False, full_count=True
)
geo_duplicates = ckan_geo_api.get_duplicate_identifiers(
organization, False, full_count=True
)
# duplicates = json_duplicates.copy()
# duplicates.update(geo_duplicates)
duplicates = {**json_duplicates, **geo_duplicates}
count = 0
for dupe_cnt in duplicates.values():
count += dupe_cnt - 1
org_overview = {
"title": "",
"name": organization,
"number_datasets_duplicated": len(duplicates),
"total_duplicate_count": count,
"total_datasets": total,
"percent_duplicate": round(
float(count) / (total if total > 0 else 1) * 100, 2
),
}
org_log.add(org_overview)
if __name__ == "__main__":
run()