예제 #1
0
    def test_cache_document(self) -> None:
        """Fetch a document only once if it exists."""

        self._db.collection.insert_one({
            '_id': 'id',
            'name': 'document',
        })
        fetched = proto.fetch_from_mongo(self._db, test_pb2.Simple,
                                         'collection', 'id')
        assert fetched
        self._db.collection.replace_one({}, {'name': 'updated document'})
        refetched = proto.fetch_from_mongo(self._db, test_pb2.Simple,
                                           'collection', 'id')
        self.assertEqual(fetched, refetched)
예제 #2
0
def get_local_stats(database: mongo.NoPiiMongoDatabase, departement_id: str, rome_id: str) \
        -> job_pb2.LocalJobStats:
    """Get a LocalJobStats proto corresponding to the local ID (departement + ROME)."""

    if not departement_id or not rome_id:
        return job_pb2.LocalJobStats()
    local_id = f'{departement_id}:{rome_id}'
    local_stats = proto.fetch_from_mongo(database, job_pb2.LocalJobStats,
                                         'local_diagnosis',
                                         local_id) or job_pb2.LocalJobStats()
    recent_job_offers = proto.fetch_from_mongo(
        database, job_pb2.LocalJobStats, 'recent_job_offers',
        local_id) or job_pb2.LocalJobStats()
    local_stats.MergeFrom(recent_job_offers)
    return local_stats
예제 #3
0
    def test_cache_can_be_cleared(self) -> None:
        """Cache can be cleared."""

        self._db.collection.insert_one({
            '_id': 'id',
            'name': 'document',
        })
        fetched = proto.fetch_from_mongo(self._db, test_pb2.Simple,
                                         'collection', 'id')
        assert fetched
        self._db.collection.replace_one({}, {'name': 'updated document'})
        proto.cache.clear()
        refetched = proto.fetch_from_mongo(self._db, test_pb2.Simple,
                                           'collection', 'id')
        assert refetched
        self.assertEqual('updated document', refetched.name)
예제 #4
0
def _get_urban_context(city_id: str) -> Optional[str]:
    target_city = proto.fetch_from_mongo(_DB, geo_pb2.FrenchCity, 'cities',
                                         city_id)
    if target_city:
        urban_context = target_city.urban_context
        return f'{urban_context:d} - {geo_pb2.UrbanContext.Name(urban_context)}'
    return None
예제 #5
0
파일: imt.py 프로젝트: b3rday/bob-emploi
def _get_best_departements_for_job_group(
        rome_id: str, database: pymongo.database.Database) -> List[str]:
    """Get departements with best market stress for a job group."""

    best_departements = (proto.fetch_from_mongo(database, job_pb2.JobGroup,
                                                'job_group_info', rome_id)
                         or job_pb2.JobGroup()).best_departements[:2]
    return [dep.departement_id for dep in best_departements]
def _get_urban_context(stats_db: mongo.NoPiiMongoDatabase,
                       city_id: str) -> Optional[str]:
    target_city = proto.fetch_from_mongo(stats_db, geo_pb2.FrenchCity,
                                         'cities', city_id)
    if target_city:
        urban_context = target_city.urban_context
        return f'{urban_context:d} - {geo_pb2.UrbanContext.Name(urban_context)}'
    return None
예제 #7
0
    def test_cache_has_ttl(self, mock_now: mock.MagicMock) -> None:
        """Cached document has only a short life."""

        mock_now.return_value = datetime.datetime(2018, 12, 19, 12)
        self._db.collection.insert_one({
            '_id': 'id',
            'name': 'document',
        })
        fetched = proto.fetch_from_mongo(self._db, test_pb2.Simple,
                                         'collection', 'id')
        assert fetched
        self._db.collection.replace_one({}, {'name': 'updated document'})
        mock_now.return_value = datetime.datetime(2018, 12, 20)
        refetched = proto.fetch_from_mongo(self._db, test_pb2.Simple,
                                           'collection', 'id')
        assert refetched
        self.assertEqual('updated document', refetched.name)
