@@ -3,120 +3,110 @@ import googleTrends from 'google-trends-api'
33// Disable SSL verification for this module (temporary workaround)
44process . env [ 'NODE_TLS_REJECT_UNAUTHORIZED' ] = '0'
55
6- export async function fetchTrendsData ( ) {
7- console . log ( ' 🔍 Fetching Google Trends data ...' )
6+ async function fetchTrendsForKeyword ( keyword ) {
7+ console . log ( ` 🔍 Fetching trends for " ${ keyword } " ...` )
88
9- try {
10- const keyword = 'couchbase'
11- const endDate = new Date ( )
12- const startDate = new Date ( )
13- startDate . setFullYear ( startDate . getFullYear ( ) - 1 ) // Last 12 months
9+ const endDate = new Date ( )
10+ const startDate = new Date ( )
11+ startDate . setFullYear ( startDate . getFullYear ( ) - 1 ) // Last 12 months
1412
15- // Fetch interest over time
16- console . log ( ' 📈 Fetching interest over time...' )
17- const interestOverTimeResponse = await googleTrends . interestOverTime ( {
18- keyword : keyword ,
19- startTime : startDate ,
20- endTime : endDate ,
21- geo : 'US' , // Focus on US market
22- granularTimeResolution : true
23- } )
13+ // Fetch interest over time
14+ const interestOverTimeResponse = await googleTrends . interestOverTime ( {
15+ keyword : keyword ,
16+ startTime : startDate ,
17+ endTime : endDate ,
18+ geo : 'US' ,
19+ granularTimeResolution : true
20+ } )
2421
25- console . log ( ' 📊 Parsing interest over time response...' )
26- const interestData = JSON . parse ( interestOverTimeResponse )
27- console . log ( ' 📊 Interest data keys:' , Object . keys ( interestData ) )
28- console . log ( ' 📊 Default keys:' , interestData . default ? Object . keys ( interestData . default ) : 'No default key' )
29-
30- if ( ! interestData . default ) {
31- console . log ( ' 📊 Full response structure:' , JSON . stringify ( interestData , null , 2 ) )
32- throw new Error ( 'Invalid response structure: missing default' )
33- }
34-
35- if ( ! interestData . default . timelineData ) {
36- console . log ( ' 📊 Default content:' , JSON . stringify ( interestData . default , null , 2 ) )
37- throw new Error ( 'Invalid response structure: missing default.timelineData' )
38- }
39-
40- console . log ( ' 📊 Timeline data sample:' , interestData . default . timelineData [ 0 ] )
41-
42- const timelineData = interestData . default . timelineData . map ( item => ( {
43- date : item . time ,
44- value : item . value ? item . value [ 0 ] : 0 ,
45- formattedTime : item . formattedTime
46- } ) )
22+ const interestData = JSON . parse ( interestOverTimeResponse )
23+ const timelineData = interestData . default . timelineData . map ( item => ( {
24+ date : item . time ,
25+ value : item . value ? item . value [ 0 ] : 0 ,
26+ formattedTime : item . formattedTime
27+ } ) )
4728
48- // Fetch related queries (top and rising)
49- console . log ( ' 🔍 Fetching related queries...' )
50- const relatedQueriesResponse = await googleTrends . relatedQueries ( {
51- keyword : keyword ,
52- startTime : startDate ,
53- endTime : endDate ,
54- geo : 'US'
55- } )
29+ // Fetch related queries (top and rising)
30+ const relatedQueriesResponse = await googleTrends . relatedQueries ( {
31+ keyword : keyword ,
32+ startTime : startDate ,
33+ endTime : endDate ,
34+ geo : 'US'
35+ } )
5636
57- console . log ( ' 📊 Parsing related queries response...' )
58- const queriesData = JSON . parse ( relatedQueriesResponse )
59- console . log ( ' 📊 Queries data keys:' , Object . keys ( queriesData ) )
60- console . log ( ' 📊 Has default:' , ! ! queriesData . default )
61- console . log ( ' 📊 Has rankedListList:' , ! ! queriesData . default ?. rankedListList )
62-
63- if ( ! queriesData . default || ! queriesData . default . rankedList ) {
64- console . log ( ' 📊 Full response structure:' , JSON . stringify ( queriesData , null , 2 ) . substring ( 0 , 500 ) + '...' )
65- throw new Error ( 'Invalid queries response structure' )
66- }
67-
68- // The structure is: rankedList[0].rankedKeyword is an array
69- const topQueries = queriesData . default . rankedList [ 0 ] ?. rankedKeyword ?. map ( item => ( {
70- query : item . query ,
71- value : item . formattedValue || item . traffic ,
72- hasData : item . hasData
73- } ) ) . slice ( 0 , 10 ) || [ ]
37+ const queriesData = JSON . parse ( relatedQueriesResponse )
38+
39+ const topQueries = queriesData . default . rankedList [ 0 ] ?. rankedKeyword ?. map ( item => ( {
40+ query : item . query ,
41+ value : item . formattedValue || item . traffic ,
42+ hasData : item . hasData
43+ } ) ) . slice ( 0 , 10 ) || [ ]
7444
75- const risingQueries = queriesData . default . rankedList [ 1 ] ?. rankedKeyword ?. map ( item => ( {
76- query : item . query ,
77- value : item . formattedValue || item . traffic ,
78- hasData : item . hasData
79- } ) ) . slice ( 0 , 10 ) || [ ]
45+ const risingQueries = queriesData . default . rankedList [ 1 ] ?. rankedKeyword ?. map ( item => ( {
46+ query : item . query ,
47+ value : item . formattedValue || item . traffic ,
48+ hasData : item . hasData
49+ } ) ) . slice ( 0 , 10 ) || [ ]
8050
81- console . log ( ` ✅ Parsed ${ topQueries . length } top queries and ${ risingQueries . length } rising queries` )
51+ console . log ( ` ✅ Fetched ${ timelineData . length } timeline points, ${ topQueries . length } top queries, ${ risingQueries . length } rising queries` )
8252
83- // Fetch interest by region
84- console . log ( ' 🗺️ Fetching interest by region...' )
85- const interestByRegionResponse = await googleTrends . interestByRegion ( {
86- keyword : keyword ,
87- startTime : startDate ,
88- endTime : endDate ,
89- geo : 'US' ,
90- resolution : 'REGION'
91- } )
53+ return {
54+ keyword,
55+ timelineData,
56+ topQueries,
57+ risingQueries,
58+ summary : {
59+ avgInterest : timelineData . reduce ( ( sum , item ) => sum + item . value , 0 ) / timelineData . length ,
60+ peakInterest : Math . max ( ...timelineData . map ( item => item . value ) ) ,
61+ currentInterest : timelineData [ timelineData . length - 1 ] ?. value || 0 ,
62+ trendDirection : timelineData . length > 1
63+ ? ( timelineData [ timelineData . length - 1 ] . value > timelineData [ timelineData . length - 2 ] . value ? 'up' : 'down' )
64+ : 'stable'
65+ }
66+ }
67+ }
9268
93- const regionData = JSON . parse ( interestByRegionResponse )
94- const regionalInterest = regionData . default . geoMapData . map ( item => ( {
95- region : item . geoName ,
96- value : item . value [ 0 ] ,
97- hasData : item . hasData
98- } ) ) . filter ( item => item . hasData )
69+ export async function fetchTrendsData ( ) {
70+ console . log ( '🔍 Fetching Google Trends data...' )
71+
72+ try {
73+ // Fetch data for both keywords
74+ const [ couchbaseData , couchbaseServerData ] = await Promise . all ( [
75+ fetchTrendsForKeyword ( 'couchbase' ) ,
76+ fetchTrendsForKeyword ( 'couchbase server' )
77+ ] )
78+
79+ // Combine timeline data for comparison
80+ const combinedTimeline = couchbaseData . timelineData . map ( ( item , index ) => ( {
81+ date : item . date ,
82+ formattedTime : item . formattedTime ,
83+ couchbase : item . value ,
84+ couchbaseServer : couchbaseServerData . timelineData [ index ] ?. value || 0
85+ } ) )
9986
100- console . log ( ` ✅ Successfully fetched trends data for "${ keyword } "` )
101- console . log ( ` 📊 Timeline points: ${ timelineData . length } ` )
102- console . log ( ` 🔍 Top queries: ${ topQueries . length } ` )
103- console . log ( ` 📈 Rising queries: ${ risingQueries . length } ` )
104- console . log ( ` 🗺️ Regions: ${ regionalInterest . length } ` )
87+ console . log ( ` ✅ Successfully fetched trends data for both keywords` )
88+ console . log ( ` 📊 Combined timeline points: ${ combinedTimeline . length } ` )
10589
10690 return {
107- keyword,
10891 lastUpdated : new Date ( ) . toISOString ( ) ,
109- interestOverTime : timelineData ,
110- topQueries : topQueries . slice ( 0 , 10 ) , // Top 10
111- risingQueries : risingQueries . slice ( 0 , 10 ) , // Top 10 rising
112- regionalInterest : regionalInterest ,
92+ couchbase : {
93+ keyword : couchbaseData . keyword ,
94+ topQueries : couchbaseData . topQueries ,
95+ risingQueries : couchbaseData . risingQueries ,
96+ summary : couchbaseData . summary
97+ } ,
98+ couchbaseServer : {
99+ keyword : couchbaseServerData . keyword ,
100+ topQueries : couchbaseServerData . topQueries ,
101+ risingQueries : couchbaseServerData . risingQueries ,
102+ summary : couchbaseServerData . summary
103+ } ,
104+ interestOverTime : combinedTimeline ,
113105 summary : {
114- avgInterest : timelineData . reduce ( ( sum , item ) => sum + item . value , 0 ) / timelineData . length ,
115- peakInterest : Math . max ( ...timelineData . map ( item => item . value ) ) ,
116- currentInterest : timelineData [ timelineData . length - 1 ] ?. value || 0 ,
117- trendDirection : timelineData . length > 1
118- ? ( timelineData [ timelineData . length - 1 ] . value > timelineData [ timelineData . length - 2 ] . value ? 'up' : 'down' )
119- : 'stable'
106+ avgInterest : couchbaseData . summary . avgInterest ,
107+ peakInterest : couchbaseData . summary . peakInterest ,
108+ currentInterest : couchbaseData . summary . currentInterest ,
109+ trendDirection : couchbaseData . summary . trendDirection
120110 }
121111 }
122112
0 commit comments