Holy SQL.

This commit is contained in:
Master Kwoth
2016-02-07 21:51:05 +01:00
parent 1ab0b01f43
commit 4f241917b6
16 changed files with 4048 additions and 156 deletions

View File

@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using SQLite;
using NadekoBot.Classes._DataModels;
namespace NadekoBot.Classes {
class DBHandler {
private static readonly DBHandler _instance = new DBHandler();
public static DBHandler Instance => _instance;
private string _filePath { get; } = "data/nadekobot.sqlite";
static DBHandler() { }
public DBHandler() {
using (var _conn = new SQLiteConnection(_filePath)) {
_conn.CreateTable<Stats>();
_conn.CreateTable<Command>();
_conn.CreateTable<Announcement>();
_conn.CreateTable<Request>();
_conn.CreateTable<TypingArticle>();
}
}
internal void InsertData<T>(T o) where T : IDataModel {
using (var _conn = new SQLiteConnection(_filePath)) {
_conn.Insert(o, typeof(T));
}
}
internal void UpdateData<T>(T o) where T : IDataModel {
using (var _conn = new SQLiteConnection(_filePath)) {
_conn.Update(o, typeof(T));
}
}
internal List<T> GetAllRows<T>() where T : IDataModel, new() {
using (var _conn = new SQLiteConnection(_filePath)) {
return _conn.Table<T>().Where(t => true).ToList();
}
}
internal T Delete<T>(int Id) where T : IDataModel, new() {
using (var _conn = new SQLiteConnection(_filePath)) {
var found = _conn.Table<T>().Where(t => t.Id == Id).FirstOrDefault();
if (found != null)
_conn.Delete<T>(found.Id);
return found;
}
}
/// <summary>
/// Updates an existing object or creates a new one
/// </summary>
internal void Save<T>(T o) where T : IDataModel, new() {
using (var _conn = new SQLiteConnection(_filePath)) {
var found = _conn.Table<T>().Where(t => t.Id == o.Id).FirstOrDefault();
if (found == null)
_conn.Insert(o, typeof(T));
else
_conn.Update(o, typeof(T));
}
}
}
}

View File

@@ -40,6 +40,9 @@ namespace NadekoBot
Console.WriteLine("Logging enabled.");
}
public TimeSpan GetUptime() =>
DateTime.Now - Process.GetCurrentProcess().StartTime;
public string GetUptimeString() {
var time = (DateTime.Now - Process.GetCurrentProcess().StartTime);
return time.Days + " days, " + time.Hours + " hours, and " + time.Minutes + " minutes.";
@@ -72,18 +75,22 @@ namespace NadekoBot
private async Task StartCollecting() {
while (true) {
await Task.Delay(new TimeSpan(1, 0, 0));
await Task.Delay(new TimeSpan(0, 30, 0));
try {
var obj = new ParseObject("Stats");
obj["OnlineUsers"] = await Task.Run(() => NadekoBot.client.Servers.Sum(x => x.Users.Count()));
obj["RealOnlineUsers"] = await Task.Run(() => NadekoBot
.client.Servers
var onlineUsers = await Task.Run(() => NadekoBot.client.Servers.Sum(x => x.Users.Count()));
var realOnlineUsers = await Task.Run(() => NadekoBot.client.Servers
.Sum(x => x.Users.Where(u => u.Status == UserStatus.Online).Count()));
obj["ConnectedServers"] = NadekoBot.client.Servers.Count();
var connectedServers = NadekoBot.client.Servers.Count();
await obj.SaveAsync();
Classes.DBHandler.Instance.InsertData(new Classes._DataModels.Stats {
OnlineUsers = onlineUsers,
RealOnlineUsers = realOnlineUsers,
Uptime = GetUptime(),
ConnectedServers = connectedServers,
DateAdded = DateTime.Now
});
} catch (Exception) {
Console.WriteLine("Parse exception in StartCollecting");
Console.WriteLine("DB Exception in stats collecting.");
break;
}
}
@@ -93,22 +100,19 @@ namespace NadekoBot
{
try {
_commandsRan++;
var obj = new ParseObject("CommandsRan");
obj["ServerId"] = e.Server.Id;
obj["ServerName"] = e.Server.Name;
obj["ChannelId"] = e.Channel.Id;
obj["ChannelName"] = e.Channel.Name;
obj["UserId"] = e.User.Id;
obj["UserName"] = e.User.Name;
obj["CommandName"] = e.Command.Text;
obj.SaveAsync();
Classes.DBHandler.Instance.InsertData(new Classes._DataModels.Command {
ServerId = (long)e.Server.Id,
ServerName = e.Server.Name,
ChannelId = (long)e.Channel.Id,
ChannelName =e.Channel.Name,
UserId = (long)e.User.Id,
UserName = e.User.Name,
CommandName = e.Command.Text,
DateAdded = DateTime.Now
});
} catch (Exception) {
Console.WriteLine("Parse error in ran command.");
}
}
}
}
}

View File

@@ -0,0 +1,21 @@
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Classes._DataModels {
class Announcement : IDataModel {
public long ServerId { get; set; } = 0;
public bool Greet { get; set; } = false;
public bool GreetPM { get; set; } = false;
public long GreetChannelId { get; set; } = 0;
public string GreetText { get; set; } = "Welcome %user%!";
public bool Bye { get; set; } = false;
public bool ByePM { get; set; } = false;
public long ByeChannelId { get; set; } = 0;
public string ByeText { get; set; } = "%user% has left the server.";
public DateTime DateAdded { get; set; } = DateTime.Now;
}
}

View File

@@ -0,0 +1,19 @@
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Classes._DataModels {
class Command : IDataModel {
public long UserId { get; set; }
public string UserName { get; set; }
public long ServerId { get; set; }
public string ServerName { get; set; }
public long ChannelId { get; set; }
public string ChannelName { get; set; }
public string CommandName { get; set; }
public DateTime DateAdded { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Classes._DataModels {
class IDataModel {
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public IDataModel() { }
}
}

View File

@@ -0,0 +1,17 @@
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Classes._DataModels {
class Request : IDataModel {
public string UserName { get; set; }
public long UserId { get; set; }
public string ServerName { get; set; }
public long ServerId { get; set; }
public string RequestText { get; set; }
public DateTime DateAdded { get; set; }
}
}

View File

@@ -0,0 +1,16 @@
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Classes._DataModels {
class Stats : IDataModel {
public int ConnectedServers { get; set; }
public int OnlineUsers { get; set; }
public TimeSpan Uptime { get; set; }
public int RealOnlineUsers { get; set; }
public DateTime DateAdded { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Classes._DataModels {
class TypingArticle : IDataModel {
public string Text { get; set; }
public DateTime DateAdded { get; set; }
}
}