add auto module fetch on startup and finished module creation form for real now

This commit is contained in:
Rene Schwarz
2021-08-09 15:08:26 +02:00
parent f1f717a37f
commit cdb86331e5
30 changed files with 269 additions and 124 deletions

Binary file not shown.

View File

@@ -1,44 +0,0 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace Server_Dashboard {
/// <summary>
/// Server information class, this will probably scale pretty big later on
/// This will hold all the information the socket will gather
/// </summary>
internal class ServerInformation {
//The ServerName
public string ServerName { get; set; }
//The unix or windows username
public string OSUserName { get; set; }
//Cpu Temp in C
public string CpuTemp { get; set; }
//Gpu Temp in C
public string GpuTemp { get; set; }
//Server uptime
public string Uptime { get; set; }
//When the server was first deployed
public string DeployDate { get; set; }
//Public IPv4 Adress
public string PublicIpAdress { get; set; }
//Private IP adress from the servers network
public string PrivateIpAdress { get; set; }
//GPU usage in %
public string GpuUsage { get; set; }
//CPU usage in %
public string CpuUsage { get; set; }
}
}

View File

@@ -5,6 +5,7 @@ using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace Server_Dashboard {
@@ -65,12 +66,77 @@ namespace Server_Dashboard {
//Open the connection
con.Open();
//SQL Query
string query = "SELECT UserID, Username, Email, CreationTime, ModuleName, MI.Image FROM UserData LEFT JOIN ModuleData MD on UserData.ID = MD.UserID LEFT JOIN ModuleIcon MI on MD.ID = MI.Module WHERE Username = @username";
string query = "SELECT ID, Username, Email, RegistrationDate FROM UserData WHERE Username = @username";
//Creates a new command
using SqlCommand com = new SqlCommand(query, con);//For security reasons the values are added with this function
//this will avoid SQL Injections
com.Parameters.AddWithValue("@username", username);
//Execute query and return number of rows affected
DataTable resultTable = new DataTable() { TableName = "Userdata" };
using SqlDataAdapter sda = new SqlDataAdapter(com);
sda.Fill(resultTable);
return resultTable;
//Checks if there are any rows successful
//If the query returns 0 the query wasn't successful
//if its any number above 0 it was successfull
//Catch any error
} catch (SqlException ex) {
return null;
} finally {
//Always close the connection
con.Close();
}
}
public static DataTable GetUserModuleData(int uid) {
//Creates the database connection
using SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerDashboardDB"].ConnectionString);
try {
//Open the connection
con.Open();
//SQL Query
string query = "SELECT Creator, CreationTime, ModuleName, MI.Image FROM ModuleData LEFT JOIN ModuleIcon MI on ModuleData.ID = MI.Module WHERE UserID = @userID";
//Creates a new command
using SqlCommand com = new SqlCommand(query, con);//For security reasons the values are added with this function
//this will avoid SQL Injections
com.Parameters.AddWithValue("@userID", uid);
//Execute query and return number of rows affected
DataTable resultTable = new DataTable();
using SqlDataAdapter sda = new SqlDataAdapter(com);
sda.Fill(resultTable);
return resultTable;
//Checks if there are any rows successful
//If the query returns 0 the query wasn't successful
//if its any number above 0 it was successfull
//Catch any error
} catch (SqlException ex) {
return null;
} finally {
//Always close the connection
con.Close();
}
}
/// <summary>
/// This function will fetch every Serverdata for each module
/// This will need some optimization, for now we just asynchronously
/// fetch the serverdata for each module
/// </summary>
/// <param name="mid">ModuleID to fetch the data from</param>
/// <returns></returns>
public static DataTable GetServerData(string mid) {
//Creates the database connection
using SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerDashboardDB"].ConnectionString);
try {
//Open the connection
con.Open();
//SQL Query
string query = "SELECT * FROM ServerData WHERE ModuleID = @mid";
//Creates a new command
using SqlCommand com = new SqlCommand(query, con);//For security reasons the values are added with this function
//this will avoid SQL Injections
com.Parameters.AddWithValue("@mid", mid);
//Execute query and return number of rows affected
DataTable resultTable = new DataTable();
using SqlDataAdapter sda = new SqlDataAdapter(com);
sda.Fill(resultTable);
@@ -113,7 +179,10 @@ namespace Server_Dashboard {
com.Parameters.AddWithValue("@time", DateTime.Now);
com.Parameters.AddWithValue("@moduleName", moduleName);
com.Parameters.AddWithValue("@serverName", serverName);
com.Parameters.Add("@moduleIcon", SqlDbType.VarBinary, int.MaxValue).Value = moduleIcon;
com.Parameters.Add("@moduleIcon", SqlDbType.VarBinary, -1).Value = moduleIcon;
if (moduleIcon == null)
com.Parameters["@moduleIcon"].Value = DBNull.Value;
//com.Parameters.AddWithValue("@moduleIcon", moduleIcon);
com.Parameters.AddWithValue("@ipAdress", ipAdress);
com.Parameters.AddWithValue("@port", port);
//Execute query and return number of rows affected
@@ -150,21 +219,19 @@ namespace Server_Dashboard {
//Open the connection
con.Open();
//SQL Query
string query = "EXEC CheckUserCookie @Cookie = @cookie, @UserName = @username, @Valid = @valid OUTPUT";
string query = "((SELECT Cookie FROM UserData WHERE Username = @username) = @cookie)";
//Creates a new command
using SqlCommand com = new SqlCommand(query, con);
//For security reasons the values are added with this function
//this will avoid SQL Injections
com.Parameters.AddWithValue("@cookie", cookie);
com.Parameters.AddWithValue("@username", username);
com.Parameters.Add("@valid", SqlDbType.Bit);
com.Parameters["@valid"].Direction = ParameterDirection.Output;
//Execute query and return number of rows affected
int sqlResponse = com.ExecuteNonQuery();
//Checks if there are any rows successful
//If the query returns 0 the query wasn't successful
//if its any number above 0 it was successfull
if ((int)com.Parameters["@Valid"].Value == 0) {
if (sqlResponse == 0) {
//Error, not successful
return 1;
} else {
@@ -191,7 +258,7 @@ namespace Server_Dashboard {
//Open the connection
con.Open();
//SQL Query
string query = "EXEC DeleteUserCookie @Username = @username";
string query = "UPDATE UserData SET Cookie = null WHERE Username = @username";
//Creates a new command
using SqlCommand com = new SqlCommand(query, con);
//For security reasons the values are added with this function
@@ -224,19 +291,19 @@ namespace Server_Dashboard {
/// <param name="cookie">The delicious locally stored cookie</param>
/// <param name="username">The User who deserves a cookie :3</param>
/// <returns>[0] is false, [1] is true, [2] connection error</returns>
public static int AddCookie(string cookie, string username) {
public static int AddCookie(string username, string cookie) {
//Creates the database connection
using SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ServerDashboardDB"].ConnectionString);
try {
//Open the connection
con.Open();
//SQL Query
string query = "EXEC AddCookieToUser @Cookie = @cookie, @UserName = @username";
string query = "UPDATE UserData SET Cookie = @cookie WHERE Username = @username";
//Creates a new command
using SqlCommand com = new SqlCommand(query, con);
//For security reasons the values are added with this function
//this will avoid SQL Injections
com.Parameters.AddWithValue("@cookie", cookie);
com.Parameters.Add("@cookie", SqlDbType.NVarChar, -1).Value = cookie;
com.Parameters.AddWithValue("@username", username);
//Execute query and return number of rows affected
int sqlResponse = com.ExecuteNonQuery();

View File

@@ -5,10 +5,7 @@ using System.Windows.Media.Imaging;
namespace Server_Dashboard {
/// <summary>
/// Dashboard Module class that holds all the information that gets displayed
/// </summary>
internal class DashboardModule {
internal class ModuleData {
//The name the user gives the module
public string ModuleName { get; set; }
@@ -16,9 +13,6 @@ namespace Server_Dashboard {
//The user who created it
public string Creator { get; set; }
//All the information that the server had
public ServerInformation ServerInfo { get; set; }
//The status indicator
public string StatusIndicator { get; set; }
@@ -32,13 +26,16 @@ namespace Server_Dashboard {
public BitmapImage ModuleIcon { get; set; }
//Creation date with System.DateTime.Now
public string CreationDate { get; set; }
public DateTime CreationDate { get; set; }
//List that contains all the serverinformation over a period of time(lifespan of the module)
public List<ServerInformation> ServerInformation { get; set; }
/// <summary>
/// This will set the Module status indicator red or green if the server is available or not
/// </summary>
/// <param name="serverAvailable"></param>
public DashboardModule(bool serverAvailable) {
public ModuleData(bool serverAvailable) {
ServerAvailable = serverAvailable;
StatusIndicator = ServerAvailable ? "#20c657" : "#e53935";
StatusIndicatorBG = ServerAvailable ? "#94eeb0" : "#ef9a9a";

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace Server_Dashboard {
/// <summary>
/// Server information class, this will probably scale pretty big later on
/// This will hold all the information the socket will gather
/// </summary>
internal class ServerInformation {
public string ServerName { get; set; }
public string OSUserName { get; set; }
public double CpuTemp { get; set; }
public double GpuTemp { get; set; }
public DateTime Uptime { get; set; }
public DateTime DeployDate { get; set; }
public string PublicIpAdress { get; set; }
public string PrivateIpAdress { get; set; }
public int GpuUsage { get; set; }
public int CpuUsage { get; set; }
public double NetworkUP { get; set; }
public double NetworkDown { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Server_Dashboard {
/// <summary>
/// This class represents a single CPU and will be added
/// to a list to account for a multi CPU system
/// </summary>
internal class CPU {
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Server_Dashboard {
/// <summary>
/// This class represents a single drive on a server, this will be added
/// to a list to account for multiple drives
/// </summary>
internal class Drives {
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Server_Dashboard {
/// <summary>
/// This class represents a single GPU and will be added to a list
/// to account for multiple GPU's
/// </summary>
internal class GPU {
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Server_Dashboard {
/// <summary>
/// This class represents the mainboard
/// It will hold information about the chipset, number of hardware etc...
/// </summary>
internal class Motherboard {
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Server_Dashboard {
/// <summary>
/// This class represents a single network interface and will be added
/// to a list to account for multiple networking interfaces
/// </summary>
internal class NetworkInterface {
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Server_Dashboard {
/// <summary>
/// This class represents RAM
/// TODO: figure out if it makes sense to create a class for the entire system memory
/// or for each and every stick
/// </summary>
internal class RAM {
}
}

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace Server_Dashboard {
/// <summary>
/// User class to store user informations
/// </summary>
internal class User {
public int UID { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public DateTime RegistrationDate { get; set; }
public User(DataTable userData) {
foreach (DataRow row in userData.Rows) {
UID = (int)row[0];
UserName = (string)row[1];
Email = (string)row[2];
RegistrationDate = (DateTime)row[3];
}
}
}
}

View File

@@ -14,46 +14,40 @@ namespace Server_Dashboard {
internal class DashboardModuleViewModel : BaseViewModel {
//List with all Modules inside
public ObservableCollection<DashboardModule> Modules { get; set; }
public ObservableCollection<ModuleData> Modules { get; set; }
//Creates Default Modules, remove before release and when implementing the actual data comming from the socket
public DashboardModuleViewModel(DataTable userdata) {
GetModules(userdata);
public DashboardModuleViewModel(DataTable moduleData) {
Modules = new ObservableCollection<ModuleData>();
foreach (DataRow row in moduleData.Rows) {
if (row[0] != null) {
byte[] iconBytes = row[3] == DBNull.Value ? null : (byte[])row[3];
Modules.Add(new ModuleData(true) {
ModuleName = (string)row?[2],
Creator = (string)row?[0],
ModuleIcon = ConvertByteToBitmapImage(iconBytes),
CreationDate = (DateTime)row?[1],
ServerInformation = null
});
}
}
}
public void GetModules(DataTable userdata) {
Modules = new ObservableCollection<DashboardModule>();
foreach (DataRow row in userdata.Rows) {
BitmapImage moduleIcon = null;
if (row[5] != DBNull.Value) {
using MemoryStream ms = new MemoryStream((byte[])row[5]);
moduleIcon = new BitmapImage();
private BitmapImage ConvertByteToBitmapImage(byte[] icon) {
if (icon != null) {
try {
using MemoryStream ms = new MemoryStream(icon);
BitmapImage moduleIcon = new BitmapImage();
moduleIcon.BeginInit();
moduleIcon.StreamSource = ms;
moduleIcon.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
moduleIcon.CacheOption = BitmapCacheOption.OnLoad;
moduleIcon.EndInit();
moduleIcon.Freeze();
return moduleIcon;
} catch { }
}
Modules.Add(new DashboardModule(true) {
ModuleName = (string)row[4],
Creator = (string)row[1],
ModuleIcon = moduleIcon,
CreationDate = DateTime.Now.ToString(),
ServerInfo = new ServerInformation() {
GpuUsage = "20",
CpuUsage = "20",
CpuTemp = "88.88",
DeployDate = DateTime.Now.ToString(),
GpuTemp = "69.69",
ServerName = "Ubuntu",
OSUserName = "crylia ",
PrivateIpAdress = "192.168.1.1",
PublicIpAdress = "85.69.102.58",
Uptime = DateTime.Now.ToString()
}
});
}
return null;
}
}
}

View File

@@ -6,6 +6,8 @@ using System.Windows.Input;
using Server_Dashboard_Socket;
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
namespace Server_Dashboard {
@@ -16,28 +18,28 @@ namespace Server_Dashboard {
#region Private Values
private readonly DashboardModuleViewModel dmvm;
private DashboardModuleViewModel dmvm;
#endregion Private Values
#region Properties
//The Username displayed defaults to Username
private string userName;
private User user;
public string UserName {
get { return userName; }
public User User {
get { return user; }
set {
if (userName != value)
userName = value;
OnPropertyChanged(nameof(userName));
if (user != value)
user = value;
OnPropertyChanged(nameof(user));
}
}
//List that contains every Module
private ObservableCollection<DashboardModule> modules;
private ObservableCollection<ModuleData> modules;
public ObservableCollection<DashboardModule> Modules {
public ObservableCollection<ModuleData> Modules {
get { return modules; }
set {
if (value != modules)
@@ -51,15 +53,13 @@ namespace Server_Dashboard {
#region Constructor
public DashboardViewModel(string username) {
UserName = username;
//Command inits
OpenLinkCommand = new RelayCommand(OpenLink);
OpenNewModuleWindowCommand = new RelayCommand(OpenNewModuleWindow);
DataTable Userdata = DatabaseHandler.GetUserData(username);
dmvm = new DashboardModuleViewModel(Userdata);
//Sets the local module to the dashboardviewmodule modules
Modules = dmvm.Modules;
DataTable userData = DatabaseHandler.GetUserData(username);
User = new User(userData);
GetModules();
}
#endregion Constructor
@@ -88,12 +88,20 @@ namespace Server_Dashboard {
private void OpenNewModuleWindow(object param) {
//Creates a new CreateModulePopup and sets this view model as datacontext
CreateModulePopup cmp = new CreateModulePopup {
DataContext = new CreateModuleViewModel(UserName)
DataContext = new CreateModuleViewModel(User.UserName)
};
//Opens it in the middle of the screen, setting the parent window as owner causes the
//application to crash when NOT in debug mode(???)
cmp.WindowStartupLocation = WindowStartupLocation.CenterScreen;
cmp.ShowDialog();
GetModules();
}
private void GetModules() {
DataTable moduleData = DatabaseHandler.GetUserModuleData(User.UID);
dmvm = new DashboardModuleViewModel(moduleData);
//Sets the local module to the dashboardviewmodule modules
Modules = dmvm.Modules;
}
#endregion Commands

View File

@@ -140,9 +140,9 @@ namespace Server_Dashboard {
DatabaseHandler.DeleteCookie(Username);
}
//If the remember user option is checked and the cookie is not set save everything locally
if (RememberUser && String.IsNullOrEmpty(Settings.Default.Cookies)) {
if (RememberUser && Settings.Default.Username != Username) {
//Creates a new GUID with the username at the end, this is the cookie
var cookie = new Guid().ToString() + Username;
var cookie = $"{Guid.NewGuid().ToString()}+user:{Username}";
//Saves cookie, Username Remember me option to the local storage (Settings.settings)
Settings.Default.Cookies = cookie;
Settings.Default.Username = Username;

View File

@@ -67,7 +67,7 @@
</ControlTemplate>
</Button.Template>
</Button>
<Button Grid.Column="4" Command="{Binding OpenLinkCommand}" Content="{Binding UserName}" Margin="10 0 10 0" Height="40" Cursor="Hand">
<Button Grid.Column="4" Command="{Binding OpenLinkCommand}" Content="{Binding User.UserName}" Margin="10 0 10 0" Height="40" Cursor="Hand">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border" CornerRadius="5" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0">

View File

@@ -1 +1 @@
8e07e5d31c9ebebb14b8f8bc5efe2ebfac9affc7
2b8960a5d1dd9bf90d7fb0287515a1b0a7fb5d35

View File

@@ -12,7 +12,7 @@ TRACE;DEBUG;NETCOREAPP;NETCOREAPP3_1;
C:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\App.xaml
8-2121814096
28614409893
35188129793
207-679868162
Controls\DoubleRoundProgressBar\DoubleRoundProgressBar.xaml;Controls\HalfRoundProgressBar\HalfRoundProgressBar.xaml;Controls\LoadingIndicator\LoadingIndicator.xaml;Controls\ServerModules\ServerModule.xaml;LoginWindow.xaml;Views\DashboardWindow.xaml;Views\Dashboard\CRUD Popup\CreateModulePopup.xaml;Views\Pages\MainDashboardPage.xaml;

View File

@@ -12,9 +12,9 @@ TRACE;DEBUG;NETCOREAPP;NETCOREAPP3_1;
C:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\App.xaml
8-2121814096
30-984917377
37-1411197477
207-679868162
Controls\DoubleRoundProgressBar\DoubleRoundProgressBar.xaml;Controls\HalfRoundProgressBar\HalfRoundProgressBar.xaml;Controls\LoadingIndicator\LoadingIndicator.xaml;Controls\ServerModules\ServerModule.xaml;LoginWindow.xaml;Views\DashboardWindow.xaml;Views\Dashboard\CRUD Popup\CreateModulePopup.xaml;Views\Pages\MainDashboardPage.xaml;
True
False

View File

@@ -1,8 +0,0 @@
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\Controls\DoubleRoundProgressBar\DoubleRoundProgressBar.xaml;;
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\Controls\HalfRoundProgressBar\HalfRoundProgressBar.xaml;;
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\Controls\LoadingIndicator\LoadingIndicator.xaml;;
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\Controls\ServerModules\ServerModule.xaml;;
FC:\Users\Crylia\Documents\Git\Server Dashboard\Server Dashboard\Views\DashboardWindow.xaml;;

View File

@@ -1,4 +1,4 @@
#pragma checksum "..\..\..\..\Views\DashboardWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "A314D0E6AD6B6AD4D75AB6130829A86ACEBFF007"
#pragma checksum "..\..\..\..\Views\DashboardWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "3593041613773C511381544197463CBAC4FA5B88"
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.

View File

@@ -1,4 +1,4 @@
#pragma checksum "..\..\..\..\Views\DashboardWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "A314D0E6AD6B6AD4D75AB6130829A86ACEBFF007"
#pragma checksum "..\..\..\..\Views\DashboardWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "3593041613773C511381544197463CBAC4FA5B88"
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.