예제 #1
0
    def __init__(self,
                 station_id,
                 name,
                 color,
                 direction_a=None,
                 direction_b=None):
        self.name = name
        station_name = (self.name.lower().replace("/", "_and_").replace(
            " ", "_").replace("-", "_").replace("'", ""))

        #
        #
        # TODO: Complete the below by deciding on a topic name, number of partitions, and number of
        # replicas
        #
        #
        topic_name = f"org.chicago.cta.station.arrivals.{station_name}"  # TODO: Come up with a better topic name
        super().__init__(
            topic_name,
            key_schema=Station.key_schema,
            value_schema=Station.
            value_schema,  # TODO: Uncomment once schema is defined
            num_partitions=1,
            num_replicas=1,
        )

        self.station_id = int(station_id)
        self.color = color
        self.dir_a = direction_a
        self.dir_b = direction_b
        self.a_train = None
        self.b_train = None
        self.turnstile = Turnstile(self)
예제 #2
0
    def __init__(self, 
                 station_id: int, 
                 name: str, 
                 color: IntEnum, 
                 direction_a=None, 
                 direction_b=None):
        self.name = name
        self.station_name = (
            self.name.lower()
            .replace("/", "_and_")
            .replace(" ", "_")
            .replace("-", "_")
            .replace("'", "")
        )
        
        topic_name = "org.chicago.cta.station.arrivals.monitor" 
        super().__init__(
            topic_name,
            key_schema=self.key_schema,
            value_schema=self.value_schema, 
            num_partitions=1, #single node broker
            num_replicas=1 #number of brokers available in the work space
        )

        self.station_id = int(station_id)
        self.color = color
        self.dir_a = direction_a
        self.dir_b = direction_b
        self.a_train = None
        self.b_train = None
        self.turnstile = Turnstile(self)
        self.topic_name = topic_name
예제 #3
0
    def __init__(self,
                 station_id,
                 name,
                 color,
                 direction_a=None,
                 direction_b=None):
        self.name = name
        station_name = (self.name.lower().replace("/", "_and_").replace(
            " ", "_").replace("-", "_").replace("'", ""))

        # with each unique station_name, a new topic will be created/assigned
        topic_name = f"com.udacity.stations.{station_name}.arrivals"
        super().__init__(
            topic_name=topic_name,
            key_schema=Station.key_schema,
            value_schema=Station.value_schema,
            num_partitions=
            5,  # can go into a separate config file instead of hard-coded
            num_replicas=
            1  # can go into a separate config file instead of hard-coded
        )

        self.station_id = int(station_id)
        self.color = color  # same as line! SEE: line.py
        self.dir_a = direction_a
        self.dir_b = direction_b
        self.a_train = None
        self.b_train = None
        self.turnstile = Turnstile(self)
예제 #4
0
    def __init__(self,
                 station_id,
                 name,
                 color,
                 direction_a=None,
                 direction_b=None):
        self.name = name
        station_name = (self.name.lower().replace("/", "_and_").replace(
            " ", "_").replace("-", "_").replace("'", ""))

        #
        #
        # TODO: Complete the below by deciding on a topic name, number of partitions, and number of
        # replicas
        #
        #
        topic_name = f"{self.TOPIC_PREFIX}{station_name}"
        super().__init__(topic_name,
                         key_schema=Station.key_schema,
                         value_schema=Station.value_schema,
                         num_partitions=self.NUM_PARTITION,
                         num_replicas=self.NUM_REPLICA)

        self.station_id = int(station_id)
        self.color = color
        self.dir_a = direction_a
        self.dir_b = direction_b
        self.a_train = None
        self.b_train = None
        self.turnstile = Turnstile(self)
    def __init__(self, station_id, name, color, direction_a=None, direction_b=None):
        self.name = name

        # TODO: Complete the below by deciding on a topic name, number of partitions, and number of
        # replicas

        topic_name = f"com.udacity.station.arrivals.v1" # TODO: Come up with a better topic name
        super().__init__(
            topic_name="com.udacity.station.arrivals.v1",
            key_schema=Station.key_schema,
            value_schema=Station.value_schema,
            num_partitions=3,
            num_replicas=1,
        )

        self.station_id = int(station_id)
        self.color = color
        self.dir_a = direction_a
        self.dir_b = direction_b
        self.a_train = None
        self.b_train = None
        self.turnstile = Turnstile(self)
