File size: 12,654 Bytes
f29bf1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b153ebd
f29bf1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b153ebd
f29bf1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b153ebd
f29bf1c
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""
High-level service for interacting with dados.gov.br API.

This service provides business logic and data transformation
for the Brazilian Open Data Portal integration.
"""

import logging
from typing import Any, Dict, List, Optional, Tuple

from src.core.exceptions import ValidationError
from src.services.cache_service import CacheService, CacheTTL
from src.tools.dados_gov_api import DadosGovAPIClient, DadosGovAPIError
from src.tools.dados_gov_models import (
    Dataset,
    DatasetSearchResult,
    Organization,
    Resource,
    ResourceSearchResult,
)

logger = logging.getLogger(__name__)


class DadosGovService:
    """
    Service for accessing and analyzing data from dados.gov.br.
    
    This service provides high-level methods for searching datasets,
    analyzing data availability, and retrieving government open data.
    """
    
    def __init__(self, api_key: Optional[str] = None):
        """
        Initialize the dados.gov.br service.
        
        Args:
            api_key: Optional API key for authentication
        """
        self.client = DadosGovAPIClient(api_key=api_key)
        self.cache = CacheService()
        
    async def close(self):
        """Close service connections"""
        await self.client.close()
        
    async def search_transparency_datasets(
        self,
        keywords: Optional[List[str]] = None,
        organization: Optional[str] = None,
        data_format: Optional[str] = None,
        limit: int = 20,
    ) -> DatasetSearchResult:
        """
        Search for transparency-related datasets.
        
        Args:
            keywords: Keywords to search for (e.g., ["transparência", "gastos", "contratos"])
            organization: Filter by specific organization
            data_format: Preferred data format (csv, json, xml)
            limit: Maximum number of results
            
        Returns:
            Search results with relevant datasets
        """
        # Build search query
        query_parts = []
        if keywords:
            query_parts.extend(keywords)
        else:
            # Default transparency-related keywords
            query_parts.extend([
                "transparência",
                "gastos públicos",
                "contratos",
                "licitações",
                "servidores",
            ])
            
        query = " OR ".join(query_parts)
        
        # Check cache
        cache_key = f"dados_gov:search:{query}:{organization}:{data_format}:{limit}"
        cached_result = await self.cache.get(cache_key)
        if cached_result:
            return DatasetSearchResult(**cached_result)
            
        try:
            # Search datasets
            result = await self.client.search_datasets(
                query=query,
                organization=organization,
                format=data_format,
                limit=limit,
            )
            
            # Parse response
            search_result = DatasetSearchResult(
                count=result.get("count", 0),
                results=[Dataset(**ds) for ds in result.get("results", [])],
                facets=result.get("facets", {}),
                search_facets=result.get("search_facets", {}),
            )
            
            # Cache result
            await self.cache.set(
                cache_key,
                search_result.model_dump(),
                ttl=CacheTTL.MEDIUM.value,
            )
            
            return search_result
            
        except DadosGovAPIError as e:
            logger.error(f"Error searching datasets: {e}")
            raise
            
    async def get_dataset_with_resources(self, dataset_id: str) -> Dataset:
        """
        Get complete dataset information including all resources.
        
        Args:
            dataset_id: Dataset identifier
            
        Returns:
            Complete dataset with resources
        """
        # Check cache
        cache_key = f"dados_gov:dataset:{dataset_id}"
        cached_dataset = await self.cache.get(cache_key)
        if cached_dataset:
            return Dataset(**cached_dataset)
            
        try:
            # Get dataset details
            result = await self.client.get_dataset(dataset_id)
            dataset = Dataset(**result.get("result", {}))
            
            # Cache result
            await self.cache.set(
                cache_key,
                dataset.model_dump(),
                ttl=CacheTTL.LONG.value,
            )
            
            return dataset
            
        except DadosGovAPIError as e:
            logger.error(f"Error getting dataset {dataset_id}: {e}")
            raise
            
    async def find_government_spending_data(
        self,
        year: Optional[int] = None,
        state: Optional[str] = None,
        city: Optional[str] = None,
    ) -> List[Dataset]:
        """
        Find datasets related to government spending.
        
        Args:
            year: Filter by specific year
            state: Filter by state (e.g., "SP", "RJ")
            city: Filter by city name
            
        Returns:
            List of relevant datasets
        """
        # Build search query
        query_parts = ["gastos", "despesas", "pagamentos", "execução orçamentária"]
        
        if year:
            query_parts.append(str(year))
        if state:
            query_parts.append(state)
        if city:
            query_parts.append(city)
            
        query = " ".join(query_parts)
        
        # Search for datasets
        result = await self.search_transparency_datasets(
            keywords=[query],
            data_format="csv",  # Prefer CSV for analysis
            limit=50,
        )
        
        # Filter results by relevance
        relevant_datasets = []
        for dataset in result.results:
            # Check if dataset is relevant based on title and description
            title_lower = dataset.title.lower()
            notes_lower = (dataset.notes or "").lower()
            
            if any(term in title_lower or term in notes_lower 
                   for term in ["gasto", "despesa", "pagamento", "execução"]):
                relevant_datasets.append(dataset)
                
        return relevant_datasets
        
    async def find_procurement_data(
        self,
        organization: Optional[str] = None,
        modality: Optional[str] = None,
    ) -> List[Dataset]:
        """
        Find datasets related to public procurement and contracts.
        
        Args:
            organization: Filter by organization
            modality: Procurement modality (e.g., "pregão", "concorrência")
            
        Returns:
            List of procurement-related datasets
        """
        keywords = ["licitação", "contratos", "pregão", "compras públicas"]
        if modality:
            keywords.append(modality)
            
        result = await self.search_transparency_datasets(
            keywords=keywords,
            organization=organization,
            limit=30,
        )
        
        return result.results
        
    async def analyze_data_availability(
        self,
        topic: str,
    ) -> Dict[str, Any]:
        """
        Analyze what data is available for a specific topic.
        
        Args:
            topic: Topic to analyze (e.g., "educação", "saúde", "segurança")
            
        Returns:
            Analysis of available data including formats, organizations, and coverage
        """
        # Search for topic-related datasets
        result = await self.search_transparency_datasets(
            keywords=[topic],
            limit=100,
        )
        
        # Analyze results
        analysis = {
            "topic": topic,
            "total_datasets": result.count,
            "analyzed_datasets": len(result.results),
            "organizations": {},
            "formats": {},
            "years_covered": set(),
            "geographic_coverage": {
                "federal": 0,
                "state": 0,
                "municipal": 0,
            },
            "update_frequency": {
                "daily": 0,
                "monthly": 0,
                "yearly": 0,
                "unknown": 0,
            },
        }
        
        # Process each dataset
        for dataset in result.results:
            # Count by organization
            if dataset.organization:
                org_name = dataset.organization.title
                analysis["organizations"][org_name] = (
                    analysis["organizations"].get(org_name, 0) + 1
                )
                
            # Count by format
            for resource in dataset.resources:
                if resource.format:
                    fmt = resource.format.upper()
                    analysis["formats"][fmt] = analysis["formats"].get(fmt, 0) + 1
                    
            # Extract years from title/description
            import re
            text = f"{dataset.title} {dataset.notes or ''}"
            years = re.findall(r'\b(19|20)\d{2}\b', text)
            analysis["years_covered"].update(years)
            
            # Detect geographic coverage
            text_lower = text.lower()
            if any(term in text_lower for term in ["federal", "brasil", "nacional"]):
                analysis["geographic_coverage"]["federal"] += 1
            elif any(term in text_lower for term in ["estado", "estadual", "uf"]):
                analysis["geographic_coverage"]["state"] += 1
            elif any(term in text_lower for term in ["município", "municipal", "cidade"]):
                analysis["geographic_coverage"]["municipal"] += 1
                
            # Detect update frequency
            if any(term in text_lower for term in ["diário", "diariamente"]):
                analysis["update_frequency"]["daily"] += 1
            elif any(term in text_lower for term in ["mensal", "mensalmente"]):
                analysis["update_frequency"]["monthly"] += 1
            elif any(term in text_lower for term in ["anual", "anualmente"]):
                analysis["update_frequency"]["yearly"] += 1
            else:
                analysis["update_frequency"]["unknown"] += 1
                
        # Convert years set to sorted list
        analysis["years_covered"] = sorted(list(analysis["years_covered"]))
        
        # Sort organizations by dataset count
        analysis["organizations"] = dict(
            sorted(
                analysis["organizations"].items(),
                key=lambda x: x[1],
                reverse=True,
            )[:10]  # Top 10 organizations
        )
        
        return analysis
        
    async def get_resource_download_url(self, resource_id: str) -> str:
        """
        Get the download URL for a specific resource.
        
        Args:
            resource_id: Resource identifier
            
        Returns:
            Direct download URL
        """
        try:
            result = await self.client.get_resource(resource_id)
            resource = Resource(**result.get("result", {}))
            return resource.url
        except DadosGovAPIError as e:
            logger.error(f"Error getting resource {resource_id}: {e}")
            raise
            
    async def list_government_organizations(self) -> List[Organization]:
        """
        List all government organizations that publish open data.
        
        Returns:
            List of organizations sorted by dataset count
        """
        # Check cache
        cache_key = "dados_gov:organizations"
        cached_orgs = await self.cache.get(cache_key)
        if cached_orgs:
            return [Organization(**org) for org in cached_orgs]
            
        try:
            # Get organizations
            result = await self.client.list_organizations()
            organizations = [
                Organization(**org) 
                for org in result.get("result", [])
            ]
            
            # Sort by package count
            organizations.sort(
                key=lambda x: x.package_count or 0,
                reverse=True,
            )
            
            # Cache result
            await self.cache.set(
                cache_key,
                [org.model_dump() for org in organizations],
                ttl=CacheTTL.LONG.value,
            )
            
            return organizations
            
        except DadosGovAPIError as e:
            logger.error(f"Error listing organizations: {e}")
            raise