Return a formatted token-savings report.
(path: Path | None = None)
| 163 | |
| 164 | |
| 165 | def format_savings_report(path: Path | None = None) -> str: |
| 166 | """Return a formatted token-savings report.""" |
| 167 | if path is None: |
| 168 | path = _get_stats_file() |
| 169 | if not path.exists(): |
| 170 | return "No stats yet. Run a search first." |
| 171 | |
| 172 | summary = build_savings_summary(path) |
| 173 | color = _use_color() |
| 174 | bar_width = 24 |
| 175 | border_width = 72 |
| 176 | heavy_line = " " + _color("38;5;244", "═" * border_width, color) |
| 177 | light_line = " " + _color("38;5;244", "─" * border_width, color) |
| 178 | |
| 179 | all_time = summary.buckets["All time"] |
| 180 | total_saved_tokens = all_time.saved_chars // 4 |
| 181 | overall_pct = round(all_time.saved_chars / all_time.file_chars * 100) if all_time.file_chars else 0 |
| 182 | efficiency_filled = round(overall_pct / 100 * bar_width) |
| 183 | efficiency_bar = _color("32", "█" * efficiency_filled, color) |
| 184 | efficiency_bar += _color("38;5;244", "░" * (bar_width - efficiency_filled), color) |
| 185 | |
| 186 | lines = [ |
| 187 | "", |
| 188 | " " + _color("1;36", "Semble Token Savings", color), |
| 189 | heavy_line, |
| 190 | "", |
| 191 | f" {_color('1', 'Total saved:', color)} " |
| 192 | f"{_color('1;33', _format_token_count(total_saved_tokens) + ' tokens', color)} " |
| 193 | f"({_color_ratio(overall_pct, color)})", |
| 194 | f" {_color('1', 'Total calls:', color)} {_color('1;33', _format_calls(all_time.calls), color)}", |
| 195 | f" {_color('1', 'Efficiency:', color)} {efficiency_bar} {_color_ratio(overall_pct, color)}", |
| 196 | "", |
| 197 | " " + _color("1", "By Period", color), |
| 198 | light_line, |
| 199 | f" {'Period':<14} {'Calls':>8} {'Saved':>14} Ratio", |
| 200 | light_line, |
| 201 | ] |
| 202 | for label, bucket in summary.buckets.items(): |
| 203 | saved_tokens = bucket.saved_chars // 4 |
| 204 | saved_str = _format_token_count(saved_tokens) + " tokens" |
| 205 | calls_str = _format_calls(bucket.calls) |
| 206 | if bucket.file_chars > 0: |
| 207 | ratio = bucket.saved_chars / bucket.file_chars |
| 208 | filled = round(ratio * bar_width) |
| 209 | row_bar = _color("32", "█" * filled, color) + _color("38;5;244", "░" * (bar_width - filled), color) |
| 210 | ratio_str = _color_ratio(round(ratio * 100), color) |
| 211 | else: |
| 212 | row_bar = _color("38;5;244", "░" * bar_width, color) |
| 213 | ratio_str = _color("38;5;244", "–", color) |
| 214 | lines.append( |
| 215 | f" {_color('1', f'{label:<14}', color)} {_color('1;33', f'{calls_str:>8}', color)} " |
| 216 | f"{_color('1;33', f'{saved_str:>14}', color)} {row_bar} {ratio_str}" |
| 217 | ) |
| 218 | |
| 219 | if summary.call_type_counts: |
| 220 | lines += [ |
| 221 | "", |
| 222 | " " + _color("1", "By Call Type", color), |