def add_news_flash_to_cache(news_flash: NewsFlash):
    try:
        if not (news_flash.accident and anyway.infographics_utils.
                is_news_flash_resolution_supported(news_flash)):
            logging.debug(
                f"add_news_flash_to_cache: news flash does not qualify:{news_flash.serialize()}"
            )
            return True
        db.get_engine().execute(
            InfographicsDataCache.__table__.insert(),  # pylint: disable=no-member
            [{
                "news_flash_id":
                news_flash.get_id(),
                "years_ago":
                y,
                "data":
                anyway.infographics_utils.create_infographics_data(
                    news_flash.get_id(), y, "he"),
            } for y in CONST.INFOGRAPHICS_CACHE_YEARS_AGO],
        )
        logging.info(f"{news_flash.get_id()} added to cache")
        return True
    except Exception as e:
        logging.exception(
            f"Exception while inserting to cache. flash_id:{news_flash}), cause:{e.__cause__}"
        )
        return False
def copy_temp_into_cache():
    num_items_cache = db.session.query(InfographicsDataCache).count()
    num_items_temp = db.session.query(InfographicsDataCacheTemp).count()
    logging.debug(
        f"num items in cache: {num_items_cache}, temp:{num_items_temp}")
    db.session.commit()
    start = datetime.now()
    with db.get_engine().begin() as conn:
        conn.execute("lock table infographics_data_cache in exclusive mode")
        logging.debug(f"in transaction, after lock")
        conn.execute("delete from infographics_data_cache")
        logging.debug(f"in transaction, after delete")
        conn.execute(
            "insert into infographics_data_cache SELECT * from infographics_data_cache_temp"
        )
        logging.debug(f"in transaction, after insert into")
    logging.info(f"cache unavailable time: {str(datetime.now() - start)}")
    num_items_cache = db.session.query(InfographicsDataCache).count()
    num_items_temp = db.session.query(InfographicsDataCacheTemp).count()
    logging.debug(
        f"num items in cache: {num_items_cache}, temp:{num_items_temp}")
    db.session.execute("truncate table infographics_data_cache_temp")
    db.session.commit()
    num_items_cache = db.session.query(InfographicsDataCache).count()
    num_items_temp = db.session.query(InfographicsDataCacheTemp).count()
    logging.debug(
        f"num items in cache: {num_items_cache}, temp:{num_items_temp}")
    db.session.commit()
Example #3
0
def build_cache_into_temp():
    start = datetime.now()
    db.session.query(InfographicsDataCacheTemp).delete()
    db.session.commit()
    for y in CONST.INFOGRAPHICS_CACHE_YEARS_AGO:
        logging.debug(f"processing years_ago:{y}")
        db.get_engine().execute(
            InfographicsDataCacheTemp.__table__.insert(),  # pylint: disable=no-member
            [{
                "news_flash_id":
                new_flash.get_id(),
                "years_ago":
                y,
                "data":
                anyway.infographics_utils.create_infographics_data(
                    new_flash.get_id(), y, "he"),
            } for new_flash in db.session.query(NewsFlash).filter(
                NewsFlash.accident).filter(
                    NewsFlash.resolution.in_(["כביש בינעירוני"])).filter(
                        not_(NewsFlash.road_segment_name == None)).all()],
        )
    logging.info(f"cache rebuild took:{str(datetime.now() - start)}")
Example #4
0
def create_tables():
    with db.get_engine().begin() as conn:
        conn.execute("TRUNCATE involved_markers_hebrew")
        conn.execute("TRUNCATE vehicles_markers_hebrew")
        conn.execute("TRUNCATE vehicles_hebrew")
        conn.execute("TRUNCATE involved_hebrew")
        conn.execute("TRUNCATE markers_hebrew")
        conn.execute("INSERT INTO markers_hebrew " + VIEWS.MARKERS_HEBREW_VIEW)
        conn.execute("INSERT INTO involved_hebrew " + VIEWS.INVOLVED_HEBREW_VIEW)
        conn.execute("INSERT INTO vehicles_hebrew " + VIEWS.VEHICLES_HEBREW_VIEW)
        conn.execute("INSERT INTO vehicles_markers_hebrew " + VIEWS.VEHICLES_MARKERS_HEBREW_VIEW)
        conn.execute("INSERT INTO involved_markers_hebrew " + VIEWS.INVOLVED_HEBREW_MARKERS_HEBREW_VIEW)
        logging.info("Created DB Hebrew Tables")
def build_cache_into_temp():
    start = datetime.now()
    db.session.query(InfographicsDataCacheTemp).delete()
    db.session.commit()
    supported_resolutions = set(
        [x.value for x in BE_CONST.SUPPORTED_RESOLUTIONS])
    for y in CONST.INFOGRAPHICS_CACHE_YEARS_AGO:
        logging.debug(f"processing years_ago:{y}")
        db.get_engine().execute(
            InfographicsDataCacheTemp.__table__.insert(),  # pylint: disable=no-member
            [{
                "news_flash_id":
                new_flash.get_id(),
                "years_ago":
                y,
                "data":
                anyway.infographics_utils.create_infographics_data(
                    new_flash.get_id(), y, "he"),
            } for new_flash in db.session.query(NewsFlash).filter(
                NewsFlash.accident).filter(
                    NewsFlash.resolution.in_(supported_resolutions)).all()],
        )
    logging.info(f"cache rebuild took:{str(datetime.now() - start)}")
Example #6
0
 def setUp(self) -> None:
     self.connection = db.get_engine().connect()
     # begin a non-ORM transaction
     self.trans = self.connection.begin()
     # bind an individual Session to the connection
     self.session = Session(bind=self.connection)
     # add data
     self.region_description = 'test_region_description'
     nf_region = NewsFlash(road1=12345678,
                           description=self.region_description,
                           accident=True,
                           resolution="מחוז",
                           lat=32.0192988,
                           lon=34.7971384)
     self.session.add(nf_region)
     self.district_description = 'test_district_description'
     nf_district = NewsFlash(road1=12345678,
                             description=self.district_description,
                             accident=True,
                             resolution="נפה",
                             lat=32.0192988,
                             lon=34.7971384)
     self.session.add(nf_district)
     self.session.commit()