Skip to content

Commit

Permalink
watch
Browse files Browse the repository at this point in the history
  • Loading branch information
weibaohui committed Nov 23, 2023
1 parent ce1c7eb commit 13aa5b2
Show file tree
Hide file tree
Showing 17 changed files with 210 additions and 150 deletions.
9 changes: 8 additions & 1 deletion BlazorApp/Chat/ChatHub.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
using System;
using System.Threading.Tasks;
using k8s;
using k8s.Models;
using Microsoft.AspNetCore.SignalR;

namespace BlazorApp.Chat;

public class ChatHub : Hub, IChatHub
public class ChatHub : Hub
{
public async Task SendMessage(string message)
{
Console.WriteLine("收到" + message);
await Clients.All.SendAsync("ReceiveMessage", message);
}

public Task SendWatchEvent(string message)
{
throw new NotImplementedException();
}
}
9 changes: 6 additions & 3 deletions BlazorApp/Chat/IChatHub.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Threading.Tasks;
using k8s;
using k8s.Models;

namespace BlazorApp.Chat;

public interface IChatHub
public interface IChatHub<in T> where T : IKubernetesObject<V1ObjectMeta>
{
public Task SendMessage(string message);
}
public Task SendMessage(string message);
public Task SendWatchEvent(string message);
}
22 changes: 22 additions & 0 deletions BlazorApp/Chat/SignalSendService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using BlazorApp.Service;
using k8s;
using k8s.Models;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;

namespace BlazorApp.Chat;

public class SignalSendService<T> where T : IKubernetesObject<V1ObjectMeta>
{
private readonly IHubContext<ChatHub> _ctx;

public SignalSendService(IHubContext<ChatHub> ctx)
{
_ctx = ctx;
}

public void Send(WatchEventType type, T item)
{
_ctx.Clients.All.SendAsync("Resource", (type, item));
}
}
9 changes: 6 additions & 3 deletions BlazorApp/Pages/ChatHubPage.razor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Threading.Tasks;
using Entity;
using k8s.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.SignalR.Client;

Expand All @@ -22,10 +24,11 @@ protected override async Task OnInitializedAsync()
.Build();

