@@ -91,11 +91,17 @@ export function useQualityMetrics() {
9191
9292 if ( issuesError ) throw issuesError ;
9393
94- // Calculate production totals
95- const totalProduced = quantities ?. reduce ( ( sum , q ) => sum + ( q . quantity_produced || 0 ) , 0 ) || 0 ;
96- const totalGood = quantities ?. reduce ( ( sum , q ) => sum + ( q . quantity_good || 0 ) , 0 ) || 0 ;
97- const totalScrap = quantities ?. reduce ( ( sum , q ) => sum + ( q . quantity_scrap || 0 ) , 0 ) || 0 ;
98- const totalRework = quantities ?. reduce ( ( sum , q ) => sum + ( q . quantity_rework || 0 ) , 0 ) || 0 ;
94+ // Calculate production totals in single pass instead of 4 separate reduces
95+ const totals = quantities ?. reduce (
96+ ( acc , q ) => ( {
97+ produced : acc . produced + ( q . quantity_produced || 0 ) ,
98+ good : acc . good + ( q . quantity_good || 0 ) ,
99+ scrap : acc . scrap + ( q . quantity_scrap || 0 ) ,
100+ rework : acc . rework + ( q . quantity_rework || 0 ) ,
101+ } ) ,
102+ { produced : 0 , good : 0 , scrap : 0 , rework : 0 }
103+ ) || { produced : 0 , good : 0 , scrap : 0 , rework : 0 } ;
104+ const { produced : totalProduced , good : totalGood , scrap : totalScrap , rework : totalRework } = totals ;
99105
100106 // Calculate yield metrics
101107 const overallYield = totalProduced > 0 ? ( totalGood / totalProduced ) * 100 : 100 ;
@@ -142,18 +148,35 @@ export function useQualityMetrics() {
142148 . sort ( ( a , b ) => b . quantity - a . quantity )
143149 . slice ( 0 , 10 ) ;
144150
145- // Calculate issue metrics
151+ // Calculate issue metrics in single pass instead of 9 separate filters
152+ const issueCounts = issues ?. reduce (
153+ ( acc , i ) => {
154+ // Count by status
155+ if ( i . status === "pending" ) acc . pending ++ ;
156+ else if ( i . status === "approved" ) acc . approved ++ ;
157+ else if ( i . status === "rejected" ) acc . rejected ++ ;
158+ else if ( i . status === "closed" ) acc . closed ++ ;
159+ // Count by severity
160+ if ( i . severity === "critical" ) acc . critical ++ ;
161+ else if ( i . severity === "high" ) acc . high ++ ;
162+ else if ( i . severity === "medium" ) acc . medium ++ ;
163+ else if ( i . severity === "low" ) acc . low ++ ;
164+ return acc ;
165+ } ,
166+ { pending : 0 , approved : 0 , rejected : 0 , closed : 0 , critical : 0 , high : 0 , medium : 0 , low : 0 }
167+ ) || { pending : 0 , approved : 0 , rejected : 0 , closed : 0 , critical : 0 , high : 0 , medium : 0 , low : 0 } ;
168+
146169 const issueMetrics = {
147170 total : issues ?. length || 0 ,
148- pending : issues ?. filter ( ( i ) => i . status === " pending" ) . length || 0 ,
149- approved : issues ?. filter ( ( i ) => i . status === " approved" ) . length || 0 ,
150- rejected : issues ?. filter ( ( i ) => i . status === " rejected" ) . length || 0 ,
151- closed : issues ?. filter ( ( i ) => i . status === " closed" ) . length || 0 ,
171+ pending : issueCounts . pending ,
172+ approved : issueCounts . approved ,
173+ rejected : issueCounts . rejected ,
174+ closed : issueCounts . closed ,
152175 bySeverity : {
153- critical : issues ?. filter ( ( i ) => i . severity === " critical" ) . length || 0 ,
154- high : issues ?. filter ( ( i ) => i . severity === " high" ) . length || 0 ,
155- medium : issues ?. filter ( ( i ) => i . severity === " medium" ) . length || 0 ,
156- low : issues ?. filter ( ( i ) => i . severity === " low" ) . length || 0 ,
176+ critical : issueCounts . critical ,
177+ high : issueCounts . high ,
178+ medium : issueCounts . medium ,
179+ low : issueCounts . low ,
157180 } ,
158181 } ;
159182
@@ -186,10 +209,10 @@ export function useScrapReasonUsage() {
186209 throw new Error ( "No tenant ID" ) ;
187210 }
188211
189- // Fetch all scrap reasons
212+ // Fetch all scrap reasons - only fields we need
190213 const { data : reasons , error : reasonsError } = await supabase
191214 . from ( "scrap_reasons" )
192- . select ( "* " )
215+ . select ( "id, code, description, category, active " )
193216 . eq ( "tenant_id" , profile . tenant_id )
194217 . order ( "code" ) ;
195218
0 commit comments