-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
executable file
·82 lines (70 loc) · 2.53 KB
/
functions.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
<?php
function get_records($path, $filename, $action) {
$ch = curl_init($path);
$fp = fopen($filename, $action);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// curl_exec($ch);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
fclose($fp);
}
function get_res($value) {
$res = str_replace('_', ' ', $value);
return ucwords($res);
}
function format_storage($value) {
$store = str_replace(',', '', $value);
return round($store * 1000);
}
function format_date($date) {
$pieces = explode('-', $date);
return $pieces[1] . '/' . $pieces[2] . '/' . $pieces[0];
}
function aggregate($target_path, $destination_path, $final_path = '', $write = false) {
$path = $target_path;
$files = scandir($path);
$write_method = ($write) ? 'wb' : 'a';
foreach($files as $file) {
if(!is_dir($file) && !preg_match('/^\./', $file)) {
$row = 1;
$res = '';
$capacity = '';
if (($handle = fopen($path . '/' . $file, "r")) !== FALSE) {
$months = array();
$fh = fopen($destination_path .'/' . $file, $write_method);
if($final_path) {
$if = fopen($final_path .'/' . $file, 'a');
}
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row == 1) {
// fputcsv($fh, $data);
} else {
$date = explode('/', $data[4]);
$date_parts = $date[0] . '/' . $date[2];
$months[$date_parts][] = $data[1];
}
$res = $data[0];
$capacity = $data[2];
$state = end($data);
$row++;
}
foreach($months as $key => $month) {
$monthly_avg = round(array_sum($month) / count($month));
$monthly_avg_pct = round(($monthly_avg / $capacity) * 100, 1);
fputcsv($fh, array($res, $monthly_avg, $capacity, $monthly_avg_pct, $key, $state));
if($final_path) {
fputcsv($if, array($res, $monthly_avg, $capacity, $monthly_avg_pct, $key, $state));
}
}
echo $res . "\n";
fclose($fh);
fclose($handle);
}
}
}
}