made installid the only key and moved some things around

This commit is contained in:
13xforever 2022-06-30 00:34:15 +05:00
parent 7dc07f282f
commit 6bf56896e6
7 changed files with 85 additions and 73 deletions

View File

@ -1,7 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using CompatApiClient;
using Microsoft.EntityFrameworkCore;
@ -9,7 +7,7 @@ namespace CompatBot.Database;
internal class HardwareDb : DbContext
{
public DbSet<HwInfo> HwInfo { get; set; } = null;
public DbSet<HwInfo> HwInfo { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
@ -23,11 +21,7 @@ internal class HardwareDb : DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UseCollation("NOCASE");
modelBuilder.Entity<HwInfo>().HasKey(m => new { m.HwId, m.CpuModel, m.GpuModel, m.OsType, });
modelBuilder.Entity<HwInfo>().HasIndex(m => m.Timestamp).HasDatabaseName("hardware_timestamp");
//configure default policy of Id being the primary key
modelBuilder.ConfigureDefaultPkConvention();
//configure name conversion for all configured entities from CamelCase to snake_case
modelBuilder.ConfigureMapping(NamingStyles.Underscore);
@ -60,10 +54,9 @@ internal enum OsType : byte
internal class HwInfo
{
public int Id { get; set; }
public long Timestamp { get; set; }
[Required, MinLength(128/8), MaxLength(512/8)]
public byte[] HwId { get; set; } = null!; // this should be either a guid or a hash of somewhat unique data (discord user id, user profile name from logs, etc)
[Required, Key, MinLength(128/8), MaxLength(512/8)]
public byte[] InstallId { get; set; } = null!; // this should be either a guid or a hash of somewhat unique data (discord user id, user profile name from logs, etc)
[Required]
public string CpuMaker { get; set; } = null!;

View File

@ -8,10 +8,10 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace CompatBot.Migrations.HardwareDbMigrations
namespace CompatBot.Database.Migrations.HardwareDbMigrations
{
[DbContext(typeof(HardwareDb))]
[Migration("20220629172134_InitialCreate")]
[Migration("20220629192852_InitialCreate")]
partial class InitialCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -23,22 +23,10 @@ namespace CompatBot.Migrations.HardwareDbMigrations
modelBuilder.Entity("CompatBot.Database.HwInfo", b =>
{
b.Property<byte[]>("HwId")
b.Property<byte[]>("InstallId")
.HasMaxLength(64)
.HasColumnType("BLOB")
.HasColumnName("hw_id");
b.Property<string>("CpuModel")
.HasColumnType("TEXT")
.HasColumnName("cpu_model");
b.Property<string>("GpuModel")
.HasColumnType("TEXT")
.HasColumnName("gpu_model");
b.Property<byte>("OsType")
.HasColumnType("INTEGER")
.HasColumnName("os_type");
.HasColumnName("install_id");
b.Property<int>("CpuFeatures")
.HasColumnType("INTEGER")
@ -49,19 +37,29 @@ namespace CompatBot.Migrations.HardwareDbMigrations
.HasColumnType("TEXT")
.HasColumnName("cpu_maker");
b.Property<string>("CpuModel")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnName("cpu_model");
b.Property<string>("GpuMaker")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnName("gpu_maker");
b.Property<int>("Id")
.HasColumnType("INTEGER")
.HasColumnName("id");
b.Property<string>("GpuModel")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnName("gpu_model");
b.Property<string>("OsName")
.HasColumnType("TEXT")
.HasColumnName("os_name");
b.Property<byte>("OsType")
.HasColumnType("INTEGER")
.HasColumnName("os_type");
b.Property<string>("OsVersion")
.HasColumnType("TEXT")
.HasColumnName("os_version");
@ -78,8 +76,8 @@ namespace CompatBot.Migrations.HardwareDbMigrations
.HasColumnType("INTEGER")
.HasColumnName("timestamp");
b.HasKey("HwId", "CpuModel", "GpuModel", "OsType")
.HasName("id");
b.HasKey("InstallId")
.HasName("pk_hw_info");
b.HasIndex("Timestamp")
.HasDatabaseName("hardware_timestamp");

View File

@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace CompatBot.Migrations.HardwareDbMigrations
namespace CompatBot.Database.Migrations.HardwareDbMigrations
{
public partial class InitialCreate : Migration
{
@ -13,23 +13,22 @@ namespace CompatBot.Migrations.HardwareDbMigrations
name: "hw_info",
columns: table => new
{
hw_id = table.Column<byte[]>(type: "BLOB", maxLength: 64, nullable: false),
cpu_model = table.Column<string>(type: "TEXT", nullable: false),
gpu_model = table.Column<string>(type: "TEXT", nullable: false),
os_type = table.Column<byte>(type: "INTEGER", nullable: false),
id = table.Column<int>(type: "INTEGER", nullable: false),
install_id = table.Column<byte[]>(type: "BLOB", maxLength: 64, nullable: false),
timestamp = table.Column<long>(type: "INTEGER", nullable: false),
cpu_maker = table.Column<string>(type: "TEXT", nullable: false),
cpu_model = table.Column<string>(type: "TEXT", nullable: false),
thread_count = table.Column<int>(type: "INTEGER", nullable: false),
cpu_features = table.Column<int>(type: "INTEGER", nullable: false),
ram_in_mb = table.Column<long>(type: "INTEGER", nullable: false),
gpu_maker = table.Column<string>(type: "TEXT", nullable: false),
gpu_model = table.Column<string>(type: "TEXT", nullable: false),
os_type = table.Column<byte>(type: "INTEGER", nullable: false),
os_name = table.Column<string>(type: "TEXT", nullable: true),
os_version = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("id", x => new { x.hw_id, x.cpu_model, x.gpu_model, x.os_type });
table.PrimaryKey("pk_hw_info", x => x.install_id);
});
migrationBuilder.CreateIndex(

View File

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace CompatBot.Migrations.HardwareDbMigrations
namespace CompatBot.Database.Migrations.HardwareDbMigrations
{
[DbContext(typeof(HardwareDb))]
partial class HardwareDbModelSnapshot : ModelSnapshot
@ -21,22 +21,10 @@ namespace CompatBot.Migrations.HardwareDbMigrations
modelBuilder.Entity("CompatBot.Database.HwInfo", b =>
{
b.Property<byte[]>("HwId")
b.Property<byte[]>("InstallId")
.HasMaxLength(64)
.HasColumnType("BLOB")
.HasColumnName("hw_id");
b.Property<string>("CpuModel")
.HasColumnType("TEXT")
.HasColumnName("cpu_model");
b.Property<string>("GpuModel")
.HasColumnType("TEXT")
.HasColumnName("gpu_model");
b.Property<byte>("OsType")
.HasColumnType("INTEGER")
.HasColumnName("os_type");
.HasColumnName("install_id");
b.Property<int>("CpuFeatures")
.HasColumnType("INTEGER")
@ -47,19 +35,29 @@ namespace CompatBot.Migrations.HardwareDbMigrations
.HasColumnType("TEXT")
.HasColumnName("cpu_maker");
b.Property<string>("CpuModel")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnName("cpu_model");
b.Property<string>("GpuMaker")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnName("gpu_maker");
b.Property<int>("Id")
.HasColumnType("INTEGER")
.HasColumnName("id");
b.Property<string>("GpuModel")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnName("gpu_model");
b.Property<string>("OsName")
.HasColumnType("TEXT")
.HasColumnName("os_name");
b.Property<byte>("OsType")
.HasColumnType("INTEGER")
.HasColumnName("os_type");
b.Property<string>("OsVersion")
.HasColumnType("TEXT")
.HasColumnName("os_version");
@ -76,8 +74,8 @@ namespace CompatBot.Migrations.HardwareDbMigrations
.HasColumnType("INTEGER")
.HasColumnName("timestamp");
b.HasKey("HwId", "CpuModel", "GpuModel", "OsType")
.HasName("id");
b.HasKey("InstallId")
.HasName("pk_hw_info");
b.HasIndex("Timestamp")
.HasDatabaseName("hardware_timestamp");

View File

@ -1,8 +1,13 @@
using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CompatBot.Utils;
using CompatBot.Utils.ResultFormatters;
using DSharpPlus.Entities;
namespace CompatBot.Database.Providers;
@ -10,7 +15,7 @@ internal static class HwInfoProvider
{
private static readonly Encoding Utf8 = new UTF8Encoding(false);
public static void AddOrUpdateSystem(NameValueCollection items)
public static async Task AddOrUpdateSystemAsync(DiscordMessage msg, NameValueCollection items, CancellationToken cancellationToken)
{
if (items["cpu_model"] is not string cpuString
|| (items["gpu_name"] ?? items["gpu_info"]) is not string gpuString
@ -30,13 +35,18 @@ internal static class HwInfoProvider
return;
}
if (gpuStringParts[0].ToLower() is not ("nvidia" or "amd" or "intel" or "apple"))
{
Config.Log.Warn($"Unknown GPU maker {gpuStringParts[0]}, plz fix");
return;
}
if (gpuStringParts[0].ToLower() is not ("nvidia" or "amd" or "ati" or "intel" or "apple"))
if (LogParserResult.IsNvidia(gpuString))
gpuStringParts = new[] { "NVIDIA", gpuString };
else if (LogParserResult.IsAmd(gpuString))
gpuStringParts = new[] { "AMD", gpuString };
else
{
Config.Log.Warn($"Unknown GPU maker {gpuStringParts[0]}, plz fix");
return;
}
var ts = DateTime.UtcNow;
var ts = msg.Timestamp.UtcDateTime;
if (items["log_start_timestamp"] is string logTs
&& DateTime.TryParse(logTs, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal, out var logTsVal))
ts = logTsVal.ToUniversalTime();
@ -44,7 +54,7 @@ internal static class HwInfoProvider
var info = new HwInfo
{
Timestamp = ts.Ticks,
HwId = GetHwId(items),
InstallId = GetHwId(items, msg),
CpuMaker = cpuStringParts[0],
CpuModel = cpuStringParts[1],
@ -60,11 +70,25 @@ internal static class HwInfoProvider
OsName = GetName(osType, items),
OsVersion = items["os_version"],
};
await using var db = new HardwareDb();
var existingItem = await db.HwInfo.FindAsync(info.InstallId).ConfigureAwait(false);
if (existingItem is null)
db.HwInfo.Add(info);
else if (existingItem.Timestamp < info.Timestamp)
db.Entry(existingItem).CurrentValues.SetValues(info);
try
{
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
Config.Log.Error(e, "Failed to update hardware db");
}
}
private static byte[] GetHwId(NameValueCollection items)
private static byte[] GetHwId(NameValueCollection items, DiscordMessage message)
{
var id = items["hw_id"] ?? items["compat_database_path"] ?? "anonymous";
var id = items["hw_id"] ?? message.Author.Id.ToString("x16") + items["compat_database_path"];
return Utf8.GetBytes(id).GetSaltedHash();
}

View File

@ -145,7 +145,6 @@ namespace CompatBot.Utils.ResultFormatters
systemInfo += $"{Environment.NewLine}GPU{(multiple ? "s" : "")}:{(multiple ? Environment.NewLine : " ")}{availableGpus}";
}
builder.AddField("Build Info", systemInfo.Trim(EmbedPager.MaxFieldLength));
HwInfoProvider.AddOrUpdateSystem(items);
}
private const int ColumnWidth = 30;

View File

@ -15,7 +15,6 @@ using CompatBot.EventHandlers.LogParsing.SourceHandlers;
using DSharpPlus;
using DSharpPlus.Entities;
using IrdLibraryClient;
using Microsoft.DotNet.PlatformAbstractions;
namespace CompatBot.Utils.ResultFormatters
{
@ -295,11 +294,13 @@ namespace CompatBot.Utils.ResultFormatters
{
CleanupValues(state);
BuildInfoSection(builder, collection);
var hwUpdateTask = HwInfoProvider.AddOrUpdateSystemAsync(message, collection, Config.Cts.Token);
var colA = BuildCpuSection(collection);
var colB = BuildGpuSection(collection);
BuildSettingsSections(builder, collection, colA, colB);
BuildLibsSection(builder, collection);
await BuildNotesSectionAsync(builder, state, client).ConfigureAwait(false);
await hwUpdateTask.ConfigureAwait(false);
}
}
else
@ -836,15 +837,15 @@ namespace CompatBot.Utils.ResultFormatters
13 => "macOS Ventura",
_ => null,
};
private static bool IsAmd(string gpuInfo)
internal static bool IsAmd(string gpuInfo)
{
return gpuInfo.Contains("Radeon", StringComparison.InvariantCultureIgnoreCase) ||
gpuInfo.Contains("AMD", StringComparison.InvariantCultureIgnoreCase) ||
gpuInfo.Contains("ATI ", StringComparison.InvariantCultureIgnoreCase);
}
private static bool IsNvidia(string gpuInfo)
internal static bool IsNvidia(string gpuInfo)
{
return gpuInfo.Contains("GeForce", StringComparison.InvariantCultureIgnoreCase) ||
gpuInfo.Contains("nVidia", StringComparison.InvariantCultureIgnoreCase) ||