Skip to content

Commit f4c562a

Browse files
committed
Refactor Firebase functions to implement lazy initialization for calculation utilities and document processors, enhancing resource management
1 parent eb62fed commit f4c562a

File tree

5 files changed

+10
-30
lines changed

5 files changed

+10
-30
lines changed

firebase.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"functions": [
77
{
88
"source": "functions-node",
9+
"codebase": "nodejs",
910
"ignore": [
1011
"node_modules",
1112
".git",

functions-node/calculateGrade.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ const admin = require('firebase-admin');
33
const { getLetterGrade } = require('./utils/gradeUtils');
44
const { calculateExactGradeStatistics, generateGradeAnalysis } = require('./utils/calculationUtils');
55

6-
// We'll use lazy initialization for calculation utilities
7-
// This will ensure they're only loaded when needed
8-
console.log('calculateGrade module loaded - calculation utilities will be initialized on first use');
9-
106
/**
117
* Cloud Function to calculate current grade based on submitted assignments
128
*/

functions-node/formatDocumentsData.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ const admin = require('firebase-admin');
33
const { DOCUMENT_TYPES, normalizeDocumentType } = require('./utils/documentUtils');
44
const { formatSyllabusData, formatGradesData, formatTranscriptData, combineFormattedData } = require('./utils/documentProcessors');
55

6-
// We'll use lazy initialization for document processors
7-
// This will ensure they're only loaded when needed
8-
console.log('formatDocumentsData module loaded - processors will be initialized on first use');
9-
106
/**
117
* Formats all document data using multiple OpenAI API calls to ensure consistent structure
128
* @param {string} userId - The user ID

functions-node/index.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@ const { formatDocumentsData } = require('./formatDocumentsData');
1111
const { DOCUMENT_TYPES, normalizeDocumentType } = require('./utils/documentUtils');
1212
const corsMiddleware = require('./utils/corsMiddleware');
1313

14-
// Create a global variable to track initialization status
15-
let adminInitialized = false;
16-
17-
// Initialize Firebase Admin - this is lightweight and needed for all functions
14+
// Initialize Firebase Admin
1815
admin.initializeApp();
1916

20-
// We'll use lazy initialization for heavier resources in each function
21-
console.log('Firebase Functions initialized with lazy loading pattern');
22-
2317
// Export functions
2418
exports.calculateCurrentGrade = calculateCurrentGrade;
2519
exports.predictFinalGrade = predictFinalGrade;

functions-node/predictGrade.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ const { getLetterGrade } = require('./utils/gradeUtils');
55
const { getOpenAIApiKey } = require('./utils/apiUtils');
66
const { calculateExactGradeStatistics, formatDataForAIPrediction } = require('./utils/calculationUtils');
77

8-
// Global variable to store the OpenAI client instance
9-
let openaiClient = null;
10-
11-
// We'll use lazy initialization instead of onInit
12-
console.log('predictGrade module loaded - OpenAI client will be initialized on first use');
13-
148
/**
159
* Cloud Function to predict final grade using AI
1610
*/
@@ -77,14 +71,13 @@ exports.predictFinalGrade = functions.https.onCall(async (data, context) => {
7771
* @returns {Promise<Object>} Prediction result
7872
*/
7973
async function getAIPrediction(data) {
80-
// Lazy initialization of OpenAI client
81-
if (!openaiClient) {
82-
console.log('Initializing OpenAI client on first use');
83-
const apiKey = getOpenAIApiKey();
84-
openaiClient = new OpenAI({
85-
apiKey: apiKey
86-
});
87-
}
74+
// Get API key
75+
const apiKey = getOpenAIApiKey();
76+
77+
// Initialize OpenAI client
78+
const openai = new OpenAI({
79+
apiKey: apiKey
80+
});
8881

8982
// Create a highly structured prompt with clear JSON instructions
9083
const prompt = `
@@ -127,7 +120,7 @@ RESPOND ONLY WITH VALID JSON IN THIS EXACT FORMAT:
127120
try {
128121
console.log('Calling OpenAI API for prediction');
129122
// Call OpenAI with strict instruction for JSON
130-
const response = await openaiClient.chat.completions.create({
123+
const response = await openai.chat.completions.create({
131124
model: "gpt-4o-mini",
132125
messages: [
133126
{

0 commit comments

Comments
 (0)