コード例 #1
0
 def print_summary(self) -> None:
     summary: Dict[str,
                   Set[str]] = defaultdict(set)  # event type <-> set[fqid]
     for write_request in self.write_requests:
         for event in write_request.events:
             summary[self.get_request_name(event)].add(event.fqid)
     logger.info("\n".join(f"{eventType}: {list(fqids)}"
                           for eventType, fqids in summary.items()))
コード例 #2
0
 def print_stats(self) -> None:
     stats: Dict[str, int] = defaultdict(int)
     for write_request in self.write_requests:
         for event in write_request.events:
             stats[self.get_request_name(event)] += 1
     stats_string = ", ".join(f"{cnt} {name}"
                              for name, cnt in stats.items())
     logger.info(f"Events executed ({stats_string})")
コード例 #3
0
 def create_position(self, information, user_id):
     statement = dedent(
         """\
         insert into positions (timestamp, user_id, information)
         values (current_timestamp, %s, %s)"""
     )
     arguments = [user_id, self.json(information)]
     self.connection.execute(statement, arguments)
     query = "select max(position) from positions"
     position = self.connection.query_single_value(query, [])
     logger.info(f"Created position {position}")
     return position
コード例 #4
0
    def write(self, write_request: WriteRequest) -> None:
        with self._lock:
            self.write_request = write_request
            # Convert request events to db events
            self.db_events = self.event_translator.translate(
                write_request.events)

            with self.database.get_context():
                self.write_with_database_context()

            stats: Dict[str, int] = defaultdict(int)
            for event in write_request.events:
                log_name = (type(event).__name__.replace(
                    "Request", "").replace("Event", "").upper())
                stats[log_name] += 1
            stats_string = ", ".join(f"{cnt} {name}"
                                     for name, cnt in stats.items())
            logger.info(f"Events executed ({stats_string})")

            # Only propagate updates to redis after the transaction has finished
            self.messaging.handle_events(self.db_events, self.position)
コード例 #5
0
    def handle_request(self, route: Route, data: JSON) -> Dict:
        """
        A generic handler for all requests. Parses the request to a python object
        according to the route_setup and execute the according route_handler.
        """

        try:
            route_configuration = route_configurations[route]
        except KeyError:
            raise BadCodingError("Invalid route metadata: " + route)

        logger.info(f"{route.upper()}-request: {data}")

        try:
            request_data = route_configuration.schema(data)
        except fastjsonschema.JsonSchemaException as e:
            if route_configuration.schema_error_handler:
                route_configuration.schema_error_handler(e)
            raise InvalidRequest(e.message)

        try:
            request_object = from_dict(
                route_configuration.request_class,
                request_data,
                Config(check_types=False),
            )
        except (TypeError, MissingValueError) as e:
            raise BadCodingError("Invalid data to initialize class\n" + str(e))

        reader = injector.get(Reader)
        route_handler = getattr(reader, route)

        if route_configuration.dev_only:
            route_handler = dev_only_route(route_handler)

        return route_handler(request_object)
コード例 #6
0
 def reserve_ids(self, collection: str, amount: int) -> List[int]:
     with self.database.get_context():
         ids = self.database.reserve_next_ids(collection, amount)
         logger.info(f"{len(ids)} ids reserved")
         return ids
コード例 #7
0
 def truncate_db(self) -> None:
     with self.database.get_context():
         self.database.truncate_db()
         logger.info("Database truncated")