Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 46 additions & 43 deletions src/codeas/ui/pages/7_🔍_Usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

from codeas.core.usage_tracker import usage_tracker

COUNT_FORMAT = "{:,d}"
COST_FORMAT = "${:.2f}"


def read_usage_data(file_path: str):
log_file = Path(file_path)
Expand Down Expand Up @@ -46,7 +49,6 @@ def calculate_usage_by_days(usage_data):
usage_by_days[date]["Requests"] += 1
usage_by_days[date]["Conversations"].add(item["conversation_id"])

# Convert sets to counts
for date in usage_by_days:
usage_by_days[date]["Conversations"] = len(usage_by_days[date]["Conversations"])

Expand All @@ -56,7 +58,7 @@ def calculate_usage_by_days(usage_data):
def calculate_usage_by_model(usage_data):
usage_by_model = {}
for item in usage_data:
model = item["model"] # Correct access to the model
model = item["model"]
if model not in usage_by_model:
usage_by_model[model] = {"Requests": 0, "Cost": 0}
usage_by_model[model]["Requests"] += 1
Expand All @@ -70,30 +72,31 @@ def calculate_usage_by_generator(usage_data):
generator = item["generator"]
if generator not in usage_by_generator:
usage_by_generator[generator] = {"Requests": 0, "Cost": 0}
usage_by_generator[generator]["Requests"] += 1
usage_by_generator[generator]["Cost"] += item["cost"]["total_cost"]
usage_by_generator[generator]["Requests"] += 1

return usage_by_generator


def display_usage_metrics(usage_data):
col1, col2, col3 = st.columns(3)

with col1:
st.metric("💬 Total conversations", count_n_conversations(usage_data))
st.metric("\ud83d\udcac Total conversations", count_n_conversations(usage_data))
with col2:
st.metric("🔢 Total requests", count_n_requests(usage_data))
st.metric("\ud83d\udd22 Total requests", count_n_requests(usage_data))
with col3:
total_cost = calculate_total_cost(usage_data)
st.metric("💰 Total cost", f"${total_cost:.2f}")
st.metric("\ud83d\udcb0 Total cost", f"{total_cost:.2f}")


def display_generator_metrics(usage_data):
col1, col2 = st.columns(2)
with col1:
st.metric("🔢 Total requests", count_n_requests(usage_data))
st.metric("\ud83d\udd22 Total requests", count_n_requests(usage_data))
with col2:
total_cost = calculate_total_cost(usage_data)
st.metric("💰 Total cost", f"${total_cost:.2f}")
st.metric("\ud83d\udcb0 Total cost", f"{total_cost:.2f}")


def display_generator_by_generator(usage_data):
Expand All @@ -105,7 +108,7 @@ def display_generator_by_generator(usage_data):
df["Percentage"] = df["Requests"] / total_requests * 100
st.dataframe(
df.set_index("Generator").style.format(
{"Cost": "${:.2f}", "Requests": "{:,d}", "Percentage": "{:.2f}%"}
{"Cost": COST_FORMAT, "Requests": COUNT_FORMAT, "Percentage": "{:.2f}%"}
)
)

Expand All @@ -125,10 +128,10 @@ def display_usage_by_day(df):
st.dataframe(
df.set_index("Date").style.format(
{
"Cost": "${:.2f}",
"Cumulative Cost": "${:.2f}",
"Requests": "{:,d}",
"Conversations": "{:,d}",
"Cost": COST_FORMAT,
"Cumulative Cost": COST_FORMAT,
"Requests": COUNT_FORMAT,
"Conversations": COUNT_FORMAT,
}
)
)
Expand All @@ -147,7 +150,7 @@ def prepare_usage_by_model_df(usage_data):
def display_usage_by_model(model_df):
st.dataframe(
model_df.set_index("Model").style.format(
{"Cost": "${:.2f}", "Requests": "{:,d}", "Percentage": "{:.2f}%"}
{"Cost": COST_FORMAT, "Requests": COUNT_FORMAT, "Percentage": "{:.2f}%"}
)
)

Expand All @@ -157,11 +160,11 @@ def display_chat_usage():

display_usage_metrics(usage_data)
if any(usage_data):
st.subheader("📅 Usage by day")
st.subheader("\ud83d\udcc5 Usage by day")
usage_by_day_df = prepare_usage_by_day_df(usage_data)
display_usage_by_day(usage_by_day_df)
if any(usage_data):
st.subheader("🤖 Usage by model")
st.subheader("\ud83e\udd16 Usage by model")
usage_by_model_df = prepare_usage_by_model_df(usage_data)
display_usage_by_model(usage_by_model_df)

Expand All @@ -170,7 +173,7 @@ def display_prompt_generator_usage():
usage_data = usage_tracker.load_data().get("generator", [])
display_generator_metrics(usage_data)
if any(usage_data):
st.subheader("🤖 Usage by generator")
st.subheader("\ud83e\udd16 Usage by generator")
display_generator_by_generator(usage_data)


Expand All @@ -183,13 +186,13 @@ def display_use_cases_usage():


