def _updateOrganisationFrequency(fh): def get_frequency(): d = unicodecsv.DictReader(fh) packagegroups = d for packagegroup in packagegroups: freq = packagegroup["Frequency"] if freq == "Monthly": frequency = "monthly" comment = "Updated monthly" elif freq == "Quarterly": frequency = "quarterly" comment = "Updated quarterly" else: frequency = "less than quarterly" comment = "Updated less than quarterly" yield packagegroup["Publisher Name"], frequency, comment for packagegroup, frequency, comment in get_frequency(): organisations = dqpackages.packageGroupOrganisations(packagegroup) if not organisations: continue for organisation in organisations: with db.session.begin(): organisation.frequency=frequency organisation.frequency_comment=comment db.session.add(organisation)
def _updateOrganisationFrequency(fh): def get_frequency(): d = fh.read() packagegroups = json.loads(d)["data"] for packagegroup in packagegroups: freq = packagegroup["frequency_code"] if freq==1: frequency = "monthly" comment = packagegroup["frequency_comment"] elif freq ==2: frequency = "quarterly" comment = packagegroup["frequency_comment"] else: frequency = "less than quarterly" comment = packagegroup["frequency_comment"] yield packagegroup["publisher"], frequency, comment for packagegroup, frequency, comment in get_frequency(): organisations = dqpackages.packageGroupOrganisations(packagegroup) if not organisations: continue for organisation in organisations: with db.session.begin(): organisation.frequency=frequency organisation.frequency_comment=comment db.session.add(organisation)
def _updateOrganisationFrequency(fh): def check_data_last_four_months(packagegroup_name, packagegroups): fourmonths_ago = (datetime.datetime.utcnow()-datetime.timedelta(days=4*30)).date() lastfourmonth_dates = filter(lambda d: d>fourmonths_ago, packagegroups[packagegroup_name]) return len(lastfourmonth_dates) def check_data_avg_months_to_publication(packagegroup_name, packagegroups): earliest_date = min(packagegroups[packagegroup_name]) earliest_date_days_ago=(datetime.datetime.utcnow().date()-earliest_date).days number_months_changes = len(packagegroups[packagegroup_name]) avg_days_per_change = earliest_date_days_ago/number_months_changes return avg_days_per_change def generate_data(): data = unicodecsv.DictReader(fh) packagegroups = {} for row in data: try: packagegroups[row['packagegroup_name']].append((datetime.date(year=int(row['year']), month=int(row['month']), day=1))) except KeyError: packagegroups[row['packagegroup_name']] = [] packagegroups[row['packagegroup_name']].append((datetime.date(year=int(row['year']), month=int(row['month']), day=1))) return packagegroups def get_frequency(): packagegroups = generate_data() for packagegroup in sorted(packagegroups.keys()): lastfour = check_data_last_four_months(packagegroup, packagegroups) avgmonths = check_data_avg_months_to_publication(packagegroup, packagegroups) if lastfour >=3: frequency = "monthly" comment = "Updated " + str(lastfour) + " times in the last 4 months" elif avgmonths<31: frequency = "monthly" comment = "Updated on average every " + str(avgmonths) + " days" elif avgmonths<93: frequency = "quarterly" comment = "Updated on average every " + str(avgmonths) + " days" else: frequency = "less than quarterly" comment = "Updated on average every " + str(avgmonths) + " days" yield packagegroup, frequency, comment for packagegroup, frequency, comment in get_frequency(): organisations = dqpackages.packageGroupOrganisations(packagegroup) if not organisations: continue for organisation in organisations: with db.session.begin(): organisation.frequency=frequency organisation.frequency_comment=comment db.session.add(organisation)