File size: 3,485 Bytes
3c0348a
 
1ade1bc
 
2dbb80d
 
 
1ade1bc
 
68cc99d
 
 
 
2dbb80d
 
af95066
 
 
 
2dbb80d
 
 
3c0348a
 
 
 
 
 
 
 
 
 
2dbb80d
3c0348a
 
 
2dbb80d
3c0348a
 
 
 
 
 
 
 
2dbb80d
3c0348a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2dbb80d
897186c
3c0348a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from dash import Input, Output
import plotly.express as px
import os

from helpers.processor import Processor
from helpers.s3 import S3Client
from helpers.models import S3Config
from global_vars import BUCKET_NAME
from app import app
from dotenv import load_dotenv
load_dotenv(".env")
print("*********************c")
print(os.getenv("AWS_ENDPOINT_URL_S3"))
# Initialisation des clients
s3_config = S3Config(
    bucket_name=BUCKET_NAME,
    endpoint_url=os.getenv("AWS_ENDPOINT_URL_S3"),
    access_key=os.getenv("AWS_ACCESS_KEY_ID"),
    secret_key=os.getenv("AWS_SECRET_ACCESS_KEY")
)
s3_client = S3Client(config=s3_config)
processor = Processor()


@app.callback(
    [
        Output("chapter-dropdown-contrib", "options"),
        Output("page-dropdown-contrib", "options"),
    ],
    Input("chapter-dropdown-contrib", "value"),
)
def update_dropdowns(chapter_value):
    df_contributions = processor.get_contribution_data(s3_client)
    if df_contributions.empty:
        return [], []

    # chapitres unique
    chapters = sorted(df_contributions["chapter"].unique())
    chapter_options = [{"label": "Tous les chapitres", "value": "all"}] + [
        {"label": ch, "value": ch} for ch in chapters
    ]

    # Récupération des pages en fonction du chapitre sélectionné
    if chapter_value and chapter_value != "all":
        pages = sorted(
            df_contributions[df_contributions["chapter"] == chapter_value]["page"].unique()
        )
    else:
        pages = sorted(df_contributions["page"].unique())

    page_options = [{"label": "Toutes les pages", "value": "all"}] + [
        {"label": p, "value": p} for p in pages
    ]

    return chapter_options, page_options


@app.callback(
    [Output("contributions-graph", "figure"), Output("data-summary", "children")],
    [
        Input("chapter-dropdown-contrib", "value"),
        Input("page-dropdown-contrib", "value"),
        Input("mode-radio", "value"),
    ],
)
def update_graph(chapter, page, mode):
    df_contributions = processor.get_contribution_data(s3_client)

    if df_contributions.empty:
        return px.bar(title="Aucune donnée disponible."), "Aucune donnée chargée."

    filtered_df_contributions = df_contributions.copy()
    if chapter != "all":
        filtered_df_contributions = filtered_df_contributions[
            filtered_df_contributions["chapter"] == chapter
        ]
    if page != "all":
        filtered_df_contributions = filtered_df_contributions[
            filtered_df_contributions["page"] == page
        ]

    contributions = (
        filtered_df_contributions.groupby("user_id").size().reset_index(name="value")
    )

    if mode == "percentage":
        total = contributions["value"].sum()
        contributions["value"] = (contributions["value"] / total * 100).round(2)
        value_label = "Contributions (%)"
    else:
        value_label = "Nombre de Contributions"

    fig = px.bar(
        contributions,
        y="user_id",
        x="value",
        labels={"user_id": "Contributeur", "value": value_label},
        title=f"Résumé des Contributions ({chapter}/{page})",
        text="value",
        orientation="h",  # Barre horizontale
    )

    fig.update_traces(textposition="outside")
    fig.update_layout(
        xaxis_title=value_label, yaxis_title="ID Contributeur", bargap=0.2
    )

    summary = f"Affichage de {len(filtered_df_contributions)} contributions de {len(contributions)} contributeurs."

    return fig, summary