Skip to content

Commit 8061c0a

Browse files
committed
fixed dashboard to use the openai api key secret
1 parent 0d4dac4 commit 8061c0a

6 files changed

Lines changed: 175 additions & 99 deletions

File tree

.github/workflows/fetch-and-deploy.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ jobs:
4242
JIRA_EMAIL: ${{ secrets.JIRA_EMAIL }}
4343
JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }}
4444
JIRA_PROJECT_KEY: ${{ secrets.JIRA_PROJECT_KEY }}
45+
# OpenAI
46+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
4547
run: npm run fetch-data
4648

4749
- name: Build dashboard

scripts/fetch-data.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = {

scripts/insights-api.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ export default async function handler(req, res) {
163163
}
164164

165165
try {
166-
const { apiKey, analysisTypes = ['traffic', 'jira', 'duplicates'] } = req.body
166+
const { analysisTypes = ['traffic', 'jira', 'duplicates'] } = req.body
167+
168+
// Use API key from environment variable (server-side) or from request (development)
169+
const apiKey = process.env.OPENAI_API_KEY || req.body.apiKey
167170

168171
if (!apiKey) {
169172
return res.status(400).json({ error: 'OpenAI API key is required' })

server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ app.use(express.static(join(__dirname, 'public')))
3939
app.listen(PORT, () => {
4040
console.log(`🚀 API server running on http://localhost:${PORT}`)
4141
console.log(`📊 Insights endpoint available at http://localhost:${PORT}/api/insights`)
42+
console.log(`🔑 Using OPENAI_API_KEY from environment: ${process.env.OPENAI_API_KEY ? '✅' : '❌ Not set'}`)
4243
})

src/App.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ function App() {
388388
{activeTab === 'insights' && (
389389
<div className="space-y-8">
390390
{/* LLM-Powered Insights */}
391-
<LLMInsights />
391+
<LLMInsights dashboardData={data} />
392392

393393
{/* Legacy Content Gaps */}
394394
<div className="bg-white rounded-xl shadow-sm border border-slate-200 p-6">

0 commit comments

Comments
 (0)