-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcsv-processor.ts
More file actions
530 lines (463 loc) · 14.7 KB
/
csv-processor.ts
File metadata and controls
530 lines (463 loc) · 14.7 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/**
* CSV Data Processor - Production-Ready
*
* Processes ANP (Agência Nacional do Petróleo) CSV data with:
* - Robust error handling and validation
* - Type safety throughout the pipeline
* - Performance optimization with caching
* - Proper encoding handling (windows-1252 → UTF-8)
* - Defensive programming against malformed data
*/
import fs from 'fs/promises';
import path from 'path';
import { parse } from 'csv-parse/sync';
import iconv from 'iconv-lite';
import type {
GasPriceRaw,
GasStation,
} from '@/types/gas';
import { isValidGasPriceRaw } from '@/types/gas';
// ============================================================================
// CONSTANTS
// ============================================================================
/**
* ANP (Agência Nacional do Petróleo) official data source URL
* Always fetch live data from government portal
*/
const ANP_DATA_URL = 'https://www.gov.br/anp/pt-br/centrais-de-conteudo/dados-abertos/arquivos/shpc/qus/ultimas-4-semanas-glp.csv';
/**
* Fallback: Local CSV file path (for development/testing only)
*/
const FALLBACK_CSV_PATH = path.join(
process.cwd(),
'data',
'ultimas-4-semanas-glp.csv'
);
/**
* Cache duration in milliseconds (30 minutes)
* Live data is cached to reduce load on government servers
*/
const CACHE_DURATION_MS = 30 * 60 * 1000;
// ============================================================================
// TYPES
// ============================================================================
interface ProcessorCache {
data: GasStation[];
timestamp: number;
}
interface ProcessingStats {
totalRecords: number;
validRecords: number;
invalidRecords: number;
duplicatesRemoved: number;
processingTimeMs: number;
}
// ============================================================================
// CACHE
// ============================================================================
let cache: ProcessorCache | null = null;
/**
* Checks if cached data is still valid
*/
function isCacheValid(): boolean {
if (!cache) return false;
const now = Date.now();
return (now - cache.timestamp) < CACHE_DURATION_MS;
}
/**
* Clears the cache (useful for testing or manual refresh)
*/
export function clearCache(): void {
cache = null;
}
// ============================================================================
// VALIDATION & TRANSFORMATION
// ============================================================================
/**
* Parses a Brazilian-formatted price string to number
*
* @param priceStr - Price string like "115,00" or "115.00"
* @returns Parsed number or NaN if invalid
*
* @example
* ```typescript
* parsePrice("115,00") // 115.00
* parsePrice("115.50") // 115.50
* parsePrice("invalid") // NaN
* ```
*/
function parsePrice(priceStr: string): number {
if (!priceStr || typeof priceStr !== 'string') {
return NaN;
}
// Remove whitespace and replace comma with dot
const normalized = priceStr.trim().replace(',', '.');
const parsed = parseFloat(normalized);
// Validate: must be positive, finite number
if (isNaN(parsed) || !isFinite(parsed) || parsed <= 0) {
return NaN;
}
return parsed;
}
/**
* Parses a Brazilian date string (DD/MM/YYYY) to Date object
*
* @param dateStr - Date string in DD/MM/YYYY format
* @returns Date object or current date if invalid
*
* @example
* ```typescript
* parseDate("22/07/2025") // Date object for July 22, 2025
* parseDate("invalid") // Current date
* ```
*/
function parseDate(dateStr: string): Date {
if (!dateStr || typeof dateStr !== 'string') {
return new Date();
}
const parts = dateStr.trim().split('/');
if (parts.length !== 3) {
return new Date();
}
const day = parseInt(parts[0] ?? '1', 10);
const month = parseInt(parts[1] ?? '1', 10) - 1; // JS months are 0-indexed
const year = parseInt(parts[2] ?? '2024', 10);
const date = new Date(year, month, day);
// Validate date is valid and not in the future
if (isNaN(date.getTime()) || date > new Date()) {
return new Date();
}
return date;
}
/**
* Builds a formatted full address from address components
*
* @param raw - Raw CSV record
* @returns Formatted address string
*/
function buildFullAddress(raw: GasPriceRaw): string {
const parts: string[] = [];
// Street name and number
if (raw['Nome da Rua']) {
const street = raw['Nome da Rua'].trim();
const number = raw['Numero Rua']?.trim();
parts.push(number ? `${street}, ${number}` : street);
}
// Neighborhood
if (raw['Bairro']) {
parts.push(raw['Bairro'].trim());
}
// CEP
if (raw['Cep']) {
parts.push(`CEP ${raw['Cep'].trim()}`);
}
return parts.join(' - ') || 'Endereço não informado';
}
/**
* Transforms a raw CSV record into a typed GasStation object
*
* @param raw - Raw CSV record
* @returns GasStation object or null if transformation fails
*/
function transformRawToStation(raw: GasPriceRaw): GasStation | null {
try {
// Validate input
if (!isValidGasPriceRaw(raw)) {
return null;
}
// Filter: Only process GLP product records
if (raw['Produto']?.trim().toUpperCase() !== 'GLP') {
return null;
}
// Parse and validate price
const price = parsePrice(raw['Valor de Venda']);
if (isNaN(price)) {
return null;
}
// Parse date
const lastUpdate = parseDate(raw['Data da Coleta']);
// Build full address
const fullAddress = buildFullAddress(raw);
// Normalize city name (remove extra spaces, convert to title case)
const city = raw['Municipio']
.trim()
.split(' ')
.map((word) => {
// Keep acronyms uppercase (e.g., "SAO PAULO" stays "SAO PAULO")
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
})
.join(' ');
return {
id: raw['CNPJ da Revenda'].trim(),
name: raw['Revenda'].trim(),
price,
city,
state: raw['Estado - Sigla'].trim().toUpperCase(),
neighborhood: raw['Bairro']?.trim() || 'Não informado',
flag: raw['Bandeira'].trim(),
lastUpdate,
fullAddress,
};
} catch (error) {
// Silently skip invalid records
console.warn('Failed to transform record:', error);
return null;
}
}
// ============================================================================
// CSV PARSING
// ============================================================================
/**
* Fetches CSV data from ANP government URL
*
* @param url - URL to fetch CSV data from
* @returns CSV content as string (decoded to UTF-8)
* @throws {Error} If fetch fails
*/
async function fetchCSVFromURL(url: string): Promise<string> {
try {
const response = await fetch(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; GasMaisBarato/1.0)',
},
// Revalidate every 30 minutes (1800 seconds)
// Note: CSV file is ~2.5MB, exceeds Next.js 2MB cache limit
// Using our own in-memory cache instead (CACHE_DURATION_MS)
next: {
revalidate: 1800,
},
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
// ANP CSV comes in UTF-8 with BOM encoding
const buffer = await response.arrayBuffer();
const content = iconv.decode(Buffer.from(buffer), 'utf-8');
return content;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to fetch CSV from URL: ${error.message}`);
}
throw new Error('Failed to fetch CSV from URL: Unknown error');
}
}
/**
* Loads and parses the CSV file from local disk (fallback only)
*
* @param filePath - Absolute path to CSV file
* @returns Array of raw CSV records
* @throws {Error} If file cannot be read or parsed
*/
async function loadCSVFromFile(filePath: string): Promise<GasPriceRaw[]> {
try {
// Read file as buffer and decode from UTF-8 (with BOM)
const buffer = await fs.readFile(filePath);
const fileContent = iconv.decode(buffer, 'utf-8');
// Parse CSV with csv-parse library
const records = parse(fileContent, {
columns: true, // Use first row as headers
skip_empty_lines: true,
trim: true,
bom: true, // Handle BOM (Byte Order Mark)
relaxColumnCount: true, // Allow inconsistent column counts
delimiter: ';', // Semicolon delimiter (ANP format)
relax_quotes: true, // Allow quotes to appear in unquoted fields
escape: '"', // Use double quote as escape character
quote: '"', // Quote character
}) as GasPriceRaw[];
return records;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to load CSV file: ${error.message}`);
}
throw new Error('Failed to load CSV file: Unknown error');
}
}
/**
* Loads and parses CSV data from live ANP URL or fallback file
*
* @param useLiveData - If true, fetch from ANP URL; if false, use local file
* @returns Array of raw CSV records
* @throws {Error} If both live fetch and fallback fail
*/
async function loadCSV(useLiveData: boolean = true): Promise<GasPriceRaw[]> {
try {
let fileContent: string;
if (useLiveData) {
// Fetch live data from ANP government portal
console.log('Fetching live data from ANP...');
fileContent = await fetchCSVFromURL(ANP_DATA_URL);
console.log('Live data fetched successfully');
} else {
// Fallback to local file
console.log('Loading data from local file...');
fileContent = await fs.readFile(FALLBACK_CSV_PATH, { encoding: 'utf-8' });
}
// Parse CSV with csv-parse library
const records = parse(fileContent, {
columns: true, // Use first row as headers
skip_empty_lines: true,
trim: true,
bom: true, // Handle BOM (Byte Order Mark)
relaxColumnCount: true, // Allow inconsistent column counts
delimiter: ';', // Semicolon delimiter (ANP format)
relax_quotes: true, // Allow quotes to appear in unquoted fields
escape: '"', // Use double quote as escape character
quote: '"', // Quote character
}) as GasPriceRaw[];
return records;
} catch (error) {
// If live data fails, try fallback
if (useLiveData) {
console.warn('Failed to fetch live data, trying fallback file...', error);
try {
return await loadCSVFromFile(FALLBACK_CSV_PATH);
} catch (fallbackError) {
console.error('Fallback also failed:', fallbackError);
throw new Error(
`Failed to load data from both live source and fallback: ${
error instanceof Error ? error.message : 'Unknown error'
}`
);
}
}
if (error instanceof Error) {
throw new Error(`Failed to load CSV: ${error.message}`);
}
throw new Error('Failed to load CSV: Unknown error');
}
}
// ============================================================================
// MAIN PROCESSING FUNCTION
// ============================================================================
/**
* Fetches, processes, and caches gas station data from ANP live source
*
* This is the main entry point for data retrieval. It:
* 1. Checks cache validity
* 2. Fetches live CSV data from ANP government portal
* 3. Validates and transforms each record
* 4. Removes duplicates based on CNPJ
* 5. Caches the result
*
* @param useLiveData - If true (default), fetch from ANP URL; if false, use local fallback
* @returns Array of processed gas stations
* @throws {Error} If CSV cannot be loaded or processed
*
* @example
* ```typescript
* // Fetch live data from ANP (default)
* const stations = await fetchAndProcessData();
*
* // Use local fallback for testing
* const stations = await fetchAndProcessData(false);
* ```
*/
export async function fetchAndProcessData(
useLiveData: boolean = true
): Promise<GasStation[]> {
const startTime = Date.now();
// Return cached data if valid
if (isCacheValid() && cache) {
console.log('Returning cached data');
return cache.data;
}
console.log('Loading and processing CSV data...');
try {
// Load raw CSV records from live ANP source
const rawRecords = await loadCSV(useLiveData);
// Transform records with validation
const stations: GasStation[] = [];
let invalidCount = 0;
for (const raw of rawRecords) {
const station = transformRawToStation(raw);
if (station) {
stations.push(station);
} else {
invalidCount++;
}
}
// Remove duplicates based on CNPJ (id)
const uniqueStations = Array.from(
new Map(stations.map((s) => [s.id, s])).values()
);
const duplicatesRemoved = stations.length - uniqueStations.length;
// Update cache
cache = {
data: uniqueStations,
timestamp: Date.now(),
};
// Log processing stats
const stats: ProcessingStats = {
totalRecords: rawRecords.length,
validRecords: uniqueStations.length,
invalidRecords: invalidCount,
duplicatesRemoved,
processingTimeMs: Date.now() - startTime,
};
console.log('Processing complete:', stats);
return uniqueStations;
} catch (error) {
// Clear cache on error
cache = null;
if (error instanceof Error) {
throw new Error(`Data processing failed: ${error.message}`);
}
throw new Error('Data processing failed: Unknown error');
}
}
// ============================================================================
// UTILITY FUNCTIONS
// ============================================================================
/**
* Gets unique city names from all stations
*
* @param stations - Array of gas stations
* @returns Sorted array of unique city names
*/
export function getUniqueCities(stations: GasStation[]): string[] {
const cities = new Set(stations.map((s) => s.city));
return Array.from(cities).sort();
}
/**
* Gets unique brand/flag names from all stations
*
* @param stations - Array of gas stations
* @returns Sorted array of unique brands
*/
export function getUniqueBrands(stations: GasStation[]): string[] {
const brands = new Set(stations.map((s) => s.flag));
return Array.from(brands).sort();
}
/**
* Validates CSV file exists and is readable (for fallback file only)
*
* @param filePath - Path to CSV file (defaults to FALLBACK_CSV_PATH)
* @returns True if file exists and is readable
*/
export async function validateCSVFile(filePath: string = FALLBACK_CSV_PATH): Promise<boolean> {
try {
await fs.access(filePath, fs.constants.R_OK);
return true;
} catch {
return false;
}
}
/**
* Tests connectivity to ANP live data source
*
* @returns True if ANP URL is accessible
*/
export async function testANPConnectivity(): Promise<boolean> {
try {
const response = await fetch(ANP_DATA_URL, {
method: 'HEAD',
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; GasMaisBarato/1.0)',
},
});
return response.ok;
} catch {
return false;
}
}