Ejemplo n.º 1
0
def add_dropdown(values, dropdowns):
    ctx = dash.callback_context
    ctx_msg = json.dumps({"states": ctx.states, "triggered": ctx.triggered, "inputs": ctx.inputs}, indent=2)
    selected_entity = None
    # If the dropdowns are empty, we are adding the dropdowns for the first level
    if not dropdowns:
        dropdowns = [get_dropdown()]
        return dropdowns, None
    if not ctx.triggered:
        raise PreventUpdate
    else:
        depth = json.loads(ctx.triggered[0]["prop_id"].split(".")[0])["id"]
        # We delete the decedents dropdowns of the current changed value
        dropdowns = dropdowns[:depth]

        entity_id = ctx.triggered[0]["value"]
        # The value can be None if the dropdown is cleared
        if entity_id:
            oc = OrganizationChart()
            oc.load_current()
            entity = oc.get_entity_by_id(entity_id)
            # If there are some children, we add a dropdown with the children choices
            if entity.children:
                dropdowns.append(get_dropdown(parent_id=entity_id, depth=entity.depth + 1))
            # Else, we set the selected_entity with the selected value
            else:
                selected_entity = entity.id

    return dropdowns, selected_entity
Ejemplo n.º 2
0
def update_odrive_plot_vehicle_make(selected_entity, filter_vehicle_type):
    oc = OrganizationChart()
    oc.load_current()
    service = oc.get_entity_by_id(selected_entity)
    print(service)
    return get_odrive_plot_vehicle_make(service.code_odrive,
                                        filter_vehicle_type)
Ejemplo n.º 3
0
def get_dropdown(parent_id="root", depth=1):
    oc = OrganizationChart()
    oc.load_current()
    return dcc.Dropdown(
        id={"type": "entity-choice-dropdown-level", "id": depth},
        options=oc.get_children_dropdown_items(parent_id),
        placeholder="Choisissez votre entité de niveau %s" % depth,
        className="mb-3",
        clearable=True,
    )
Ejemplo n.º 4
0
def on_selected_entity_show_selected_entity(selected_entity):
    if selected_entity is not None:
        oc = OrganizationChart()
        oc.load_current()
        service = oc.get_entity_by_id(selected_entity)
        return html.Div(
            [
                html.H3(service.parent.label, style={"text-align": "center"}),
                html.Br(),
                html.H4(service.label, style={"text-align": "center"}),
            ]
        )
Ejemplo n.º 5
0
def set_slider_range(selected_entity):
    """
    Will get min / max date range to setup the range slider
    """
    oc = OrganizationChart()
    oc.load_current()
    service = oc.get_entity_by_id(selected_entity)
    data = oh.get_structure_data(service.code_osfi)
    min_date = data["Date"].min()
    max_date = data["Date"].max()
    datelist = pd.date_range(start=min_date, end=max_date, freq="M").to_list()
    marks = get_marks(datelist)
    min_value = unix_time_millis(min_date)
    max_value = unix_time_millis(max_date)
    return min_value, max_value, marks
Ejemplo n.º 6
0
def fill_dash_table_with_buildings(selected_entity):
    oc = OrganizationChart()
    oc.load_current()
    service = oc.get_entity_by_id(selected_entity)
    data = oh.get_structure_data(service.code_osfi)
    columns_to_keep = [
        "Nom du bien", "Building type", "Adresse", "Code postal", "Ville",
        "Departement"
    ]
    columns = [{"name": i, "id": i} for i in columns_to_keep]
    hidden_columns = []
    for c in columns:
        if c["name"] not in ["Nom du bien", "Ville"]:
            c["hideable"] = True
            hidden_columns.append(c["id"])
    row_selectable = "multi"
    buildings = data[columns_to_keep].drop_duplicates()
    selected_rows = list(range(0, len(buildings)))
    data_to_return = buildings.to_dict("records")
    return columns, row_selectable, selected_rows, hidden_columns, data_to_return
def ochw_to_oc(organization_chart_html_wrapper):

    root = Entity(id="root", label="root", activated=True)
    elements = {}
    elements["root"] = root
    for entity_wrapper in PreOrderIter(organization_chart_html_wrapper._root):
        if entity_wrapper.id == "root":
            continue
        parent_id = entity_wrapper.parent.id
        entity = Entity(
            id=entity_wrapper.id,
            label=entity_wrapper.label,
            code_chorus=entity_wrapper.code_chorus,
            code_osfi=entity_wrapper.code_osfi,
            code_odrive=entity_wrapper.code_odrive,
            activated=entity_wrapper.activated,
        )

        entity.parent = elements[parent_id]
        elements[entity.id] = entity
    oc = OrganizationChart()
    oc._root = root
    return oc
