Browse Source

Добавил раcчет worker unpaid #EBT-16

Veloe 4 years ago
parent
commit
4fe5f7474a
2 changed files with 33 additions and 73 deletions
  1. 1 1
      EthermineBotTelegram/EFDatabase.cs
  2. 32 72
      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; }
     }
 }

+ 32 - 72
EthermineBotTelegram/Program.cs

@@ -251,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,8 +280,8 @@ namespace EthermineBotTelegram
                     if (lastMinerRecord == null || lastMinerRecord.time !=
                         newMinerRecord.time)
                     {
-                        Botdb.miners.AddOrUpdate(newMinerRecord);
-                        await Botdb.SaveChangesAsync();
+                        Botdb.miners.Add(newMinerRecord);
+                        //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);
@@ -291,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;
@@ -300,89 +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
-                                if (lastMinerRecord != null)
+
+                                var lastWorkerRecord = Botdb.workers.Where(w => w.worker == newWorkerRecord.worker)
+                                    .OrderByDescending(w => w.time)
+                                    .FirstOrDefault();
+                                if (lastWorkerRecord != null)
                                 {
-                                    await Console.Out.WriteLineAsync($"lastMinerRecord exists");
-                                    //lastTime = 0;
-                                    //if (Botdb.workers.Where(m => m.worker == newWorkerRecord.worker).FirstOrDefault() != null) 
-                                    //   lastTime = Botdb.workers.Where(m => m.worker == newWorkerRecord.worker).Max(m => m.time);
- 
-                                var lastWorkerRecord = Botdb.workers.Where(w => w.wallet == newWorkerRecord.worker)
-                                         .OrderByDescending(w => w.time).FirstOrDefault();
-                                    if (lastWorkerRecord != null)
+                                    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($"lastWorkerRecord time {lastWorkerRecord.time}");
-                                        if (lastMinerRecord.time == newMinerRecord.time - 600)
-                                        {
-                                            await Console.Out.WriteLineAsync($"lastMinerRecord 600s from new");
-                                            var prevMinerRecord = Botdb.miners.Where(m =>
-                                                    m.wallet == newMinerRecord.wallet && m.time == lastMinerRecord.time - 600)
-                                                .FirstOrDefault();
-                                            if (prevMinerRecord != null)
-                                            {
-                                                if (lastMinerRecord.unpaid == prevMinerRecord.unpaid)
-                                                {
-                                                    await Console.Out.WriteLineAsync(
-                                                        $"prevMinerRecord exists and 600s from new");
-                                                    //var lastWorkerRecord600 = Botdb.workers.Where(m =>
-                                                    //    m.worker == newWorkerRecord.worker &&
-                                                    //    m.time == newWorkerRecord.time - 600).FirstOrDefault();
-                                                    if (lastWorkerRecord.time == newWorkerRecord.time - 600)
-                                                    {
-                                                        await Console.Out.WriteLineAsync(
-                                                            $"lastWorkerRecord exists and 600s from new");
-                                                        await Console.Out.WriteLineAsync($"Hard calc here");
-                                                    }
-                                                }
-                                                else
-                                                {
-                                                    await Console.Out.WriteLineAsync($"Normal calc here");
-                                                    newWorkerRecord.worker_unpaid =
-                                                        lastWorkerRecord.worker_unpaid + (long)((newMinerRecord.unpaid - lastMinerRecord.unpaid) *
-                                                        (newWorkerRecord.reported_hashrate / (double)newMinerRecord.reported_hashrate));
-                                                }
-                                            }
-                                            else
-                                            {
-                                                await Console.Out.WriteLineAsync($"Normal calc here!");
-                                                newWorkerRecord.worker_unpaid =
-                                                    lastWorkerRecord.worker_unpaid + (long)((newMinerRecord.unpaid - lastMinerRecord.unpaid) *
-                                                        (newWorkerRecord.reported_hashrate / (double)newMinerRecord.reported_hashrate));
-                                            }
-                                        }
-                                        else
-                                        {
-                                            await Console.Out.WriteLineAsync($"Just add new row with old data!");
+                                        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
                                     {
-                                        await Console.Out.WriteLineAsync($"Just add new row with 0");
                                         newWorkerRecord.worker_unpaid = 0;
                                     }
                                 }
                                 else
                                 {
-                                    await Console.Out.WriteLineAsync($"Just add new row with 0");
                                     newWorkerRecord.worker_unpaid = 0;
                                 }
-                                await Console.Out.WriteLineAsync($"newWorkerRecord worker unpaid : {newWorkerRecord.worker_unpaid}");
-
-                                var lastWokerRecord = Botdb.workers.Where(w => w.wallet == newWorkerRecord.worker).OrderByDescending(w=> w.time).FirstOrDefault();
-                                if (lastWokerRecord == null || lastWokerRecord.time !=
-                                    newMinerRecord.time)
-                                {
-                                    await Console.Out.WriteLineAsync(
-                                        $"Create new record for {currentWorker.data[i].worker} finished");
-                                    Botdb.workers.AddOrUpdate(newWorkerRecord);
-                                    await Botdb.SaveChangesAsync();
-                                    await Console.Out.WriteLineAsync($"Added new row for {currentWorker.data[i].worker}");
-                                }
+                                await Console.Out.WriteLineAsync($"newWorkerRecord worker unpaid = {newWorkerRecord.worker_unpaid}");
+                               
+                                if (lastWorkerRecord == null || newWorkerRecord.time != lastWorkerRecord.time)
+                                    Botdb.workers.Add(newWorkerRecord);
+                                //Botdb.SaveChanges();
                                 else
                                 {
-                                    await Console.Out.WriteLineAsync($"Row with miner: {newWorkerRecord.worker} and time: {newWorkerRecord.time} already exists!");
+                                    Botdb.workers.Add(newWorkerRecord);
                                 }
                             }
                         }
@@ -391,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
                 {