Skip to content

Commit

Permalink
HLS streaming and RecomendationAlog full impl, + preparation for depl…
Browse files Browse the repository at this point in the history
…oyment
  • Loading branch information
Radoslav Radev committed Apr 3, 2024
1 parent 2173fd5 commit b53b208
Show file tree
Hide file tree
Showing 70 changed files with 1,232 additions and 60 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/APIGatewayPipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: ApiGateway Build Test N Publish

on:
push:
branches: [ "main" ]
paths:
- 'Backend/APIGateway/**'
pull_request:
branches: [ "main" ]
paths:
- 'Backend/APIGateway/**'

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Restore dependencies for APIGateway project
run: dotnet restore Backend/APIGateway/APIGateway.csproj

- name: Build APIGateway project
run: dotnet build Backend/APIGateway/APIGateway.csproj --no-restore




- name: Log in to DockerHub
uses: docker/login-action@v1
if: success()
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v2
if: success()
with:
context: ./Backend/
file: Backend/APIGateway/Dockerfile
push: true
tags: openvidstreamer/APIGateway:latest
2 changes: 2 additions & 0 deletions Backend/.idea/.idea.OpenVidStreamer/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Backend/APIGateway/apigateway-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ spec:
spec:
containers:
- name: apigateway
image: <your-apigateway-image>:latest
image: openvidstreamer/APIGateway:latest
ports:
- containerPort: 8081
29 changes: 29 additions & 0 deletions Backend/Account/account-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: account-deployment
spec:
replicas: 2
selector:
matchLabels:
app: account
template:
metadata:
labels:
app: account
spec:
containers:
- name: account
image: openvidstreamer/account:latest
ports:
- containerPort: 8081
env:
- name: CONSUL_IP
value: consul-service.default.svc.cluster.local
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: servicePort
value: "8081"

File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions Backend/Common/MBcontracts/UpdateVideoToPublicRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public record UpdateVideoToPublicRequest
{
public Guid VideoId { get; set; }
public decimal VideoLength { get; set; }
}
2 changes: 1 addition & 1 deletion Backend/OpenVidStreamer.ManagementNdiscovary/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use the official Consul image as a parent image
FROM consul:latest
FROM consul:1.15.4

# Set the working directory in the container
WORKDIR /consul/config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
spec:
containers:
- name: consul
image: <my-consul-image>:latest
image: openvidstreamer/consul:latest
ports:
- containerPort: 8500
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace RecommendationAlgo.Controllers;
public class VideoStatsController(RecommendationRepository _repo) : ControllerBase
{
[HttpGet("getVideoStatistics")]
public async Task<ActionResult<VideoStats>> GetVideoStats([FromBody] Guid videoId)
public async Task<ActionResult<VideoStats>> GetVideoStats([FromQuery] Guid videoId)
{
return Ok(await _repo.GetVideoStatistics(videoId));
}

[HttpPost("likeVideo")]
public async Task<ActionResult> LikeVideo([FromBody] Guid videoId)
public async Task<ActionResult> LikeVideo([FromQuery] Guid videoId)
{
HttpContext.Request.Headers.TryGetValue("Authorization", out var token);
var accId = Common.AccIdExtractorFromHttpContext.ExtractAccIdUpnFromJwtToken(token);
Expand All @@ -23,7 +23,7 @@ public async Task<ActionResult> LikeVideo([FromBody] Guid videoId)
}

[HttpPost("dislikeVideo")]
public async Task<ActionResult> DislikeVideo([FromBody] Guid videoId)
public async Task<ActionResult> DislikeVideo([FromQuery] Guid videoId)
{

HttpContext.Request.Headers.TryGetValue("Authorization", out var token);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace RecommendationAlgo.Migrations
{
/// <inheritdoc />
public partial class addedPublishDate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "PublishedAt",
table: "VideoStats",
type: "datetime(6)",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "PublishedAt",
table: "VideoStats");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<int>("Category")
.HasColumnType("int");

b.Property<DateTime>("PublishedAt")
.HasColumnType("datetime(6)");

b.Property<decimal>("VideoLength")
.HasColumnType("decimal(65,30)");

Expand Down
4 changes: 4 additions & 0 deletions Backend/RecommendationAlgo/Repository/Entities/VideoStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ public class VideoStats
[Required]
public VideoCategory Category { get; set; } = VideoCategory.Other;

[Required]
public DateTime PublishedAt { get; set; } = DateTime.Now;


}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public async Task DislikeVideo(Guid userId, Guid videoId)
else
{
watchHistory.Liked = VideoLikeEnum.Disliked;

}

await dbContext.SaveChangesAsync();
Expand Down Expand Up @@ -199,6 +200,10 @@ public async Task<List<Guid>> GetRecommendedVideosBasedOnLikeSimilarity(Guid use
/// </summary>
public async Task<List<Guid>> GetAlgoRecommendedVideos(Guid accId, int topN)
{




// Find all the videos that the current user has liked
var likedVideos = dbContext.WatchHistories.Where(w => w.UserId == accId && w.Liked == VideoLikeEnum.Liked)
.Select(w => w.VideoId);
Expand Down Expand Up @@ -238,9 +243,32 @@ public async Task<List<Guid>> GetAlgoRecommendedVideos(Guid accId, int topN)
});

// Sort the videos by the final score in descending order and take the top N
var recommendedVideos = await finalQuery.OrderByDescending(v => v.FinalScore).Take(topN).Select(v => v.VideoId)
var recommendedVideos = await finalQuery.OrderByDescending(v => v.FinalScore).ThenByDescending(v=>v.VideoId).Take(topN).Select(v => v.VideoId)
.ToListAsync();


int numberOfNewVideosToInject = 0;

if (recommendedVideos.Count < topN) // new users dont have watch history and have 0 recommendations
{
numberOfNewVideosToInject = topN - recommendedVideos.Count;
}else if (recommendedVideos.Count >= topN)
{
numberOfNewVideosToInject = 5;
recommendedVideos.RemoveRange(recommendedVideos.Count - 5, 5);
}
var newVideos =
await dbContext.VideoStats.OrderByDescending(v => v.PublishedAt).Select(v => v.VideoId).Take(numberOfNewVideosToInject).ToListAsync();

var random = new Random();
for (int i = 0; i <= numberOfNewVideosToInject; i++)
{
if (i >= newVideos.Count) break;
var a = 1;
recommendedVideos.Insert(random.Next(recommendedVideos.Count), newVideos[i]);
}


return recommendedVideos;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ spec:
spec:
containers:
- name: recommendationalgo
image: <your-microservice-image>:latest
image: openvidstreamer/recommendationalgo:latest
ports:
- containerPort: 8081
env:
Expand Down
Loading

0 comments on commit b53b208

Please sign in to comment.