forked from MPOS/php-mpos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics.class.php
929 lines (888 loc) · 41.2 KB
/
statistics.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
<?php
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
/*
* We give access to plenty of statistics through this class
* Statistics should be non-intrusive and not change any
* rows in our database to ensure data integrity for the backend
**/
class Statistics extends Base {
protected $table = 'statistics_shares';
protected $table_user_stats = 'statistics_users';
private $getcache = true;
// Disable fetching values from cache
public function setGetCache($set=false) {
$this->getcache = $set;
}
public function getGetCache() {
return $this->getcache;
}
public function getAllUserMiningStats() {
return $this->allUserMiningStats;
}
public function getUserStatsTableName() {
return $this->table_user_stats;
}
/**
* Get our first block found
*
**/
public function getFirstBlockFound() {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare("
SELECT IFNULL(MIN(time), 0) AS time FROM " . $this->block->getTableName());
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result())
return $result->fetch_object()->time;
return false;
}
/**
* Fetch last found blocks by time
**/
function getLastBlocksbyTime() {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
COUNT(id) AS Total,
IFNULL(SUM(IF(confirmations > 0, 1, 0)), 0) AS TotalValid,
IFNULL(SUM(IF(confirmations = -1, 1, 0)), 0) AS TotalOrphan,
IFNULL(SUM(IF(confirmations > 0, difficulty, 0)), 0) AS TotalDifficulty,
IFNULL(SUM(IF(confirmations > -1, shares, 0)), 0) AS TotalShares,
IFNULL(SUM(IF(confirmations > -1, amount, 0)), 0) AS TotalAmount,
IFNULL(SUM(IF(FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), 1, 0)), 0) AS 1HourTotal,
IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), 1, 0)), 0) AS 1HourValid,
IFNULL(SUM(IF(confirmations = -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), 1, 0)), 0) AS 1HourOrphan,
IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), difficulty, 0)), 0) AS 1HourDifficulty,
IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), shares, 0)), 0) AS 1HourShares,
IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 3600 SECOND), amount, 0)), 0) AS 1HourAmount,
IFNULL(SUM(IF(FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), 1, 0)), 0) AS 24HourTotal,
IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), 1, 0)), 0) AS 24HourValid,
IFNULL(SUM(IF(confirmations = -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), 1, 0)), 0) AS 24HourOrphan,
IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), difficulty, 0)), 0) AS 24HourDifficulty,
IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), shares, 0)), 0) AS 24HourShares,
IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 86400 SECOND), amount, 0)), 0) AS 24HourAmount,
IFNULL(SUM(IF(FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), 1, 0)), 0) AS 7DaysTotal,
IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), 1, 0)), 0) AS 7DaysValid,
IFNULL(SUM(IF(confirmations = -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), 1, 0)), 0) AS 7DaysOrphan,
IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), difficulty, 0)), 0) AS 7DaysDifficulty,
IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), shares, 0)), 0) AS 7DaysShares,
IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 604800 SECOND), amount, 0)), 0) AS 7DaysAmount,
IFNULL(SUM(IF(FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), 1, 0)), 0) AS 4WeeksTotal,
IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), 1, 0)), 0) AS 4WeeksValid,
IFNULL(SUM(IF(confirmations = -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), 1, 0)), 0) AS 4WeeksOrphan,
IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), difficulty, 0)), 0) AS 4WeeksDifficulty,
IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), shares, 0)), 0) AS 4WeeksShares,
IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 2419200 SECOND), amount, 0)), 0) AS 4WeeksAmount,
IFNULL(SUM(IF(FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), 1, 0)), 0) AS 12MonthTotal,
IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), 1, 0)), 0) AS 12MonthValid,
IFNULL(SUM(IF(confirmations = -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), 1, 0)), 0) AS 12MonthOrphan,
IFNULL(SUM(IF(confirmations > 0 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), difficulty, 0)), 0) AS 12MonthDifficulty,
IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), shares, 0)), 0) AS 12MonthShares,
IFNULL(SUM(IF(confirmations > -1 AND FROM_UNIXTIME(time) >= DATE_SUB(now(), INTERVAL 29030400 SECOND), amount, 0)), 0) AS 12MonthAmount
FROM " . $this->block->getTableName());
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result()) {
$aData = $result->fetch_assoc();
$aData['TotalEstimatedShares'] = $this->coin->calcEstaimtedShares($aData['TotalDifficulty']);
$aData['1HourEstimatedShares'] = $this->coin->calcEstaimtedShares($aData['1HourDifficulty']);
$aData['24HourEstimatedShares'] = $this->coin->calcEstaimtedShares($aData['24HourDifficulty']);
$aData['7DaysEstimatedShares'] = $this->coin->calcEstaimtedShares($aData['7DaysDifficulty']);
$aData['4WeeksEstimatedShares'] = $this->coin->calcEstaimtedShares($aData['4WeeksDifficulty']);
$aData['12MonthEstimatedShares'] = $this->coin->calcEstaimtedShares($aData['12MonthDifficulty']);
return $this->memcache->setCache(__FUNCTION__, $aData);
}
return $this->sqlError();
}
/**
* Get our last $limit blocks found
* @param limit int Last limit blocks
* @return array
**/
public function getBlocksFound($limit=10) {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $limit)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
b.*,
a.username AS finder,
a.is_anonymous AS is_anonymous,
ROUND(difficulty * POW(2, 32 - " . $this->coin->getTargetBits() . "), " . $this->coin->getShareDifficultyPrecision() . ") AS estshares
FROM " . $this->block->getTableName() . " AS b
LEFT JOIN " . $this->user->getTableName() . " AS a
ON b.account_id = a.id
ORDER BY height DESC LIMIT ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result())
return $this->memcache->setCache(__FUNCTION__ . $limit, $result->fetch_all(MYSQLI_ASSOC), 5);
return $this->sqlError();
}
/**
* Get our last $limit blocks found by height
* @param limit int Last limit blocks
* @return array
**/
public function getBlocksFoundHeight($iHeight=0, $limit=10) {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $iHeight . $limit)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
b.*,
a.username AS finder,
a.is_anonymous AS is_anonymous,
ROUND(difficulty * POW(2, 32 - " . $this->coin->getTargetBits() . "), 4) AS estshares
FROM " . $this->block->getTableName() . " AS b
LEFT JOIN " . $this->user->getTableName() . " AS a
ON b.account_id = a.id
WHERE b.height <= ?
ORDER BY height DESC LIMIT ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("ii", $iHeight, $limit) && $stmt->execute() && $result = $stmt->get_result())
return $this->memcache->setCache(__FUNCTION__ . $iHeight . $limit, $result->fetch_all(MYSQLI_ASSOC), 5);
return $this->sqlError();
}
/**
* Get SUM of blocks found and generated Coins for each Account
* @param limit int Last limit blocks
* @return array
**/
public function getBlocksSolvedbyAccount($limit=25) {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $limit)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
b.*,
a.username AS finder,
a.is_anonymous AS is_anonymous,
COUNT(b.id) AS solvedblocks,
SUM(b.amount) AS generatedcoins
FROM " . $this->block->getTableName() . " AS b
LEFT JOIN " . $this->user->getTableName() . " AS a
ON b.account_id = a.id
WHERE confirmations > 0
GROUP BY finder
ORDER BY solvedblocks DESC LIMIT ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result())
return $this->memcache->setCache(__FUNCTION__ . $limit, $result->fetch_all(MYSQLI_ASSOC), 5);
return $this->sqlError();
}
/**
* Get SUM of blocks found and generated Coins for each worker
* @param limit int Last limit blocks
* @return array
**/
public function getBlocksSolvedbyWorker($account_id, $limit=25) {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $account_id . $limit)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
worker_name AS finder,
COUNT(id) AS solvedblocks,
SUM(amount) AS generatedcoins
FROM " . $this->block->getTableName() . "
WHERE account_id = ? AND worker_name != 'unknown'
GROUP BY finder
ORDER BY solvedblocks DESC LIMIT ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("ii", $account_id, $limit) && $stmt->execute() && $result = $stmt->get_result())
return $this->memcache->setCache(__FUNCTION__ . $account_id . $limit, $result->fetch_all(MYSQLI_ASSOC), 5);
return $this->sqlError();
}
/**
* Currently the only function writing to the database
* Stored per block user statistics of valid and invalid shares
* @param aStats array Array with user id, valid and invalid shares
* @param iBlockId int Block ID as store in the Block table
* @return bool
**/
public function updateShareStatistics($aStats, $iBlockId) {
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, valid, invalid, block_id) VALUES (?, ?, ?, ?)");
if ($this->checkStmt($stmt) && $stmt->bind_param('iddi', $aStats['id'], $aStats['valid'], $aStats['invalid'], $iBlockId) && $stmt->execute()) return true;
return $this->sqlError();
}
/**
* insert user round and pplns shares merged array
**/
public function insertPPLNSStatistics($aStats, $iBlockId) {
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, valid, invalid, pplns_valid, pplns_invalid, block_id) VALUES (?, ?, ?, ?, ?, ?)");
if ($this->checkStmt($stmt) && $stmt->bind_param('iddddi', $aStats['id'], $aStats['valid'], $aStats['invalid'], $aStats['pplns_valid'], $aStats['pplns_invalid'], $iBlockId) && $stmt->execute()) return true;
return $this->sqlError();
}
/**
* Get our current pool hashrate for the past 10 minutes across both
* shares and shares_archive table
* @param none
* @return data object Return our hashrateas an object
**/
public function getCurrentHashrate($interval=180) {
$this->debug->append("STA " . __METHOD__, 4);
if ($this->getGetCache() && $data = $this->memcache->getStatic(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
(
(
SELECT IFNULL(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty)), 0) AS shares
FROM " . $this->share->getTableName() . "
WHERE time > DATE_SUB(now(), INTERVAL ? SECOND)
AND our_result = 'Y'
) + (
SELECT IFNULL(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty)), 0) AS shares
FROM " . $this->share->getArchiveTableName() . "
WHERE time > DATE_SUB(now(), INTERVAL ? SECOND)
AND our_result = 'Y'
)
) AS shares
FROM DUAL");
if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) {
$hashrate = $this->coin->calcHashrate($result->fetch_object()->shares, $interval);
return $this->memcache->setStaticCache(__FUNCTION__, $hashrate);
}
return $this->sqlError();
}
/**
* Same as getCurrentHashrate but for Shares
* @param none
* @return data object Our share rate in shares per second
**/
public function getCurrentShareRate($interval=180) {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->getStatic(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
(
(
SELECT ROUND(SUM(difficulty) / ?, " . $this->coin->getShareDifficultyPrecision() . ") AS sharerate
FROM " . $this->share->getTableName() . "
WHERE time > DATE_SUB(now(), INTERVAL ? SECOND)
AND our_result = 'Y'
) + (
SELECT ROUND(SUM(difficulty) / ?, " . $this->coin->getShareDifficultyPrecision() . ") AS sharerate
FROM " . $this->share->getArchiveTableName() . "
WHERE time > DATE_SUB(now(), INTERVAL ? SECOND)
AND our_result = 'Y'
)
) AS sharerate
FROM DUAL");
if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $interval, $interval, $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setStaticCache(__FUNCTION__, $result->fetch_object()->sharerate);
return $this->sqlError();
}
/**
* Get total shares for this round, since last block found
* @param none
* @return data array invalid and valid shares
**/
public function getRoundShares() {
$this->debug->append("STA " . __METHOD__, 4);
// Try the statistics cron cache, then function cache, then fallback to SQL
if ($data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) {
$this->debug->append("Found data in statistics cache", 2);
$total = array('valid' => 0, 'invalid' => 0);
foreach ($data['data'] as $aUser) {
$total['valid'] += $aUser['valid'];
$total['invalid'] += $aUser['invalid'];
}
return $total;
}
if ($data = $this->memcache->get(STATISTICS_ROUND_SHARES)) {
$this->debug->append("Found data in local cache", 2);
return $data;
}
$stmt = $this->mysqli->prepare("
SELECT
IFNULL(SUM(IF(our_result='Y', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0) AS valid,
IFNULL(SUM(IF(our_result='N', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0) AS invalid
FROM " . $this->share->getTableName() . "
WHERE UNIX_TIMESTAMP(time) > IFNULL((SELECT MAX(time) FROM " . $this->block->getTableName() . "), 0)");
if ( $this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() )
return $this->memcache->setCache(STATISTICS_ROUND_SHARES, $result->fetch_assoc());
return $this->sqlError();
}
/**
* Get amount of shares for a all users
* Used in statistics cron to refresh memcache data
* @param account_id int User ID
* @return data array invalid and valid share counts
**/
public function getAllUserShares() {
$this->debug->append("STA " . __METHOD__, 4);
if (! $data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) {
$data['share_id'] = 0;
$data['data'] = array();
}
$stmt = $this->mysqli->prepare("
SELECT
IFNULL(SUM(IF(our_result='Y', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) AS valid,
IFNULL(SUM(IF(our_result='N', IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty), 0)), 0) AS invalid,
u.id AS id,
u.donate_percent AS donate_percent,
u.is_anonymous AS is_anonymous,
u.username AS username
FROM " . $this->share->getTableName() . " AS s,
" . $this->user->getTableName() . " AS u
WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 )
AND UNIX_TIMESTAMP(s.time) > IFNULL(
(
SELECT MAX(b.time)
FROM " . $this->block->getTableName() . " AS b
) ,0 )
AND s.id > ?
GROUP BY u.id");
if ($stmt && $stmt->bind_param('i', $data['share_id']) && $stmt->execute() && $result = $stmt->get_result()) {
$data_new = array();
while ($row = $result->fetch_assoc()) {
if (! array_key_exists($row['id'], $data['data'])) {
$data['data'][$row['id']] = $row;
} else {
$data['data'][$row['id']]['valid'] += $row['valid'];
$data['data'][$row['id']]['invalid'] += $row['invalid'];
$data['data'][$row['id']]['donate_percent'] = $row['donate_percent'];
$data['data'][$row['id']]['is_anonymous'] = $row['is_anonymous'];
}
}
$data['share_id'] = $this->share->getLastInsertedShareId();
return $this->memcache->setCache(STATISTICS_ALL_USER_SHARES, $data);
}
return $this->sqlError();
}
/**
* Get amount of shares for a specific user
* @param username str username
* @param account_id int account id
* @return data array invalid and valid share counts
**/
public function getUserShares($username, $account_id=NULL) {
$this->debug->append("STA " . __METHOD__, 4);
// Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL
if ($data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) {
if (array_key_exists($account_id, $data['data']))
return $data['data'][$account_id];
// We have no cached value, we return defaults
return array('valid' => 0, 'invalid' => 0, 'donate_percent' => 0, 'is_anonymous' => 0);
}
if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
IFNULL(SUM(IF(our_result='Y', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0) AS valid,
IFNULL(SUM(IF(our_result='N', IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0)), 0) AS invalid
FROM " . $this->share->getTableName() . "
WHERE username LIKE ?
AND UNIX_TIMESTAMP(time) >IFNULL((SELECT MAX(b.time) FROM " . $this->block->getTableName() . " AS b),0)");
$username = $username . ".%";
if ($stmt && $stmt->bind_param("s", $username) && $stmt->execute() && $result = $stmt->get_result())
return $this->memcache->setCache(__FUNCTION__ . $account_id, $result->fetch_assoc());
return $this->sqlError();
}
/**
* Admin panel specific query
* @return data array User settings and shares
**/
public function getAllUserStats($filter='%',$limit=1,$start=0) {
$this->debug->append("STA " . __METHOD__, 4);
$sql = "
SELECT
a.id AS id,
a.is_admin as is_admin,
a.is_locked as is_locked,
a.no_fees as no_fees,
a.username AS username,
a.donate_percent AS donate_percent,
a.email AS email,
a.last_login as last_login
FROM " . $this->user->getTableName() . " AS a";
if (is_array($filter)) {
$aFilter = array();
foreach ($filter as $key => $value) {
if (isset($value) && $value != "" ) {
switch ($key) {
case 'account':
$aFilter[] = "a.username LIKE ?";
$this->addParam('s', $value);
break;
case 'email':
$aFilter[] = "a.email LIKE ?";
$this->addParam('s', $value);
break;
case 'is_admin':
$aFilter[] = "a.is_admin = ?";
$this->addParam('i', $value);
break;
case 'is_locked':
$aFilter[] = "a.is_locked = ?";
$this->addParam('i', $value);
break;
case 'no_fees':
$aFilter[] = "a.no_fees = ?";
$this->addParam('i', $value);
break;
}
}
}
}
if (!empty($aFilter)) {
$sql .= " WHERE ";
$sql .= implode(' AND ', $aFilter);
}
$sql .= "
ORDER BY username
LIMIT ?,?";
$this->addParam('i', $start);
$this->addParam('i', $limit);
$stmt = $this->mysqli->prepare($sql);
if ($this->checkStmt($stmt) && call_user_func_array( array($stmt, 'bind_param'), $this->getParam()) && $stmt->execute() && $result = $stmt->get_result()) {
// Add our cached shares to the users
$aUsers = array();
while ($row = $result->fetch_assoc()) {
$row['shares'] = $this->getUserShares($row['username'], $row['id']);
$aUsers[] = $row;
}
if (count($aUsers) > 0) {
return $aUsers;
}
}
return $this->sqlError();
}
/**
* Fetch all user hashrates based on shares and archived shares
* Store it in cache, also keep a copy of the data internally to
* return it for further processing
* @return data array Set of all user stats
**/
public function fetchAllUserMiningStats($interval=180) {
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("
SELECT
a.id AS id,
a.username AS account,
COUNT(DISTINCT t1.username) AS workers,
IFNULL(SUM(t1.difficulty), 0) AS shares,
ROUND(SUM(t1.difficulty) / ?, 2) AS sharerate,
IFNULL(AVG(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty)), 0) AS avgsharediff
FROM (
SELECT
id,
IF(difficulty = 0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty) AS difficulty,
username
FROM " . $this->share->getTableName() . "
WHERE time > DATE_SUB(now(), INTERVAL ? SECOND) AND our_result = 'Y'
UNION
SELECT
share_id,
IF(difficulty = 0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty) AS difficulty,
username
FROM " . $this->share->getArchiveTableName() . "
WHERE time > DATE_SUB(now(), INTERVAL ? SECOND) AND our_result = 'Y'
) AS t1
LEFT JOIN " . $this->user->getTableName() . " AS a
ON SUBSTRING_INDEX( t1.username, '.', 1 ) = a.username
WHERE a.id IS NOT NULL
GROUP BY account
ORDER BY shares DESC
");
if ($this->checkStmt($stmt) && $stmt->bind_param("iii", $interval, $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) {
$aData = array();
while ($row = $result->fetch_assoc()) {
$aData['data'][$row['id']] = $row;
$aData['data'][$row['id']]['hashrate'] = $this->coin->calcHashrate($row['shares'], $interval);
}
$this->allUserMiningStats = $aData;
return $this->memcache->setStaticCache(STATISTICS_ALL_USER_HASHRATES, $aData, 600);
} else {
return $this->sqlError();
}
}
/**
* Store our gathered data into our statistic table for users
* @param aData array Data created by fetchAllUserMiningStats
* @return bool true or false
**/
public function storeAllUserMiningStatsSnapshot($aData) {
$this->debug->append("STA " . __METHOD__, 4);
if (!isset($aData['data'])) return false;
// initilize
$timestamp = time(); // Store all entries with the same timestamp to reduce cardinality
$ok = 0;
$failed = 0;
foreach ($aData['data'] as $key => $aUserData) {
$stmt = $this->mysqli->prepare("
INSERT INTO " . $this->getUserStatsTableName() . "
( account_id, hashrate, workers, sharerate, timestamp ) VALUES ( ?, ?, ?, ?, ?)");
if ($this->checkStmt($stmt) && $stmt->bind_param("ididi", $aUserData['id'], $aUserData['hashrate'], $aUserData['workers'], $aUserData['sharerate'], $timestamp) && $stmt->execute() ) {
$ok++;
} else {
$failed++;
}
}
return array('ok' => $ok, 'failed' => $failed);
}
/**
* Fetch unpaid PPS shares for an account
* @param username string Username
* @param account_id int User ID
* @param last_paid_pps_id int Last paid out share by pps_payout cron
* @return data int Sum of unpaid diff1 shares
**/
public function getUserUnpaidPPSShares($username, $account_id=NULL, $last_paid_pps_id) {
$this->debug->append("STA " . __METHOD__, 4);
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
IFNULL(SUM(IF(difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty)), 0) AS total
FROM " . $this->share->getTableName() . "
WHERE username LIKE ?
AND id > ?
AND our_result = 'Y'");
$username = $username . ".%";
if ($this->checkStmt($stmt) && $stmt->bind_param("si", $username, $last_paid_pps_id) && $stmt->execute() && $result = $stmt->get_result() )
return $this->memcache->setCache(__FUNCTION__ . $account_id, $result->fetch_object()->total);
return $this->sqlError();
}
/**
* Get Shares per x interval by user
* @param username string username
* @param $account_id int account id
* @return data integer Current Sharerate in diff1 shares/s
**/
public function getUserMiningStats($username, $account_id=NULL, $interval=180) {
$this->debug->append("STA " . __METHOD__, 4);
// Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL
if ($this->getGetCache() && $data = $this->memcache->getStatic(STATISTICS_ALL_USER_HASHRATES)) {
if (array_key_exists($account_id, $data['data'])) {
$retData['hashrate'] = $data['data'][$account_id]['hashrate'];
$retData['sharerate'] = $data['data'][$account_id]['sharerate'];
$retData['avgsharediff'] = $data['data'][$account_id]['avgsharediff'];
return $retData;
}
return array('hashrate' => (float)0, 'sharerate' => (float)0, 'avgsharediff' => (float)0);
}
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
IFNULL(SUM(difficulty) / ?, 0) AS sharerate,
IFNULL(SUM(difficulty), 0) AS shares,
IFNULL(AVG(difficulty), 0) AS avgsharediff
FROM (
SELECT
id, our_result, IF(difficulty = 0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty) AS difficulty
FROM
" . $this->share->getTableName() . "
WHERE username LIKE ?
AND time > DATE_SUB(now(), INTERVAL ? SECOND)
AND our_result = 'Y'
UNION
SELECT
share_id, our_result, IF(difficulty = 0, POW(2, (" . $this->config['difficulty'] . " - 16)), difficulty) AS difficulty
FROM
" . $this->share->getArchiveTableName() . "
WHERE username LIKE ?
AND time > DATE_SUB(now(), INTERVAL ? SECOND)
AND our_result = 'Y'
) AS temp");
$username = $username . ".%";
if ($this->checkStmt($stmt) && $stmt->bind_param("isisi", $interval, $username, $interval, $username, $interval) && $stmt->execute() && $result = $stmt->get_result() ) {
$aData = $result->fetch_assoc();
$aData['hashrate'] = $this->coin->calcHashrate($aData['shares'], $interval);
return $this->memcache->setCache(__FUNCTION__ . $account_id, $aData);
}
return $this->sqlError();
}
/**
* get our top contributors for either shares or hashrate
* @param type string shares or hashes
* @param limit int Limit result to $limit
* @return data array Users with shares, account or hashrate, account
**/
public function getTopContributors($type='shares', $limit=15) {
$this->debug->append("STA " . __METHOD__, 4);
switch ($type) {
case 'shares':
if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $type . $limit)) return $data;
if ($data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) {
// Use global cache to build data, if we have any data there
if (!empty($data['data']) && is_array($data['data'])) {
foreach($data['data'] as $key => $aUser) {
$shares[$key] = $aUser['valid'];
$username[$key] = $aUser['username'];
}
array_multisort($shares, SORT_DESC, $username, SORT_ASC, $data['data']);
$count = 0;
foreach ($data['data'] as $key => $aUser) {
if ($count == $limit) break;
$count++;
$data_new[$key]['shares'] = $aUser['valid'];
$data_new[$key]['account'] = $aUser['username'];
$data_new[$key]['donate_percent'] = $aUser['donate_percent'];
$data_new[$key]['is_anonymous'] = $aUser['is_anonymous'];
}
return $data_new;
}
}
// No cached data, fallback to SQL ONLY if we don't use memcache
if ($this->config['memcache']['enabled'] && $this->config['memcache']['force']['contrib_shares']) {
// Do not use SQL queries and a second layer of caching
$this->debug->append('Skipping SQL queries due to config option', 4);
return false;
}
$stmt = $this->mysqli->prepare("
SELECT
a.username AS account,
a.donate_percent AS donate_percent,
a.is_anonymous AS is_anonymous,
IFNULL(SUM(IF(s.difficulty=0, POW(2, (" . $this->config['difficulty'] . " - 16)), s.difficulty)), 0) AS shares
FROM " . $this->share->getTableName() . " AS s
LEFT JOIN " . $this->user->getTableName() . " AS a
ON SUBSTRING_INDEX( s.username, '.', 1 ) = a.username
WHERE our_result = 'Y'
GROUP BY account
ORDER BY shares DESC
LIMIT ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result())
return $this->memcache->setCache(__FUNCTION__ . $type . $limit, $result->fetch_all(MYSQLI_ASSOC));
return $this->sqlError();
break;
case 'hashes':
if ($this->getGetCache() && $data = $this->memcache->getStatic(__FUNCTION__ . $type . $limit)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
a.username AS account,
a.donate_percent AS donate_percent,
a.is_anonymous AS is_anonymous,
IFNULL(SUM(t1.difficulty), 0) AS shares
FROM
(
SELECT id, IFNULL(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0) AS difficulty, username FROM " . $this->share->getTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) AND our_result = 'Y'
UNION
SELECT share_id, IFNULL(IF(difficulty=0, pow(2, (" . $this->config['difficulty'] . " - 16)), difficulty), 0) AS difficulty, username FROM " . $this->share->getArchiveTableName() ." WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) AND our_result = 'Y'
) AS t1
LEFT JOIN " . $this->user->getTableName() . " AS a
ON SUBSTRING_INDEX( t1.username, '.', 1 ) = a.username
GROUP BY account
ORDER BY shares DESC LIMIT ?");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) {
$aData = array();
$count = 0;
while ($row = $result->fetch_assoc()) {
$aData[$count] = $row;
$aData[$count]['hashrate'] = $this->coin->calcHashrate($row['shares'], 600);
$count++;
}
return $this->memcache->setStaticCache(__FUNCTION__ . $type . $limit, $aData);
}
return $this->sqlError();
break;
}
}
/**
* get Hourly hashrate for a user
* @param username string Username
* @param $account_id int account id
* @return data array NOT FINISHED YET
**/
public function getHourlyMiningStatsByAccount($account_id, $format='array', $days = 1) {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
timestamp,
FROM_UNIXTIME(timestamp, '%Y-%m-%d %H:%i') AS time,
AVG(hashrate) AS hashrate,
AVG(workers) AS workers,
AVG(sharerate) AS sharerate
FROM " . $this->getUserStatsTableName() . "
WHERE FROM_UNIXTIME(timestamp) >= DATE_SUB(NOW(), INTERVAL $days DAY)
AND account_id = ?
GROUP BY DAY(FROM_UNIXTIME(timestamp)), HOUR(FROM_UNIXTIME(timestamp))");
if ($this->checkStmt($stmt) && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result()) {
$aData = $result->fetch_all(MYSQLI_ASSOC);
if ($format == 'json') $aData = json_encode($aData);
return $this->memcache->setCache(__FUNCTION__ . $account_id . $format, $aData);
}
return $this->sqlError();
}
/**
* get user estimated payouts based on share counts
* @param value1 mixed Round shares OR share rate
* @param value2 mixed User shares OR share difficulty
* @param dDonate double User donation setting
* @param bNoFees bool User no-fees option setting
* @return aEstimates array User estimations
**/
public function getUserEstimates($value1, $value2, $dDonate, $bNoFees, $ppsvalue=0) {
$this->debug->append("STA " . __METHOD__, 4);
if ($this->config['payout_system'] != 'pps') {
if (@$value1['valid'] > 0 && @$value2['valid'] > 0) {
$this->config['reward_type'] == 'fixed' ? $reward = $this->config['reward'] : $reward = $this->block->getAverageAmount();
$aEstimates['block'] = round(( (float)$value2['valid'] / (float)$value1['valid'] ) * (float)$reward, 8);
$bNoFees == 0 ? $aEstimates['fee'] = round(((float)$this->config['fees'] / 100) * (float)$aEstimates['block'], 8) : $aEstimates['fee'] = 0;
$aEstimates['donation'] = round((( (float)$dDonate / 100) * ((float)$aEstimates['block'] - (float)$aEstimates['fee'])), 8);
$aEstimates['payout'] = round((float)$aEstimates['block'] - (float)$aEstimates['donation'] - (float)$aEstimates['fee'], 8);
} else {
$aEstimates['block'] = 0;
$aEstimates['fee'] = 0;
$aEstimates['donation'] = 0;
$aEstimates['payout'] = 0;
}
} else {
// Hack so we can use this method for PPS estimates too
// value1 = shares/s
// value2 = avg share difficulty
if (@$value1 > 0 && @$value2 > 0) {
$hour = 60 * 60;
$pps = $value1 * $value2 * $ppsvalue;
$aEstimates['hours1'] = $pps * $hour;
$aEstimates['hours24'] = $pps * 24 * $hour;
$aEstimates['days7'] = $pps * 24 * 7 * $hour;
$aEstimates['days14'] = $pps * 14 * 24 * $hour;
$aEstimates['days30'] = $pps * 30 * 24 * $hour;
} else {
$aEstimates['hours1'] = 0;
$aEstimates['hours24'] = 0;
$aEstimates['days7'] = 0;
$aEstimates['days14'] = 0;
$aEstimates['days30'] = 0;
}
}
return $aEstimates;
}
/**
* Get pool stats last 24 hours
* @param limit int Last number of hours
* @return array
**/
public function getPoolStatsHours($hour=24) {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__ . $hour)) return $data;
$stmt = $this->mysqli->prepare("
SELECT
IFNULL(COUNT(id), 0) as count,
IFNULL(AVG(difficulty), 0) as average,
IFNULL(SUM(shares), 0) as shares,
IFNULL(SUM(amount), 0) as rewards
FROM " . $this->block->getTableName() . "
WHERE FROM_UNIXTIME(time) > DATE_SUB(now(), INTERVAL ? HOUR)
AND confirmations >= 1");
if ($this->checkStmt($stmt) && $stmt->bind_param("i", $hour) && $stmt->execute() && $result = $stmt->get_result()) {
$aData = $result->fetch_assoc();
$aData['expected'] = $this->coin->calcEstaimtedShares($aData['average']);
return $this->memcache->setCache(__FUNCTION__ . $hour, $aData);
}
return $this->sqlError();
}
/**
* Caclulate estimated shares based on network difficulty and pool difficulty
* @param dDiff double Network difficulty
* @return shares integer Share count
**/
public function getEstimatedShares($dDiff) {
return $this->coin->calcEstaimtedShares($dDiff);
}
/**
* Get the Expected Time per Block in the whole Network in seconde
* @return seconds double Seconds per Block
*/
public function getExpectedTimePerBlock($type='network',$hashrate = 0){
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
if ($this->bitcoin->can_connect() === true) {
if ($type == 'network') {
$hashrate = $this->bitcoin->getnetworkhashps();
} else {
// We need hashes/second and expect khash as input
$hashrate = $hashrate * 1000;
}
$dDifficulty = $this->bitcoin->getdifficulty();
} else {
$hashrate = 1;
$dDifficulty = 1;
}
if ($hashrate <= 0) $hashrate = 1;
return $this->memcache->setCache(__FUNCTION__ . '_' . $type, $this->coin->calcNetworkExpectedTimePerBlock($dDifficulty, $hashrate));
}
/**
* Get the Expected next Difficulty
* @return difficulty double Next difficulty
**/
public function getExpectedNextDifficulty(){
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
if ($this->bitcoin->can_connect() === true) {
$dDifficulty = $this->bitcoin->getdifficulty();
$dNetworkHashrate = $this->bitcoin->getnetworkhashps();
} else {
$dDifficulty = 1;
$dNetworkHashrate = 1;
}
return $this->memcache->setCache(__FUNCTION__, $this->coin->calcExpectedNextDifficulty($dDifficulty, $dNetworkHashrate));
}
/**
* Get Number of blocks until next difficulty change
* @return blocks int blocks until difficulty change
**/
public function getBlocksUntilDiffChange(){
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
if ($this->bitcoin->can_connect() === true) {
$iBlockcount = $this->bitcoin->getblockcount();
} else {
$iBlockcount = 1;
}
return $this->memcache->setCache(__FUNCTION__, $this->config['coindiffchangetarget'] - ($iBlockcount % $this->config['coindiffchangetarget']));
}
/**
* Get current PPS value
* @return value double PPS Value
**/
public function getPPSValue() {
// Fetch RPC difficulty
if ($this->bitcoin->can_connect() === true) {
$dDifficulty = $this->bitcoin->getdifficulty();
} else {
$dDifficulty = 1;
}
if ($this->config['pps']['reward']['type'] == 'blockavg' && $this->block->getBlockCount() > 0) {
$pps_reward = round($this->block->getAvgBlockReward($this->config['pps']['blockavg']['blockcount']));
} else {
if ($this->config['pps']['reward']['type'] == 'block') {
if ($aLastBlock = $this->block->getLast()) {
$pps_reward = $aLastBlock['amount'];
} else {
$pps_reward = $this->config['pps']['reward']['default'];
}
} else {
$pps_reward = $this->config['pps']['reward']['default'];
}
}
return round($this->coin->calcPPSValue($pps_reward, $dDifficulty), 12);
}
/**
* Get all currently active users in the past 2 minutes
* @param interval int Time in seconds to fetch shares from
* @return data mixed int count if any users are active, false otherwise
**/
public function getCountAllActiveUsers($interval=120) {
$this->debug->append("STA " . __METHOD__, 4);
if ($data = $this->memcache->get(__FUNCTION__)) return $data;
$stmt = $this->mysqli->prepare("
SELECT COUNT(DISTINCT(SUBSTRING_INDEX( `username` , '.', 1 ))) AS total
FROM " . $this->share->getTableName() . "
WHERE our_result = 'Y'
AND time > DATE_SUB(now(), INTERVAL ? SECOND)");
if ($this->checkStmt($stmt) && $stmt->bind_param('i', $interval) && $stmt->execute() && $result = $stmt->get_result())
return $this->memcache->setCache(__FUNCTION__, $result->fetch_object()->total);
return $this->sqlError();
}
/**
* Purge older entries from our statistics_users table
**/
public function purgeUserStats($days = 1) {
// Fallbacks if unset
$stmt = $this->mysqli->prepare("DELETE FROM " . $this->getUserStatsTableName() . " WHERE FROM_UNIXTIME(timestamp) <= DATE_SUB(NOW(), INTERVAL ? DAY)");
if ($this->checkStmt($stmt) && $stmt->bind_param('i', $days) && $stmt->execute())
return $stmt->affected_rows;
return $this->sqlError();
}
}
$statistics = new Statistics();
$statistics->setDebug($debug);
$statistics->setMysql($mysqli);
$statistics->setShare($share);
$statistics->setUser($user);
$statistics->setBlock($block);
$statistics->setMemcache($memcache);
$statistics->setConfig($config);
$statistics->setBitcoin($bitcoin);
$statistics->setErrorCodes($aErrorCodes);
$statistics->setCoin($coin);