//receive event
_hubConnection.On<string>("ReceiveMessage", async (message) =>
_hubConnection.On<ResourceWatchEntity<V1Pod>>("ReceiveMessage", async (data) =>
{
Console.WriteLine("page收到消息" + message);
MessageReceived += message;
Console.WriteLine("page收到消息" + data.Message);
Console.WriteLine("page收到消息" + data.Item.Metadata.Name);
MessageReceived += data.Item.Metadata.Name;
await InvokeAsync(StateHasChanged);
});

Expand Down
4 changes: 3 additions & 1 deletion BlazorApp/Pages/Deployment/DeploymentIndex.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Threading.Tasks;
using AntDesign.TableModels;
using BlazorApp.Service;
using BlazorApp.Service.impl;
using BlazorApp.Utils;
using k8s.Models;
using Microsoft.AspNetCore.Components;

Expand Down Expand Up @@ -71,4 +73,4 @@ private async Task OnDeployClick(V1Deployment deploy)
var options = PageDrawerService.DefaultOptions($"{deploy.Kind ?? "Deployment"}:{deploy.Name()}");
await PageDrawerService.ShowDrawerAsync<DeploymentDetailView, V1Deployment, bool>(options, deploy);
}
}
}
16 changes: 8 additions & 8 deletions BlazorApp/Pages/Pod/PodIndex.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
@using Extension
@using BlazorApp.Pages.Workload
@using BlazorApp.Pages.Container
@if (tps.PagedItems != null)
@if (_tps.PagedItems != null)
{
<Card Bordered="false" Hoverable="true">
<TitleTemplate>
<SearchToolBar Title="Pods" Count="@tps.Total"
<SearchToolBar Title="Pods" Count="@_tps.Total"
OnSearch="OnSearchHandler"
OnNsSelected="OnNsSelectedHandler">
</SearchToolBar>
Expand All @@ -17,13 +17,13 @@
<Body>

<Table TItem="V1Pod"
DataSource="@tps.PagedItems"
DataSource="@_tps.PagedItems"
Size=@TableSize.Small Bordered="true"
Total="tps.Total"
@bind-PageIndex="tps.PageIndex"
@bind-PageSize="tps.PageSize"
@bind-SelectedRows="tps.SelectedRows"
Loading="tps.Loading"
Total="_tps.Total"
@bind-PageIndex="_tps.PageIndex"
@bind-PageSize="_tps.PageSize"
@bind-SelectedRows="_tps.SelectedRows"
Loading="_tps.Loading"
OnChange="OnChange"
ScrollX="1500">
<Selection Key="@(context.Name())" Width="40" Fixed="left"/>
Expand Down
24 changes: 13 additions & 11 deletions BlazorApp/Pages/Pod/PodIndex.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using AntDesign.TableModels;
using BlazorApp.Pages.Node;
using BlazorApp.Service;
using BlazorApp.Service.impl;
using BlazorApp.Utils;
using Entity;
using k8s.Models;
using Microsoft.AspNetCore.Components;
Expand All @@ -23,26 +25,26 @@ public partial class PodIndex : ComponentBase
private IPageDrawerService PageDrawerService { get; set; }

[Inject]
private IWatchService WatchServicee { get; set; }
private IWatchService WatchService { get; set; }


private TablePagedService<V1Pod> tps;
private TablePagedService<V1Pod> _tps;


private string _selectedNs = "";


protected override async Task OnInitializedAsync()
{
tps = new TablePagedService<V1Pod>(PodService);
await tps.GetData(_selectedNs);
_tps = new TablePagedService<V1Pod>(PodService);
await _tps.GetData(_selectedNs);
//TODO 改为接收watch
var timer = new Timer(1000);
timer.Enabled = true;
timer.Elapsed += async (o, args) =>
{
if (!PodService.Changed()) return;
await tps.GetData(_selectedNs);
await _tps.GetData(_selectedNs);
await InvokeAsync(StateHasChanged);
// Console.WriteLine("refreshPods");
};
Expand All @@ -52,31 +54,31 @@ protected override async Task OnInitializedAsync()
private async Task OnNsSelectedHandler(string ns)
{
_selectedNs = ns;
await tps.OnNsSelectedHandler(ns);
await _tps.OnNsSelectedHandler(ns);
await InvokeAsync(StateHasChanged);
}

public void RemoveSelection(string uid)
{
tps.SelectedRows = tps.SelectedRows.Where(x => x.Metadata.Uid != uid);
_tps.SelectedRows = _tps.SelectedRows.Where(x => x.Metadata.Uid != uid);
}


private async Task OnChange(QueryModel<V1Pod> queryModel)
{
tps.OnChange(queryModel);
_tps.OnChange(queryModel);
await InvokeAsync(StateHasChanged);
}

private async Task OnSearchHandler(string key)
{
if (string.IsNullOrEmpty(key))
{
await tps.GetData(_selectedNs);
await _tps.GetData(_selectedNs);
}
else
{
tps.OnSearch(tps.OriginItems.Where(w => w.Name().Contains(key)).ToList());
_tps.OnSearch(_tps.OriginItems.Where(w => w.Name().Contains(key)).ToList());
}

await InvokeAsync(StateHasChanged);
Expand All @@ -103,7 +105,7 @@ private async Task PodDeleteHandler(V1Pod pod)

private async Task OnPodChanged(string obj)
{
await tps.GetData(_selectedNs);
await _tps.GetData(_selectedNs);
await InvokeAsync(StateHasChanged);
}
}
Expand Down
4 changes: 3 additions & 1 deletion BlazorApp/Pages/ReplicaSet/ReplicaSetIndex.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Threading.Tasks;
using AntDesign.TableModels;
using BlazorApp.Service;
using BlazorApp.Service.impl;
using BlazorApp.Utils;
using k8s.Models;
using Microsoft.AspNetCore.Components;

Expand Down Expand Up @@ -72,4 +74,4 @@ private async Task OnRsClick(V1ReplicaSet rs)
await PageDrawerService.ShowDrawerAsync<ReplicaSetDetailView, V1ReplicaSet, bool>(options, rs);
}
}
}
}
7 changes: 3 additions & 4 deletions BlazorApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@
builder.Services.AddAntDesign();
builder.Services.Configure<ProSettings>(builder.Configuration.GetSection("ProSettings"));
builder.Services.AddSingleton<IKubeService, KubeService>();
builder.Services.AddHttpClient();

