summary refs log tree commit diff
path: root/SystemdCtl.Client/Pages/ServiceManage.razor
blob: 851f1b60d2b0932f86bef903f341ead86add1619 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
@page "/Service/{ServiceName}/Manage"
@using LibSystemdCli.Models
@using LibSystemdCli
@using System.Text.RegularExpressions
@using ArcaneLibs.Extensions
@using SystemdCtl.Client.Abstractions
@using System.Diagnostics.Metrics
@using System.Text.Json
@* @attribute [StreamRendering] *@
@rendermode InteractiveWebAssembly
@inject NavigationManager NavigationManager


<PageTitle>Manage @ServiceName</PageTitle>

<h1>Managing @ServiceName (@ServiceInfo?.Status)</h1>

<div class="row">
    <div class="col-12">
        <h3>Actions</h3>
        <div class="card">
            <div class="card-body">
                @if (ServiceInfo.IsRunning) {
                    <LinkButton OnClick="@Stop">Stop</LinkButton>
                    <LinkButton OnClick="@Restart">Restart</LinkButton>
                    <LinkButton OnClick="@Kill">Kill</LinkButton>
                }
                else {
                    <LinkButton OnClick="@Start">Start</LinkButton>
                }
            </div>
        </div>
    </div>
</div>


@* //simple log view *@
<div class="row">
    <div class="col-12">
        <h3>Logs</h3>
        <div class="card">
            <div class="card-body">
                <pre>
                    <table class="table table-sm table-striped">
                        @foreach (var line in LogLines) {
                            <tr>
                                <td>@line.Exe.Split('/').Last()</td>
                                <td>@line.Message</td>  
                            </tr>
                        }
                    </table>
                </pre>
            </div>
        </div>
    </div>
</div>

@code {

    [Parameter]
    public string ServiceName { get; set; } = "";

    private static bool IsClient => !Environment.CommandLine.Contains("/");

    private List<SystemdJournalLogItem> LogLines { get; set; } = new();
    private SystemdServiceData? ServiceInfo { get; set; } = new();

    protected override async Task OnInitializedAsync() {
        Console.WriteLine("OnInitializedAsync");
        await Run();
    }

    private async Task Run() {
        if (!IsClient) return;
        int history = 100;
        LogLines.Clear();
        GetServiceDataTask();
        var Http = new StreamingHttpClient() { BaseAddress = new Uri(NavigationManager.BaseUri) };
        var jso = new JsonSerializerOptions() {
            DefaultBufferSize = 1,
            AllowTrailingCommas = true
        };
        var _items = Http.GetAsyncEnumerableFromJsonAsync<SystemdJournalLogItem>($"/api/unit/{ServiceName}/logs?contextLines={history}", jso);
        await foreach (var item in _items) {
            // LogLines.RemoveAll(x=>x.SystemdInvocationId != item.SystemdInvocationId);
            LogLines.Add(item);
            if (LogLines.Count > history) {
                LogLines.RemoveAt(0);
            }

            StateHasChanged();
        }
    }

    private async Task GetServiceDataTask() {
        if (!IsClient) return;
        var Http = new StreamingHttpClient() { BaseAddress = new Uri(NavigationManager.BaseUri) };
        while (true) {
            ServiceInfo = await Http.GetFromJsonAsync<SystemdServiceData>($"/api/unit/{ServiceName}/data");
            StateHasChanged();
            await Task.Delay(TimeSpan.FromSeconds(5));
        }
    }

    private string Capitalize(string input) {
        return input switch {
            null => throw new ArgumentNullException(nameof(input)),
            "" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
            _ => input.First().ToString().ToUpper() + input[1..]
        };
    }

    private async Task Start() {
        var Http = new StreamingHttpClient() { BaseAddress = new Uri(NavigationManager.BaseUri) };
        await Http.GetAsync($"/api/unit/{ServiceName}/start", null);
    }

    private async Task Stop() {
        var Http = new StreamingHttpClient() { BaseAddress = new Uri(NavigationManager.BaseUri) };
        await Http.GetAsync($"/api/unit/{ServiceName}/stop", null);
    }

    private async Task Restart() {
        var Http = new StreamingHttpClient() { BaseAddress = new Uri(NavigationManager.BaseUri) };
        await Http.GetAsync($"/api/unit/{ServiceName}/restart", null);
    }

    private async Task Kill() {
        var Http = new StreamingHttpClient() { BaseAddress = new Uri(NavigationManager.BaseUri) };
        await Http.GetAsync($"/api/unit/{ServiceName}/kill", null);
    }

}