| 99 | export type InsightOutput = {output: string} | {error: string}; |
| 100 | |
| 101 | export function getInsightOutput( |
| 102 | result: TraceResult, |
| 103 | insightSetId: string, |
| 104 | insightName: InsightName, |
| 105 | deviceScope?: DevTools.CrUXManager.DeviceScope | null, |
| 106 | ): InsightOutput { |
| 107 | if (!result.insights) { |
| 108 | return { |
| 109 | error: 'No Performance insights are available for this trace.', |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | const insightSet = result.insights.get(insightSetId); |
| 114 | if (!insightSet) { |
| 115 | return { |
| 116 | error: |
| 117 | 'No Performance Insights for the given insight set id. Only use ids given in the "Available insight sets" list.', |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | const matchingInsight = |
| 122 | insightName in insightSet.model ? insightSet.model[insightName] : null; |
| 123 | if (!matchingInsight) { |
| 124 | return { |
| 125 | error: `No Insight with the name ${insightName} found. Double check the name you provided is accurate and try again.`, |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | const formatter = new DevTools.PerformanceInsightFormatter( |
| 130 | DevTools.AgentFocus.fromParsedTrace(result.parsedTrace), |
| 131 | matchingInsight, |
| 132 | deviceScope, |
| 133 | ); |
| 134 | return {output: formatter.formatInsight()}; |
| 135 | } |