def display_documentation_usage(usage_data):
st.subheader("📚 Documentation")
st.subheader("\ud83d\udcda Documentation")
docs_data = usage_data.get("generate_docs", {})
col1, col2 = st.columns(2)
with col1:
st.metric("🔢 Total requests", docs_data.get("count", 0))
st.metric("\ud83d\udd22 Total requests", docs_data.get("count", 0))
with col2:
st.metric("💰 Total cost", f"${docs_data.get('total_cost', 0):.2f}")
st.metric("\ud83d\udcb0 Total cost", f"{docs_data.get('total_cost', 0):.2f}")

with st.expander("Sections"):
sections = [
Expand Down Expand Up @@ -218,69 +221,69 @@ def display_documentation_usage(usage_data):
df = pd.DataFrame(sections_data)
st.dataframe(
df.set_index("Section").style.format(
{"Requests": "{:,d}", "Cost": "${:.2f}"}
{"Requests": COUNT_FORMAT, "Cost": COST_FORMAT}
),
)


def display_deployment_usage(usage_data):
st.subheader("🚀 Deployment")
st.subheader("\ud83d\ude80 Deployment")
col1, col2 = st.columns(2)
with col1:
st.write("**Defining deployment strategy**:")
define_data = usage_data.get("define_deployment", {})
st.metric("🔢 Total requests", define_data.get("count", 0))
st.metric("💰 Total cost", f"${define_data.get('total_cost', 0):.2f}")
st.metric("\ud83d\udd22 Total requests", define_data.get("count", 0))
st.metric("\ud83d\udcb0 Total cost", f"{define_data.get('total_cost', 0):.2f}")
with col2:
st.write("**Generating deployment**:")
generate_data = usage_data.get("generate_deployment", {})
st.metric("🔢 Total requests", generate_data.get("count", 0))
st.metric("💰 Total cost", f"${generate_data.get('total_cost', 0):.2f}")
st.metric("\ud83d\udd22 Total requests", generate_data.get("count", 0))
st.metric("\ud83d\udcb0 Total cost", f"{generate_data.get('total_cost', 0):.2f}")


def display_testing_usage(usage_data):
st.subheader("🧪 Testing")
st.subheader("\ud83e\uddea Testing")
col1, col2 = st.columns(2)
with col1:
st.write("**Defining testing strategy**:")
strategy_data = usage_data.get("define_testing_strategy", {})
st.metric("🔢 Total requests", strategy_data.get("count", 0))
st.metric("💰 Total cost", f"${strategy_data.get('total_cost', 0):.2f}")
st.metric("\ud83d\udd22 Total requests", strategy_data.get("count", 0))
st.metric("\ud83d\udcb0 Total cost", f"{strategy_data.get('total_cost', 0):.2f}")
with col2:
st.write("**Generating tests from strategy**:")
tests_data = usage_data.get("generate_tests_from_strategy", {})
st.metric("🔢 Total requests", tests_data.get("count", 0))
st.metric("💰 Total cost", f"${tests_data.get('total_cost', 0):.2f}")
st.metric("\ud83d\udd22 Total requests", tests_data.get("count", 0))
st.metric("\ud83d\udcb0 Total cost", f"{tests_data.get('total_cost', 0):.2f}")


def display_refactoring_usage(usage_data):
st.subheader("🔄 Refactoring")
st.subheader("\ud83d\udd04 Refactoring")
col1, col2, col3 = st.columns(3)
with col1:
st.write("**Defining refactoring files**:")
define_data = usage_data.get("define_refactoring_files", {})
st.metric("🔢 Total requests", define_data.get("count", 0))
st.metric("💰 Total cost", f"${define_data.get('total_cost', 0):.2f}")
st.metric("\ud83d\udd22 Total requests", define_data.get("count", 0))
st.metric("\ud83d\udcb0 Total cost", f"{define_data.get('total_cost', 0):.2f}")
with col2:
st.write("**Generating proposed changes**:")
propose_data = usage_data.get("generate_proposed_changes", {})
st.metric("🔢 Total requests", propose_data.get("count", 0))
st.metric("💰 Total cost", f"${propose_data.get('total_cost', 0):.2f}")
st.metric("\ud83d\udd22 Total requests", propose_data.get("count", 0))
st.metric("\ud83d\udcb0 Total cost", f"{propose_data.get('total_cost', 0):.2f}")
with col3:
st.write("**Generating diffs**:")
diffs_data = usage_data.get("generate_diffs", {})
st.metric("🔢 Total requests", diffs_data.get("count", 0))
st.metric("💰 Total cost", f"${diffs_data.get('total_cost', 0):.2f}")
st.metric("\ud83d\udd22 Total requests", diffs_data.get("count", 0))
st.metric("\ud83d\udcb0 Total cost", f"{diffs_data.get('total_cost', 0):.2f}")


def usage_page():
st.subheader("🔍 Usage")
with st.expander("Chat", icon="💬", expanded=True):
st.subheader("\ud83d\udd0d Usage")
with st.expander("Chat", icon="\ud83d\udcac", expanded=True):
display_chat_usage()
with st.expander("Prompt generator", icon="📝", expanded=True):
with st.expander("Prompt generator", icon="\ud83d\udcdd", expanded=True):
display_prompt_generator_usage()
with st.expander("Use cases", icon="🤖", expanded=True):
with st.expander("Use cases", icon="\ud83e\udd16", expanded=True):
display_use_cases_usage()


usage_page()
usage_page()