예제 #8
0
def get_city_location(database: mongo.NoPiiMongoDatabase, city_id: str) \
        -> Optional[geo_pb2.FrenchCity]:
    """Get lat/long coordinates for a city from its ID."""

    fetched = proto.fetch_from_mongo(database, geo_pb2.FrenchCity, 'cities',
                                     city_id)
    if fetched:
        fetched.city_id = city_id
    return fetched
def _get_job_domain(stats_db: mongo.NoPiiMongoDatabase,
                    job: job_pb2.Job) -> str:
    target_job = proto.fetch_from_mongo(stats_db, job_pb2.JobGroup,
                                        'job_group_info',
                                        job.job_group.rome_id)
    if target_job and target_job.domain:
        return target_job.domain
    rome_first_letter = job.job_group.rome_id[:1]
    return _ROME_DOMAINS.get(rome_first_letter, 'Unknown')
예제 #10
0
    def test_cache_is_finite(self) -> None:
        """Only the last cached values are kept."""

        self._db.collection.insert_many([{
            '_id': str(i),
            'name': f'{i}th document',
        } for i in range(1000)])
        for i in range(1000):
            proto.fetch_from_mongo(self._db, test_pb2.Simple, 'collection',
                                   str(i))
        self._db.collection.drop()
        self._db.collection.insert_one({
            '_id': '0',
            'name': 'updated 0th document'
        })
        refetched = proto.fetch_from_mongo(self._db, test_pb2.Simple,
                                           'collection', '0')
        assert refetched
        self.assertEqual('updated 0th document', refetched.name)
예제 #11
0
    def test_fetch_document(self) -> None:
        """Fetches a document if it exists."""

        self._db.collection.insert_one({
            '_id': 'id',
            'name': 'document',
        })
        fetched = proto.fetch_from_mongo(self._db, test_pb2.Simple,
                                         'collection', 'id')
        assert fetched
        self.assertEqual('document', fetched.name)
예제 #12
0
    def list_nearby_cities(self, project: scoring_base.ScoringProject) \
            -> List[commute_pb2.CommutingCity]:
        """Compute and store all interesting cities that are not too close and not too far.

        Those cities will be used by the Commute advice.
        """

        job_group = project.details.target_job.job_group.rome_id

        interesting_cities_for_rome = (proto.fetch_from_mongo(
            project.database, commute_pb2.HiringCities, 'hiring_cities',
            job_group) or commute_pb2.HiringCities()).hiring_cities

        if not interesting_cities_for_rome:
            return []

        target_city = geo.get_city_location(project.database,
                                            project.details.city.city_id)
        if not target_city:
            return []

        commuting_cities = _get_commuting_cities(interesting_cities_for_rome,
                                                 target_city)
        sorted_commuting_cities = sorted(
            commuting_cities,
            key=lambda city: city.relative_offers_per_inhabitant,
            reverse=True)

        obvious_cities = [
            city for city in sorted_commuting_cities
            if city.distance_km < _MIN_CITY_DISTANCE
        ]

        interesting_cities = [
            city for city in sorted_commuting_cities
            if city.distance_km >= _MIN_CITY_DISTANCE
        ]

        # If there is only one city nearby and no obvious city, the nearby city becomes obvious, so
        # we do not recommend it.
        if len(interesting_cities) == 1 and not obvious_cities:
            return []

        return interesting_cities
예제 #13
0
def get_users_counts(
        database: mongo.NoPiiMongoDatabase) -> Optional[stats_pb2.UsersCount]:
    """Get the count of users in departements and in job groups."""

    return proto.fetch_from_mongo(database, stats_pb2.UsersCount, 'user_count',
                                  '')
예제 #14
0
    def test_missing_doc(self) -> None:
        """Does not fetch anything if document does not exist."""

        fetched = proto.fetch_from_mongo(self._db, test_pb2.Simple, 'unknown',
                                         'any_id')
        self.assertFalse(fetched)