Database should be faster, disabled unit converter temporarily

This commit is contained in:
Master Kwoth 2017-06-22 22:16:58 +02:00
parent 0aa65b2953
commit bed3001ce1
4 changed files with 104 additions and 93 deletions

View File

@ -1,94 +1,94 @@
using Discord; //using Discord;
using Discord.Commands; //using Discord.Commands;
using NadekoBot.Attributes; //using NadekoBot.Attributes;
using NadekoBot.Extensions; //using NadekoBot.Extensions;
using NadekoBot.Services.Utility; //using NadekoBot.Services.Utility;
using System; //using System;
using System.Linq; //using System.Linq;
using System.Threading.Tasks; //using System.Threading.Tasks;
namespace NadekoBot.Modules.Utility //namespace NadekoBot.Modules.Utility
{ //{
public partial class Utility // public partial class Utility
{ // {
[Group] // [Group]
public class UnitConverterCommands : NadekoSubmodule // public class UnitConverterCommands : NadekoSubmodule
{ // {
private readonly ConverterService _service; // private readonly ConverterService _service;
public UnitConverterCommands(ConverterService service) // public UnitConverterCommands(ConverterService service)
{ // {
_service = service; // _service = service;
} // }
[NadekoCommand, Usage, Description, Aliases] // [NadekoCommand, Usage, Description, Aliases]
public async Task ConvertList() // public async Task ConvertList()
{ // {
var res = _service.Units.GroupBy(x => x.UnitType) // var res = _service.Units.GroupBy(x => x.UnitType)
.Aggregate(new EmbedBuilder().WithTitle(GetText("convertlist")) // .Aggregate(new EmbedBuilder().WithTitle(GetText("convertlist"))
.WithColor(NadekoBot.OkColor), // .WithColor(NadekoBot.OkColor),
(embed, g) => embed.AddField(efb => // (embed, g) => embed.AddField(efb =>
efb.WithName(g.Key.ToTitleCase()) // efb.WithName(g.Key.ToTitleCase())
.WithValue(String.Join(", ", g.Select(x => x.Triggers.FirstOrDefault()) // .WithValue(String.Join(", ", g.Select(x => x.Triggers.FirstOrDefault())
.OrderBy(x => x))))); // .OrderBy(x => x)))));
await Context.Channel.EmbedAsync(res); // await Context.Channel.EmbedAsync(res);
} // }
[NadekoCommand, Usage, Description, Aliases] // [NadekoCommand, Usage, Description, Aliases]
public async Task Convert(string origin, string target, decimal value) // public async Task Convert(string origin, string target, decimal value)
{ // {
var originUnit = _service.Units.Find(x => x.Triggers.Select(y => y.ToLowerInvariant()).Contains(origin.ToLowerInvariant())); // var originUnit = _service.Units.Find(x => x.Triggers.Select(y => y.ToLowerInvariant()).Contains(origin.ToLowerInvariant()));
var targetUnit = _service.Units.Find(x => x.Triggers.Select(y => y.ToLowerInvariant()).Contains(target.ToLowerInvariant())); // var targetUnit = _service.Units.Find(x => x.Triggers.Select(y => y.ToLowerInvariant()).Contains(target.ToLowerInvariant()));
if (originUnit == null || targetUnit == null) // if (originUnit == null || targetUnit == null)
{ // {
await ReplyErrorLocalized("convert_not_found", Format.Bold(origin), Format.Bold(target)).ConfigureAwait(false); // await ReplyErrorLocalized("convert_not_found", Format.Bold(origin), Format.Bold(target)).ConfigureAwait(false);
return; // return;
} // }
if (originUnit.UnitType != targetUnit.UnitType) // if (originUnit.UnitType != targetUnit.UnitType)
{ // {
await ReplyErrorLocalized("convert_type_error", Format.Bold(originUnit.Triggers.First()), Format.Bold(targetUnit.Triggers.First())).ConfigureAwait(false); // await ReplyErrorLocalized("convert_type_error", Format.Bold(originUnit.Triggers.First()), Format.Bold(targetUnit.Triggers.First())).ConfigureAwait(false);
return; // return;
} // }
decimal res; // decimal res;
if (originUnit.Triggers == targetUnit.Triggers) res = value; // if (originUnit.Triggers == targetUnit.Triggers) res = value;
else if (originUnit.UnitType == "temperature") // else if (originUnit.UnitType == "temperature")
{ // {
//don't really care too much about efficiency, so just convert to Kelvin, then to target // //don't really care too much about efficiency, so just convert to Kelvin, then to target
switch (originUnit.Triggers.First().ToUpperInvariant()) // switch (originUnit.Triggers.First().ToUpperInvariant())
{ // {
case "C": // case "C":
res = value + 273.15m; //celcius! // res = value + 273.15m; //celcius!
break; // break;
case "F": // case "F":
res = (value + 459.67m) * (5m / 9m); // res = (value + 459.67m) * (5m / 9m);
break; // break;
default: // default:
res = value; // res = value;
break; // break;
} // }
//from Kelvin to target // //from Kelvin to target
switch (targetUnit.Triggers.First().ToUpperInvariant()) // switch (targetUnit.Triggers.First().ToUpperInvariant())
{ // {
case "C": // case "C":
res = res - 273.15m; //celcius! // res = res - 273.15m; //celcius!
break; // break;
case "F": // case "F":
res = res * (9m / 5m) - 459.67m; // res = res * (9m / 5m) - 459.67m;
break; // break;
} // }
} // }
else // else
{ // {
if (originUnit.UnitType == "currency") // if (originUnit.UnitType == "currency")
{ // {
res = (value * targetUnit.Modifier) / originUnit.Modifier; // res = (value * targetUnit.Modifier) / originUnit.Modifier;
} // }
else // else
res = (value * originUnit.Modifier) / targetUnit.Modifier; // res = (value * originUnit.Modifier) / targetUnit.Modifier;
} // }
res = Math.Round(res, 4); // res = Math.Round(res, 4);
await Context.Channel.SendConfirmAsync(GetText("convert", value, (originUnit.Triggers.First()).SnPl(value.IsInteger() ? (int)value : 2), res, (targetUnit.Triggers.First() + "s").SnPl(res.IsInteger() ? (int)res : 2))); // await Context.Channel.SendConfirmAsync(GetText("convert", value, (originUnit.Triggers.First()).SnPl(value.IsInteger() ? (int)value : 2), res, (targetUnit.Triggers.First() + "s").SnPl(res.IsInteger() ? (int)res : 2)));
} // }
} // }
} // }
} //}

