mirror of
https://bitbucket.org/anguist/ntpa
synced 2025-12-08 14:58:42 +00:00
SQL optimization and related stuff
This commit is contained in:
@@ -29,7 +29,10 @@ using Ntp.Data.Provider;
|
||||
|
||||
namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
public class AssociationEntryMapper : FilteredSqlDatabaseMapper<AssociationEntry>
|
||||
/// <summary>
|
||||
/// OR/M mapper for table associationEntry.
|
||||
/// </summary>
|
||||
public sealed class AssociationEntryMapper : SqlDatabaseMapper<AssociationEntry>
|
||||
{
|
||||
internal AssociationEntryMapper(LogBase log)
|
||||
: base(log)
|
||||
|
||||
@@ -29,7 +29,7 @@ using Ntp.Common.Log;
|
||||
namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
/// <summary>
|
||||
/// ORM mapper for table driftReading.
|
||||
/// OR/M mapper for table driftReading.
|
||||
/// </summary>
|
||||
public sealed class DriftReadingDatabaseMapper : FilteredSqlDatabaseMapper<DriftReading>
|
||||
{
|
||||
|
||||
@@ -22,9 +22,13 @@
|
||||
using System;
|
||||
using Ntp.Analyzer.Objects;
|
||||
using Ntp.Common.Log;
|
||||
using Ntp.Data.Provider;
|
||||
|
||||
namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
/// <summary>
|
||||
/// OR/M mapper for tables with a large amount of rows.
|
||||
/// </summary>
|
||||
public abstract class FilteredSqlDatabaseMapper<T> : SqlDatabaseMapper<T>
|
||||
where T : PersistentObject
|
||||
{
|
||||
@@ -33,12 +37,6 @@ namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the primary key of the entity to extract.
|
||||
/// </summary>
|
||||
/// <value>The primary key.</value>
|
||||
public int? FilterId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the time to use when extracting data. Only readings with a timestamp
|
||||
/// later than FilterTime gets extracted.
|
||||
@@ -49,7 +47,6 @@ namespace Ntp.Analyzer.Data.Sql
|
||||
/// <summary>
|
||||
/// Gets or sets the host
|
||||
/// </summary>
|
||||
/// <remarks>Always set this property while using other filter properties</remarks>
|
||||
/// <value>The host.</value>
|
||||
public Host FilterHost { get; set; }
|
||||
|
||||
@@ -63,17 +60,36 @@ namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
string preparedSql = base.PrepareSql(sql);
|
||||
|
||||
if (FilterId != null)
|
||||
{
|
||||
preparedSql += " WHERE Id = " + FilterId;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (FilterTime != null)
|
||||
preparedSql += " WHERE time > '" + FilterTime.Value.ToString("yyyy-MM-dd HH:mm") + "'";
|
||||
if (FilterTime == null && FilterHost == null && FilterPeer == null)
|
||||
return preparedSql;
|
||||
|
||||
if (FilterHost != null)
|
||||
preparedSql += " AND hostId = " + FilterHost.Id;
|
||||
bool filterAdded = false;
|
||||
|
||||
preparedSql += " WHERE ";
|
||||
|
||||
if (FilterTime != null)
|
||||
{
|
||||
var time = FilterTime.Value.ToUniversalTime();
|
||||
var datePart = SqlDatabaseFactory.Instance.DateAddMinutes("time", "zone");
|
||||
preparedSql += $"{datePart} > \'{time.ToString("yyyy-MM-dd HH:mm:ss")}\'";
|
||||
filterAdded = true;
|
||||
}
|
||||
|
||||
if (filterAdded)
|
||||
preparedSql += " AND ";
|
||||
|
||||
if (FilterHost != null)
|
||||
{
|
||||
preparedSql += $"hostId = {FilterHost.Id}";
|
||||
filterAdded = true;
|
||||
}
|
||||
|
||||
if (filterAdded)
|
||||
preparedSql += " AND ";
|
||||
|
||||
if (FilterPeer != null)
|
||||
{
|
||||
preparedSql += $"peerId = {FilterPeer.Id}";
|
||||
}
|
||||
|
||||
return preparedSql;
|
||||
|
||||
@@ -28,7 +28,7 @@ using Ntp.Common.Log;
|
||||
namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
/// <summary>
|
||||
/// ORM mapper for table host.
|
||||
/// OR/M mapper for table host.
|
||||
/// </summary>
|
||||
public sealed class HostDatabaseMapper : SqlDatabaseMapper<Host>
|
||||
{
|
||||
|
||||
@@ -29,6 +29,9 @@ using Ntp.Data.Provider;
|
||||
|
||||
namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
/// <summary>
|
||||
/// OR/M mapper for table hostIoReading.
|
||||
/// </summary>
|
||||
public sealed class HostIoReadingDatabaseMapper : FilteredSqlDatabaseMapper<HostIoReading>
|
||||
{
|
||||
internal HostIoReadingDatabaseMapper(HostDatabaseMapper hostMapper, LogBase log)
|
||||
|
||||
@@ -29,7 +29,7 @@ using Ntp.Common.Log;
|
||||
namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
/// <summary>
|
||||
/// ORM mapper for table hostReading.
|
||||
/// OR/M mapper for table hostReading.
|
||||
/// </summary>
|
||||
public sealed class HostReadingDatabaseMapper : FilteredSqlDatabaseMapper<HostReading>
|
||||
{
|
||||
|
||||
@@ -27,7 +27,10 @@ using Ntp.Common.Log;
|
||||
|
||||
namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
public class PeerActivityDatabaseMapper : SqlDatabaseMapper<PeerActivity>
|
||||
/// <summary>
|
||||
/// OR/M mapper for table peerActivity.
|
||||
/// </summary>
|
||||
public sealed class PeerActivityDatabaseMapper : SqlDatabaseMapper<PeerActivity>
|
||||
{
|
||||
internal PeerActivityDatabaseMapper(
|
||||
HostDatabaseMapper hostMapper,
|
||||
|
||||
@@ -28,7 +28,7 @@ using Ntp.Common.Log;
|
||||
namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
/// <summary>
|
||||
/// ORM mapper for table peer.
|
||||
/// OR/M mapper for table peer.
|
||||
/// </summary>
|
||||
public sealed class PeerDatabaseMapper : SqlDatabaseMapper<Peer>
|
||||
{
|
||||
|
||||
@@ -29,7 +29,7 @@ using Ntp.Common.Log;
|
||||
namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
/// <summary>
|
||||
/// ORM mapper for table peerReading.
|
||||
/// OR/M mapper for table peerReading.
|
||||
/// </summary>
|
||||
public sealed class PeerReadingDatabaseMapper : FilteredSqlDatabaseMapper<PeerReading>
|
||||
{
|
||||
|
||||
@@ -26,7 +26,10 @@ using Ntp.Common.Log;
|
||||
|
||||
namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
public class ReadingBulkMapper : SqlDatabaseMapper<ReadingBulk>
|
||||
/// <summary>
|
||||
/// OR/M mapper for ReadingBulk objects. Reading bulks are not persisted.
|
||||
/// </summary>
|
||||
public sealed class ReadingBulkMapper : SqlDatabaseMapper<ReadingBulk>
|
||||
{
|
||||
public ReadingBulkMapper(LogBase log)
|
||||
: base(log)
|
||||
|
||||
@@ -34,7 +34,7 @@ using Ntp.Data.Provider;
|
||||
namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for ORM mappers. Can be used for mapping object stored in sql databases.
|
||||
/// Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases.
|
||||
/// </summary>
|
||||
public abstract class SqlDatabaseMapper<T> : DataMapper<T>, ITableInitializer
|
||||
where T : PersistentObject
|
||||
|
||||
@@ -30,7 +30,7 @@ using Ntp.Common.Log;
|
||||
namespace Ntp.Analyzer.Data.Sql
|
||||
{
|
||||
/// <summary>
|
||||
/// ORM mapper for table timeServer.
|
||||
/// OR/M mapper for table timeServer.
|
||||
/// </summary>
|
||||
public sealed class TimeServerDatabaseMapper : SqlDatabaseMapper<TimeServer>
|
||||
{
|
||||
|
||||
@@ -73,6 +73,11 @@ namespace Ntp.Data.Provider
|
||||
return new MySqlParameter(name, value);
|
||||
}
|
||||
|
||||
public override string DateAddMinutes(string dateColumn, string minuteColumn)
|
||||
{
|
||||
return $"{dateColumn} + interval {minuteColumn} minute";
|
||||
}
|
||||
|
||||
public override string PrepareCheckTableSql(string table)
|
||||
{
|
||||
return PrepareSql(string.Format(CheckTableSql, Config.Name, table));
|
||||
|
||||
@@ -107,6 +107,11 @@ namespace Ntp.Data.Provider
|
||||
return new NpgsqlParameter(name, value);
|
||||
}
|
||||
|
||||
public override string DateAddMinutes(string dateColumn, string minuteColumn)
|
||||
{
|
||||
return $"{dateColumn} + {minuteColumn} * INTERVAL '1 minute'";
|
||||
}
|
||||
|
||||
public override string PrepareCheckTableSql(string table)
|
||||
{
|
||||
return PrepareSql(string.Format(CheckTableSql, Config.Name, table));
|
||||
@@ -150,14 +155,7 @@ namespace Ntp.Data.Provider
|
||||
if (Config.ConnectionTimeout.HasValue)
|
||||
b.Append($"Timeout={Config.ConnectionTimeout.Value};");
|
||||
|
||||
if (Config.EnableSsl)
|
||||
{
|
||||
b.Append(@"SslMode=Require;");
|
||||
}
|
||||
else
|
||||
{
|
||||
b.Append(@"SslMode=Disable;");
|
||||
}
|
||||
b.Append(Config.EnableSsl ? @"SslMode=Require;" : @"SslMode=Disable;");
|
||||
|
||||
if (Config.Protocol.HasValue)
|
||||
b.Append($"Protocol={Config.Protocol.Value};");
|
||||
|
||||
@@ -58,6 +58,8 @@ namespace Ntp.Data.Provider
|
||||
|
||||
public abstract void CreateDatabase();
|
||||
|
||||
public abstract string DateAddMinutes(string dateColumn, string minuteColumn);
|
||||
|
||||
public static void Initialize(IDatabaseConfiguration factoryConfiguration)
|
||||
{
|
||||
Config = factoryConfiguration;
|
||||
|
||||
Reference in New Issue
Block a user