|
1 | 1 | import { firestoreOld } from '../utils/db.js';
|
2 | 2 | const firestore = firestoreOld;
|
3 | 3 |
|
4 |
| -import { createSuccessResponse } from '../utils/helpers.js'; |
5 | 4 | import {
|
6 |
| - REQUIRED_PARAMS, |
7 |
| - validateRequiredParams, |
8 |
| - sendValidationError, |
9 |
| - applyDateFilters, |
10 |
| - applyStandardFilters, |
11 |
| - preprocessParams, |
12 |
| - handleControllerError |
| 5 | + getLatestDate, |
| 6 | + generateQueryCacheKey, |
| 7 | + getCachedQueryResult, |
| 8 | + setCachedQueryResult |
13 | 9 | } from '../utils/controllerHelpers.js';
|
14 | 10 |
|
15 | 11 | const TABLE = 'adoption';
|
16 | 12 |
|
17 | 13 | /**
|
18 |
| - * List adoption data with filtering |
| 14 | + * List adoption data with filtering - Optimized version |
19 | 15 | */
|
20 | 16 | const listAdoptionData = async (req, res) => {
|
21 | 17 | try {
|
22 | 18 | const params = req.query;
|
23 | 19 |
|
24 |
| - // Validate required parameters |
25 |
| - const requiredParams = [ |
26 |
| - REQUIRED_PARAMS.GEO, |
27 |
| - REQUIRED_PARAMS.RANK, |
28 |
| - REQUIRED_PARAMS.TECHNOLOGY |
29 |
| - ]; |
| 20 | + // Validate required parameters inline for speed |
| 21 | + if (!params.geo || !params.rank || !params.technology) { |
| 22 | + res.statusCode = 400; |
| 23 | + res.end(JSON.stringify({ |
| 24 | + success: false, |
| 25 | + errors: [ |
| 26 | + ...(!params.geo ? [{ geo: 'missing geo parameter' }] : []), |
| 27 | + ...(!params.rank ? [{ rank: 'missing rank parameter' }] : []), |
| 28 | + ...(!params.technology ? [{ technology: 'missing technology parameter' }] : []) |
| 29 | + ] |
| 30 | + })); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Fast preprocessing - handle 'latest' date and technology array |
| 35 | + const techArray = params.technology ? decodeURIComponent(params.technology).split(',') : []; |
| 36 | + |
| 37 | + // Handle 'latest' date with caching |
| 38 | + let startDate = params.start; |
| 39 | + if (startDate === 'latest') { |
| 40 | + startDate = await getLatestDate(firestore, TABLE); |
| 41 | + } |
| 42 | + |
| 43 | + // Create cache key for this specific query |
| 44 | + const queryFilters = { |
| 45 | + geo: params.geo, |
| 46 | + rank: params.rank, |
| 47 | + technology: techArray, |
| 48 | + startDate: startDate, |
| 49 | + endDate: params.end |
| 50 | + }; |
| 51 | + const cacheKey = generateQueryCacheKey(TABLE, queryFilters); |
30 | 52 |
|
31 |
| - const validationErrors = validateRequiredParams(params, requiredParams); |
32 |
| - if (validationErrors) { |
33 |
| - sendValidationError(res, validationErrors); |
| 53 | + // Check cache first |
| 54 | + const cachedResult = getCachedQueryResult(cacheKey); |
| 55 | + if (cachedResult) { |
| 56 | + res.statusCode = 200; |
| 57 | + res.end(JSON.stringify(cachedResult)); |
34 | 58 | return;
|
35 | 59 | }
|
36 | 60 |
|
37 |
| - // Preprocess parameters and get technology array |
38 |
| - const { params: processedParams, techArray } = await preprocessParams(firestore, params, TABLE); |
39 |
| - const data = []; |
| 61 | + // Build optimized query |
| 62 | + let query = firestore.collection(TABLE); |
40 | 63 |
|
41 |
| - // Query for each technology |
42 |
| - for (const technology of techArray) { |
43 |
| - let query = firestore.collection(TABLE); |
| 64 | + // Apply required filters |
| 65 | + query = query.where('geo', '==', params.geo); |
| 66 | + query = query.where('rank', '==', params.rank); |
44 | 67 |
|
45 |
| - // Apply standard filters including version filter |
46 |
| - query = applyStandardFilters(query, processedParams, technology, techArray); |
| 68 | + // Apply technology filter efficiently |
| 69 | + if (techArray.length <= 30) { |
| 70 | + // Use 'in' operator for batch processing (Firestore limit: 30 values) |
| 71 | + query = query.where('technology', 'in', techArray); |
| 72 | + } else { |
| 73 | + // Parallel queries for >30 technologies (rare case) |
| 74 | + const queryPromises = techArray.map(async (technology) => { |
| 75 | + let individualQuery = firestore.collection(TABLE) |
| 76 | + .where('geo', '==', params.geo) |
| 77 | + .where('rank', '==', params.rank) |
| 78 | + .where('technology', '==', technology); |
47 | 79 |
|
48 |
| - // Apply date filters |
49 |
| - query = applyDateFilters(query, processedParams); |
| 80 | + if (startDate) individualQuery = individualQuery.where('date', '>=', startDate); |
| 81 | + if (params.end) individualQuery = individualQuery.where('date', '<=', params.end); |
50 | 82 |
|
51 |
| - // Execute query |
52 |
| - const snapshot = await query.get(); |
53 |
| - snapshot.forEach(doc => { |
54 |
| - data.push(doc.data()); |
| 83 | + const snapshot = await individualQuery.get(); |
| 84 | + const results = []; |
| 85 | + snapshot.forEach(doc => results.push(doc.data())); |
| 86 | + return results; |
55 | 87 | });
|
| 88 | + |
| 89 | + const results = await Promise.all(queryPromises); |
| 90 | + const data = results.flat(); |
| 91 | + |
| 92 | + // Cache the result |
| 93 | + setCachedQueryResult(cacheKey, data); |
| 94 | + |
| 95 | + res.statusCode = 200; |
| 96 | + res.end(JSON.stringify(data)); |
| 97 | + return; |
56 | 98 | }
|
57 | 99 |
|
58 |
| - // Send response |
| 100 | + // Apply date filters |
| 101 | + if (startDate) query = query.where('date', '>=', startDate); |
| 102 | + if (params.end) query = query.where('date', '<=', params.end); |
| 103 | + |
| 104 | + // Execute single optimized query |
| 105 | + const snapshot = await query.get(); |
| 106 | + const data = []; |
| 107 | + snapshot.forEach(doc => { |
| 108 | + data.push(doc.data()); |
| 109 | + }); |
| 110 | + |
| 111 | + // Cache the result |
| 112 | + setCachedQueryResult(cacheKey, data); |
| 113 | + |
| 114 | + // Direct response without wrapper functions |
59 | 115 | res.statusCode = 200;
|
60 |
| - res.end(JSON.stringify(createSuccessResponse(data))); |
| 116 | + res.end(JSON.stringify(data)); |
61 | 117 | } catch (error) {
|
62 |
| - handleControllerError(res, error, 'fetching adoption data'); |
| 118 | + console.error('Error fetching adoption data:', error); |
| 119 | + res.statusCode = 500; |
| 120 | + res.end(JSON.stringify({ |
| 121 | + errors: [{ error: 'Failed to fetch adoption data' }] |
| 122 | + })); |
63 | 123 | }
|
64 | 124 | };
|
65 | 125 |
|
|
0 commit comments