forked from FoldingCoin/Calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.php
139 lines (106 loc) · 3.43 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
<?php
//snapshot.php - Ingest weekly FAH snapshot into database
//Copyrght © 2014 FoldingCoin, All Rights Reserved
include('functions.php');
include('db.php');
$db=dbConnect();
$mode='liveDaily';
$ourTeam=226728;
echo "========Beginning $mode mode run for team $ourTeam at ".date('c')."\n";
if($mode=='test'){
$delayDays=0;
$delayHours=0;
$delayMinutes=1;
}elseif($mode=='live'){
$delayDays=6;
$delayHours=23;
$delayMinutes=45;
}elseif($mode=='testDaily'){
$delayDays=0;
$delayHours=0;
$delayMinutes=1;
}elseif($mode=='liveDaily'){
$delayDays=0;
$delayHours=23;
$delayMinutes=45;
}
$delayTime=($delayDays*86400)+($delayHours*3600)+($delayMinutes*60);
//find last $snapshotId
$lastSnapId='';
$lastSnapQuery="SELECT * FROM fahcredits WHERE mode = '$mode' ORDER BY timestamp DESC LIMIT 1";
echo "$lastSnapQuery\n";
if ($lastSnapResults = $db->query($lastSnapQuery)) {
while( $lastSnapRow = $lastSnapResults->fetch_assoc() ){
$lastSnapId = $lastSnapRow['snapshotId'];
$lastSnapTimestamp=$lastSnapRow['timestamp'];
echo "from db $lastSnapId\n";
}
}
$snapId=$lastSnapId+1;
echo "calculated $snapId database gave $lastSnapId\n";
$timestamp=time();
$NextRun=$lastSnapTimestamp+$delayTime;
if($NextRun>$timestamp){
echo "Too soon to run since last time, $NextRun > $timestamp.\n";
exit();
}
$insertTimestamp=$timestamp;
$statsUrls[]='http://fah-web.stanford.edu/daily_user_summary.txt.bz2';
$statsUrls[]='http://fah-web.stanford.edu/daily_team_summary.txt.bz2';
$snapshotStamp=date('Ymd',$timestamp);
foreach($statsUrls as $statsUrl){
list($discard,$localFile)=explode('edu/',$statsUrl);
$localFile=$snapshotStamp.$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";
}
}
echo "extracting file for snapId $snapId...\n";
$localFileBase='daily_user_summary.txt.bz2';
$localFile=$snapshotStamp.$localFileBase;
///////////////////take this out after initial snapshot ingest
//$localFile='fahSnapshots/snap1.txt.bz2';
$bz = bzopen($localFile, "r") or die("Couldn't open $localFile");
$lines='';
$lastFileChunk='';
$fileChunk='';
while (!feof($bz)) {
//echo "{{{{{{{chunkbreak $lastFileChunk }}}}}}}";
$fileChunk = $lastFileChunk.bzread($bz, 4096);
$lines=explode("\n",$fileChunk);
$lastFileChunk=$lines[count($lines)-1];
foreach($lines as $line){
if($line!=$lastFileChunk){
//echo "Raw File - $line\n";
if(!preg_match("/ /",$line)){
//echo "Tab - $line\n";
list($name,$credits,$workUnits,$team)=explode("\t",$line);
if($team==$ourTeam){
//echo "$name has $credits credits.\n";
$snapInsertQuery = "INSERT INTO fahcredits (snapshotId,timestamp,address,cumulativeCredits,mode) VALUES($snapId,$insertTimestamp,'$name',$credits,'$mode')";
echo "$snapInsertQuery;\n";
$db->query($snapInsertQuery);
}
}elseif(preg_match("/ PDT /",$line)){
//echo "No Tab - $line\n";
//Mon Jul 21 20:20:01 PDT 2014
$insertTimestamp=strtotime($line);
//echo "UNIX Time $insertTimestamp\n";
//echo "convert back to formatted date ".date("c",$insertTimestamp)."\n";
/*list($wday,$monText,$day,$time,$tzone,$year)=explode(" ",$line);
list($hour,$min,$sec)=explode(":",$time);
$insertTimestamp=mktime($hour,$min,$sec,$day,$year);*/
}
}
}
}
bzclose($bz);
?>