def generate_fixtures_for_domain(domain, db, data_source): location_ids = [location.get_id for location in Location.by_domain(domain)] dates = get_last_n_months(NUMBER_OF_MONTHS_FOR_FIXTURES) for date in dates: for location_id in location_ids: data_source.configure(config={ "startdate": date[0], "enddate": date[1], "location_id": location_id, "domain": domain }) report_data = data_source.get_data() for report_slug in report_data: rows = dict(report_data[report_slug].get("data", [])) name = report_data[report_slug].get("name") # Remove cached fixture docs db.delete_docs(FixtureReportResult.all_by_composite_key(domain, location_id, date[0].strftime("%Y-%m-%d"), date[1].strftime("%Y-%m-%d"), report_slug)) FixtureReportResult.save_result(domain, location_id, date[0].date(), date[1].date(), report_slug, rows, name)
def _facility_to_fixture(facility, startdate, enddate): facility_id = facility.get_id facility_element = ElementTree.Element('facility', attrib={ 'id': facility_id, 'name': _(facility.name) }) report_data = {} for report_slug in self.report_slugs: report_data[ report_slug] = FixtureReportResult.by_composite_key( self.domain.name, facility_id, startdate.strftime("%Y-%m-%d"), enddate.strftime("%Y-%m-%d"), report_slug) if report_data[report_slug] is None: name = self.reports[report_slug].name rows = self.reports[report_slug].get_initial_row_data() fixture_result = FixtureReportResult( domain=self.domain.name, location_id=facility_id, start_date=startdate, end_date=enddate, report_slug=report_slug, rows=rows, name=name) report_data[report_slug] = fixture_result facility_element = (_reports_to_fixture(report_data, facility_element)) return facility_element
def generate_fixtures_for_domain(domain, db, data_source): location_ids = SQLLocation.active_objects.filter(domain=domain).location_ids() dates = get_last_n_months(NUMBER_OF_MONTHS_FOR_FIXTURES) for date in dates: for location_id in location_ids: data_source.configure(config={ "startdate": date[0], "enddate": date[1], "location_id": location_id, "domain": domain }) report_data = data_source.get_data() for report_slug in report_data: rows = dict(report_data[report_slug].get("data", [])) name = report_data[report_slug].get("name") # Remove cached fixture docs db.delete_docs(FixtureReportResult.all_by_composite_key(domain, location_id, json_format_date(date[0]), json_format_date(date[1]), report_slug)) FixtureReportResult.save_result(domain, location_id, date[0].date(), date[1].date(), report_slug, rows, name)
def handle_fixture_update(sender, xform, cases, **kwargs): if hasattr(xform, "domain") and xform.domain == TEST_DOMAIN\ and hasattr(xform, "xmlns") and xform.xmlns in ALL_M4CHANGE_FORMS: db = FixtureReportResult.get_db() data_source = M4ChangeReportDataSource() date_range = get_last_month() location_id = get_user_by_id(xform.form['meta']['userID']).get_domain_membership(xform.domain).location_id results_for_last_month = FixtureReportResult.get_report_results_by_key(domain=xform.domain, location_id=location_id, start_date=date_range[0].strftime("%Y-%m-%d"), end_date=date_range[1].strftime("%Y-%m-%d")) db.delete_docs(results_for_last_month) data_source.configure(config={ "startdate": date_range[0], "enddate": date_range[1], "location_id": location_id, "domain": xform.domain }) report_data = data_source.get_data() for report_slug in report_data: rows = dict(report_data[report_slug].get("data", [])) name = report_data[report_slug].get("name") FixtureReportResult.save_result(xform.domain, location_id, date_range[0].date(), date_range[1].date(), report_slug, rows, name)
def _facility_to_fixture(facility, startdate, enddate): facility_id = facility.location_id facility_element = ElementTree.Element('facility', { 'id': facility_id, 'name': _(facility.name) }) report_data = {} m4change_data_source = M4ChangeReportDataSource() report_slugs = m4change_data_source.get_report_slugs() reports = dict((report.slug, report) for report in m4change_data_source.get_reports()) for report_slug in report_slugs: report_data[ report_slug] = FixtureReportResult.by_composite_key( restore_user.domain, facility_id, json_format_date(startdate), json_format_date(enddate), report_slug) if report_data[report_slug] is None: name = reports[report_slug].name rows = reports[report_slug].get_initial_row_data() fixture_result = FixtureReportResult( domain=restore_user.domain, location_id=facility_id, start_date=startdate, end_date=enddate, report_slug=report_slug, rows=rows, name=name) report_data[report_slug] = fixture_result facility_element = (_reports_to_fixture(report_data, facility_element)) return facility_element
def generate_fixtures_for_domain(domain, db, data_source): location_ids = [location.get_id for location in Location.by_domain(domain)] dates = get_last_n_months(NUMBER_OF_MONTHS_FOR_FIXTURES) for date in dates: for location_id in location_ids: data_source.configure( config={ "startdate": date[0], "enddate": date[1], "location_id": location_id, "domain": domain }) report_data = data_source.get_data() for report_slug in report_data: rows = dict(report_data[report_slug].get("data", [])) name = report_data[report_slug].get("name") # Remove cached fixture docs db.delete_docs( FixtureReportResult.all_by_composite_key( domain, location_id, date[0].strftime("%Y-%m-%d"), date[1].strftime("%Y-%m-%d"), report_slug)) FixtureReportResult.save_result(domain, location_id, date[0].date(), date[1].date(), report_slug, rows, name)
def generate_production_fixtures(): db = FixtureReportResult.get_db() data_source = M4ChangeReportDataSource() for domain in M4CHANGE_DOMAINS: generate_fixtures_for_domain(domain, db, data_source)
def _facility_to_fixture(facility, startdate, enddate): facility_id = facility.get_id facility_element = ElementTree.Element("facility", attrib={"id": facility_id, "name": _(facility.name)}) report_data = {} m4change_data_source = M4ChangeReportDataSource() report_slugs = m4change_data_source.get_report_slugs() reports = dict((report.slug, report) for report in m4change_data_source.get_reports()) for report_slug in report_slugs: report_data[report_slug] = FixtureReportResult.by_composite_key( domain.name, facility_id, json_format_date(startdate), json_format_date(enddate), report_slug ) if report_data[report_slug] is None: name = reports[report_slug].name rows = reports[report_slug].get_initial_row_data() fixture_result = FixtureReportResult( domain=domain.name, location_id=facility_id, start_date=startdate, end_date=enddate, report_slug=report_slug, rows=rows, name=name, ) report_data[report_slug] = fixture_result facility_element = _reports_to_fixture(report_data, facility_element) return facility_element
def _facility_to_fixture(facility, startdate, enddate): facility_id = facility.get_id facility_element = ElementTree.Element("facility", attrib={"id": facility_id, "name": _(facility.name)}) report_data = {} for report_slug in self.report_slugs: report_data[report_slug] = FixtureReportResult.by_composite_key( self.domain.name, facility_id, startdate.strftime("%Y-%m-%d"), enddate.strftime("%Y-%m-%d"), report_slug, ) if report_data[report_slug] is None: name = self.reports[report_slug].name rows = self.reports[report_slug].get_initial_row_data() fixture_result = FixtureReportResult( domain=self.domain.name, location_id=facility_id, start_date=startdate, end_date=enddate, report_slug=report_slug, rows=rows, name=name, ) report_data[report_slug] = fixture_result facility_element = _reports_to_fixture(report_data, facility_element) return facility_element
def generate_fixtures_for_locations(): client = get_redis_client() start_date, end_date = get_last_n_months(1)[0] db = FixtureReportResult.get_db() data_source = M4ChangeReportDataSource() for domain in M4CHANGE_DOMAINS: redis_key = REDIS_FIXTURE_KEYS[domain] redis_lock_key = REDIS_FIXTURE_LOCK_KEYS[domain] lock = client.lock(redis_lock_key, timeout=5) location_ids = [] if lock.acquire(blocking=True): try: location_ids_str = client.get(redis_key) location_ids = json.loads( location_ids_str if location_ids_str else "[]") client.set(redis_key, '[]') finally: release_lock(lock, True) for location_id in location_ids: data_source.configure( config={ "startdate": start_date, "enddate": end_date, "location_id": location_id, "domain": domain }) report_data = data_source.get_data() for report_slug in report_data: # Remove cached fixture docs db.delete_docs( FixtureReportResult.all_by_composite_key( domain, location_id, json_format_date(start_date), json_format_date(end_date), report_slug)) rows = dict(report_data[report_slug].get("data", [])) name = report_data[report_slug].get("name") FixtureReportResult.save_result(domain, location_id, start_date.date(), end_date.date(), report_slug, rows, name)
def generate_fixtures_for_locations(): client = get_redis_client() start_date, end_date = get_last_n_months(1)[0] db = FixtureReportResult.get_db() data_source = M4ChangeReportDataSource() for domain in M4CHANGE_DOMAINS: redis_key = REDIS_FIXTURE_KEYS[domain] redis_lock_key = REDIS_FIXTURE_LOCK_KEYS[domain] lock = client.lock(redis_lock_key, timeout=5) location_ids = [] if lock.acquire(blocking=True): try: location_ids_str = client.get(redis_key) location_ids = json.loads(location_ids_str if location_ids_str else "[]") client.set(redis_key, '[]') finally: release_lock(lock, True) for location_id in location_ids: data_source.configure(config={ "startdate": start_date, "enddate": end_date, "location_id": location_id, "domain": domain }) report_data = data_source.get_data() for report_slug in report_data: # Remove cached fixture docs db.delete_docs( FixtureReportResult.all_by_composite_key( domain, location_id, json_format_date(start_date), json_format_date(end_date), report_slug) ) rows = dict(report_data[report_slug].get("data", [])) name = report_data[report_slug].get("name") FixtureReportResult.save_result(domain, location_id, start_date.date(), end_date.date(), report_slug, rows, name)
def generate_fixtures_for_domain(domain, db, data_source): # Remove all FixtureReportResult instances, as they would either be deleted or replaced anyway db.delete_docs(FixtureReportResult.by_domain(domain=domain)) location_ids = [location.get_id for location in Location.by_domain(domain)] dates = get_last_n_months(NUMBER_OF_MONTHS_FOR_FIXTURES) for date in dates: for location_id in location_ids: data_source.configure(config={ "startdate": date[0], "enddate": date[1], "location_id": location_id, "domain": domain }) report_data = data_source.get_data() for report_slug in report_data: rows = dict(report_data[report_slug].get("data", [])) name = report_data[report_slug].get("name") FixtureReportResult.save_result(domain, location_id, date[0].date(), date[1].date(), report_slug, rows, name)
def _facility_to_fixture(facility, startdate, enddate): facility_id = facility.get_id facility_element = ElementTree.Element('facility', attrib={ 'id': facility_id, 'name': _(facility.name) }) report_data = {} for report_slug in self.report_slugs: report_data[report_slug] = FixtureReportResult.by_composite_key( self.domain.name, facility_id, json_format_date(startdate), json_format_date(enddate), report_slug) if report_data[report_slug] is None: name = self.reports[report_slug].name rows = self.reports[report_slug].get_initial_row_data() fixture_result = FixtureReportResult(domain=self.domain.name, location_id=facility_id, start_date=startdate, end_date=enddate, report_slug=report_slug, rows=rows, name=name) report_data[report_slug] = fixture_result facility_element = (_reports_to_fixture(report_data, facility_element)) return facility_element