-
Notifications
You must be signed in to change notification settings - Fork 56
/
lib.userstat.php
114 lines (84 loc) · 3.37 KB
/
lib.userstat.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
<?php
/* Copyright (C) 2013 Linus Unnebäck <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace wizstats\userstat;
function getAllBalances() {
if($balanacesjsondec = apc_fetch('balance')) {
} else {
$balance = file_get_contents("/var/lib/eligius/$serverid/balances.json");
$balanacesjsondec = json_decode($balance, true);
// Store Cache for 10 minutes
apc_store('balance', $balanacesjsondec, 600);
}
return $balanacesjsondec;
}
function getAllBalancesSM() {
if($balanacesjsondecSM = apc_fetch('balance_smpps')) {
} else {
$balanacesjsonSM = file_get_contents("/var/lib/eligius/$serverid/smpps_lastblock.json");
$balanacesjsondecSM = json_decode($balanacesjsonSM,true);
// Store Cache forever (10 days)
apc_store('balance_smpps', $balanacesjsondecSM, 864000);
}
return $balanacesjsondecSM;
}
function getBalanceForUser($link, $user_id, $givenuser) {
global $psqlschema, $serverid;
$return = array();
$balanacesjsondec = getAllBalances();
$mybal = $balanacesjsondec[$givenuser];
if ($mybal) {
$return['bal'] = (isset($mybal["balance"] )) ? $mybal["balance"] : 0;
$return['ec'] = (isset($mybal["credit"] )) ? $mybal["credit"] : 0;
$return['everpaid'] = (isset($mybal["everpaid"])) ? $mybal["everpaid"] : 0;
$return['lbal'] = (isset($mybal["included_balance_estimate"])) ? ($return['bal'] - $mybal["included_balance_estimate"]) : $return['bal'];
$return['lec'] = (isset($mybal["included_credit_estimate"] )) ? ($return['ec'] - $mybal["included_credit_estimate"] ) : $return['ec'];
$return['datadate'] = $mybal["newest"];
$return['balupdate'] = $mybal["last_balance_update"];
} else {
# fall back to sql
$sql = "select * from $psqlschema.stats_balances where server=$serverid and user_id=$user_id order by time desc limit 1";
$result = pg_exec($link, $sql);
$numrows = pg_numrows($result);
if (!$numrows) {
$return['bal'] = "N/A";
$return['ec'] = "N/A";
$return['lbal'] = "N/A";
$return['datadate'] = "N/A";
} else {
$row = pg_fetch_array($result, 0);
$return['bal'] = $row["balance"];
$return['ec'] = $row["credit"];
$return['lbal'] = "N/A";
$return['datadate'] = $row["time"];
$return['everpaid'] = $row["everpaid"];
}
}
return $return;
}
function getBalanceForUserSM($link, $user_id, $givenuser) {
$balanacesjsondecSM = getAllBalancesSM();
$mybalSM = $balanacesjsondecSM[$givenuser];
if ($mybalSM) {
# SMPPS credit needed to be halved for the pool to be statistically viable
$smppsec = $mybalSM["credit"];
$smppshalf = $mybalSM["credit"]/2;
$smppsec -= $smppshalf;
} else {
$smppsec = 0;
}
return array('smppsec' => $smppsec);
}