Skip to content

Commit 20e1c68

Browse files
committed
Values added
1 parent 7ff9df9 commit 20e1c68

File tree

5 files changed

+125
-4
lines changed

5 files changed

+125
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Collections.Generic;
2+
3+
namespace Monbsoft.MachineMonitor.Helpers
4+
{
5+
public static class BitHelper
6+
{
7+
private static readonly Dictionary<int, string> byteUnits = new Dictionary<int, string>
8+
{
9+
{ 0, "bits/s" },
10+
{ 1, "Kbits/s" },
11+
{ 2, "Mbits/s" },
12+
{ 3, "Gbits/s" },
13+
{ 4, "Tbits/s" }
14+
};
15+
16+
public static string DisplayByte(double bytes)
17+
{
18+
int index = 0;
19+
bool find = false;
20+
21+
double rate = bytes * 8;
22+
double next;
23+
while (!find && index < 4)
24+
{
25+
next = rate / 1000;
26+
if (next >= 1.0d)
27+
{
28+
index++;
29+
rate = next;
30+
}
31+
else
32+
{
33+
find = true;
34+
}
35+
}
36+
return $"{rate.ToString("F1")} {byteUnits[index]}";
37+
}
38+
}
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Monbsoft.MachineMonitor.Helpers
8+
{
9+
public static class OctetHelper
10+
{
11+
private static readonly Dictionary<int, string> octetUnits = new Dictionary<int, string>
12+
{
13+
{0, "Mo" },
14+
{1, "Go" },
15+
{2, "To" },
16+
{3, "Po" }
17+
};
18+
public static string DisplayMega(double mega)
19+
{
20+
bool find = false;
21+
double value = mega;
22+
int index = 0;
23+
while(!find && index <3)
24+
{
25+
double next = value / 1024;
26+
if(next >= 1.0)
27+
{
28+
value = next;
29+
}
30+
else
31+
{
32+
index++;
33+
find = true;
34+
35+
}
36+
}
37+
38+
return $"{value.ToString("F1")} {octetUnits[index]}";
39+
}
40+
}
41+
}

src/MachineMonitor/MachineMonitor.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
<SubType>Designer</SubType>
7575
</ApplicationDefinition>
7676
<Compile Include="Configuration\ConfigurationStore.cs" />
77+
<Compile Include="Helpers\BitHelper.cs" />
78+
<Compile Include="Helpers\OctetHelper.cs" />
7779
<Compile Include="Messages\UpdatedConfigurationMessage.cs" />
7880
<Compile Include="Properties\Resource.Designer.cs">
7981
<AutoGen>True</AutoGen>
@@ -145,5 +147,6 @@
145147
<LastGenOutput>Resource.Designer.cs</LastGenOutput>
146148
</EmbeddedResource>
147149
</ItemGroup>
150+
<ItemGroup />
148151
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
149152
</Project>

src/MachineMonitor/ViewModels/MainViewModel.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using GalaSoft.MvvmLight;
22
using GalaSoft.MvvmLight.Messaging;
33
using Monbsoft.MachineMonitor.Configuration;
4+
using Monbsoft.MachineMonitor.Helpers;
45
using Monbsoft.MachineMonitor.Messages;
56
using Monbsoft.MachineMonitor.Views;
67
using System;
@@ -23,6 +24,8 @@ public class MainViewModel : ViewModelBase
2324
private double _cpu;
2425
private double _disk;
2526
private PerformanceCounter _diskCounter;
27+
private string _displayMemory;
28+
private string _displayNetwork;
2629
private double _memoryTotal;
2730
private double _network;
2831
private PerformanceCounter _networkCounter;
@@ -72,6 +75,22 @@ public double Disk
7275
set { Set(ref _disk, value); }
7376
}
7477

78+
/// <summary>
79+
/// Gets or sets the usage of the memory with the specified unit.
80+
/// </summary>
81+
public string DisplayMemory
82+
{
83+
get { return _displayMemory; }
84+
set { Set(ref _displayMemory, value); }
85+
}
86+
/// <summary>
87+
/// Gets or sets the usage of the network with the specified unit.
88+
/// </summary>
89+
public string DisplayNetwork
90+
{
91+
get { return _displayNetwork; }
92+
set { Set(ref _displayNetwork, value); }
93+
}
7594
/// <summary>
7695
/// Gets or sets the percentage of the network usage.
7796
/// </summary>
@@ -109,8 +128,7 @@ public void Initialize(MainWindow view)
109128
foreach(var mobject in searcher.Get())
110129
{
111130
_memoryTotal = ToDouble(mobject, "TotalPhysicalMemory", Mega);
112-
}
113-
131+
}
114132
}
115133

116134
/// <summary>
@@ -132,6 +150,8 @@ private double CalculatePercent()
132150
double free = _memoryCounter.NextValue();
133151
double use = (_memoryTotal - free);
134152
double percent = Math.Round((use / _memoryTotal) * 100, 2);
153+
154+
DisplayMemory = OctetHelper.DisplayMega(use);
135155
return percent;
136156
}
137157
private double GetPercentageNetwork()
@@ -140,11 +160,16 @@ private double GetPercentageNetwork()
140160
{
141161
return 0d;
142162
}
143-
double value = ((double)_networkCounter.NextValue() * 8) / 1000000;
163+
double bytes = ((double)_networkCounter.NextValue());
164+
165+
double value = ((double)bytes * 8) / 1000000;
144166
if (_networkMax < value)
145167
{
146168
_networkMax = value;
147169
}
170+
171+
DisplayNetwork = BitHelper.DisplayByte(bytes);
172+
148173
return value * 100 / _networkMax;
149174
}
150175
private void HandleUpdatedConfiguration(UpdatedConfigurationMessage updatedMessage)

src/MachineMonitor/Views/MainWindow.xaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<Grid.RowDefinitions>
3737
<RowDefinition Height="Auto"/>
3838
<RowDefinition Height="Auto"/>
39+
<RowDefinition Height="Auto"/>
3940
<RowDefinition/>
4041
</Grid.RowDefinitions>
4142
<!-- cpu -->
@@ -64,6 +65,12 @@
6465
Value="{Binding Ram, Mode=OneWay}"
6566
Width="50">
6667
</controls:RadialProgressBar>
68+
<TextBlock Foreground="White"
69+
Grid.Column="1"
70+
Grid.Row="2"
71+
Margin="0,5"
72+
Text="{Binding DisplayMemory, Mode=OneWay}"
73+
TextAlignment="Center"/>
6774
<!-- Disk -->
6875
<TextBlock Grid.Column="2"
6976
Style="{StaticResource TitleStyle}"
@@ -91,6 +98,12 @@
9198
Thickness="6"
9299
Value="{Binding Network, Mode=OneWay}"
93100
Width="50">
94-
</controls:RadialProgressBar>
101+
</controls:RadialProgressBar>
102+
<TextBlock Foreground="White"
103+
Grid.Column="3"
104+
Grid.Row="2"
105+
Margin="0,5"
106+
Text="{Binding DisplayNetwork, Mode=OneWay}"
107+
TextAlignment="Center"/>
95108
</Grid>
96109
</Window>

0 commit comments

Comments
 (0)