Example #1
0
def registry_init():
    registry = registry_import()

    for station in registry:
        station_dict = station.dict(exclude={"id"})
        # pprint(station_dict)

        # pylint:disable no-member
        station_model = fromdict(Station(), station_dict)
        station_model.approved = True
        station_model.approved_at = datetime.now()
        station_model.approved_by = "opennem.registry"
        station_model.created_by = "opennem.registry"

        # location
        station_model.location = fromdict(
            Location(),
            station_dict["location"],
            exclude=["id"],
        )

        if station.location.lat and station.location.lng:
            station_model.location.geom = "SRID=4326;POINT({} {})".format(
                station.location.lng, station.location.lat)

        for fac in station.facilities:
            f = Facility(**fac.dict(
                exclude={
                    "id",
                    "fueltech",
                    "status",
                    "network",
                    "revision_ids",
                    "scada_power",
                }))

            f.network_id = fac.network.code

            if fac.fueltech:
                f.fueltech_id = fac.fueltech.code

            f.status_id = fac.status.code

            f.approved = True
            f.approved_by = "opennem.registry"
            f.created_by = "opennem.registry"
            f.approved_at = datetime.now()
            f.created_at = datetime.now()

            station_model.facilities.append(f)

        s.add(station_model)

    s.commit()
Example #2
0
def registry_init() -> None:
    registry = registry_import()
    session = SessionLocal()

    for station in registry:
        station_dict = station.dict(exclude={"id"})
        # pprint(station_dict)

        station_model = session.query(Station).filter_by(
            code=station.code).one_or_none()

        if not station_model:
            # pylint:disable no-member
            station_model = fromdict(Station(), station_dict)
            station_model.approved = True
            station_model.approved_at = datetime.now()
            station_model.approved_by = "opennem.registry"
            station_model.created_by = "opennem.registry"

        # location
        station_model.location = fromdict(
            Location(),
            station_dict["location"],
            exclude=["id"],
        )

        if station.location.lat and station.location.lng:
            station_model.location.geom = "SRID=4326;POINT({} {})".format(
                station.location.lng, station.location.lat)

        session.add(station_model)
        session.commit()

        for fac in station.facilities:
            f = (session.query(Facility).filter_by(code=fac.code).filter_by(
                network_id=fac.network.code).one_or_none())

            if not f:
                print("new facility {} {}".format(fac.code, fac.network.code))
                f = Facility(**fac.dict(
                    exclude={
                        "id",
                        "fueltech",
                        "status",
                        "network",
                        "revision_ids",
                        "scada_power",
                    }))
                f.approved_by = "opennem.registry"
                f.created_by = "opennem.registry"
                f.approved_at = datetime.now()
                f.created_at = datetime.now()

            f.network_id = fac.network.code

            if fac.fueltech:
                f.fueltech_id = fac.fueltech.code

            if fac.network.code:
                f.network_code = fac.network.code

            f.status_id = fac.status.code

            f.approved = True

            if station_model.id:
                f.station_id = station_model.id
            else:
                station_model.facilities.append(f)
                session.add(station_model)

            session.add(f)

        try:
            session.commit()
        except IntegrityError as e:
            logger.error(e)
Example #3
0
def load_revision(records, created_by):
    logger.info("Running db test")

    for station_record in records:
        station_model = s.query(Station).filter(
            Station.code == station_record.code).one_or_none()

        if not station_model:
            logger.info(
                f"New station {station_record.name} {station_record.code}")

            station_dict = station_record.dict(
                include={"code", "network_name", "name", "location"})
            # pprint(station_dict)

            # pylint:disable no-member
            station_model = fromdict(Station(), station_dict)
            station_model.approved = False
            station_model.created_by = created_by

            # location
            if "location" in station_dict and station_dict["location"]:
                station_model.location = fromdict(Location(),
                                                  station_dict["location"],
                                                  exclude=["id"])

                if station_record.location.lat and station_record.location.lng:
                    station_model.location.geom = station_record.location.geom

            s.add(station_model)
            s.commit()

            station_record.id = station_model.id

            # revision = revision_factory(
            #     station_record, ["code", "name", "network_name"], created_by,
            # )

            # if revision:
            #     station_model.revisions.append(revision)

            # s.add(station_model)
            # s.commit()

        else:
            for field in ["name"]:
                if getattr(station_model, field) != getattr(
                        station_record, field):
                    revision_factory(station_record, field, created_by)

        for facility in station_record.facilities:
            facility_model = s.query(Facility).filter(
                Facility.code == facility.code).first()

            if not facility_model:
                logger.info("New facility %s => %s", station_record.name,
                            facility.code)

                facility_dict = facility.dict(
                    include={
                        "code",
                        "network",
                        # "network_id",
                        "dispatch_type",
                        "station",
                        # "station_id",
                        # "status",
                        "network_code",
                        "network_region",
                        "network_name",
                    })
                # pprint(station_dict)

                # pylint:disable no-member
                facility_model = fromdict(Facility(), facility_dict)
                facility_model.network_id = facility.network.code
                facility_model.approved = False
                facility_model.created_by = created_by

                s.add(facility_model)
                s.commit()

                facility.id = facility_model.id

                # @NOTE don't create revisions for new facilities
                # revision = revision_factory(
                #     facility, ["code", "dispatch_type"], created_by
                # )

                # if revision:
                #     facility_model.revisions.append(revision)

                # s.add(facility_model)
                # s.commit()

            else:
                facility.id = facility_model.id

            for field in [
                    "fueltech",
                    "status",
                    "capacity_registered",
            ]:
                revision = None

                if compare_record_differs(facility_model, facility, field):
                    # logger.info(
                    #     "%s and %s differ",
                    #     getattr(facility, field),
                    #     getattr(facility_model, field),
                    # )

                    revision = revision_factory(facility, field, created_by)

                if revision:
                    facility_model.revisions.append(revision)

            s.add(facility_model)

            station_model.facilities.append(facility_model)
            s.add(station_model)
            s.commit()