예제 #1
0
 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
예제 #2
0
def generate_production_fixtures():

    db = FixtureReportResult.get_db()
    data_source = M4ChangeReportDataSource()

    for domain in M4CHANGE_DOMAINS:
        generate_fixtures_for_domain(domain, db, data_source)
예제 #3
0
 def __init__(self, id, user, domain, location_id):
     self.id = id
     self.user = user
     self.dates = get_last_n_months(NUMBER_OF_MONTHS_FOR_FIXTURES)
     self.domain = domain
     self.location_id = location_id
     m4change_data_source = M4ChangeReportDataSource()
     self.report_slugs = m4change_data_source.get_report_slugs()
     self.reports = dict((report.slug, report)
                         for report in m4change_data_source.get_reports())
예제 #4
0
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)