예제 #6
0
class Station(Producer):
    """Defines a single station"""

    key_schema = avro.load(
        f"{Path(__file__).parents[0]}/schemas/arrival_key.json")

    #
    # TODO: Define this value schema in `schemas/station_value.json, then uncomment the below
    #
    value_schema = avro.load(
        f"{Path(__file__).parents[0]}/schemas/arrival_value.json")

    def __init__(self,
                 station_id,
                 name,
                 color,
                 direction_a=None,
                 direction_b=None):
        self.name = name
        station_name = (self.name.lower().replace("/", "_and_").replace(
            " ", "_").replace("-", "_").replace("'", ""))

        #
        #
        # TODO: Complete the below by deciding on a topic name, number of partitions, and number of
        # replicas
        #
        #
        topic_name = f"org.chicago.cta.station.arrivals.{station_name}"  # TODO: Come up with a better topic name
        super().__init__(
            topic_name,
            key_schema=Station.key_schema,
            value_schema=Station.
            value_schema,  # TODO: Uncomment once schema is defined
            num_partitions=1,
            num_replicas=1,
        )

        self.station_id = int(station_id)
        self.color = color
        self.dir_a = direction_a
        self.dir_b = direction_b
        self.a_train = None
        self.b_train = None
        self.turnstile = Turnstile(self)

    def run(self, train, direction, prev_station_id, prev_direction):
        """Simulates train arrivals at this station"""
        #
        #
        # TODO: Complete this function by producing an arrival message to Kafka
        #
        #
        #logger.info("arrival kafka integration incomplete - skipping")
        self.producer.produce(
            topic=self.topic_name,
            key={"timestamp": self.time_millis()},
            value={
                "station_id": str(self.station_id),
                "train_id": str(train.train_id),
                "direction": direction,
                "line": self.color.name,
                "train_status": str(train.status),
                "prev_station_id": str(prev_station_id),
                "prev_direction": str(prev_direction),
            },
        )

    def __str__(self):
        return "Station | {:^5} | {:<30} | Direction A: | {:^5} | departing to {:<30} | Direction B: | {:^5} | departing to {:<30} | ".format(
            self.station_id,
            self.name,
            self.a_train.train_id if self.a_train is not None else "---",
            self.dir_a.name if self.dir_a is not None else "---",
            self.b_train.train_id if self.b_train is not None else "---",
            self.dir_b.name if self.dir_b is not None else "---",
        )

    def __repr__(self):
        return str(self)

    def arrive_a(self, train, prev_station_id, prev_direction):
        """Denotes a train arrival at this station in the 'a' direction"""
        self.a_train = train
        self.run(train, "a", prev_station_id, prev_direction)

    def arrive_b(self, train, prev_station_id, prev_direction):
        """Denotes a train arrival at this station in the 'b' direction"""
        self.b_train = train
        self.run(train, "b", prev_station_id, prev_direction)

    def close(self):
        """Prepares the producer for exit by cleaning up the producer"""
        self.turnstile.close()
        super(Station, self).close()
class Station(Producer):
    """Defines a single station"""

    key_schema = avro.load(f"{Path(__file__).parents[0]}/schemas/arrival_key.json")

    #
    # TODO: Define this value schema in `schemas/station_value.json, then uncomment the below
    #
    value_schema = avro.load(f"{Path(__file__).parents[0]}/schemas/arrival_value.json")

    def __init__(self, station_id, name, color, direction_a=None, direction_b=None):
        self.name = name

        # TODO: Complete the below by deciding on a topic name, number of partitions, and number of
        # replicas

        topic_name = f"com.udacity.station.arrivals.v1" # TODO: Come up with a better topic name
        super().__init__(
            topic_name="com.udacity.station.arrivals.v1",
            key_schema=Station.key_schema,
            value_schema=Station.value_schema,
            num_partitions=3,
            num_replicas=1,
        )

        self.station_id = int(station_id)
        self.color = color
        self.dir_a = direction_a
        self.dir_b = direction_b
        self.a_train = None
        self.b_train = None
        self.turnstile = Turnstile(self)


    def run(self, train, direction, prev_station_id, prev_direction):
        """Simulates train arrivals at this station"""
        # TODO: Complete this function by producing an arrival message to Kafka
        try:
            self.producer.produce(
                topic=self.topic_name,
                key={"timestamp": self.time_millis()},
                value={
                    "station_id": self.station_id,
                    "train_id": train.train_id,
                    "direction": direction,
                    "line": self.color.name,
                    "train_status": train.status.name,
                    "prev_station_id": prev_station_id,
                    "prev_direction": prev_direction
                }
            )
        except Exception as e:
            logger.fatal(e)
            raise e

    def __str__(self):
        return "Station | {:^5} | {:<30} | Direction A: | {:^5} | departing to {:<30} | Direction B: | {:^5} | departing to {:<30} | ".format(
            self.station_id,
            self.name,
            self.a_train.train_id if self.a_train is not None else "---",
            self.dir_a.name if self.dir_a is not None else "---",
            self.b_train.train_id if self.b_train is not None else "---",
            self.dir_b.name if self.dir_b is not None else "---",
        )

    def __repr__(self):
        return str(self)

    def arrive_a(self, train, prev_station_id, prev_direction):
        """Denotes a train arrival at this station in the 'a' direction"""
        self.a_train = train
        self.run(train, "a", prev_station_id, prev_direction)

    def arrive_b(self, train, prev_station_id, prev_direction):
        """Denotes a train arrival at this station in the 'b' direction"""
        self.b_train = train
        self.run(train, "b", prev_station_id, prev_direction)

    def close(self):
        """Prepares the producer for exit by cleaning up the producer"""
        self.turnstile.close()
        super(Station, self).close()