Ejemplo n.º 8
0
def update_graphs(selected_entity, n_clicks, prestation_types: list, years, unreliable):
    oc = OrganizationChart()
    oc.load_current()
    service = oc.get_entity_by_id(selected_entity)
    chorus_dt_df = ch.get_structure_data(service.code_chorus).copy()
    if "fiable" not in chorus_dt_df.columns:
        chorus_dt_df["fiable"] = True
    filters = (
        chorus_dt_df.prestation.isin(prestation_types)
        & chorus_dt_df.date_debut_mission.dt.year.isin(years)
        & (chorus_dt_df.fiable.isin(unreliable) | chorus_dt_df.fiable)
    )
    chorus_dt_df = chorus_dt_df.loc[filters, :]
    return [
        get_kpi_emissions(chorus_dt_df),
        get_kpi_trips_count(chorus_dt_df),
        get_kpi_distance(chorus_dt_df),
        get_donut_by_prestation_type(chorus_dt_df),
        get_scatter_by_emission(chorus_dt_df),
        get_emissions_timeseries(chorus_dt_df),
        get_hist_top_emission(chorus_dt_df),
        get_dashtable_by_emission(chorus_dt_df),
    ]
Ejemplo n.º 9
0
def get_data(selected_entity, selected_rows, buildings, slider_values):
    oc = OrganizationChart()
    oc.load_current()
    entity = oc.get_entity_by_id(selected_entity)
    data = oh.get_structure_data(entity.code_osfi)

    # Start of month for min slider range
    min_date = pd.Timestamp(unix_to_date(
        slider_values[0])) - pd.offsets.MonthBegin(1)
    max_date = pd.Timestamp(unix_to_date(slider_values[1]))

    # If no rows are selected, we are returning an empty dataframe
    # with the same structure as data
    if selected_rows is None or len(selected_rows) == 0:
        return pd.DataFrame().reindex_like(data)
    else:
        biens = [buildings[int(i)] for i in selected_rows]
        biens = pd.DataFrame(biens)
        codes = biens["Nom du bien"]
        data_to_display = data[data["Nom du bien"].isin(codes)]
        data_to_display = pd.DataFrame(data_to_display)
        data_to_display = data_to_display[data_to_display["Date"] >= min_date]
        data_to_display = data_to_display[data_to_display["Date"] <= max_date]
        return data_to_display
Ejemplo n.º 10
0
def prepare_exports():
    oc = OrganizationChart()
    oc.load_current()
    leaves = oc.get_leaves()
    print("Preparing exports")
    for entity in tqdm(oc.get_leaves()):
        tqdm.write("Exporting excel for entity: %s" % entity.id)
        data_export = DataExport(entity.id)
        with open("/data/cleaned/exports/%s.xlsx" % entity.id, "wb") as file_id:
            file_id.write(data_export.get_file_as_bytes().read())
                    continue
                descendor.visible = False
                descendor.expand = False

    def toggle_entity_visible(self, entity_id):
        entity = self.get_entity_by_id(entity_id)
        entity_ancestors = entity.ancestors
        for other_entity in PreOrderIter(self._root):
            if other_entity.id == "root":
                continue
            elif other_entity.parent.id == "root":
                other_entity.visible = True
                other_entity.expand = False
            else:
                other_entity.visible = False
                other_entity.expand = False
        for other_entity in entity_ancestors[::-1]:
            if other_entity.id == "root":
                continue
            else:
                self.toggle_expand(other_entity.id)


if __name__ == "__main__":
    organization_chart = OrganizationChart("/data/entities_test_tree_2.tsv")
    och = OrganizationChartHtmlWrapper(organization_chart)

    tree_json = json.dumps(PreOrderIter(organization_chart._root),
                           cls=OrganizationChartJsonEncoder)
    # oc = OrganizationChartJsonEncoder().from_json(tree_json)
Ejemplo n.º 12
0
 def __init__(self, selected_entity):
     oc = OrganizationChart()
     oc.load_current()
     self.service = oc.get_entity_by_id(selected_entity)
     self.load_data()