View File

@ -159,7 +159,7 @@ namespace NadekoBot
#region utility #region utility
var remindService = new RemindService(Client, BotConfig, Db, startingGuildIdList); var remindService = new RemindService(Client, BotConfig, Db, startingGuildIdList);
var repeaterService = new MessageRepeaterService(this, Client, AllGuildConfigs); var repeaterService = new MessageRepeaterService(this, Client, AllGuildConfigs);
var converterService = new ConverterService(Db); //var converterService = new ConverterService(Db);
var commandMapService = new CommandMapService(AllGuildConfigs); var commandMapService = new CommandMapService(AllGuildConfigs);
var patreonRewardsService = new PatreonRewardsService(Credentials, Db, Currency); var patreonRewardsService = new PatreonRewardsService(Credentials, Db, Currency);
var verboseErrorsService = new VerboseErrorsService(AllGuildConfigs, Db, CommandHandler, helpService); var verboseErrorsService = new VerboseErrorsService(AllGuildConfigs, Db, CommandHandler, helpService);
@ -230,7 +230,7 @@ namespace NadekoBot
.Add(commandMapService) .Add(commandMapService)
.Add(remindService) .Add(remindService)
.Add(repeaterService) .Add(repeaterService)
.Add(converterService) //.Add(converterService)
.Add(verboseErrorsService) .Add(verboseErrorsService)
.Add(patreonRewardsService) .Add(patreonRewardsService)
.Add(pruneService) .Add(pruneService)

View File

@ -36,6 +36,17 @@ namespace NadekoBot.Services
context.Database.Migrate(); context.Database.Migrate();
context.EnsureSeedData(); context.EnsureSeedData();
//set important sqlite stuffs
var conn = context.Database.GetDbConnection();
conn.Open();
context.Database.ExecuteSqlCommand("PRAGMA journal_mode=WAL");
using (var com = conn.CreateCommand())
{
com.CommandText = "PRAGMA journal_mode=WAL; PRAGMA synchronous=OFF";
com.ExecuteNonQuery();
}
return context; return context;
} }

View File

@ -89,7 +89,7 @@ namespace NadekoBot.Services.Impl
? "sqlite" ? "sqlite"
: dbSection["Type"], : dbSection["Type"],
string.IsNullOrWhiteSpace(dbSection["ConnectionString"]) string.IsNullOrWhiteSpace(dbSection["ConnectionString"])
? "Filename=./data/NadekoBot.db" ? "Filename=./data/NadekoBot.db"
: dbSection["ConnectionString"]); : dbSection["ConnectionString"]);
} }
catch (Exception ex) catch (Exception ex)