Ejemplo n.º 1
0
    def get_workers(self, start_day=None):
        #        return self.load_workers_original(start_day)
        # def load_workers_original(self,start_day = '20191101'):
        db_session = self.cnx
        try:
            log.info(f"testing team_id = {self.team_id}")
            team = team_service.get(db_session=db_session,
                                    team_id=self.team_id)
            if team is None:
                log.error(f"team_id={self.team_id} is invalid")
        except Exception as e:
            print(e)
            log.error(f"team_id={self.team_id} is invalid")

        k_workers = pd.read_sql(
            db_session.query(Worker, Location.geo_latitude,
                             Location.geo_longitude, Location.location_code)
            # .filter(Location.id == Worker.location_id)
            .outerjoin(Location, Worker.location_id == Location.id).filter(
                Worker.team_id == self.team_id).statement,
            db_session.bind,
        )
        # k_workers = db_session.query(Worker).filter(Worker.team_id == self.team_id).all()
        mean_long = (team.flex_form_data.get("geo_longitude_max", 0) +
                     team.flex_form_data.get("geo_longitude_min", 0)) / 2
        mean_lat = (team.flex_form_data.get("geo_latitude_max", 0) +
                    team.flex_form_data.get("geo_latitude_min", 0)) / 2
        k_workers['geo_longitude'].fillna(mean_long, inplace=True)
        k_workers['geo_latitude'].fillna(mean_lat, inplace=True)
        k_workers['location_code'].fillna("central_loc", inplace=True)

        # TODO, fillna.

        return k_workers
Ejemplo n.º 2
0
    def set_team_env_window_latest_offset(self, offset=0):
        team = team_service.get(db_session=self.db_session,
                                team_id=self.team_id)
        team.latest_env_kafka_offset = offset
        self.db_session.add(team)
        self.db_session.commit()

        return True
Ejemplo n.º 3
0
    def __init__(self, team_id=None):
        # Create the database connection.
        self.db_session = SessionLocal()
        self.cnx = self.db_session

        if team_id is None:
            # TODO In future, I get default from db
            raise Exception("internal error: team_id can not be None")
        else:
            self.team_id = team_id
            team = team_service.get(db_session=self.db_session,
                                    team_id=self.team_id)

            if team is None:
                raise Exception("team_id is invalid")
Ejemplo n.º 4
0
    def get_workers(self, start_day=None):
        #        return self.load_workers_original(start_day)
        # def load_workers_original(self,start_day = '20191101'):
        db_session = self.cnx
        try:
            log.info(f"testing team_id = {self.team_id}")
            team = team_service.get(db_session=db_session,
                                    team_id=self.team_id)
            if team is None:
                log.error(f"team_id={self.team_id} is invalid")
        except Exception as e:
            print(e)
            log.error(f"team_id={self.team_id} is invalid")

        k_workers = pd.read_sql(
            db_session.query(Worker).filter(
                Worker.team_id == self.team_id).statement,
            db_session.bind,
        )

        return k_workers
Ejemplo n.º 5
0
    def get_worker_absence(self):
        #        return self.load_workers_original(start_day)
        # def load_workers_original(self,start_day = '20191101'):

        db_session = self.cnx
        team = team_service.get(db_session=db_session, team_id=self.team_id)

        if team is None:
            log.error(f"team_id={self.team_id} is invalid")

        absence_df = pd.read_sql(
            db_session.query(
                WorkerAbsence,
                Worker.code,
            ).filter(
                Worker.id == WorkerAbsence.scheduled_primary_worker_id).filter(
                    Worker.team_id == self.team_id).filter(
                        WorkerAbsence.scheduled_primary_worker_id ==
                        Worker.id).statement,
            db_session.bind,
        )

        return absence_df
Ejemplo n.º 6
0
    def __init__(self, org_code=None, team_id=None):
        # Create the database connection.
        self.db_session = get_schema_session(org_code)
        self.cnx = self.db_session
        self.db_schema = f"dispatch_organization_{org_code}"
        self.org_id = None

        if team_id is None:
            # TODO In future, I get default from db
            raise HTTPException(
                status_code=400,
                detail=
                f"internal error: team_id can not be None, or is not in your organization.",
            )
        team = team_service.get(db_session=self.db_session, team_id=team_id)
        if team is None:
            raise HTTPException(
                status_code=400,
                detail=f"The team is not found in your organization.",
            )
        org = org_service.get(db_session=self.db_session, org_code=org_code)
        if (org is None) or (org.id != team.org_id):
            raise HTTPException(
                status_code=400,
                detail=f"The team is not in your organization ( { org_code}).",
            )

        self.team = team
        self.team_id = team_id
        self.team_code = team.code
        self.org_code = org_code
        self.org_id = org.id

        self.workers_dict_by_id = {}  # Dictionary of dict
        self.workers_by_code_dict = {}  # Dictionary of dict
        self.workers_db_dict = {}
        self.jobs_db_dict = {}
Ejemplo n.º 7
0
def delete_worker(*,
                  db_session: Session = Depends(get_db),
                  worker_id: int,
                  current_user: DispatchUser = Depends(get_current_user)):
    """
    Delete a worker contact.
    """
    worker = get(db_session=db_session, worker_id=worker_id)
    if not worker:
        raise HTTPException(status_code=404,
                            detail="The worker with this id does not exist.")

    delete(db_session=db_session, worker_id=worker_id)
    # use zulip
    zulip_dict = get_zulip_client_by_org_id(current_user.org_id)
    if zulip_dict:
        zulip_core = zulip_dict['client']
        team = team_service.get(db_session=db_session, team_id=worker.team_id)
        worker.team = team
        zulip_core.update_add_subscribe_user_group(
            worker,
            org_code=current_user.org_code,
            team_code=worker.team.code,
            flag=False)
Ejemplo n.º 8
0
 def get_team_flex_form_data(self):
     team = team_service.get(db_session=self.db_session,
                             team_id=self.team_id)
     return team.flex_form_data
Ejemplo n.º 9
0
 def get_team_env_window_latest_offset(self):
     team = team_service.get(db_session=self.db_session,
                             team_id=self.team_id)
     return team.latest_env_kafka_offset
Ejemplo n.º 10
0
def test_delete(session, team_contact):
    from dispatch.team.service import delete, get

    delete(db_session=session, team_contact_id=team_contact.id)
    assert not get(db_session=session, team_contact_id=team_contact.id)
Ejemplo n.º 11
0
def test_get(session, team_contact):
    from dispatch.team.service import get

    t_team = get(db_session=session, team_contact_id=team_contact.id)
    assert t_team.id == team_contact.id