Skip to content

Commit 33ba032

Browse files
authored
Merge pull request #148 from ghinks/feat/cache-clearing
feat: add cache management CLI options and remove unused functions
2 parents edd9c24 + 9a96655 commit 33ba032

File tree

4 files changed

+74
-62
lines changed

4 files changed

+74
-62
lines changed

reviewtally/cache/sqlite_cache.py

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -224,36 +224,6 @@ def set_pr_review( # noqa: PLR0913
224224

225225
conn.commit()
226226

227-
def delete_pr_review(
228-
self,
229-
owner: str,
230-
repo: str,
231-
pull_number: int,
232-
) -> bool:
233-
"""
234-
Delete a PR review cache entry.
235-
236-
Args:
237-
owner: Repository owner
238-
repo: Repository name
239-
pull_number: Pull request number
240-
241-
Returns:
242-
True if entry was deleted, False if not found
243-
244-
"""
245-
conn = self._get_connection()
246-
247-
cursor = conn.execute(
248-
"""
249-
DELETE FROM pr_reviews_cache
250-
WHERE owner = ? AND repo = ? AND pull_number = ?
251-
""",
252-
(owner, repo, pull_number),
253-
)
254-
conn.commit()
255-
return cursor.rowcount > 0
256-
257227
# PR Metadata cache methods
258228

259229
def get_pr_metadata(
@@ -344,36 +314,6 @@ def set_pr_metadata( # noqa: PLR0913
344314

345315
conn.commit()
346316

347-
def delete_pr_metadata(
348-
self,
349-
owner: str,
350-
repo: str,
351-
pr_number: int,
352-
) -> bool:
353-
"""
354-
Delete a PR metadata cache entry.
355-
356-
Args:
357-
owner: Repository owner
358-
repo: Repository name
359-
pr_number: Pull request number
360-
361-
Returns:
362-
True if entry was deleted, False if not found
363-
364-
"""
365-
conn = self._get_connection()
366-
367-
cursor = conn.execute(
368-
"""
369-
DELETE FROM pr_metadata_cache
370-
WHERE owner = ? AND repo = ? AND pr_number = ?
371-
""",
372-
(owner, repo, pr_number),
373-
)
374-
conn.commit()
375-
return cursor.rowcount > 0
376-
377317
def get_pr_metadata_date_range(
378318
self,
379319
owner: str,

reviewtally/cli/parse_cmd_line.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class CommandLineArgs(TypedDict):
3030
plot_individual: bool
3131
individual_chart_metric: str
3232
use_cache: bool
33+
clear_cache: bool
34+
clear_expired_cache: bool
35+
show_cache_stats: bool
3336

3437

3538
def print_toml_version() -> None:
@@ -164,6 +167,21 @@ def parse_cmd_line() -> CommandLineArgs: # noqa: C901, PLR0912, PLR0915
164167
action="store_true",
165168
help="Disable PR review caching (always fetch fresh data from API)",
166169
)
170+
parser.add_argument(
171+
"--clear-cache",
172+
action="store_true",
173+
help="Clear all cached data and exit",
174+
)
175+
parser.add_argument(
176+
"--clear-expired-cache",
177+
action="store_true",
178+
help="Clear only expired cached data and exit",
179+
)
180+
parser.add_argument(
181+
"--cache-stats",
182+
action="store_true",
183+
help="Show cache statistics and exit",
184+
)
167185

168186
args = parser.parse_args()
169187
# catch ValueError if the date format is not correct
@@ -236,4 +254,7 @@ def parse_cmd_line() -> CommandLineArgs: # noqa: C901, PLR0912, PLR0915
236254
plot_individual=args.plot_individual,
237255
individual_chart_metric=args.individual_chart_metric,
238256
use_cache=not args.no_cache,
257+
clear_cache=args.clear_cache,
258+
clear_expired_cache=args.clear_expired_cache,
259+
show_cache_stats=args.cache_stats,
239260
)

reviewtally/main.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from __future__ import annotations
22

3+
import sys
34
import time
45
from typing import Any
56

67
from tqdm import tqdm
78

89
from reviewtally.analysis.sprint_periods import calculate_sprint_periods
910
from reviewtally.analysis.team_metrics import calculate_sprint_team_metrics
11+
from reviewtally.cache.cache_manager import CacheManager
1012
from reviewtally.cli.parse_cmd_line import CommandLineArgs, parse_cmd_line
1113
from reviewtally.data_collection import (
1214
ProcessRepositoriesContext,
@@ -32,6 +34,15 @@ def main() -> None:
3234
# Parse command line arguments
3335
args = parse_cmd_line()
3436

37+
# Handle cache management operations
38+
if (
39+
args["clear_cache"]
40+
or args["clear_expired_cache"]
41+
or args["show_cache_stats"]
42+
):
43+
_handle_cache_operations(args)
44+
sys.exit(0)
45+
3546
# Get repositories
3647
timestamped_print(
3748
f"Calling get_repos_by_language {time.time() - start_time:.2f} "
@@ -191,5 +202,45 @@ def _handle_sprint_plotting(context: SprintPlottingContext) -> None:
191202
print(f"Plotting failed: {e}") # noqa: T201
192203

193204

205+
def _handle_cache_operations(args: CommandLineArgs) -> None:
206+
"""Handle cache management operations."""
207+
cache_manager = CacheManager()
208+
209+
if args["show_cache_stats"]:
210+
if cache_manager.cache:
211+
stats = cache_manager.cache.get_stats()
212+
print("Cache Statistics:") # noqa: T201
213+
print(f" Database path: {stats['db_path']}") # noqa: T201
214+
print(f" Total entries: {stats['total_entries']}") # noqa: T201
215+
print(f" Valid entries: {stats['valid_entries']}") # noqa: T201
216+
print(f" Expired entries: {stats['expired_entries']}") # noqa: T201
217+
print(f" Cache size: {stats['cache_size_mb']} MB") # noqa: T201
218+
print(f" Database size: {stats['db_size_mb']} MB") # noqa: T201
219+
print("\nBy Table:") # noqa: T201
220+
for table_name, table_stats in stats["by_table"].items():
221+
print(f" {table_name}:") # noqa: T201
222+
print(f" Total: {table_stats['total']}") # noqa: T201
223+
print(f" Valid: {table_stats['valid']}") # noqa: T201
224+
print(f" Expired: {table_stats['expired']}") # noqa: T201
225+
size_mb = table_stats["size_bytes"] / (1024 * 1024)
226+
print(f" Size: {size_mb:.2f} MB") # noqa: T201
227+
else:
228+
print("Cache is disabled") # noqa: T201
229+
230+
if args["clear_expired_cache"]:
231+
if cache_manager.cache:
232+
removed = cache_manager.cache.cleanup_expired()
233+
print(f"Cleared {removed} expired cache entries") # noqa: T201
234+
else:
235+
print("Cache is disabled") # noqa: T201
236+
237+
if args["clear_cache"]:
238+
if cache_manager.cache:
239+
removed = cache_manager.cache.clear_all()
240+
print(f"Cleared all {removed} cache entries") # noqa: T201
241+
else:
242+
print("Cache is disabled") # noqa: T201
243+
244+
194245
if __name__ == "__main__":
195246
main()

tests/cli/test_parse_cmd_line.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ def test_valid_dates_no_exit(self, mock_argv: Any, mock_exit: Any) -> None:
212212
# Assert
213213
mock_exit.assert_not_called()
214214
self.assertIsInstance(result, dict)
215-
# Check it's the right type
216-
self.assertEqual(len(result), 14)
215+
# Check it's the right type (14 original + 3 cache options = 17)
216+
self.assertEqual(len(result), 17)
217217

218218
# Verify the parsed dates
219219
self.assertEqual(result["org_name"], "test-org")

0 commit comments

Comments
 (0)