def get_event_and_link_dicts(self):
        self._formatted_urls = {}

        # the kind of object varies depending on the filter.
        (
            key_object_class,
            event_query_kwargs,
            key_obj_filter_dict,
            key_object_order_column,
        ) = self.event_filter.query_args_info

        self._most_recent_date = utils.get_most_recent_date(self._session)
        key_objects = (self._session.query(key_object_class).filter_by(
            **key_obj_filter_dict).order_by(key_object_order_column))

        self._events = {}
        self._details = {}
        self._titles = {}

        for key in key_objects:
            event_list = (self._session.query(
                datamodel.HistoricalEvent).order_by(
                    datamodel.HistoricalEvent.start_date_days.asc(),
                    datamodel.HistoricalEvent.event_type.asc(),
                ).filter_by(**{
                    event_query_kwargs: key
                }).all())
            self.collect_event_dicts(event_list, key)
        return self._events, self._titles, self._details, self._formatted_urls
def update_content(tab_value, search, date_fraction, dash_plot_checklist,
                   country_perspective):
    config.CONFIG.normalize_stacked_plots = ("normalize_stacked_plots"
                                             in dash_plot_checklist)
    game_id, matches = _get_game_ids_matching_url(search)
    if not matches:
        logger.warning(f"Could not find a game matching {game_id}")
        return render_template("404_page.html",
                               game_not_found=True,
                               game_name=game_id)
    game_id = matches[0]

    games_dict = datamodel.get_available_games_dict()
    if game_id not in games_dict:
        logger.warning(f"Game ID {game_id} does not match any known game!")
        return []

    logger.info(
        f"dash_server.update_content: Tab is {tab_value}, Game is {game_id}")
    with datamodel.get_db_session(game_id) as session:
        current_date = utils.get_most_recent_date(session)

    children = []
    if tab_value in visualization_data.THEMATICALLY_GROUPED_PLOTS:
        plots = visualization_data.THEMATICALLY_GROUPED_PLOTS[tab_value]
        plot_data = visualization_data.get_current_execution_plot_data(
            game_id, country_perspective)
        for plot_spec in plots:
            figure_data = get_figure_data(plot_data, plot_spec)
            if not figure_data:
                continue
            figure_layout = get_figure_layout(plot_spec)
            figure_layout["title"] = plot_spec.title
            figure = go.Figure(data=figure_data, layout=figure_layout)

            children.append(
                html.Div(
                    [
                        dcc.Graph(
                            id=plot_spec.plot_id,
                            figure=figure,
                            style=dict(textAlign="center"),
                        )
                    ],
                    style=dict(
                        margin="auto",
                        width=f"{config.CONFIG.plot_width}px",
                        height=f"{config.CONFIG.plot_height}px",
                    ),
                ))
    else:
        slider_date = 0.01 * date_fraction * current_date
        children.append(get_galaxy(game_id, slider_date))
        children.append(
            html.P(
                f"Galaxy Map at {datamodel.days_to_date(slider_date)}",
                style=TEXT_STYLE,
            ))
    return children
def adjust_slider_values(tab_value, search):
    _, matches = _get_game_ids_matching_url(search)
    with datamodel.get_db_session(matches[0]) as session:
        max_date = utils.get_most_recent_date(session)

    if tab_value == config.GALAXY_MAP_TAB:
        marks = {0: "2200.01.01", 100: datamodel.days_to_date(max_date)}
        for x in range(20, 100, 20):
            marks[x] = datamodel.days_to_date(x / 100 * max_date)
        logger.info(f"Setting marks to {marks}")
        return marks
    else:
        return {}
 def leader_details(self, leader_model: datamodel.Leader) -> Dict[str, str]:
     country_url = self._get_url_for(leader_model.country)
     details = {
         "Leader Name":
         leader_model.leader_name,
         "Gender":
         game_info.convert_id_to_name(leader_model.gender),
         "Species":
         leader_model.species.species_name,
         "Class":
         f"{game_info.convert_id_to_name(leader_model.leader_class)} in the {country_url}",
         "Born":
         datamodel.days_to_date(leader_model.date_born),
         "Hired":
         datamodel.days_to_date(leader_model.date_hired),
         "Last active":
         datamodel.days_to_date(
             utils.get_most_recent_date(self._session) if leader_model.
             is_active else leader_model.last_date),
         "Status":
         "Active" if leader_model.is_active else "Dead or Dismissed",
     }
     return details
def update_content(
    tab_value, search, date_fraction, dash_plot_checklist, country_perspective
):
    config.CONFIG.normalize_stacked_plots = (
        "normalize_stacked_plots" in dash_plot_checklist
    )
    game_id, matches = _get_game_ids_matching_url(search)
    if not matches:
        logger.warning(f"Could not find a game matching {game_id}")
        return render_template("404_page.html", game_not_found=True, game_name=game_id)
    game_id = matches[0]

    games_dict = datamodel.get_available_games_dict()
    if game_id not in games_dict:
        logger.warning(
            f"Game ID {game_id} does not match any known game! (URL parameter {search})"
        )
        return []

    logger.info(f"dash_server.update_content: Tab is {tab_value}, Game is {game_id}")
    with datamodel.get_db_session(game_id) as session:
        current_date = utils.get_most_recent_date(session)

    children = []
    if tab_value in config.CONFIG.tab_layout:
        plots = visualization_data.get_plot_specifications_for_tab_layout().get(
            tab_value
        )
        plot_data = visualization_data.get_current_execution_plot_data(
            game_id, country_perspective
        )
        for plot_spec in plots:
            if not plot_spec:
                continue  # just in case it's possible to sneak in an invalid ID
            start = time.time()
            figure_data = get_raw_plot_data_dicts(plot_data, plot_spec)
            end = time.time()
            logger.debug(
                f"Prepared figure {plot_spec.title} in {end - start:5.3f} seconds."
            )
            if not figure_data:
                continue
            figure_layout = get_figure_layout(plot_spec)
            figure_layout["title"] = plot_spec.title
            figure = go.Figure(data=figure_data, layout=figure_layout)

            children.append(
                html.Div(
                    [
                        dcc.Graph(
                            id=plot_spec.plot_id,
                            figure=figure,
                            style=dict(textAlign="center"),
                        )
                    ],
                    style=dict(margin="auto", width=f"{config.CONFIG.plot_width}px",),
                )
            )
    else:
        slider_date = 0.01 * date_fraction * current_date

        children.append(
            html.Div(
                [get_galaxy(game_id, slider_date), SELECT_SYSTEM_DEFAULT],
                style=dict(
                    margin="auto",
                    width=f"{config.CONFIG.plot_width}px",
                    # height=f"{config.CONFIG.plot_height}px",
                    backgroundColor=BACKGROUND_DARK,
                ),
            )
        )
    return children