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
/
Copy pathsnapshot.php
executable file
·185 lines (143 loc) · 6.12 KB
/
snapshot.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
<?php
//snapshot.php - Store snapshot of Folding@home public stats
//Copyright © 2015 FoldingCoin Inc., All Rights Reserved
$projBase='/home/fldcPlatform/mergedFolding/';
$projBin='bin';
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();
$statsUrls[]='http://fah-web.stanford.edu/daily_team_summary.txt.bz2';
$statsUrls[]='http://fah-web.stanford.edu/daily_user_summary.txt.bz2';
$todayYYYYMMDD=date("Ymd");
if(preg_match("/test/",$mode)){
//$todayYYYYMMDD='20141231';
}
///date debug
//$todayYYYYMMDD=20150216;
///date debug
echo "run $todayYYYYMMDD\n";
//Download the files from FAH
foreach($statsUrls as $statsUrl){
list($discard,$localFile)=explode('edu/',$statsUrl);
$localFile=$projBase.'FAHdata/'.$todayYYYYMMDD.$localFile;
echo "$localFile\n";
if(!file_exists($localFile)){
echo "Downloading $localFile...\n";
if (!copy($statsUrl, $localFile)) {
echo "failed to copy $statsUrl...\n";
}else{
echo "Copied $statsUrl to $localFile.\n";
}
}else{
echo "$localFile exists, not downloading ...\n";
}
}
//Open the bzipped file
$bz = bzopen($localFile, "r") or die("Couldn't open $localFile");
$lines='';
$lastFileChunk='';
$fileChunk='';
$insertTimestamp='notfound';
//go through the bz file
while (!feof($bz)) {
//take 4096 bytes at a time from the bz
//now here's the tricky part, if this is the 2nd,3rd,4th chunk and so on...
// ...we got a "lastFileChunk" which is the final line, which will be a partial line
// becuase it's partial, we add it at the beginning of the next 4096 bytes
// this maintains continuity of the lines
// neat trick! I'm proud of myself! <grin>
$fileChunk = $lastFileChunk.bzread($bz, 4096);
//spit the lines from the 4096 bytes on carriage returns \n
$lines=explode("\n",$fileChunk);
//count the lines, and store the last line for the next pass of 4096 bytes
// count the lines and subtract one, as the array starts counting at 0
$lastFileChunk=$lines[count($lines)-1];
foreach($lines as $line){
$normalizedFolders='';
//echo "processing $line\n";
//don't process the lastFileChunk
if($line!=$lastFileChunk){
if(!preg_match("/ /",$line)){
//echo "insertTimestamp $insertTimestamp ".date("c",$insertTimestamp)."$line\n";
$normalizedFolder=new normalizedFolder();
//if we don't have a valid bitcoin address, see if it's an ownTeam folder
if(!checkAddress($normalizedFolder->address)){
$normalizedFolder->ownTeamFinder($line,$platformAssets);
if(checkAddress($normalizedFolder->address)){
$normalizedFolders[]=$normalizedFolder;
}
}
//if we don't have a valid bitcoin address, it wasn't an ownTeam folder so try for an anyTeam folder
if(!checkAddress($normalizedFolder->address)){
$normalizedFolder->anyTeamFinder($line,$platformAssets);
if(checkAddress($normalizedFolder->address)){
$normalizedFolders[]=$normalizedFolder;
}
}
//if we still don't have a valid bitcoin address, it could be an _ALL_ folder
if(!checkAddress($normalizedFolder->address) AND preg_match("/\_ALL\_/",$line)){
foreach($platformAssets as $platformAsset){
$normalizedFolder=new normalizedFolder();
$normalizedFolder->allAssetsFinder($line,$platformAsset);
if(checkAddress($normalizedFolder->address)){
$normalizedFolders[]=$normalizedFolder;
}
}
}
if($normalizedFolders!=''){
foreach($normalizedFolders as $normalizedFolder){
//if we have a valid bitcoin address, prepare a snapshot and insert it
if(checkAddress($normalizedFolder->address)){
$folderRecords=buildSnapshotRecords($normalizedFolder,$insertTimestamp,$mode);
//echo "Found a good folder\n";
//var_dump($normalizedFolder);
foreach($folderRecords as $folderRecord){
var_dump($folderRecord);
$insertRecords[]=$folderRecord;
}
}
}
}
//builds the UNIX timestamp from the first line of the FAH file
}elseif(preg_match("/ PDT /",$line) OR preg_match("/ PST /",$line)){
$insertTimestamp=strtotime($line);
}
}
}
}
foreach($insertRecords as $insertRecord){
$insertResult=insertRecord($insertRecord);
}
$runEnd=time();
echo "End ".date("c",$runEnd)."\n";
$runTime=$runEnd-$runStart;
echo "script ran in $runTime secs.\n";
//end main code
function buildSnapshotRecords($normalizedFolder,$snapshotTimestamp,$mode){
$snapshotRecords='';
$recordAsset=$normalizedFolder->assetName;
$thisSnapshotRecord=new snapshotRecord($recordAsset,$normalizedFolder,$snapshotTimestamp,$mode);
$snapshotRecords[]=$thisSnapshotRecord;
return($snapshotRecords);
}
function insertRecord($insertRecord){
$insertResult='start';
//$insertQuery="INSERT INTO fldcPlatform.platformCredits (snapshotTimestamp,assetName,snaptype,address,friendlyName,fahName,fahTeam,cumulativeCredits,mode) VALUES (".$insertRecord->snapshotTimestamp.",'".$insertRecord->assetName."','".$insertRecord->snaptype."','".$insertRecord->address."','".$insertRecord->friendlyName."','".$insertRecord->fahName."',".$insertRecord->fahTeam.",".$insertRecord->cumulativeCredits.",'".$insertRecord->mode."')";
//echo "$insertQuery;\n";
$db=dbConnect();
if ($stmt = $db->prepare("INSERT INTO fldcPlatform.platformCredits (snapshotTimestamp,assetName,snaptype,address,friendlyName,fahName,fahTeam,fahSHA256,cumulativeCredits,mode) VALUES (?,?,?,?,?,?,?,?,?,?)")) {
//var_dump($insertRecord);
/* bind parameters for markers */
$stmt->bind_param("isssssisis", $insertRecord->snapshotTimestamp,$insertRecord->assetName,$insertRecord->snaptype,$insertRecord->address,$insertRecord->friendlyName,$insertRecord->fahName,$insertRecord->fahTeam,$insertRecord->fahSHA256,$insertRecord->cumulativeCredits,$insertRecord->mode);
/* execute query */
$stmt->execute();
//var_dump($stmt);
/* close statement */
$stmt->close();
}
return($insertResult);
}
?>