Add HW DB

This commit is contained in:
13xforever
2022-06-29 19:08:19 +05:00
parent e61d852f12
commit 8c1468defa
6 changed files with 312 additions and 2 deletions

View File

@@ -3,13 +3,13 @@
"isRoot": true,
"tools": {
"strawberryshake.tools": {
"version": "11.2.2",
"version": "12.11.1",
"commands": [
"dotnet-graphql"
]
},
"dotnet-ef": {
"version": "5.0.5",
"version": "6.0.6",
"commands": [
"dotnet-ef"
]

View File

@@ -31,6 +31,10 @@ namespace CompatBot.Database
if (!await ImportNamesPool(db, Config.Cts.Token))
return false;
}
await using (var db = new HardwareDb())
if (!await UpgradeAsync(db, Config.Cts.Token))
return false;
return true;
}

View File

@@ -0,0 +1,81 @@
using System;
using System.ComponentModel.DataAnnotations;
using CompatApiClient;
using Microsoft.EntityFrameworkCore;
namespace CompatBot.Database;
internal class HardwareDb : DbContext
{
public DbSet<HwInfo> HwInfo { get; set; } = null;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var dbPath = DbImporter.GetDbPath("hw.db", Environment.SpecialFolder.LocalApplicationData);
#if DEBUG
optionsBuilder.UseLoggerFactory(Config.LoggerFactory);
#endif
optionsBuilder.UseSqlite($"Data Source=\"{dbPath}\"");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
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);
}
}
[Flags]
internal enum CpuFeatures
{
None = 0b_00000000_00000000_00000000_00000000,
Avx = 0b_00000000_00000000_00000000_00000001,
Avx2 = 0b_00000000_00000000_00000000_00000010,
Avx512 = 0b_00000000_00000000_00000000_00000100,
Avx512IL = 0b_00000000_00000000_00000000_00001000,
Fma3 = 0b_00000000_00000000_00000000_00010000,
Fma4 = 0b_00000000_00000000_00000000_00100000,
Tsx = 0b_00000000_00000000_00000000_01000000,
TsxFa = 0b_00000000_00000000_00000000_10000000,
}
internal enum OsType : byte
{
Unknown = 0,
Windows = 1,
Linux = 2,
MacOs = 3,
Bsd = 4,
}
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]
public string CpuMaker { get; set; } = null!;
[Required]
public string CpuModel { get; set; } = null!;
public int ThreadCount { get; set; }
public CpuFeatures CpuFeatures { get; set; }
public long RamInMb { get; set; }
[Required]
public string GpuMaker { get; set; } = null!;
[Required]
public string GpuModel { get; set; } = null!;
public OsType OsType { get; set; }
public string? OsName { get; set; }
public string? OsVersion { get; set; }
}

View File

@@ -0,0 +1,90 @@
// <auto-generated />
using System;
using CompatBot.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace CompatBot.Migrations.HardwareDbMigrations
{
[DbContext(typeof(HardwareDb))]
[Migration("20220629140504_InitialCreate")]
partial class InitialCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "6.0.6");
modelBuilder.Entity("CompatBot.Database.HwInfo", b =>
{
b.Property<byte[]>("HwId")
.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");
b.Property<int>("CpuFeatures")
.HasColumnType("INTEGER")
.HasColumnName("cpu_features");
b.Property<string>("CpuMaker")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnName("cpu_maker");
b.Property<string>("GpuMaker")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnName("gpu_maker");
b.Property<int>("Id")
.HasColumnType("INTEGER")
.HasColumnName("id");
b.Property<string>("OsName")
.HasColumnType("TEXT")
.HasColumnName("os_name");
b.Property<string>("OsVersion")
.HasColumnType("TEXT")
.HasColumnName("os_version");
b.Property<long>("RamInMb")
.HasColumnType("INTEGER")
.HasColumnName("ram_in_mb");
b.Property<int>("ThreadCount")
.HasColumnType("INTEGER")
.HasColumnName("thread_count");
b.Property<long>("Timestamp")
.HasColumnType("INTEGER")
.HasColumnName("timestamp");
b.HasKey("HwId", "CpuModel", "GpuModel", "OsType")
.HasName("id");
b.HasIndex("Timestamp")
.HasDatabaseName("hardware_timestamp");
b.ToTable("hw_info");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace CompatBot.Migrations.HardwareDbMigrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
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),
timestamp = table.Column<long>(type: "INTEGER", nullable: false),
cpu_maker = 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),
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 });
});
migrationBuilder.CreateIndex(
name: "hardware_timestamp",
table: "hw_info",
column: "timestamp");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "hw_info");
}
}
}

View File

@@ -0,0 +1,88 @@
// <auto-generated />
using System;
using CompatBot.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace CompatBot.Migrations.HardwareDbMigrations
{
[DbContext(typeof(HardwareDb))]
partial class HardwareDbModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "6.0.6");
modelBuilder.Entity("CompatBot.Database.HwInfo", b =>
{
b.Property<byte[]>("HwId")
.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");
b.Property<int>("CpuFeatures")
.HasColumnType("INTEGER")
.HasColumnName("cpu_features");
b.Property<string>("CpuMaker")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnName("cpu_maker");
b.Property<string>("GpuMaker")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnName("gpu_maker");
b.Property<int>("Id")
.HasColumnType("INTEGER")
.HasColumnName("id");
b.Property<string>("OsName")
.HasColumnType("TEXT")
.HasColumnName("os_name");
b.Property<string>("OsVersion")
.HasColumnType("TEXT")
.HasColumnName("os_version");
b.Property<long>("RamInMb")
.HasColumnType("INTEGER")
.HasColumnName("ram_in_mb");
b.Property<int>("ThreadCount")
.HasColumnType("INTEGER")
.HasColumnName("thread_count");
b.Property<long>("Timestamp")
.HasColumnType("INTEGER")
.HasColumnName("timestamp");
b.HasKey("HwId", "CpuModel", "GpuModel", "OsType")
.HasName("id");
b.HasIndex("Timestamp")
.HasDatabaseName("hardware_timestamp");
b.ToTable("hw_info");
});
#pragma warning restore 612, 618
}
}
}