This repository has been archived by the owner on May 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.php
executable file
·211 lines (167 loc) · 7.52 KB
/
stats.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
<?php
//stats.php - Generate Payout Stats
//Copyright © 2015 FoldingCoin Inc., All Rights Reserved
$projBase='/home/fldcPlatform/mergedFolding/';
$projBin='bin';
$reportsDir='/var/www/html/platformReports/';
include($projBase.$projBin.'/includes/functions.php');
include($projBase.$projBin.'/includes/classes.php');
include($projBase.'/config/db.php');
$runStart=time();
echo "Start ".date("c",$runStart)."\n";
$platformAssets=populateAssets();
foreach($platformAssets as $platformAsset){
$payoutRecords='';
$assetName=$platformAsset->assetName;
$mostRecentSnapshot=getMostRecentSnapshot($assetName,$mode);
///
//$mostRecentSnapshot=1424064001;
///
$payoutRecords=populatePayoutRecords($assetName,$mostRecentSnapshot,$mode);
//var_dump($payoutRecords);
if($payoutRecords!=""){
echo "Now Processing Stats for $assetName\n";
writePayoutReport($platformAsset,$payoutRecords,$mostRecentSnapshot,$reportsDir.$assetName,$mode,$runStart);
writeStatsFile($assetName,$payoutRecords,$mostRecentSnapshot,$reportsDir.$assetName,$mode);
createHtmlIndexes($platformAsset,$reportsDir.$assetName);
}
}
function writePayoutReport($platformAsset,$payoutRecords,$mostRecentSnapshot,$reportsDir,$mode,$runStart){
$assetName=$platformAsset->assetName;
$assetLogoUrl=$platformAsset->logoUrl;
$reportYYYYMMDD=date("Ymd",$mostRecentSnapshot);
$reportFileName=$reportsDir.'/'.$reportYYYYMMDD.'-'."payoutReport.html";
//write the HTML for the folder credits/coins report
$reportHtml='';
$reportHtml=$reportHtml."<html><head><title>$assetName :: Distribution Report</title></head>\n";
$reportHtml=$reportHtml."<body><h2>$assetName :: Distribution Report</h2>\n";
$reportHtml=$reportHtml."<img src=$assetLogoUrl>\n";
$reportHtml=$reportHtml."<p>Distribution Snapshot dated ".date("c",$mostRecentSnapshot)."</p>\n";
$reportTotalCredits=0;
$reportTotalTokens=0;
$reportTotalPct=0;
//figure out totals
foreach($payoutRecords as $payoutRecord){
$reportTotalCredits=$reportTotalCredits+$payoutRecord->periodCredits;
$reportTotalTokens=$reportTotalTokens+$payoutRecord->periodTokens;
}
if($reportTotalCredits==0){
$reportTotalCredits=.00000001;
}
$tokensPerCredit=$reportTotalTokens/$reportTotalCredits;
$reportHtml=$reportHtml."<p>In this awarding period, 1 folding credit is worth ".number_format($tokensPerCredit,8)." $assetName tokens.</p>\n";
$reportHtml=$reportHtml."<p>Payouts:</p>\n<table border = 1><tr><th>Folder Name</th><th>Folder Team</th><th>Folder Address</th><th>Credits Folded This Period</th><th>Percentage</th><th>$assetName Paid</th></tr>\n";
//then go through the payouts one at a time
foreach($payoutRecords as $payoutRecord){
$address=$payoutRecord->address;
$periodTokens=$payoutRecord->periodTokens;
$periodCredits=$payoutRecord->periodCredits;
$friendlyName=$payoutRecord->friendlyName;
$fahTeam=$payoutRecord->fahTeam;
if($reportTotalTokens==0){
$reportTotalTokens=0.00000001;
}
$folderPct=number_format((($periodTokens/$reportTotalTokens)*100),2);
$reportTotalPct=$reportTotalPct+$folderPct;
if($periodTokens>0){
$reportHtml=$reportHtml."<tr><td>$friendlyName</td><td>$fahTeam</td><td>$address</td><td>".number_format($periodCredits)."</td><td>$folderPct%</td><td>".number_format($periodTokens,8)."</td></tr>\n";
}
}
$reportHtml=$reportHtml."<tr><td></td><td></td><td>Totals</td><td>".number_format($reportTotalCredits)."</td><td>".round($reportTotalPct,0)."</td><td>".number_format(round($reportTotalTokens,0))."</td>\n";
$reportHtml=$reportHtml."</table>\n";
$reportHtml=$reportHtml."<p>This report generated ".date("c",$runStart)."</p>\n";
$reportHtml=$reportHtml."</body></html>\n";
$reportHtmlFileHandle=fopen($reportFileName,"w");
fwrite($reportHtmlFileHandle,$reportHtml);
fclose($reportHtmlFileHandle);
}
function writeStatsFile($assetName,$payoutRecords,$mostRecentSnapshot,$reportsDir,$mode){
$statFileName=$reportsDir."/stats.txt";
$statCode='';
$activeFolders=0;
$allFolders=0;
$totalFoldedCredits=0;
foreach($payoutRecords as $payoutRecord){
if($payoutRecord->periodCredits>0){
$activeFolders=$activeFolders+1;
}
$totalFoldedCredits=$totalFoldedCredits+$payoutRecord->periodCredits;
$allFolders=$allFolders+1;
}
$allCredits=getAllCredits($mostRecentSnapshot,$assetName,$mode);
$statCode=$statCode."|<b>All $assetName Folders</b>\n";
$statCode=$statCode."Folders: |$activeFolders ($allFolders)\n";
$statCode=$statCode."Awarding Period Credits: |".number_format($totalFoldedCredits)."\n";
$statCode=$statCode."Credits: |".number_format($allCredits)."";
//echo "=begin stats====\n";
//echo "$statCode\n";
//echo "=end stats====\n";
$statFileHandle=fopen($statFileName,"w");
fwrite($statFileHandle,$statCode);
fclose($statFileHandle);
}
function getAllCredits($mostRecentSnapshot,$assetName,$mode){
$currentSnapRecords=populateCurrentSnapRecords($mostRecentSnapshot,$assetName,$mode);
$allCredits=0;
foreach($currentSnapRecords as $currentSnapRecord){
$allCredits=$allCredits+$currentSnapRecord->cumulativeCredits;
}
return($allCredits);
}
function populatePayoutRecords($assetName,$mostRecentSnapshot,$mode){
$db=dbConnect();
//find all the payouts for this timestamp and asset
$payoutsQuery="SELECT * FROM fldcPlatform.platformPayouts WHERE payoutTimestamp = $mostRecentSnapshot AND assetName = '$assetName' AND mode = '$mode' ORDER BY payoutCredits DESC";
if ($payoutsResults=$db->query($payoutsQuery)) {
while($payoutsRow=$payoutsResults->fetch_assoc()){
$assetName=$payoutsRow['assetName'];
$address=$payoutsRow['address'];
$snapshotTimestamp=$payoutsRow['payoutTimestamp'];
$friendlyName=$payoutsRow['friendlyName'];
$fahName=$payoutsRow['fahName'];
$fahTeam=$payoutsRow['fahTeam'];
$fahSHA256=$payoutsRow['fahSHA256'];
$cumulativeCredits=$payoutsRow['cumulativeCredits'];
$mode=$payoutsRow['mode'];
$payoutCredits=$payoutsRow['payoutCredits'];
$payoutTokens=$payoutsRow['payoutTokens'];
$normalizedFolder=new normalizedFolder();
$normalizedFolder->assetName=$assetName;
$normalizedFolder->address=$address;
$normalizedFolder->friendlyName=$friendlyName;
$normalizedFolder->fahName=$fahName;
$normalizedFolder->fahTeam=$fahTeam;
$payoutRecords[]=new payoutRecord($snapshotTimestamp,$assetName,$normalizedFolder,$mode,$payoutCredits,$payoutTokens);
}
}
if(isset($payoutRecords)){
return($payoutRecords);
}else{
return('');
}
}
function createHtmlIndexes($platformAsset,$reportsDir){
$assetName=$platformAsset->assetName;
$logoUrl=$platformAsset->logoUrl;
$assetIndexFileName=$reportsDir."/index.html";
$assetIndexHtml='';
$assetIndexHtml=$assetIndexHtml."<html><head><title>$assetName :: Distribution History</title></head>
<body><h2>$assetName :: Distribution Summary</h2>
<img src=$logoUrl>\n";
$files=scandir($reportsDir,1);
foreach($files as $file){
if(!is_dir($file) AND preg_match("/html/",$file) AND !preg_match("/reports/",$file) AND !preg_match("/index/",$file)){
$yyyy=substr($file,0,4);
$mm=substr($file,4,2);
$dd=substr($file,6,2);
$unixtime=mktime(8,0,5,$mm,$dd,$yyyy);
$month=date("F",$unixtime);
$assetIndexHtml=$assetIndexHtml."<p><a href=$file>Payouts $month $dd, $yyyy</a></p>\n";
}
}
$assetIndexHtml=$assetIndexHtml."</body></html>\n";
$assetIndexFileHandle=fopen($assetIndexFileName,"w");
fwrite($assetIndexFileHandle,$assetIndexHtml);
fclose($assetIndexFileHandle);
}
?>