@@ -11,7 +11,7 @@ import {
1111const TABLE = 'adoption' ;
1212
1313/**
14- * List adoption data with filtering - Optimized version
14+ * List adoption data with filtering
1515 */
1616const listAdoptionData = async ( req , res ) => {
1717 try {
@@ -34,7 +34,6 @@ const listAdoptionData = async (req, res) => {
3434 // Fast preprocessing - handle 'latest' date and technology array
3535 const techArray = params . technology ? decodeURIComponent ( params . technology ) . split ( ',' ) : [ ] ;
3636
37- // Handle 'latest' date with caching
3837 let startDate = params . start ;
3938 if ( startDate === 'latest' ) {
4039 startDate = await getLatestDate ( firestore , TABLE ) ;
@@ -58,50 +57,34 @@ const listAdoptionData = async (req, res) => {
5857 return ;
5958 }
6059
61- // Build optimized query
60+ // Build query
6261 let query = firestore . collection ( TABLE ) ;
6362
6463 // Apply required filters
6564 query = query . where ( 'geo' , '==' , params . geo ) ;
6665 query = query . where ( 'rank' , '==' , params . rank ) ;
6766
68- // Apply technology filter efficiently
67+ // Apply technology filter
6968 if ( techArray . length <= 30 ) {
70- // Use 'in' operator for batch processing (Firestore limit: 30 values)
69+ // Use 'in' operator for batch processing (Firestore limit: 30 values https://cloud.google.com/firestore/docs/query-data/queries#limits_on_or_queries )
7170 query = query . where ( 'technology' , 'in' , techArray ) ;
7271 } 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 ) ;
79-
80- if ( startDate ) individualQuery = individualQuery . where ( 'date' , '>=' , startDate ) ;
81- if ( params . end ) individualQuery = individualQuery . where ( 'date' , '<=' , params . end ) ;
82-
83- const snapshot = await individualQuery . get ( ) ;
84- const results = [ ] ;
85- snapshot . forEach ( doc => results . push ( doc . data ( ) ) ) ;
86- return results ;
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 ) ) ;
72+ res . statusCode = 400 ;
73+ res . end ( JSON . stringify ( {
74+ success : false ,
75+ errors : [ { technology : 'Too many technologies specified. Maximum 30 allowed.' } ]
76+ } ) ) ;
9777 return ;
9878 }
9979
10080 // Apply date filters
10181 if ( startDate ) query = query . where ( 'date' , '>=' , startDate ) ;
10282 if ( params . end ) query = query . where ( 'date' , '<=' , params . end ) ;
10383
104- // Execute single optimized query
84+ // Apply field projection to exclude geo/rank
85+ query = query . select ( 'date' , 'technology' , 'adoption' ) ;
86+
87+ // Execute query
10588 const snapshot = await query . get ( ) ;
10689 const data = [ ] ;
10790 snapshot . forEach ( doc => {
@@ -111,7 +94,7 @@ const listAdoptionData = async (req, res) => {
11194 // Cache the result
11295 setCachedQueryResult ( cacheKey , data ) ;
11396
114- // Direct response without wrapper functions
97+ // Direct response
11598 res . statusCode = 200 ;
11699 res . end ( JSON . stringify ( data ) ) ;
117100 } catch ( error ) {
0 commit comments