builder.Services.AddScoped<IConfigService, ConfigService>();
builder.Services.AddSingleton<IBaseService, BaseService>();
builder.Services.AddSingleton<IWatchService, WatchService>();
builder.Services.AddScoped<IConfigService, ConfigService>();
builder.Services.AddScoped<INodeService, NodeService>();
builder.Services.AddScoped<IPodService, PodService>();
builder.Services.AddScoped<IDeploymentService, DeploymentService>();
builder.Services.AddScoped<IReplicaSetService, ReplicaSetService>();
builder.Services.AddScoped<IEventService, EventService>();
builder.Services.AddScoped<INamespaceService, NamespaceService>();
builder.Services.AddScoped<IPageDrawerService, PageDrawerService>();
builder.Services.AddSingleton<IWatchService, WatchService>();
builder.Services.AddScoped<IOpenAiService, OpenAiService>();
builder.Services.AddScoped<IKubectlService, KubectlService>();
builder.Services.AddScoped<IRockAiService, RockAiService>();
builder.Services.AddHttpClient();

var app = builder.Build();
// Configure the HTTP request pipeline.
Expand Down
16 changes: 8 additions & 8 deletions BlazorApp/Service/impl/NamespaceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ public NamespaceService(IBaseService baseService)
/// <summary>
/// 缓存当前NS列表
/// </summary>
private IList<V1Namespace> ns;
private IList<V1Namespace> _ns;

public async Task<IList<V1Namespace>> GetNamespaces()
{
if (ns != null)
if (_ns != null)
{
return ns;
return _ns;
}

_ = await List();
return ns;
return _ns;
}

public async Task<V1NamespaceList> List()
{

var list = await _baseService.Client().ListNamespaceAsync();
var list = await _baseService.Client().ListNamespaceAsync();
//缓存当前获取到的NS,每次获取都做强制更新
ns = list.Items.ToList();
_ns = list.Items.ToList();
return list;
}
}
}
}
8 changes: 6 additions & 2 deletions BlazorApp/Service/impl/PodService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
using System.Linq;
using System.Net.WebSockets;
using System.Threading.Tasks;
using BlazorApp.Chat;
using BlazorApp.Utils;
using Entity;
using k8s;
using k8s.Models;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;

namespace BlazorApp.Service.impl
Expand All @@ -14,18 +18,18 @@ public class PodService : IPodService
{
private readonly IBaseService _baseService;
private readonly IWatchService _watchService;
private readonly IServiceScope? _scope;
private readonly IServiceScope _scope;
private ResourceCache<V1Pod> _cache = ResourceCache<V1Pod>.Instance();

public PodService(IBaseService baseService, IServiceScopeFactory serviceScopeFactory)
{
_baseService = baseService;
_scope = serviceScopeFactory.CreateScope();
_watchService = _scope.ServiceProvider.GetService<IWatchService>();

// Console.WriteLine("PodService 初始化");
}


public bool Changed()
{
return _cache.Changed();
Expand Down
Loading

0 comments on commit 13aa5b2

Please sign in to comment.