예제 #8
0
class Station(Producer):
    """Defines a single station"""

    key_schema = avro.load(f"{Path(__file__).parents[0]}/schemas/arrival_key.json")

    value_schema = avro.load(f"{Path(__file__).parents[0]}/schemas/arrival_value.json")

    def __init__(self, 
                 station_id: int, 
                 name: str, 
                 color: IntEnum, 
                 direction_a=None, 
                 direction_b=None):
        self.name = name
        self.station_name = (
            self.name.lower()
            .replace("/", "_and_")
            .replace(" ", "_")
            .replace("-", "_")
            .replace("'", "")
        )
        
        topic_name = "org.chicago.cta.station.arrivals.monitor" 
        super().__init__(
            topic_name,
            key_schema=self.key_schema,
            value_schema=self.value_schema, 
            num_partitions=1, #single node broker
            num_replicas=1 #number of brokers available in the work space
        )

        self.station_id = int(station_id)
        self.color = color
        self.dir_a = direction_a
        self.dir_b = direction_b
        self.a_train = None
        self.b_train = None
        self.turnstile = Turnstile(self)
        self.topic_name = topic_name

    def run(self, train: Train, 
            direction: str, 
            prev_station_id: int, 
            prev_direction: str) -> None:
        """Simulates train arrivals at this station"""
        
        logger.info("arrival of train: " + train.train_id + " at station: " + self.name + " topic name: " + self.topic_name + " line: " + self.color.name)
        self.producer.produce(
            topic=self.topic_name,
            key={"timestamp": self.time_millis()},
            value={
                "station_id": self.station_id,
                "train_id": train.train_id,
                "direction": direction,
                "line": self.color.name,
                "train_status": train.status.name,
                "prev_station_id": prev_station_id,
                "prev_direction": prev_direction
            }
        )

    def __str__(self):
        return "Station | {:^5} | {:<30} | Direction A: | {:^5} | departing to {:<30} | Direction B: | {:^5} | departing to {:<30} | ".format(
            self.station_id,
            self.name,
            self.a_train.train_id if self.a_train is not None else "---",
            self.dir_a.name if self.dir_a is not None else "---",
            self.b_train.train_id if self.b_train is not None else "---",
            self.dir_b.name if self.dir_b is not None else "---",
        )

    def __repr__(self):
        return str(self)

    def arrive_a(self, train: Train, 
                 prev_station_id: int, 
                 prev_direction: str) -> None:
        """Denotes a train arrival at this station in the 'a' direction"""
        self.a_train = train
        self.run(train, "a", prev_station_id, prev_direction)

    def arrive_b(self, train: Train, 
                 prev_station_id: int, 
                 prev_direction: str) -> None:
        """Denotes a train arrival at this station in the 'b' direction"""
        self.b_train = train
        self.run(train, "b", prev_station_id, prev_direction)

    def close(self):
        """Prepares the producer for exit by cleaning up the producer"""
        self.turnstile.close()
        super(Station, self).close()
