Skip to content

Commit

Permalink
84: Removed reset from playground app
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmatys committed May 23, 2024
1 parent 4d2dd74 commit 067d7f4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ namespace ASSISTENTE.Application.Abstractions.Interfaces;
public interface IMaintenanceService
{
public Task<Result> InitAsync();
public Task<Result> ResetAsync();
}

This file was deleted.

41 changes: 30 additions & 11 deletions API/ASSISTENTE.Infrastructure/Services/MaintenanceService.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@
using ASSISTENTE.Application.Abstractions.Interfaces;
using ASSISTENTE.Infrastructure.Qdrant;
using ASSISTENTE.Language.Enums;
using ASSISTENTE.Persistence.Configuration;
using Microsoft.EntityFrameworkCore;

namespace ASSISTENTE.Infrastructure.Services;

public sealed class MaintenanceService(
IQdrantService qdrantService
IQdrantService qdrantService,
IAssistenteDbContext context
) : IMaintenanceService
{
public async Task<Result> InitAsync()
{
var resourceTypes = Enum.GetValues(typeof(ResourceType));
var results = new List<Result>();

foreach (var resourceType in Enum.GetValues(typeof(ResourceType)))
var clearanceResult = await ClearDatabase();

if (clearanceResult.IsFailure) return clearanceResult;

foreach (var type in resourceTypes)
{
var result = await qdrantService.DropCollectionAsync($"embeddings-{type}");

results.Add(result);
}

foreach (var type in resourceTypes)
{
var result = await qdrantService.CreateCollectionAsync($"embeddings-{resourceType}");
var result = await qdrantService.CreateCollectionAsync($"embeddings-{type}");

results.Add(result);
}

return Result.Combine(results);
}

public async Task<Result> ResetAsync()
private async Task<Result> ClearDatabase()
{
var results = new List<Result>();

foreach (var resourceType in Enum.GetValues(typeof(ResourceType)))
try
{
var result = await qdrantService.DropCollectionAsync($"embeddings-{resourceType}");
var resources = await context.Resources.ToListAsync();

results.Add(result);
}
context.Resources.RemoveRange(resources);

return Result.Combine(results);
await context.SaveChangesAsync();

return Result.Success();
}
catch
{
return Result.Failure("Failed to clear database");
}
}
}
10 changes: 3 additions & 7 deletions API/ASSISTENTE.Playground/Commons/PlaygroundParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ namespace ASSISTENTE.Playground.Commons;

internal sealed class PlaygroundParams
{
[Option('r', "reset", Required = false, HelpText = "Reset the playground")]
public bool Reset { get; set; }

[Option('q', "question", Required = false, HelpText = "Question to ask the playground")]
public string? Question { get; set; }

[Option('l', "learn", Required = false, HelpText = "Learn from a file")]
[Option('l', "learn", Required = false, HelpText = "Learn from a files")]
public bool Learn { get; set; }

public bool IsValid => Reset || Learn || !string.IsNullOrWhiteSpace(Question);
public static string NotValidMessage => "Select an option: -r, -l or -q";
public bool IsValid => Learn || !string.IsNullOrWhiteSpace(Question);
public static string NotValidMessage => "Select an option: -l or -q";
}
9 changes: 0 additions & 9 deletions API/ASSISTENTE.Playground/Playground.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using ASSISTENTE.Application.Abstractions.Interfaces;
using ASSISTENTE.Application.Handlers.Knowledge.Commands;
using ASSISTENTE.Application.Handlers.Maintenance.Commands;
using ASSISTENTE.Common.Extensions;
using MediatR;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -36,12 +35,4 @@ public async Task LearnAsync()
.Log("Learning completed!", logger)
.LogError(logger);
}

public async Task ResetAsync()
{
var result = await mediator.Send(ResetCommand.Create());

result
.LogError(logger);
}
}
2 changes: 0 additions & 2 deletions API/ASSISTENTE.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

Log.Information("Starting Playground...");

if (parsedParams.Reset)
await playground.ResetAsync();
if (parsedParams.Learn)
await playground.LearnAsync();
if (parsedParams.Question != null)
Expand Down

0 comments on commit 067d7f4

Please sign in to comment.