ntminer 5 years ago
parent
commit
98cde05e4e

+ 8 - 4
src/NTMinerDataSchemas/Core/Gpus/GpuName.cs

@@ -4,6 +4,13 @@
             return value >= 4 * NTKeyword.ULongG;
         }
 
+        public static string Format(GpuType gpuType, string gpuName, ulong totalMemory) {
+            ulong totalMemoryGb = (totalMemory + NTKeyword.ULongG - 1) / NTKeyword.ULongG;
+            // 通常显卡的名称上会带显存大小,比如1060分3G版和6G版所以NVIDIA命名显卡的时候
+            // 已经带上了显存信息,但不能假定带了显存信息所以这里拼接上显存信息。
+            return $"{gpuType.GetName()}///{gpuName}///{totalMemoryGb.ToString()}";
+        }
+
         public GpuName() { }
 
         public GpuType GpuType { get; set; }
@@ -32,10 +39,7 @@
         /// </summary>
         /// <returns></returns>
         public override string ToString() {
-            ulong totalMemoryGb = (this.TotalMemory + NTKeyword.ULongG - 1) / NTKeyword.ULongG;
-            // 通常显卡的名称上会带显存大小,比如1060分3G版和6G版所以NVIDIA命名显卡的时候
-            // 已经带上了显存信息,但不能假定带了显存信息所以这里拼接上显存信息。
-            return $"{this.GpuType.GetName()}///{this.Name}///{totalMemoryGb.ToString()}";
+            return Format(this.GpuType, this.Name, this.TotalMemory);
         }
     }
 }

+ 16 - 0
src/NTMinerDataSchemas/Core/Gpus/GpuNameCount.cs

@@ -0,0 +1,16 @@
+namespace NTMiner.Core.Gpus {
+    public class GpuNameCount : GpuName {
+        public static GpuNameCount Create(GpuName gpuName) {
+            return new GpuNameCount {
+                GpuType = gpuName.GpuType,
+                Count = 0,
+                Name = gpuName.Name,
+                TotalMemory = gpuName.TotalMemory
+            };
+        }
+
+        public GpuNameCount() { }
+
+        public int Count { get; set; }
+    }
+}

+ 1 - 0
src/NTMinerDataSchemas/NTMinerDataSchemas.csproj

@@ -44,6 +44,7 @@
     <Compile Include="ConsoleOutLine.cs" />
     <Compile Include="Core\CaptchaData.cs" />
     <Compile Include="Core\Gpus\GpuName.cs" />
+    <Compile Include="Core\Gpus\GpuNameCount.cs" />
     <Compile Include="Core\Gpus\IGpuName.cs" />
     <Compile Include="Core\ICaptcha.cs" />
     <Compile Include="Core\IOperationResult.cs" />

+ 2 - 7
src/WebApiServer/Core/IGpuNameSet.cs

@@ -3,12 +3,7 @@ using System.Collections.Generic;
 
 namespace NTMiner.Core {
     public interface IGpuNameSet {
-        /// <summary>
-        /// 
-        /// </summary>
-        /// <param name="gpuName"></param>
-        /// <param name="gpuTotalMemory">字节</param>
-        void Add(string gpuName, ulong gpuTotalMemory);
-        IEnumerable<IGpuName> AsEnumerable();
+        void Add(GpuType gpuType, string gpuName, ulong gpuTotalMemory);
+        IEnumerable<GpuNameCount> AsEnumerable();
     }
 }

+ 1 - 1
src/WebApiServer/Core/Impl/ClientDataSet.cs

@@ -132,7 +132,7 @@ namespace NTMiner.Core.Impl {
                 _speedDataRedis.SetAsync(new SpeedData(speedDto, DateTime.Now));
             }
             foreach (var gpuSpeedData in speedDto.GpuTable) {
-                _gpuNameSet.Add(gpuSpeedData.Name, gpuSpeedData.TotalMemory);
+                _gpuNameSet.Add(speedDto.GpuType, gpuSpeedData.Name, gpuSpeedData.TotalMemory);
             }
             ClientData clientData = GetByClientId(speedDto.ClientId);
             if (clientData == null) {

+ 17 - 13
src/WebApiServer/Core/Impl/GpuNameSet.cs

@@ -1,10 +1,10 @@
 using NTMiner.Core.Gpus;
+using System;
 using System.Collections.Generic;
 
 namespace NTMiner.Core.Impl {
     public class GpuNameSet : IGpuNameSet {
-        private readonly HashSet<GpuName> _hashSet = new HashSet<GpuName>();
-        private readonly HashSet<GpuName> _toSaves = new HashSet<GpuName>();
+        private readonly Dictionary<string, GpuNameCount> _dic = new Dictionary<string, GpuNameCount>(StringComparer.OrdinalIgnoreCase);
 
         public GpuNameSet() {
         }
@@ -14,22 +14,26 @@ namespace NTMiner.Core.Impl {
         /// </summary>
         /// <param name="gpuName"></param>
         /// <param name="gpuTotalMemory"></param>
-        public void Add(string gpuName, ulong gpuTotalMemory) {
-            if (string.IsNullOrEmpty(gpuName) || !GpuName.IsValidTotalMemory(gpuTotalMemory)) {
+        public void Add(GpuType gpuType, string gpuName, ulong gpuTotalMemory) {
+            if (gpuType == GpuType.Empty || string.IsNullOrEmpty(gpuName) || !GpuName.IsValidTotalMemory(gpuTotalMemory)) {
                 return;
             }
-            var item = new GpuName {
-                Name = gpuName,
-                TotalMemory = gpuTotalMemory
-            };
-            bool isNew = _hashSet.Add(item);
-            if (isNew) {
-                _toSaves.Add(item);
+            string key = GpuName.Format(gpuType, gpuName, gpuTotalMemory);
+            if (_dic.TryGetValue(key, out GpuNameCount gpuNameCount)) {
+                gpuNameCount.Count++;
+            }
+            else {
+                gpuNameCount = GpuNameCount.Create(new GpuName {
+                    GpuType = gpuType,
+                    Name = gpuName,
+                    TotalMemory = gpuTotalMemory
+                });
+                _dic.Add(key, gpuNameCount);
             }
         }
 
-        public IEnumerable<IGpuName> AsEnumerable() {
-            return _hashSet;
+        public IEnumerable<GpuNameCount> AsEnumerable() {
+            return _dic.Values;
         }
     }
 }