예제 #9
0
class Station(Producer):
    """
    Defines a single station
    """
    # load AVSC for key and value
    key_schema = avro.load(
        f"{Path(__file__).parents[0]}/schemas/arrival_key.json")
    value_schema = avro.load(
        f"{Path(__file__).parents[0]}/schemas/arrival_value.json")

    def __init__(self,
                 station_id,
                 name,
                 color,
                 direction_a=None,
                 direction_b=None):
        self.name = name
        station_name = (self.name.lower().replace("/", "_and_").replace(
            " ", "_").replace("-", "_").replace("'", ""))

        # with each unique station_name, a new topic will be created/assigned
        topic_name = f"com.udacity.stations.{station_name}.arrivals"
        super().__init__(
            topic_name=topic_name,
            key_schema=Station.key_schema,
            value_schema=Station.value_schema,
            num_partitions=
            5,  # can go into a separate config file instead of hard-coded
            num_replicas=
            1  # can go into a separate config file instead of hard-coded
        )

        self.station_id = int(station_id)
        self.color = color  # same as line! SEE: line.py
        self.dir_a = direction_a
        self.dir_b = direction_b
        self.a_train = None
        self.b_train = None
        self.turnstile = Turnstile(self)

    def run(self, train, direction, prev_station_id, prev_direction):
        """
        Simulates train arrivals at this station
        """
        # TODO: change logging level later
        logger.info(f"producing to {self.topic_name}")

        self.producer.produce(
            topic=self.topic_name,
            key={"timestamp": self.time_millis()},
            value={
                "station_id": self.station_id,
                "train_id": train.train_id,
                "direction": direction,
                "line": self.color.name,
                "train_status": train.status.name,
                "prev_station_id": prev_station_id,
                "prev_direction": prev_direction
            },
        )

    def __str__(self):
        return "Station | {:^5} | {:<30} | Direction A: | {:^5} | departing to {:<30} | Direction B: | {:^5} | departing to {:<30} | ".format(
            self.station_id,
            self.name,
            self.a_train.train_id if self.a_train is not None else "---",
            self.dir_a.name if self.dir_a is not None else "---",
            self.b_train.train_id if self.b_train is not None else "---",
            self.dir_b.name if self.dir_b is not None else "---",
        )

    def __repr__(self):
        return str(self)

    def arrive_a(self, train, prev_station_id, prev_direction):
        """
        Denotes a train arrival at this station in the 'a' direction
        """
        self.a_train = train
        self.run(train, "a", prev_station_id, prev_direction)

    def arrive_b(self, train, prev_station_id, prev_direction):
        """
        Denotes a train arrival at this station in the 'b' direction
        """
        self.b_train = train
        self.run(train, "b", prev_station_id, prev_direction)

    def close(self):
        """
        Prepares the producer for exit by cleaning up the producer
        """
        self.turnstile.close()
        super(Station, self).close()
예제 #10
0
class Station(Producer):
    """Defines a single station"""

    key_schema = avro.load(
        f"{Path(__file__).parents[0]}/schemas/arrival_key.json")
    value_schema = avro.load(
        f"{Path(__file__).parents[0]}/schemas/arrival_value.json")
    NUM_PARTITION = 3
    NUM_REPLICA = 1
    TOPIC_PREFIX = "org.chicago.cta.station.arrivals."

    def __init__(self,
                 station_id,
                 name,
                 color,
                 direction_a=None,
                 direction_b=None):
        self.name = name
        station_name = (self.name.lower().replace("/", "_and_").replace(
            " ", "_").replace("-", "_").replace("'", ""))

        #
        #
        # TODO: Complete the below by deciding on a topic name, number of partitions, and number of
        # replicas
        #
        #
        topic_name = f"{self.TOPIC_PREFIX}{station_name}"
        super().__init__(topic_name,
                         key_schema=Station.key_schema,
                         value_schema=Station.value_schema,
                         num_partitions=self.NUM_PARTITION,
                         num_replicas=self.NUM_REPLICA)

        self.station_id = int(station_id)
        self.color = color
        self.dir_a = direction_a
        self.dir_b = direction_b
        self.a_train = None
        self.b_train = None
        self.turnstile = Turnstile(self)

    def run(self, train: Train, direction, prev_station_id, prev_direction):
        """Simulates train arrivals at this station"""
        """
            station_id, station_name, color, direction_a, direction_b
            train, direction, prev_station_id, prev_direction
        """
        #
        #
        # TODO: Complete this function by producing an arrival message to Kafka
        #
        #
        self.producer.produce(topic=self.topic_name,
                              key={"timestamp": self.time_millis()},
                              value={
                                  "station_id": self.station_id,
                                  "train_id": train.train_id,
                                  "direction": direction,
                                  "line": self.color.name,
                                  "train_status": train.status.name,
                                  "prev_station_id": prev_station_id,
                                  "prev_direction": prev_direction
                              },
                              value_schema=self.value_schema,
                              key_schema=self.key_schema)

    def __str__(self):
        return "Station | {:^5} | {:<30} | Direction A: | {:^5} | departing to {:<30} | Direction B: | {:^5} | departing to {:<30} | ".format(
            self.station_id,
            self.name,
            self.a_train.train_id if self.a_train is not None else "---",
            self.dir_a.name if self.dir_a is not None else "---",
            self.b_train.train_id if self.b_train is not None else "---",
            self.dir_b.name if self.dir_b is not None else "---",
        )

    def __repr__(self):
        return str(self)

    def arrive_a(self, train, prev_station_id, prev_direction):
        """Denotes a train arrival at this station in the 'a' direction"""
        self.a_train = train
        self.run(train, "a", prev_station_id, prev_direction)

    def arrive_b(self, train, prev_station_id, prev_direction):
        """Denotes a train arrival at this station in the 'b' direction"""
        self.b_train = train
        self.run(train, "b", prev_station_id, prev_direction)

    def close(self):
        """Prepares the producer for exit by cleaning up the producer"""
        self.turnstile.close()
        super(Station, self).close()