@@ -69,6 +69,77 @@ function generateInsights(analytics, jira) {
6969 return insights
7070}
7171
72+ // Fetch insights using OpenAI
73+ async function fetchAIInsights ( analyticsData , jiraData ) {
74+ console . log ( '🧠 Fetching AI-powered insights...' )
75+
76+ if ( ! process . env . OPENAI_API_KEY ) {
77+ console . log ( '⚠️ OPENAI_API_KEY not found, skipping AI insights generation' )
78+ return null
79+ }
80+
81+ try {
82+ // Import the insights API handler
83+ const { default : insightsHandler } = await import ( './insights-api.js' )
84+
85+ // Create a mock request object with the dashboard data
86+ const mockReq = {
87+ method : 'POST' ,
88+ body : {
89+ apiKey : process . env . OPENAI_API_KEY ,
90+ analysisTypes : [ 'traffic' , 'jira' , 'duplicates' ]
91+ }
92+ }
93+
94+ // Create a mock response object
95+ let responseData = null
96+ const mockRes = {
97+ status : ( code ) => ( {
98+ json : ( data ) => {
99+ responseData = data
100+ return Promise . resolve ( )
101+ }
102+ } )
103+ }
104+
105+ // Temporarily override the getDashboardData function in insights-api.js
106+ const originalGetDashboardData = ( await import ( './insights-api.js' ) ) . getDashboardData
107+ // We need to patch this to use our current data instead of reading from file
108+ const insightsApiModule = await import ( './insights-api.js' )
109+
110+ // Create a temporary file with our data for the insights API to read
111+ const tempDataPath = path . join ( __dirname , '..' , 'temp-dashboard-data.json' )
112+ const tempDashboardData = {
113+ analytics : analyticsData ,
114+ jira : jiraData
115+ }
116+ fs . writeFileSync ( tempDataPath , JSON . stringify ( tempDashboardData , null , 2 ) )
117+
118+ // Override the data path in the insights API
119+ const originalFilename = insightsApiModule . __filename
120+ insightsApiModule . __filename = tempDataPath
121+
122+ // Call the insights handler
123+ await insightsHandler ( mockReq , mockRes )
124+
125+ // Clean up temp file
126+ if ( fs . existsSync ( tempDataPath ) ) {
127+ fs . unlinkSync ( tempDataPath )
128+ }
129+
130+ if ( responseData && responseData . success ) {
131+ console . log ( ' ✅ AI insights generated successfully' )
132+ return responseData . results
133+ } else {
134+ console . log ( '⚠️ Failed to generate AI insights' )
135+ return null
136+ }
137+ } catch ( error ) {
138+ console . error ( '❌ Error fetching AI insights:' , error . message )
139+ return null
140+ }
141+ }
142+
72143/**
73144 * Main execution
74145 */
@@ -102,6 +173,12 @@ async function main() {
102173 }
103174
104175 const insights = generateInsights ( analyticsData , jiraData )
176+
177+ // Fetch AI insights if OpenAI API key is available
178+ const aiInsights = await fetchAIInsights ( analyticsData , jiraData )
179+ if ( aiInsights ) {
180+ insights . aiInsights = aiInsights
181+ }
105182
106183 // Combine into dashboard data
107184 const dashboardData = {
0 commit comments