Spaces:
Sleeping
Sleeping
File size: 7,027 Bytes
4fd5a04 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | """Data models for PageSpeed Insights analysis."""
from dataclasses import dataclass
from typing import List, Optional, Dict, Any
from datetime import datetime
@dataclass
class Opportunity:
"""Represents a performance optimization opportunity."""
title: str
description: str
savings: float
@dataclass
class PageSpeedData:
"""Contains PageSpeed Insights analysis data for a single URL."""
url_original: str
fetch_time: str
device: str
performance_score: float
accessibility_score: float
best_practices_score: float
seo_score: float
fcp: float
lcp: float
tbt: float
cls: float
speed_index: float
opportunities: List[Opportunity]
@classmethod
def from_lighthouse_data(cls, lighthouse_data: Dict[str, Any], site_url: str, strategy: str) -> 'PageSpeedData':
"""Create PageSpeedData from Lighthouse API response."""
audits = lighthouse_data.get('audits', {})
categories = lighthouse_data.get('categories', {})
opportunities = []
for audit_id, audit_data in audits.items():
if audit_data.get('details', {}).get('type') == 'opportunity':
if audit_data.get('score', 1) < 1:
opportunities.append(Opportunity(
title=audit_data.get('title', ''),
description=audit_data.get('description', ''),
savings=audit_data.get('details', {}).get('overallSavingsMs', 0) / 1000
))
return cls(
url_original=lighthouse_data.get('finalDisplayedUrl', site_url),
fetch_time=lighthouse_data.get('fetchTime', datetime.now().isoformat()),
device=strategy,
performance_score=categories.get('performance', {}).get('score', 0) * 100,
accessibility_score=categories.get('accessibility', {}).get('score', 0) * 100,
best_practices_score=categories.get('best-practices', {}).get('score', 0) * 100,
seo_score=categories.get('seo', {}).get('score', 0) * 100,
fcp=audits.get('first-contentful-paint', {}).get('numericValue', 0) / 1000,
lcp=audits.get('largest-contentful-paint', {}).get('numericValue', 0) / 1000,
tbt=audits.get('total-blocking-time', {}).get('numericValue', 0),
cls=audits.get('cumulative-layout-shift', {}).get('numericValue', 0),
speed_index=audits.get('speed-index', {}).get('numericValue', 0) / 1000,
opportunities=opportunities
)
@dataclass
class MetricComparison:
"""Comparison of a single metric between two analyses."""
antes: float
depois: float
diferenca: float
@dataclass
class ComparisonResult:
"""Complete comparison result between two PageSpeed analyses."""
performance: MetricComparison
accessibility: MetricComparison
best_practices: MetricComparison
seo: MetricComparison
fcp: MetricComparison
lcp: MetricComparison
tbt: MetricComparison
cls: MetricComparison
speed_index: MetricComparison
@classmethod
def from_data(cls, data1: PageSpeedData, data2: PageSpeedData) -> 'ComparisonResult':
"""Create comparison result from two PageSpeedData objects."""
return cls(
performance=MetricComparison(
antes=data1.performance_score,
depois=data2.performance_score,
diferenca=data2.performance_score - data1.performance_score
),
accessibility=MetricComparison(
antes=data1.accessibility_score,
depois=data2.accessibility_score,
diferenca=data2.accessibility_score - data1.accessibility_score
),
best_practices=MetricComparison(
antes=data1.best_practices_score,
depois=data2.best_practices_score,
diferenca=data2.best_practices_score - data1.best_practices_score
),
seo=MetricComparison(
antes=data1.seo_score,
depois=data2.seo_score,
diferenca=data2.seo_score - data1.seo_score
),
fcp=MetricComparison(
antes=round(data1.fcp, 2),
depois=round(data2.fcp, 2),
diferenca=round(data2.fcp - data1.fcp, 2)
),
lcp=MetricComparison(
antes=round(data1.lcp, 2),
depois=round(data2.lcp, 2),
diferenca=round(data2.lcp - data1.lcp, 2)
),
tbt=MetricComparison(
antes=round(data1.tbt, 0),
depois=round(data2.tbt, 0),
diferenca=round(data2.tbt - data1.tbt, 0)
),
cls=MetricComparison(
antes=round(data1.cls, 3),
depois=round(data2.cls, 3),
diferenca=round(data2.cls - data1.cls, 3)
),
speed_index=MetricComparison(
antes=round(data1.speed_index, 2),
depois=round(data2.speed_index, 2),
diferenca=round(data2.speed_index - data1.speed_index, 2)
)
)
def to_dict(self) -> Dict[str, Dict[str, float]]:
"""Convert comparison result to dictionary format for display."""
return {
'Performance': {
'antes': self.performance.antes,
'depois': self.performance.depois,
'diferenca': self.performance.diferenca
},
'Accessibility': {
'antes': self.accessibility.antes,
'depois': self.accessibility.depois,
'diferenca': self.accessibility.diferenca
},
'Best Practices': {
'antes': self.best_practices.antes,
'depois': self.best_practices.depois,
'diferenca': self.best_practices.diferenca
},
'SEO': {
'antes': self.seo.antes,
'depois': self.seo.depois,
'diferenca': self.seo.diferenca
},
'FCP (s)': {
'antes': self.fcp.antes,
'depois': self.fcp.depois,
'diferenca': self.fcp.diferenca
},
'LCP (s)': {
'antes': self.lcp.antes,
'depois': self.lcp.depois,
'diferenca': self.lcp.diferenca
},
'TBT (ms)': {
'antes': self.tbt.antes,
'depois': self.tbt.depois,
'diferenca': self.tbt.diferenca
},
'CLS': {
'antes': self.cls.antes,
'depois': self.cls.depois,
'diferenca': self.cls.diferenca
},
'Speed Index (s)': {
'antes': self.speed_index.antes,
'depois': self.speed_index.depois,
'diferenca': self.speed_index.diferenca
}
} |