Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<div class="currently-loaded-models-content">
<span>Used Linkers: </span>
<!-- TODO FIXIT(#70): populate from api-->
<!-- TODO FIXIT(#61): populate from api-->
@for (linker of []; track linker) {
{{ linker }}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component, computed, input } from '@angular/core';
import { TreeNode } from '../../../../../types/tree-node';
import { SelectableTreeComponent } from '../../../../../components/selectable-tree/selectable-tree.component';
import { InstanceInfo } from '../../../../../types/instance';

@Component({
selector: 'app-currently-loaded-models',
Expand All @@ -10,10 +9,10 @@ import { InstanceInfo } from '../../../../../types/instance';
imports: [SelectableTreeComponent],
})
export class CurrentlyLoadedModelsComponent {
instanceInfo = input.required<InstanceInfo>();
instanceId = input.required<string>();

loadedFiles = computed<TreeNode[]>(() => {
// TODO FIXIT(#70): populate from api
// TODO FIXIT(#61): populate from api
return convertToTreeNodes({});
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Component, computed, input } from '@angular/core';
import { InstanceInfo } from '../../../../../types/instance';
import { createRxResourceHandler } from '../../../../../utils/resource';
import { ProjectContextService } from '../../../../../services/projects/project-context.service';
import { CenteredSpinnerComponent } from '../../../../../components/centered-spinner/centered-spinner.component';
Expand All @@ -18,39 +17,35 @@ import { ProjectContext } from '../../../../../types/returned-context-slice';
})
export class ProjectContextComponent {
projectId = input.required<string>();
instanceInfo = input.required<InstanceInfo>();
instanceId = input.required<string>();

context = computed<TreeNode[]>(() => {
return convertToTreeNodes(this.projectContextResource.value() ?? []);
});

projectContextResource = createRxResourceHandler({
request: () => ({ projectId: this.projectId(), instanceInfo: this.instanceInfo() }),
loader: (params) => this.projectContextService.getProjectContext(params.request.projectId, params.request.instanceInfo.id),
request: () => ({ projectId: this.projectId(), instanceId: this.instanceId() }),
loader: (params) => this.projectContextService.getProjectContext(params.request.projectId, params.request.instanceId),
});

clearContextHandler = apiHandler(
(params: { projectId: string; instanceId: string }) => this.projectContextService.clearContext(params.projectId, params.instanceId),
(data) => {
console.log(data);
}
() => this.projectContextResource.reload()
);

reloadContextHandler = apiHandler(
(params: { projectId: string; instanceId: string }) => this.projectContextService.reloadContext(params.projectId, params.instanceId),
(data) => {
console.log(data);
}
() => this.projectContextResource.reload()
);

constructor(private projectContextService: ProjectContextService) {}

onReloadModelsClick() {
this.reloadContextHandler.execute({ projectId: this.projectId(), instanceId: this.instanceInfo().id });
this.reloadContextHandler.execute({ projectId: this.projectId(), instanceId: this.instanceId() });
}

onClearContextButtonClick() {
this.clearContextHandler.execute({ projectId: this.projectId(), instanceId: this.instanceInfo().id });
this.clearContextHandler.execute({ projectId: this.projectId(), instanceId: this.instanceId() });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
} @else if (currentInstanceInfoResource.error()) {
<app-error-state [error]="currentInstanceInfoResource.error()!" />
} @else {
<app-currently-loaded-models [instanceInfo]="currentInstanceInfoResource.value()!" />
<app-currently-loaded-models [instanceId]="currentInstanceInfoResource.value()!.id" />
}

<mat-divider />
Expand All @@ -41,7 +41,7 @@
} @else if (currentInstanceInfoResource.error()) {
<app-error-state [error]="currentInstanceInfoResource.error()!" />
} @else {
<app-project-context [projectId]="projectId()!" [instanceInfo]="currentInstanceInfoResource.value()!" />
<app-project-context [projectId]="projectId()!" [instanceId]="currentInstanceInfoResource.value()!.id" />
}
</mat-card>
}
Expand Down
3 changes: 3 additions & 0 deletions src/Adapters/Driven/Rest/Api/IContextApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace ScriptBee.Rest.Api;

public interface IContextApi
{
[Get("/api/context")]
Task<IEnumerable<RestContextSlice>> Get(CancellationToken cancellationToken);

[Post("/api/context/clear")]
Task Clear(CancellationToken cancellationToken);

Expand Down
15 changes: 15 additions & 0 deletions src/Adapters/Driven/Rest/Contracts/RestContextSlice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using ScriptBee.Domain.Model.Context;

namespace ScriptBee.Rest.Contracts;

public class RestContextSlice
{
public required string Model { get; set; }

public required IEnumerable<string> PluginIds { get; set; }

public ContextSlice Map()
{
return new ContextSlice(Model, PluginIds);
}
}
24 changes: 24 additions & 0 deletions src/Adapters/Driven/Rest/GetInstanceContextAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Refit;
using ScriptBee.Domain.Model.Context;
using ScriptBee.Domain.Model.Instance;
using ScriptBee.Ports.Instance;
using ScriptBee.Rest.Api;

namespace ScriptBee.Rest;

public class GetInstanceContextAdapter(IHttpClientFactory httpClientFactory) : IGetInstanceContext
{
public async Task<IEnumerable<ContextSlice>> Get(
InstanceInfo instanceInfo,
CancellationToken cancellationToken = default
)
{
var client = httpClientFactory.CreateClient();
client.BaseAddress = new Uri(instanceInfo.Url);

var contextApi = RestService.For<IContextApi>(client);

var restContextSlices = await contextApi.Get(cancellationToken);
return restContextSlices.Select(s => s.Map());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using ScriptBee.Domain.Model.Context;

namespace ScriptBee.Analysis.Web.EndpointDefinitions.Context.Contracts;

public record WebContextSlice(string Model, IEnumerable<string> PluginIds)
{
public static WebContextSlice Map(ContextSlice contextSlice)
{
return new WebContextSlice(contextSlice.Model, contextSlice.PluginIds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Http.HttpResults;
using ScriptBee.Analysis.Web.EndpointDefinitions.Context.Contracts;
using ScriptBee.Common.Web;
using ScriptBee.Service.Analysis;
using ScriptBee.UseCases.Analysis;

namespace ScriptBee.Analysis.Web.EndpointDefinitions.Context;

public class GetContextEndpoint : IEndpointDefinition
{
public void DefineServices(IServiceCollection services)
{
services.AddSingleton<IGetContextUseCase, GetContextService>();
}

public void DefineEndpoints(IEndpointRouteBuilder app)
{
app.MapGet("/api/context", GetContext);
}

private static Ok<IEnumerable<WebContextSlice>> GetContext(IGetContextUseCase useCase)
{
var contextSlices = useCase.Get();

return TypedResults.Ok(contextSlices.Select(WebContextSlice.Map));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using ScriptBee.Domain.Model.Context;

namespace ScriptBee.Web.EndpointDefinitions.Context.Contracts;

public record WebProjectContextSlice(string Model, IEnumerable<string> PluginIds)
{
public static WebProjectContextSlice Map(ContextSlice contextSlice)
{
return new WebProjectContextSlice(contextSlice.Model, contextSlice.PluginIds);
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using ScriptBee.Common.Web;
using ScriptBee.Domain.Model.Instance;
using ScriptBee.Domain.Model.Project;
using ScriptBee.Service.Project.Context;
using ScriptBee.UseCases.Project.Context;
using ScriptBee.Web.EndpointDefinitions.Context.Contracts;
using ScriptBee.Web.Exceptions;

namespace ScriptBee.Web.EndpointDefinitions.Context;

public class GetProjectContextEndpoint : IEndpointDefinition
{
public void DefineServices(IServiceCollection services)
{
// TODO FIXIT: update dependencies
services.AddSingleton<IGetInstanceContextUseCase, GetInstanceContextService>();
}

public void DefineEndpoints(IEndpointRouteBuilder app)
{
app.MapGet("/api/projects/{projectId}/instances/{instanceId}/context", GetCurrentContext);
}

private static async Task<Ok<IEnumerable<WebGetProjectContextResponse>>> GetCurrentContext(
private static async Task<
Results<Ok<IEnumerable<WebProjectContextSlice>>, NotFound<ProblemDetails>>
> GetCurrentContext(
HttpContext context,
[FromRoute] string projectId,
[FromRoute] string instanceId
[FromRoute] string instanceId,
IGetInstanceContextUseCase useCase,
CancellationToken cancellationToken
)
{
await Task.CompletedTask;

// TODO FIXIT: remove hardcoded value
var query = new GetInstanceContextQuery(
ProjectId.FromValue(projectId),
new InstanceId(instanceId)
);
var result = await useCase.Get(query, cancellationToken);

return TypedResults.Ok(
(IEnumerable<WebGetProjectContextResponse>)
[
new WebGetProjectContextResponse("Repository", ["InspectorGit", "honeydew"]),
new WebGetProjectContextResponse("Class", ["honeydew"]),
]
return result.Match<
Results<Ok<IEnumerable<WebProjectContextSlice>>, NotFound<ProblemDetails>>
>(
slices => TypedResults.Ok(slices.Select(s => WebProjectContextSlice.Map(s))),
error => error.ToProblem(context)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static IServiceCollection AddRestConfig(this IServiceCollection services)
.AddHttpClient()
.AddSingleton<IGetPlugins, GetPluginsAdapter>()
.AddSingleton<IGetScriptLanguages, GetScriptLanguagesAdapter>()
.AddSingleton<IGetInstanceContext, GetInstanceContextAdapter>()
.AddSingleton<IClearInstanceContext, ClearInstanceContextAdapter>()
.AddSingleton<ILinkInstanceContext, LinkInstanceContextAdapter>()
.AddSingleton<ILoadInstanceContext, LoadInstanceContextAdapter>();
Expand Down
3 changes: 3 additions & 0 deletions src/Application/Domain/Model/Context/ContextSlice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace ScriptBee.Domain.Model.Context;

public record ContextSlice(string Model, IEnumerable<string> PluginIds);
19 changes: 19 additions & 0 deletions src/Application/Domain/Service.Analysis/GetContextService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using ScriptBee.Domain.Model.Context;
using ScriptBee.UseCases.Analysis;

namespace ScriptBee.Service.Analysis;

public class GetContextService(IProjectManager projectManager) : IGetContextUseCase
{
public IEnumerable<ContextSlice> Get()
{
var project = projectManager.GetProject();

return project
.Context.Models.Keys.GroupBy(tuple => tuple.Item1)
.Select(grouping => new ContextSlice(
grouping.Key,
grouping.Select(tuple => tuple.Item2).ToList()
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using OneOf;
using ScriptBee.Domain.Model.Context;
using ScriptBee.Domain.Model.Errors;
using ScriptBee.Ports.Instance;
using ScriptBee.UseCases.Project.Context;

namespace ScriptBee.Service.Project.Context;

using GetInstanceContextResult = OneOf<IEnumerable<ContextSlice>, InstanceDoesNotExistsError>;

public class GetInstanceContextService(
IGetProjectInstance getProjectInstance,
IGetInstanceContext getInstanceContext
) : IGetInstanceContextUseCase
{
public async Task<GetInstanceContextResult> Get(
GetInstanceContextQuery query,
CancellationToken cancellationToken = default
)
{
var result = await getProjectInstance.Get(query.InstanceId, cancellationToken);

return await result.Match<Task<GetInstanceContextResult>>(
async instanceInfo =>
GetInstanceContextResult.FromT0(
await getInstanceContext.Get(instanceInfo, cancellationToken)
),
error => Task.FromResult<GetInstanceContextResult>(error)
);
}
}
12 changes: 12 additions & 0 deletions src/Application/Ports/Driven/Ports.Instance/IGetInstanceContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using ScriptBee.Domain.Model.Context;
using ScriptBee.Domain.Model.Instance;

namespace ScriptBee.Ports.Instance;

public interface IGetInstanceContext
{
Task<IEnumerable<ContextSlice>> Get(
InstanceInfo instanceInfo,
CancellationToken cancellationToken = default
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using ScriptBee.Domain.Model.Context;

namespace ScriptBee.UseCases.Analysis;

public interface IGetContextUseCase
{
IEnumerable<ContextSlice> Get();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using ScriptBee.Domain.Model.Instance;
using ScriptBee.Domain.Model.Project;

namespace ScriptBee.UseCases.Project.Context;

public record GetInstanceContextQuery(ProjectId ProjectId, InstanceId InstanceId);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using OneOf;
using ScriptBee.Domain.Model.Context;
using ScriptBee.Domain.Model.Errors;

namespace ScriptBee.UseCases.Project.Context;

public interface IGetInstanceContextUseCase
{
Task<OneOf<IEnumerable<ContextSlice>, InstanceDoesNotExistsError>> Get(
GetInstanceContextQuery query,
CancellationToken cancellationToken = default
);
}
Loading
Loading