Browse Source

Merge branch 'inprogress' of Veloe/EthermineBotTelegram into develop

Слияние рабочей версии расчета worker_unpaid #EBT-16
Veloe 4 years ago
parent
commit
bb8b6fdae9
2 changed files with 44 additions and 19 deletions
  1. 1 1
      EthermineBotTelegram/EFDatabase.cs
  2. 43 18
      EthermineBotTelegram/Program.cs

+ 1 - 1
EthermineBotTelegram/EFDatabase.cs

@@ -61,6 +61,6 @@ namespace EFDatabase
         public int valid_shares { get; set; }
         public int stale_shares { get; set; }
         public int invalid_shares { get; set; }
-        public long worker_unpaid { get; set; }
+        public double worker_unpaid { get; set; }
     }
 }

+ 43 - 18
EthermineBotTelegram/Program.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Configuration;
 using System.Data.Entity;
+using System.Data.Entity.Migrations;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -250,7 +251,7 @@ namespace EthermineBotTelegram
             }
         }
 
-        public async static Task UpdateData()
+        public static async Task UpdateData()
         {
             var usersList = Botdb.users.Where(u=>u.wallet != null).ToList();
             foreach (users u in usersList)
@@ -280,7 +281,7 @@ namespace EthermineBotTelegram
                         newMinerRecord.time)
                     {
                         Botdb.miners.Add(newMinerRecord);
-                        await Botdb.SaveChangesAsync();
+                        //await Botdb.SaveChangesAsync();
                         await Console.Out.WriteLineAsync($"Added new row for {newMinerRecord.wallet}");
                         url = AppSettings.poolApiUrl + "/miner/" + u.wallet + "/workers";
                         var currentWorker = JsonDownloader._download_serialized_json_data<JsonWorker>(url);
@@ -290,7 +291,7 @@ namespace EthermineBotTelegram
                             for (int i = 0; i < currentWorker.data.Count(); i++)
                             {
                                 await Console.Out.WriteLineAsync($"Create new record for {currentWorker.data[i].worker}");
-                                workers newWorkerRecord = new workers();
+                                var newWorkerRecord = new workers();
                                 newWorkerRecord.wallet = newMinerRecord.wallet;
                                 newWorkerRecord.time = currentWorker.data[i].time;
                                 newWorkerRecord.worker = currentWorker.data[i].worker;
@@ -299,25 +300,47 @@ namespace EthermineBotTelegram
                                 newWorkerRecord.valid_shares = currentWorker.data[i].validShares;
                                 newWorkerRecord.stale_shares = currentWorker.data[i].staleShares;
                                 newWorkerRecord.invalid_shares = currentWorker.data[i].invalidShares;
-                                //add func to calc worker_unpaid here
-                                newWorkerRecord.worker_unpaid = 0;
-                                
-                                lastTime = 0;
-                                if (Botdb.workers.Where(m => m.wallet == newMinerRecord.wallet).FirstOrDefault() != null) 
-                                    lastTime = Botdb.workers.Where(m => m.wallet == newMinerRecord.wallet).Max(m => m.time);
-                                var lastWokerRecord = Botdb.workers.Where(m => m.wallet == newWorkerRecord.worker && m.time == lastTime).FirstOrDefault();
-                                if (lastWokerRecord == null || lastWokerRecord.time !=
-                                    newMinerRecord.time)
+
+                                var lastWorkerRecord = Botdb.workers.Where(w => w.worker == newWorkerRecord.worker)
+                                    .OrderByDescending(w => w.time)
+                                    .FirstOrDefault();
+                                if (lastWorkerRecord != null)
                                 {
-                                    await Console.Out.WriteLineAsync(
-                                        $"Create new record for {currentWorker.data[i].worker} finished");
-                                    Botdb.workers.Add(newWorkerRecord);
-                                    await Botdb.SaveChangesAsync();
-                                    await Console.Out.WriteLineAsync($"Added new row for {currentWorker.data[i].worker}");
+                                    await Console.Out.WriteLineAsync($"lastWorkerRecord time = {lastWorkerRecord.time}");
+                                    lastMinerRecord = Botdb.miners.Where(w => w.wallet == newMinerRecord.wallet)
+                                        .OrderByDescending(w => w.time)
+                                        .FirstOrDefault();
+                                    if (lastMinerRecord != null)
+                                    {
+                                        await Console.Out.WriteLineAsync($"lastMinerRecord time = {lastMinerRecord.time}");
+                                        //no check that last balance and prev balance are the same
+                                        newWorkerRecord.worker_unpaid = lastWorkerRecord.worker_unpaid +
+                                                                        (newMinerRecord.unpaid -
+                                                                         lastMinerRecord.unpaid) *
+                                                                        (newWorkerRecord.reported_hashrate /
+                                                                         ((double) newMinerRecord.reported_hashrate));
+                                        if (Double.IsNaN(newWorkerRecord.worker_unpaid) ||
+                                            Double.IsInfinity(newWorkerRecord.worker_unpaid))
+                                            newWorkerRecord.worker_unpaid = lastWorkerRecord.worker_unpaid;
+                                        await Console.Out.WriteLineAsync($"newWorkerRecord unpaid change = {newMinerRecord.unpaid - lastMinerRecord.unpaid}");
+                                    }
+                                    else
+                                    {
+                                        newWorkerRecord.worker_unpaid = 0;
+                                    }
                                 }
                                 else
                                 {
-                                    await Console.Out.WriteLineAsync($"Row with miner: {newWorkerRecord.worker} and time: {newWorkerRecord.time} already exists!");
+                                    newWorkerRecord.worker_unpaid = 0;
+                                }
+                                await Console.Out.WriteLineAsync($"newWorkerRecord worker unpaid = {newWorkerRecord.worker_unpaid}");
+                               
+                                if (lastWorkerRecord == null || newWorkerRecord.time != lastWorkerRecord.time)
+                                    Botdb.workers.Add(newWorkerRecord);
+                                //Botdb.SaveChanges();
+                                else
+                                {
+                                    Botdb.workers.Add(newWorkerRecord);
                                 }
                             }
                         }
@@ -326,6 +349,8 @@ namespace EthermineBotTelegram
                     {
                         await Console.Out.WriteLineAsync($"Row with wallet: {newMinerRecord.wallet} and time: {newMinerRecord.time} already exists!");
                     }
+                    await Botdb.SaveChangesAsync();
+                    await Console.Out.WriteLineAsync($"Saved");
                 }
                 else
                 {