def interact_organigram(
    expand_n_clicks,
    activate_n_clicks,
    update_entity_modal_open_button_n_clicks,
    new_entity_modal_open_button_n_clicks,
    entity_modal_close_button_n_clicks,
    entity_modal_valid_button_n_clicks,
    entity_modal_delete_button_n_clicks,
    json_tree,
    state_modal_mode,
    state_modal_entity_id,
    state_modal_parent_id,
    state_modal_label,
    state_modal_code_chorus,
    state_modal_code_odrive,
    state_modal_code_osfi,
):
    ctx = dash.callback_context
    # If the json tree is empty, we will load the tree from the app organization chart
    # Else we will load this json tree
    if not json_tree:

        oc = OrganizationChart()
        oc.load_current()
        ochw = oc_to_ochw(oc)
    else:
        ochw = OrganizationChartHtmlWrapper()
        ochw.load_json(json_tree)

    modal_is_open = None
    modal_mode = None
    modal_entity_id = None

    if not ctx.triggered:
        pass
    else:
        element_full_id = ctx.triggered[0]["prop_id"].split(".")[0]
        if element_full_id == "back-office-entity-new-modal-open-button":
            modal_is_open = True
            modal_mode = "new"
        elif element_full_id == "back-office-entity-modal-close-button":
            modal_is_open = False
        elif element_full_id == "back-office-entity-modal-delete-button":
            entity = ochw.get_entity_by_id(state_modal_entity_id)
            entity.parent = None
            modal_is_open = False

        elif element_full_id == "back-office-entity-modal-valid-button":
            modal_is_open = False
            if state_modal_mode == "new":
                entity = EntityHtmlWrapper(id=str(uuid.uuid4()), label=state_modal_label)
                entity.code_chorus = state_modal_code_chorus
                entity.code_odrive = state_modal_code_odrive
                entity.code_osfi = state_modal_code_osfi
                parent = ochw.get_entity_by_id(state_modal_parent_id)
                entity.parent = parent
                entity.visible = True
                entity.expand = False
                ochw.toggle_entity_visible(entity.id)
            elif state_modal_mode == "update":
                entity_id = state_modal_entity_id
                parent_id = state_modal_parent_id
                entity = ochw.get_entity_by_id(entity_id)
                parent = ochw.get_entity_by_id(parent_id)
                entity.parent = parent
                entity.label = state_modal_label
                entity.code_chorus = state_modal_code_chorus
                entity.code_odrive = state_modal_code_odrive
                entity.code_osfi = state_modal_code_osfi
                ochw.toggle_entity_visible(entity.id)
        else:
            element_full_id = json.loads(element_full_id)
            print(element_full_id)
            element_id = element_full_id["id"]
            element_type = element_full_id["type"]

            if element_type == "back-office-entity-activated":
                print(element_full_id)
                ochw.toggle_activation(element_id)
            elif element_type == "back-office-entity-expand":
                ochw.toggle_expand(element_id)
            elif element_type == "back-office-entity-update-open-button":
                entity = ochw.get_entity_by_id(element_id)
                modal_is_open = True
                modal_mode = "update"
                modal_entity_id = entity.id

    return (modal_is_open, modal_mode, modal_entity_id, ochw.to_json(), ochw.get_html_elements())
Ejemplo n.º 14
0
def update_odrive_montly_kilometer(selected_entity, filter_vehicle_type):
    oc = OrganizationChart()
    oc.load_current()
    service = oc.get_entity_by_id(selected_entity)
    return get_odrive_montly_kilometer(service.code_odrive,
                                       filter_vehicle_type)
Ejemplo n.º 15
0
def update_total_emissions_vehicles(selected_entity, filter_vehicle_type):
    oc = OrganizationChart()
    oc.load_current()
    service = oc.get_entity_by_id(selected_entity)
    return get_total_emisions_vehicles(service.code_odrive,
                                       filter_vehicle_type)
Ejemplo n.º 16
0
def on_click_go_to_dashboard(n_clicks, selected_entity):
    if n_clicks:
        oc = OrganizationChart()
        oc.load_current()
        entity = oc.get_entity_by_id(selected_entity)
        return dcc.Location(id="url-redirect-to-dashboard", pathname="/tableau_de_bord/%s" % entity.id)
Ejemplo n.º 17
0
def update_histogram_by_prestation(selected_entity, filter_vehicle_type):
    oc = OrganizationChart()
    oc.load_current()
    service = oc.get_entity_by_id(selected_entity)
    return get_histogram_by_entity_type(service.code_odrive,
                                        filter_vehicle_type)