Skip to content

Commit

Permalink
Merge pull request #49 from umbraco-community/v1/bugfix/json-deserial…
Browse files Browse the repository at this point in the history
…ization

V1/bugfix/json deserialization
  • Loading branch information
rickbutterfield authored Nov 13, 2024
2 parents e0478e7 + 2d222d3 commit e468c58
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,13 @@
"type": "string",
"description": "Invalid HTML elements for RichText Editor.\n ",
"default": "font"
},
"CloudApiKey": {
"type": [
"null",
"string"
],
"description": "Cloud API Key for TinyMCE. This is required to use TinyMCE premium plugins.\n "
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public class ExternalResource
public string? Url { get; set; }

[JsonProperty("size")]
public decimal Size { get; set; } = 0;
public int? Size { get; set; } = 0;

public ExternalResource() { }

public ExternalResource(string url, decimal size)
public ExternalResource(string url, int? size)
{
Url = url;
Size = size;
Expand Down
59 changes: 8 additions & 51 deletions src/Umbraco.Community.Sustainability/Models/SustainabilityData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,22 @@ namespace Umbraco.Community.Sustainability.Models
{
public record SustainabilityData
{
public int pageWeight { get; set; }
public string carbonRating { get; set; }
public Emissions emissions { get; set; }
public Resource[] resources { get; set; }
public int? pageWeight { get; set; }
public string? carbonRating { get; set; }
public Emissions? emissions { get; set; }
public Resource[]? resources { get; set; }
}

public record Emissions
{
public float co2 { get; set; }
public double? co2 { get; set; }
public bool green { get; set; }
public Variables variables { get; set; }
}

public record Variables
{
public string description { get; set; }
public int bytes { get; set; }
public Gridintensity gridIntensity { get; set; }
public float dataReloadRatio { get; set; }
public float firstVisitPercentage { get; set; }
public float returnVisitPercentage { get; set; }
}

public record Gridintensity
{
public string description { get; set; }
public float network { get; set; }
public float dataCenter { get; set; }
public float production { get; set; }
public float device { get; set; }
}

public record Resource
{
public string name { get; set; }
public string entryType { get; set; }
public float startTime { get; set; }
public float duration { get; set; }
public string initiatorType { get; set; }
public string deliveryType { get; set; }
public string nextHopProtocol { get; set; }
public string renderBlockingStatus { get; set; }
public int workerStart { get; set; }
public int redirectStart { get; set; }
public int redirectEnd { get; set; }
public float fetchStart { get; set; }
public float domainLookupStart { get; set; }
public float domainLookupEnd { get; set; }
public float connectStart { get; set; }
public float secureConnectionStart { get; set; }
public float connectEnd { get; set; }
public float requestStart { get; set; }
public float responseStart { get; set; }
public int firstInterimResponseStart { get; set; }
public float responseEnd { get; set; }
public int transferSize { get; set; }
public int encodedBodySize { get; set; }
public int decodedBodySize { get; set; }
public int responseStatus { get; set; }
public object[] serverTiming { get; set; }
public string? name { get; set; }
public string? initiatorType { get; set; }
public int? transferSize { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SustainabilityResponse
public decimal TotalSize { get; set; } = 0;

[JsonProperty("totalEmissions")]
public float TotalEmissions { get; set; } = 0;
public double TotalEmissions { get; set; } = 0;

[JsonProperty("carbonRating")]
public string? CarbonRating { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ await page.AddScriptTagAsync(new PageAddScriptTagOptions()

return new SustainabilityResponse()
{
TotalSize = sustainabilityData.pageWeight,
TotalEmissions = sustainabilityData.emissions.co2,
CarbonRating = sustainabilityData.carbonRating,
TotalSize = sustainabilityData?.pageWeight.GetValueOrDefault() ?? 0,
TotalEmissions = sustainabilityData?.emissions?.co2.GetValueOrDefault() ?? 0,
CarbonRating = sustainabilityData?.carbonRating,
ResourceGroups = resourceGroups
};
}

private ExternalResourceGroup GetExternalResourceGroup(ResourceGroupType groupType, IList<Resource> resources)
{
var initiator = ExternalResourceGroup.GetInitiatorType(groupType);
var resourcesByType = resources.Where(x => x.initiatorType.Equals(initiator) && x.transferSize > 0);
var resourcesByType = resources.Where(x => !string.IsNullOrEmpty(x.initiatorType) && x.initiatorType.Equals(initiator) && x.transferSize > 0);

var transferSize = 0;
var resourceList = new List<ExternalResource>();
foreach (var resource in resourcesByType.OrderByDescending(x => x.transferSize))
{
transferSize += resource.transferSize;
transferSize += resource.transferSize.GetValueOrDefault();
resourceList.Add(new ExternalResource(resource.name, resource.transferSize));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div ng-controller="Umbraco.Sustainability.ContentApp.Controller as vm">
<div class="umb-logviewer">
<div class="umb-logviewer__main-content">
<div class="umb-logviewer__main-content" style="width: calc(100% - 420px)">
<uui-box headline="Loading sustainability report" ng-if="loading">
<p>It looks like you haven't run a report on this page yet. Click the button below to get started.</p>
<uui-button look="primary"
Expand All @@ -12,7 +12,7 @@
</uui-box>

<uui-box style="margin-bottom: 16px;" headline="{{group.name}}" ng-repeat="group in sustainabilityData.resourceGroups" ng-if="!loading">
<p style="margin-bottom: 0;" slot="header">Total size: {{(group.totalSize / 1024).toFixed(2)}}KB</p>
<p style="margin-bottom: 0;" slot="header-actions">Total size: {{(group.totalSize / 1024).toFixed(2)}}KB</p>
<ul>
<li ng-repeat="resource in group.resources">
{{resource.url}} ({{(resource.size / 1024).toFixed(2)}}KB)
Expand All @@ -21,9 +21,9 @@
</uui-box>
</div>

<div class="umb-logviewer__sidebar" ng-if="!loading">
<div class="umb-logviewer__sidebar" style="flex: 0 0 400px;" ng-if="!loading">
<uui-box headline="Sustainability report" style="margin-bottom: 16px;">
<p slot="header" style="margin-bottom: 0;">Last tested: {{lastTested}}</p>
<p slot="headline" style="margin-bottom: 0; font-weight: 400;">Last tested: {{lastTested}}</p>

<uui-button look="primary"
ng-click="vm.clickButton()"
Expand All @@ -34,11 +34,11 @@
</uui-box>

<div class="umb-box-row" style="margin-bottom: 16px;">
<uui-box headline="Page size" style="margin-left: 10px; margin-right: 10px; flex: 1;">
<uui-box headline="Page size" style="margin-left: 8px; margin-right: 8px; flex: 1;">
{{(sustainabilityData.totalSize / 1024).toFixed(2)}}KB
</uui-box>

<uui-box headline="CO₂ per page view" style="margin-left: 10px; margin-right: 10px; flex: 1;">
<uui-box headline="CO₂ per page view" style="margin-left: 8px; margin-right: 8px; flex: 1;">
{{sustainabilityData.totalEmissions.toFixed(4)}}g
</uui-box>

Expand Down

0 comments on commit e468c58

Please sign in to comment.