Skip to content

Commit df51db0

Browse files
Merge pull request #1 from rickbutterfield/feature/db-table
Add page metrics table
2 parents f9a0c83 + 320b543 commit df51db0

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Umbraco.Cms.Core.Composing;
2+
using Umbraco.Cms.Core.DependencyInjection;
3+
using Umbraco.Cms.Core.Notifications;
4+
using Umbraco.Community.Sustainability.Notifications;
5+
6+
namespace Umbraco.Community.Sustainability.Composers
7+
{
8+
public class PageMetricsComposer : IComposer
9+
{
10+
public void Compose(IUmbracoBuilder builder)
11+
{
12+
builder.AddNotificationHandler<UmbracoApplicationStartingNotification, PageMetricsNotificationHandler>();
13+
}
14+
}
15+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Umbraco.Cms.Infrastructure.Scoping;
3+
using Umbraco.Cms.Web.Common.Controllers;
4+
using Umbraco.Community.Sustainability.Models;
5+
using Umbraco.Community.Sustainability.Schemas;
6+
7+
namespace Umbraco.Community.Sustainability.Controllers
8+
{
9+
public class PageMetricsController : UmbracoApiController
10+
{
11+
private readonly IScopeProvider _scopeProvider;
12+
13+
public PageMetricsController(IScopeProvider scopeProvider)
14+
{
15+
_scopeProvider = scopeProvider;
16+
}
17+
18+
[HttpGet]
19+
public IEnumerable<PageMetric> GetPageMetrics(int pageId)
20+
{
21+
using var scope = _scopeProvider.CreateScope();
22+
var queryResults = scope.Database.Fetch<PageMetric>($"SELECT * FROM {PageMetric.TableName} WHERE NodeId = @0", pageId);
23+
scope.Complete();
24+
25+
return queryResults;
26+
}
27+
28+
[HttpGet]
29+
public void TestData()
30+
{
31+
var rand = new Random();
32+
var pageMetric = new PageMetric()
33+
{
34+
NodeId = 1,
35+
RequestedBy = "Thomas",
36+
RequestDate = DateTime.Now,
37+
PageWeight = new decimal(rand.NextDouble()),
38+
PageData = "example data"
39+
};
40+
41+
using var scope = _scopeProvider.CreateScope();
42+
scope.Database.Insert(pageMetric);
43+
scope.Complete();
44+
}
45+
46+
[HttpPost]
47+
public void AddPageMetric(PageMetric pageMetric)
48+
{
49+
using var scope = _scopeProvider.CreateScope();
50+
scope.Database.Insert(pageMetric);
51+
scope.Complete();
52+
}
53+
}
54+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Extensions.Logging;
2+
using Umbraco.Cms.Infrastructure.Migrations;
3+
using Umbraco.Community.Sustainability.Schemas;
4+
5+
namespace Umbraco.Community.Sustainability.Migrations
6+
{
7+
public class AddPageMetricsTable : MigrationBase
8+
{
9+
public AddPageMetricsTable(IMigrationContext context) : base(context)
10+
{
11+
}
12+
13+
protected override void Migrate()
14+
{
15+
Logger.LogDebug("Running migration {MigrationStep}", "AddPageMetricsTable");
16+
17+
if (TableExists(PageMetric.TableName) == false)
18+
{
19+
Create.Table<PageMetric>().Do();
20+
}
21+
else
22+
{
23+
Logger.LogDebug("The database table {DbTable} already exists, skipping", PageMetric.TableName);
24+
}
25+
}
26+
}
27+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Umbraco.Cms.Core.Events;
7+
using Umbraco.Cms.Core.Migrations;
8+
using Umbraco.Cms.Core.Notifications;
9+
using Umbraco.Cms.Core.Scoping;
10+
using Umbraco.Cms.Core.Services;
11+
using Umbraco.Cms.Core;
12+
using Umbraco.Cms.Infrastructure.Migrations.Upgrade;
13+
using Umbraco.Cms.Infrastructure.Migrations;
14+
using Umbraco.Community.Sustainability.Migrations;
15+
using Umbraco.Community.Sustainability.Schemas;
16+
17+
namespace Umbraco.Community.Sustainability.Notifications
18+
{
19+
public class PageMetricsNotificationHandler : INotificationHandler<UmbracoApplicationStartingNotification>
20+
{
21+
private readonly IMigrationPlanExecutor _migrationPlanExecutor;
22+
private readonly ICoreScopeProvider _coreScopeProvider;
23+
private readonly IKeyValueService _keyValueService;
24+
private readonly IRuntimeState _runtimeState;
25+
26+
public PageMetricsNotificationHandler(
27+
ICoreScopeProvider coreScopeProvider,
28+
IMigrationPlanExecutor migrationPlanExecutor,
29+
IKeyValueService keyValueService,
30+
IRuntimeState runtimeState)
31+
{
32+
_migrationPlanExecutor = migrationPlanExecutor;
33+
_coreScopeProvider = coreScopeProvider;
34+
_keyValueService = keyValueService;
35+
_runtimeState = runtimeState;
36+
}
37+
38+
public void Handle(UmbracoApplicationStartingNotification notification)
39+
{
40+
if (_runtimeState.Level < RuntimeLevel.Run)
41+
{
42+
return;
43+
}
44+
45+
// Create a migration plan for a specific project/feature
46+
// We can then track that latest migration state/step for this project/feature
47+
var migrationPlan = new MigrationPlan(PageMetric.TableName);
48+
49+
// This is the steps we need to take
50+
// Each step in the migration adds a unique value
51+
migrationPlan.From(string.Empty)
52+
.To<AddPageMetricsTable>("pagemetrics-init");
53+
54+
// Go and upgrade our site (Will check if it needs to do the work or not)
55+
// Based on the current/latest step
56+
var upgrader = new Upgrader(migrationPlan);
57+
upgrader.Execute(
58+
_migrationPlanExecutor,
59+
_coreScopeProvider,
60+
_keyValueService);
61+
}
62+
}
63+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using NPoco;
2+
using Umbraco.Cms.Infrastructure.Persistence.DatabaseAnnotations;
3+
4+
namespace Umbraco.Community.Sustainability.Schemas
5+
{
6+
[TableName(TableName)]
7+
[PrimaryKey("Id", AutoIncrement = true)]
8+
[ExplicitColumns]
9+
public class PageMetric
10+
{
11+
public const string TableName = "umbPageMetrics";
12+
13+
[PrimaryKeyColumn(AutoIncrement = true, IdentitySeed = 1)]
14+
[Column("Id")]
15+
public int Id { get; set; }
16+
17+
[Column("NodeId")]
18+
public int NodeId { get; set; }
19+
20+
[Column("RequestedBy")]
21+
public string RequestedBy { get; set; }
22+
23+
[Column("RequestDate")]
24+
public DateTime RequestDate { get; set; }
25+
26+
[Column("PageWeight")]
27+
public decimal PageWeight { get; set; }
28+
29+
[SpecialDbType(SpecialDbTypes.NVARCHARMAX)]
30+
[NullSetting(NullSetting = NullSettings.Null)]
31+
public string? PageData { get; set; }
32+
}
33+
}

0 commit comments

Comments
 (0)