File size: 2,365 Bytes
e62c239
 
 
 
 
 
 
 
 
38d3eed
 
e62c239
a8853dd
e62c239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Any, Dict, Optional
from smolagents.tools import Tool
from tools.car_sharing_db import CarSharingDB

class GetMonthlyStatsTool(Tool):
    name = "get_monthly_stats"
    description = "Retrieves monthly car sharing statistics for a specific user."
    inputs = {
        'user_name': {'type': 'string', 'description': 'Name of the user to get statistics for'},
        'month': {'type': 'integer', 'description': 'Month number (1-12). If not provided, current month is used.', 'nullable': True},
        'year': {'type': 'integer', 'description': 'Year (e.g., 2023). If not provided, current year is used.', 'nullable': True}
    }
    output_type = "any"

    def __init__(self, db_path="car_sharing.db"):
        self.db = CarSharingDB(db_path)
        self.is_initialized = True

    def forward(self, user_name: str, month: Optional[int] = None, year: Optional[int] = None) -> Dict[str, Any]:
        """
        Get monthly car sharing statistics for a user.
        
        Args:
            user_name: Name of the user to get statistics for
            month: Month number (1-12). If not provided, current month is used.
            year: Year (e.g., 2023). If not provided, current year is used.
            
        Returns:
            A dictionary with monthly statistics
        """
        try:
            # Convert month and year to integers if provided
            if month is not None:
                month = int(month)
                if month < 1 or month > 12:
                    return {
                        "success": False,
                        "error": "Month must be between 1 and 12",
                        "total_km": 0
                    }
            
            if year is not None:
                year = int(year)
            
            # Get monthly statistics
            stats = self.db.get_monthly_stats(user_name, month, year)
            
            # Add success flag
            stats["success"] = True
            
            return stats
        except ValueError:
            return {
                "success": False,
                "error": "Month and year must be integers",
                "total_km": 0
            }
        except Exception as e:
            return {
                "success": False,
                "error": str(e),
                "total_km": 0
            }