Skip to content

Commit

Permalink
Adding Watch Time per session
Browse files Browse the repository at this point in the history
  • Loading branch information
efonsecab committed Apr 11, 2024
1 parent 39a65fb commit 504278e
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public FairPlayCombinedDbContext(DbContextOptions<FairPlayCombinedDbContext> opt

public virtual DbSet<VideoVisibility> VideoVisibility { get; set; }

public virtual DbSet<VideoWatchTime> VideoWatchTime { get; set; }

public virtual DbSet<VwBalance> VwBalance { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down Expand Up @@ -576,6 +578,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.Property(e => e.VideoVisibilityId).ValueGeneratedNever();
});

modelBuilder.Entity<VideoWatchTime>(entity =>
{
entity.HasOne(d => d.VideoInfo).WithMany(p => p.VideoWatchTime)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_VideoWatchTime_VideoInfo");
entity.HasOne(d => d.WatchedByApplicationUser).WithMany(p => p.VideoWatchTime).HasConstraintName("FK_VideoWatchTime_AspNetUsers");
});

modelBuilder.Entity<VwBalance>(entity =>
{
entity.ToView("vwBalance", "FairPlayBudget");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,7 @@ public partial class VideoInfo
[ForeignKey("VideoVisibilityId")]
[InverseProperty("VideoInfo")]
public virtual VideoVisibility VideoVisibility { get; set; }

[InverseProperty("VideoInfo")]
public virtual ICollection<VideoWatchTime> VideoWatchTime { get; set; } = new List<VideoWatchTime>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#nullable disable
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using FairPlayCombined.DataAccess.Models.dboSchema;
using FairPlayCombined.DataAccess.Models.FairPlayBudgetSchema;
using FairPlayCombined.DataAccess.Models.FairPlayDatingSchema;
using FairPlayCombined.DataAccess.Models.FairPlayShopSchema;
using FairPlayCombined.DataAccess.Models.FairPlaySocialSchema;
using FairPlayCombined.DataAccess.Models.FairPlayTubeSchema;


namespace FairPlayCombined.DataAccess.Models.FairPlayTubeSchema;

[Table("VideoWatchTime", Schema = "FairPlayTube")]
[Index("SessionId", Name = "UI_VideoWatchTime_SessionId", IsUnique = true)]
[Index("VideoInfoId", "SessionId", Name = "UI_VideoWatchTime_VideoInfoId_SessionId", IsUnique = true)]
public partial class VideoWatchTime
{
[Key]
public long VideoWatchTimeId { get; set; }

public long VideoInfoId { get; set; }

public Guid SessionId { get; set; }

public DateTimeOffset SessionStartDatetime { get; set; }

public int WatchTime { get; set; }

[StringLength(450)]
public string WatchedByApplicationUserId { get; set; }

[ForeignKey("VideoInfoId")]
[InverseProperty("VideoWatchTime")]
public virtual VideoInfo VideoInfo { get; set; }

[ForeignKey("WatchedByApplicationUserId")]
[InverseProperty("VideoWatchTime")]
public virtual AspNetUsers WatchedByApplicationUser { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public partial class AspNetUsers
[InverseProperty("ApplicationUser")]
public virtual ICollection<VideoPlan> VideoPlan { get; set; } = new List<VideoPlan>();

[InverseProperty("WatchedByApplicationUser")]
public virtual ICollection<VideoWatchTime> VideoWatchTime { get; set; } = new List<VideoWatchTime>();

[ForeignKey("UserId")]
[InverseProperty("User")]
public virtual ICollection<AspNetRoles> Role { get; set; } = new List<AspNetRoles>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@
"Name": "[FairPlayTube].[VideoVisibility]",
"ObjectType": 0
},
{
"Name": "[FairPlayTube].[VideoWatchTime]",
"ObjectType": 0
},
{
"Name": "[FairPlayBudget].[vwBalance]",
"ObjectType": 3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using FairPlayCombined.Common.GeneratorsAttributes;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FairPlayCombined.Models.FairPlayTube.VideoWatchTime
{
public class VideoWatchTimeModel: IListModel
{
public long? VideoWatchTimeId { get; set; }
[Required]
[DeniedValues(default(long))]
public long VideoInfoId { get; set; }
[Required]
public Guid? SessionId { get; set; }= Guid.Empty;
[Required]
public DateTimeOffset? SessionStartDatetime { get; set; }
public int WatchTime { get; set; }
[StringLength(450)]
public string? WatchedByApplicationUserId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using FairPlayCombined.DataAccess.Data;
using FairPlayCombined.DataAccess.Models.FairPlayTubeSchema;
using FairPlayCombined.Models.FairPlayTube.VideoWatchTime;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FairPlayCombined.Services.FairPlayTube
{
public partial class VideoWatchTimeService(
IDbContextFactory<FairPlayCombinedDbContext> dbContextFactory,
ILogger<VideoWatchTimeService> logger) : BaseService
{
public async Task CreateVideoWatchTimeAsync(string videoId,
VideoWatchTimeModel videoWatchTimeModel,
CancellationToken cancellationToken)
{
logger.LogInformation(message: "Start of method: {methodName}", nameof(CreateVideoWatchTimeAsync));
var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken: cancellationToken);
var videoInfoEntity = await dbContext.VideoInfo.SingleAsync(p => p.VideoId == videoId,
cancellationToken: cancellationToken);
VideoWatchTime? entity = new()
{
SessionId = videoWatchTimeModel.SessionId!.Value,
SessionStartDatetime = DateTimeOffset.UtcNow,
VideoInfoId = videoInfoEntity.VideoInfoId,
WatchedByApplicationUserId = videoWatchTimeModel.WatchedByApplicationUserId,
WatchTime = videoWatchTimeModel.WatchTime
};
await dbContext.VideoWatchTime.AddAsync(entity, cancellationToken: cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken: cancellationToken);
}
public async Task UpdateVideoWatchTimeAsync(string videoId,
VideoWatchTimeModel videoWatchTimeModel,
CancellationToken cancellationToken)
{
logger.LogInformation(message: "Start of method: {methodName}", nameof(UpdateVideoWatchTimeAsync));
var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken: cancellationToken);
VideoWatchTime? entity = null;
entity = await dbContext.VideoWatchTime
.SingleOrDefaultAsync(p => p.SessionId == videoWatchTimeModel.SessionId,
cancellationToken: cancellationToken);
entity!.WatchTime = videoWatchTimeModel.WatchTime;
await dbContext.SaveChangesAsync(cancellationToken: cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,11 @@
<Property Name="ParentElementType" Value="SqlTable" />
<Property Name="NewName" Value="VideoPlanThumbnailId" />
</Operation>
<Operation Name="Rename Refactor" Key="439df478-3354-4198-9880-4c19a3b58772" ChangeDateTime="04/11/2024 14:58:27">
<Property Name="ElementName" Value="[dbo].[VideoWatchTime].[Id]" />
<Property Name="ElementType" Value="SqlSimpleColumn" />
<Property Name="ParentElementName" Value="[dbo].[VideoWatchTime]" />
<Property Name="ParentElementType" Value="SqlTable" />
<Property Name="NewName" Value="VideoWatchTimeId" />
</Operation>
</Operations>
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
<Build Include="dbo\Tables\PromptVariable.sql" />
<Build Include="FairPlayTube\Tables\VideoPlan.sql" />
<Build Include="FairPlayTube\Tables\VideoPlanThumbnail.sql" />
<Build Include="FairPlayTube\Tables\VideoWatchTime.sql" />
</ItemGroup>
<ItemGroup>
<PostDeploy Include="Scripts\1-Script.PostDeployment1.sql" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE TABLE [FairPlayTube].[VideoWatchTime]
(
[VideoWatchTimeId] BIGINT NOT NULL CONSTRAINT PK_VideoWatchTime PRIMARY KEY IDENTITY,
[VideoInfoId] BIGINT NOT NULL,
[SessionId] UNIQUEIDENTIFIER NOT NULL,
[SessionStartDatetime] DATETIMEOFFSET NOT NULL,
[WatchTime] INT NOT NULL,
[WatchedByApplicationUserId] NVARCHAR(450) NULL,
CONSTRAINT [FK_VideoWatchTime_VideoInfo] FOREIGN KEY ([VideoInfoId]) REFERENCES [FairPlayTube].[VideoInfo]([VideoInfoId]),
CONSTRAINT [FK_VideoWatchTime_AspNetUsers] FOREIGN KEY ([WatchedByApplicationUserId]) REFERENCES [dbo].[AspNetUsers]([Id])
)

GO

CREATE UNIQUE INDEX [UI_VideoWatchTime_SessionId] ON [FairPlayTube].[VideoWatchTime] ([SessionId])

GO

CREATE UNIQUE INDEX [UI_VideoWatchTime_VideoInfoId_SessionId] ON [FairPlayTube].[VideoWatchTime] ([VideoInfoId],[SessionId])
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
@page "/Public/WatchVideo/{VideoId}"
@implements IAsyncDisposable
@using FairPlayCombined.Interfaces
@using FairPlayCombined.Models.AzureVideoIndexer
@using FairPlayCombined.Services.Common
@using FairPlayCombined.Services.FairPlayTube
@using FairPlayTube.Components.Spinners

@rendermode NoPreRender

@inject AzureVideoIndexerServiceConfiguration azureVideoIndexerServiceConfiguration
@inject AzureVideoIndexerService azureVideoIndexerService
@inject IJSRuntime jsRuntime
@inject VideoWatchTimeService videoWatchTimeService
@inject ILogger<WatchVideo> logger;
@inject IUserProviderService userProviderService


<FluentLabel Typo="Typography.H3">WatchVideo</FluentLabel>
Expand Down Expand Up @@ -87,9 +92,39 @@ else
}

[JSInvokable]
public void UpdateWatchTime(decimal watchTime, string currentSessionGuid)
public async void UpdateWatchTime(int watchTime, string currentSessionGuid)
{
System.Diagnostics.Debug.WriteLine($"Watch Time (s): {watchTime}. Session: {currentSessionGuid}");
try
{
var currentUserId = this.userProviderService.GetCurrentUserId();
System.Diagnostics.Debug.WriteLine($"Watch Time (s): {watchTime}. Session: {currentSessionGuid}");
if (watchTime == 1)
{
await this.videoWatchTimeService.CreateVideoWatchTimeAsync(
this.VideoId!,
new FairPlayCombined.Models.FairPlayTube.VideoWatchTime.VideoWatchTimeModel()
{
WatchedByApplicationUserId = currentUserId,
SessionId = Guid.Parse(currentSessionGuid),
WatchTime = watchTime
}, cancellationToken: cancellationTokenSource.Token);
}
else if (watchTime % 30 == 0)
{
await this.videoWatchTimeService.UpdateVideoWatchTimeAsync(
this.VideoId!,
new FairPlayCombined.Models.FairPlayTube.VideoWatchTime.VideoWatchTimeModel()
{
WatchedByApplicationUserId = currentUserId,
SessionId = Guid.Parse(currentSessionGuid),
WatchTime = watchTime
}, cancellationToken: cancellationTokenSource.Token);
}
}
catch (Exception ex)
{
logger.LogError(exception: ex, "Error: {message}", ex.Message);
}
}

private void OnUseFallbackMode()
Expand Down
1 change: 1 addition & 0 deletions src/FairPlayCombinedSln/FairPlayTube/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
builder.Services.AddTransient<VideoDigitalMarketingDailyPostsService>();
builder.Services.AddTransient<VideoPlanService>();
builder.Services.AddTransient<PromptGeneratorService>();
builder.Services.AddTransient<VideoWatchTimeService>();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

Expand Down

0 comments on commit 504278e

Please sign in to comment.