コード例 #1
0
ファイル: random_cisu_repository.py プロジェクト: Bbemol/enki
 def build(self) -> AffairEntity:
     return AffairEntity(
         eventId=UidFactory().build(),
         createdAt=self.clock_seed.generate(),
         severity=Severity.random(),
         eventLocation=LocationTypeFactory().build(),
         primaryAlert=PrimaryAlertFactory().build(),
         otherAlert=[],
     )
コード例 #2
0
 def _add(self, affair: AffairEntity) -> bool:
     return self.client.index(
         index=self.index_name,
         id=affair.uuid,
         body=json.dumps(
             affair.to_dict(),
             cls=EnkiJsonEncoder,
         ),
         refresh=True,
     )
コード例 #3
0
ファイル: affair_service.py プロジェクト: Bbemol/enki
 def build_affair_from_xml_file(xml_path: str) -> AffairEntity:
     affair_dom = xml.dom.minidom.parse(xml_path)
     edxl_message = EdxlEntity.from_xml(affair_dom)
     return AffairEntity(**edxl_message.resource.message.choice.to_dict())
コード例 #4
0
client = Elasticsearch([f"{ELASTIC_HOST}:{ELASTIC_PORT}"],
                       http_auth=(ELASTIC_USERNAME, ELASTIC_PASSWORD))

print(f"Ping client : {client.ping()}")

with open("templates/affairs.json") as f:
    affair_mapping = json.load(f)

response = client.indices.create(
    index=AFFAIRS_INDEX_NAME,
    body=affair_mapping,
    ignore=400  # ignore 400 already exists code
)

affairs = [
    AffairEntity(**edxl.resource.message.choice.to_dict())
    for edxl in [build_edxl_from_record(record) for record in tqdm(records)]
    if edxl
]


def chunks(lst, n):
    for i in range(0, len(lst), n):
        yield lst[i:i + n]


for chunk in chunks(affairs, 5000):
    actions = [{
        "_id":
        affair["eventId"],
        "_index":
コード例 #5
0
 def _map_es_results_with_affairs(results: dict):
     return [
         AffairEntity(**hit["_source"]) for hit in results['hits']['hits']
     ]
コード例 #6
0
 def _match_uuid(self, uuid: str) -> Union[AffairEntity, None]:
     try:
         return AffairEntity(
             **self.client.get(index=self.index_name, id=uuid)["_source"])
     except NotFoundError as e:
         return None
コード例 #7
0
def create_affairs(
    number: int,
    event_type: EvenementType,
    insee_code: str = None,
    dept_code: str = None,
):

    uow = current_app.context

    communes_path = dir_path + "/data/communes-version-simplifiee.geojson"
    dept_path = dir_path + "/data/departements-version-simplifiee.geojson"

    if insee_code:
        with open(communes_path) as f:
            data = json.load(f)["features"]
        code = insee_code
    elif dept_code:
        with open(dept_path) as f:
            data = json.load(f)["features"]
        code = dept_code

    else:
        code = "77"
        with open(dept_path) as f:
            data = json.load(f)["features"]

    selected_shape = [
        feature for feature in data if feature["properties"]["code"] == code
    ][0]
    polygon = Polygon(selected_shape["geometry"]["coordinates"][0])
    points: List[Point] = generate_random(number=number, polygon=polygon)
    whats_happen_codes = [
        random.choice(EVENEMENT_TYPE_TO_CISU_CODE.get(event_type))
        for _ in range(len(points))
    ]
    affairs = []
    for point, whatsHappenCode in zip(points, whats_happen_codes):
        affair = AffairEntity(
            eventId=UidFactory().build(),
            createdAt=datetime.now(),
            severity=Severity.random(),
            eventLocation=LocationType(name="ok",
                                       address=["test"],
                                       coord=CoordType(point.y,
                                                       point.x,
                                                       height=0),
                                       type=LocationShape.POINT),
            primaryAlert=PrimaryAlertEntity(
                alertId=AlertId(UidFactory().build()),
                receivedAt=datetime.utcnow(),
                reporting=Reporting.random(),
                alertInformation="C'est une information concernant l'alerte",
                alertLocation=LocationTypeFactory().build(),
                call=CallFactory().build(),
                caller=CallerFactory().build(),
                callTaker=CallTakerFactory().build(),
                resource=[],
                alertCode=AlertCode(
                    version=Version("latest"),
                    whatsHappen=WhatsHappenConstants().get_by_code(
                        whatsHappenCode),
                    locationKind=LocationKindFactory().build(),
                    riskThreat=RiskThreatFactory().build(),
                    healthMotive=HealthMotiveFactory().build(),
                    victims=VictimsFactory().build(),
                ),
                primary=True),
            otherAlert=[],
        )
        simple_affair_entity: SimpleAffairEntity = SimpleAffairEntity(
            uuid=str(uuid4()), sge_hub_id=affair.uuid, default_affair=affair)
        affairs.append(simple_affair_entity)
    with uow:
        uow.session.add_all(affairs)