Skip to content

Commit

Permalink
Reducing calls to Video Indexer API
Browse files Browse the repository at this point in the history
  • Loading branch information
efonsecab committed Apr 14, 2024
1 parent 2e26701 commit b58db18
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public FairPlayCombinedDbContext(DbContextOptions<FairPlayCombinedDbContext> opt

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

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

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

public virtual DbSet<VideoIndexingMargin> VideoIndexingMargin { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <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("VideoIndexerSupportedLanguage", Schema = "FairPlayTube")]
[Index("LanguageCode", Name = "UI_VideoIndexerSupportedLanguage_LanguageCode", IsUnique = true)]
[Index("Name", Name = "UI_VideoIndexerSupportedLanguage_Name", IsUnique = true)]
public partial class VideoIndexerSupportedLanguage
{
[Key]
public int VideoIndexerSupportedLanguageId { get; set; }

[Required]
[StringLength(50)]
public string Name { get; set; }

[Required]
[StringLength(10)]
public string LanguageCode { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@
"Name": "[FairPlayTube].[VideoDigitalMarketingPlan]",
"ObjectType": 0
},
{
"Name": "[FairPlayTube].[VideoIndexerSupportedLanguage]",
"ObjectType": 0
},
{
"Name": "[FairPlayTube].[VideoIndexingCost]",
"ObjectType": 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using FairPlayCombined.DataAccess.Data;
using FairPlayCombined.Models.AzureVideoIndexer;
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 SupportedLanguageService(
IDbContextFactory<FairPlayCombinedDbContext> dbContextFactory,
ILogger<SupportedLanguageService> logger) : BaseService
{
public async Task<SupportedLanguageModel[]?> GetAllSupportedLanguageAsync(
CancellationToken cancellationToken)
{
logger.LogInformation(message: "Start of method: {methodName}", nameof(GetAllSupportedLanguageAsync));
var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
var result = await dbContext.VideoIndexerSupportedLanguage
.Select(p => new SupportedLanguageModel
{
name = p.Name,
languageCode = p.LanguageCode
})
.ToArrayAsync(cancellationToken: cancellationToken);
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,11 @@
<Property Name="ParentElementType" Value="SqlTable" />
<Property Name="NewName" Value="VideoWatchTimeId" />
</Operation>
<Operation Name="Rename Refactor" Key="dd23fec8-eee8-4a9f-8772-7c9aca60ae9a" ChangeDateTime="04/14/2024 15:15:20">
<Property Name="ElementName" Value="[dbo].[VideoIndexerSupportedLanguage].[Id]" />
<Property Name="ElementType" Value="SqlSimpleColumn" />
<Property Name="ParentElementName" Value="[dbo].[VideoIndexerSupportedLanguage]" />
<Property Name="ParentElementType" Value="SqlTable" />
<Property Name="NewName" Value="VideoIndexerSupportedLanguageId" />
</Operation>
</Operations>
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
<Build Include="FairPlayTube\Tables\VideoPlan.sql" />
<Build Include="FairPlayTube\Tables\VideoPlanThumbnail.sql" />
<Build Include="FairPlayTube\Tables\VideoWatchTime.sql" />
<Build Include="FairPlayTube\Tables\VideoIndexerSupportedLanguage.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,14 @@
CREATE TABLE [FairPlayTube].[VideoIndexerSupportedLanguage]
(
[VideoIndexerSupportedLanguageId] INT NOT NULL CONSTRAINT PK_VideoIndexerSupportedLanguage PRIMARY KEY IDENTITY,
[Name] NVARCHAR(50) NOT NULL,
[LanguageCode] NVARCHAR(10) NOT NULL
)

GO

CREATE UNIQUE INDEX [UI_VideoIndexerSupportedLanguage_Name] ON [FairPlayTube].[VideoIndexerSupportedLanguage] ([Name])

GO

CREATE UNIQUE INDEX [UI_VideoIndexerSupportedLanguage_LanguageCode] ON [FairPlayTube].[VideoIndexerSupportedLanguage] ([LanguageCode])
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
var dbContext = await dbContextFactory.CreateDbContextAsync(stoppingToken);
var azureVideoIndexerService =
scope.ServiceProvider.GetRequiredService<AzureVideoIndexerService>();
GetAccessTokenResponseModel? getviTokenResult = null;
if (!stoppingToken.IsCancellationRequested)
{
getviTokenResult = await AuthenticateAsync(azureVideoIndexerService, stoppingToken);
var viSupportedLanguages = await azureVideoIndexerService
.GetSupportedLanguagesAsync(getviTokenResult!.AccessToken!, stoppingToken);
if (viSupportedLanguages != null && viSupportedLanguages?.Length > 0)
{
foreach (var singleViSupportedLanguage in viSupportedLanguages!)
{
if (await dbContext.VideoIndexerSupportedLanguage
.SingleOrDefaultAsync(p => p.LanguageCode == singleViSupportedLanguage.languageCode,
stoppingToken) is null)
{
await dbContext.VideoIndexerSupportedLanguage.AddAsync(
new VideoIndexerSupportedLanguage()
{
LanguageCode = singleViSupportedLanguage.languageCode,
Name = singleViSupportedLanguage.name
}, stoppingToken);
await dbContext.SaveChangesAsync(stoppingToken);
}
}
}
}
while (!stoppingToken.IsCancellationRequested)
{
if (logger.IsEnabled(LogLevel.Information))
Expand All @@ -31,7 +56,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
await GetAllVideosInProcessingStatusAsync(dbContext, stoppingToken);
if (allVideosInProcessingStatus.Length != 0)
{
GetAccessTokenResponseModel? getviTokenResult = await AuthenticateAsync(azureVideoIndexerService, stoppingToken);
var videosIndex = await azureVideoIndexerService.SearchVideosByIdsAsync(
getviTokenResult!.AccessToken!, allVideosInProcessingStatus, stoppingToken);
LogVideoIndexStatus(logger, videosIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@inject VideoWatchTimeService videoWatchTimeService
@inject ILogger<WatchVideo> logger;
@inject IUserProviderService userProviderService
@inject SupportedLanguageService supportedLanguageService


<FluentLabel Typo="Typography.H3">WatchVideo</FluentLabel>
Expand Down Expand Up @@ -81,9 +82,8 @@ else
.GetVideoIndexAsync(this.VideoId!, viAccessTokenResponse!.AccessToken!,
this.cancellationTokenSource.Token);
this.VideoUrl = $"{videoIndex!.videos![0].publishedUrl}?accessToken={viAccessTokenResponse.AccessToken}";
this.SupportedLanguages = await azureVideoIndexerService
.GetSupportedLanguagesAsync(viAccessTokenResponse!.AccessToken!,
this.cancellationTokenSource.Token);
this.SupportedLanguages = await supportedLanguageService
.GetAllSupportedLanguageAsync(this.cancellationTokenSource.Token);
StateHasChanged();
await jsRuntime.InvokeVoidAsync("initializeVideoJsPlayer", "my_video_1", Guid.NewGuid().ToString(), this.dotNetObjectReference);
this.IsBusy = false;
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 @@ -237,6 +237,7 @@
builder.Services.AddTransient<VideoPlanService>();
builder.Services.AddTransient<PromptGeneratorService>();
builder.Services.AddTransient<VideoWatchTimeService>();
builder.Services.AddTransient<SupportedLanguageService>();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

Expand Down

0 comments on commit b58db18

Please sign in to comment.