summary refs log tree commit diff
path: root/SystemdCtl.Client/Pages/Services.razor
blob: f2e8bb9fe7c794f3490afbc821b5314d7ec96940 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
@page "/Services"
@using LibSystemdCli.Models
@using System.Text.RegularExpressions
@using SystemdCtl.Client.Abstractions
@* @attribute [StreamRendering] *@
@rendermode InteractiveWebAssembly
@inject NavigationManager NavigationManager


<PageTitle>Services</PageTitle>

<h1>Services</h1>

<span>
    <label>Type: </label>
    <InputSelect @bind-Value="TypeFilter">
        <option value="">All</option>
        @foreach (var i in UnitTypes) {
            <option value="@i">@Capitalize(i)</option>
        }
    </InputSelect>
</span>
<span>
    <InputCheckbox @bind-Value="@ShowSystem"></InputCheckbox>
    <label>Show system services</label>
</span>

@if (filteredItems is not { Count: > 0 }) {
    <p>
        <em>Loading...</em>
    </p>
}
else {
    <table class="table">
        <thead>
            <tr>
                <th>Service</th>
                <th>Description</th>
                <th>Status</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var unit in filteredItems) {
                <tr>
                    <td>@unit.Unit</td>
                    <td>@unit.Description</td>
                    <td>@unit.Active</td>
                    <td><LinkButton href="@($"/Service/{unit.Unit}/Manage")">Manage</LinkButton></td>
                </tr>
                @foreach (var frag in unit.FragmentPaths) {
                    <tr>
                        <td/>
                        <td>@frag</td>
                        <td/>
                    </tr>
                }
            }
        </tbody>
    </table>
}

@code {

    private static Regex[] AlwaysHidden = new Regex[] {
        //services
        new Regex(@"^systemd-fsck@.*\.service$"),
        new Regex(@"^modprobe@.*\.service$"),
        new Regex(@"^xen.*\.service$"),
        new Regex(@"^virt.*\.service$"),
        new Regex(@"^libvirt.*\.service$"),
        new Regex(@"^systemd-.*\.service$"),
        //sockets
        new Regex(@"^virt.*\.socket$"),
        new Regex(@"^systemd.*\.socket$"),
        new Regex(@"^libvirt.*\.socket$"),
        //device
        new(@"^dev-disk-by.*\.device$"),
        new(@"^sys-device.*\.device$"),
        new(@".*-by\\x2d.*\.device$"),
        //mount
        new(@"^run-credentials.*\.mount"),
        //target
        new(@"^blockdev@dev-disk-by.*\.target$")
    };

    private static bool IsClient => !Environment.CommandLine.Contains("/");
    private List<SystemdUnitListItem>? items = new();
    private List<SystemdUnitListItem>? filteredItems = new();
    private List<string> UnitTypes = new();
    private string _typeFilter = "";
    private bool _showSystem;

    public string TypeFilter {
        get => _typeFilter;
        set {
            _typeFilter = value;
            FilterItems();
        }
    }

    public bool ShowSystem {
        get => _showSystem;
        set {
            _showSystem = value;
            FilterItems();
        }
    }

    protected override async Task OnInitializedAsync() {
        // await Task.Delay(500);

        // items = await SystemdExecutor.GetUnits();
        Console.WriteLine("OnInitializedAsync");
        await ReloadItems();
    }

    private async Task ReloadItems() {
        if (!IsClient) return;

        items.Clear();
        var Http = new StreamingHttpClient() { BaseAddress = new Uri(NavigationManager.BaseUri) };
        var _items = Http.GetAsyncEnumerableFromJsonAsync<SystemdUnitListItem>("/api/listUnits");
        await foreach (var item in _items) {
            items.Add(item);
            if (items.Count % 10 == 0)
                await FilterItems();
        }

        await FilterItems();
    }

    private async Task FilterItems() {
        var filter = items.Where(x => true);//!AlwaysHidden.Any(y => y.IsMatch(x.Unit)));
        if (!_showSystem)
            filter = filter.Where(x => !x.IsSystem);

        UnitTypes = filter.Select(x => x.UnitType).Distinct().ToList();
        filter = filter.Where(x => x.Unit.EndsWith(TypeFilter)).ToList();

        filteredItems = filter.ToList();
        StateHasChanged();
    }

    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..]
        };
    }

}