-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloggerNetParser.class.php
More file actions
212 lines (173 loc) · 8.65 KB
/
loggerNetParser.class.php
File metadata and controls
212 lines (173 loc) · 8.65 KB
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
212
<?php
class loggerNetParser{
public $inputString = '';
public $collumnMapping = array();
public $parsedDataRows = array();
public $parsedMetaRows = array();
public $generatedJson = array();
// Initialize
function __construct($input = ''){
$this->inputString = $input;
}
// Populate data variables from input string
function parseData($numMetaRows = 4){
// explode input string on line breaks
$rows = preg_split("/\\r\\n|\\r|\\n/" , $this->inputString);
// get just meta rows
$metaRows = array_slice($rows,0,$numMetaRows);
// split meta rows
foreach($metaRows as $metaRow){
$this->parsedMetaRows[] = str_getcsv($metaRow);
}
// get just data rows
$dataRows = array_slice($rows,$numMetaRows);
foreach($dataRows as $dataRow){
// last row is often blank, so don't add that
if(trim($dataRow) != ''){
$this->parsedDataRows[] = str_getcsv($dataRow);
}
}
}
// Optionally provide the index of a meta row to use as keys for the returned json rows
function generateJson($metaRowIndexForKeys = ''){
if($metaRowIndexForKeys != ''){
$preJsonArray = array();
foreach($this->parsedDataRows as $parsedDataRow){
$preJsonArray[] = array_combine($this->parsedMetaRows[$metaRowIndexForKeys],$parsedDataRow);
}
return json_encode($preJsonArray);
}
else{
return json_encode($this->parsedDataRows);
}
}
// Generate json for just one collumn of the data, usefull for javascript charts
// range is an array with lower and upper limit indexes
function generateCollumnJson($collumnIndex,$range=''){
// if range is set and we can use it
if(
is_array($range)
&& isset($range[1])
&& count($this->parsedDataRows)-1 >= $range[1]
&& isset($range[0])
&& $range[0] >= 0
&& $range[0] <= $range[1]
&& is_numeric($collumnIndex)
){
return json_encode(array_slice(array_column($this->parsedDataRows,$collumnIndex),$range[0],$range[1]-$range[0]));
}
// return the whole collumn if we've got it
elseif(count($this->parsedDataRows) && is_numeric($collumnIndex)){
return json_encode(array_column($this->parsedDataRows,$collumnIndex) );
}
}
// Same as generateCollumnJson except just returns array
function getCollumnArray($collumnIndex,$range=''){
// if range is set and we can use it
if(
is_array($range)
&& isset($range[1])
&& count($this->parsedDataRows)-1 >= $range[1]
&& isset($range[0])
&& $range[0] >= 0
&& $range[0] <= $range[1]
&& is_numeric($collumnIndex)
){
return array_slice(array_column($this->parsedDataRows,$collumnIndex),$range[0],$range[1]-$range[0]);
}
// return the whole collumn if we've got it
elseif(count($this->parsedDataRows) && is_numeric($collumnIndex)){
return array_column($this->parsedDataRows,$collumnIndex);
}
}
// Generate formatted array of a time collumn
function generateFormattedTimeCollumnArray($collumnIndex=0,$timeFormat='g:ia',$timeInputFormat='Y-m-d H:i:s',$range=''){
if(count($this->parsedDataRows) && is_numeric($collumnIndex)){
$thisCollumn = array_column($this->parsedDataRows,$collumnIndex);
foreach($thisCollumn as $timeIndex => $timeString){
if(DateTime::createFromFormat($timeInputFormat, $timeString)){
$thisCollumn[$timeIndex] = date($timeFormat,DateTime::createFromFormat($timeInputFormat, $timeString)->getTimestamp());
}
}
// if a range is set return just that portion
if(is_array($range)){
return array_slice($thisCollumn,$range[0],$range[1]-$range[0]);
}
// return entire formatted collumn
else{
return $thisCollumn;
}
}
}
// Generate json for a collumn and format it
function generateFormattedTimeCollumnJson($collumnIndex=0,$timeFormat='g:ia',$timeInputFormat='Y-m-d H:i:s',$range=''){
if(count($this->parsedDataRows) && is_numeric($collumnIndex)){
$thisCollumn = array_column($this->parsedDataRows,$collumnIndex);
foreach($thisCollumn as $timeIndex => $timeString){
if(DateTime::createFromFormat($timeInputFormat, $timeString)){
$thisCollumn[$timeIndex] = date($timeFormat,DateTime::createFromFormat($timeInputFormat, $timeString)->getTimestamp());
}
}
// if a range is set return just that portion
if(is_array($range)){
return json_encode(array_slice($thisCollumn,$range[0],$range[1]-$range[0]));
}
// return entire formatted collumn
else{
return json_encode($thisCollumn);
}
}
}
// Returns max value in collumn
function getCollumnMax($collumnIndex){
if(count($this->parsedDataRows) && is_numeric($collumnIndex)){
return max(array_column($this->parsedDataRows,$collumnIndex));
}
}
// Returns max value in collumn
function getCollumnMin($collumnIndex){
if(count($this->parsedDataRows) && is_numeric($collumnIndex)){
return min(array_column($this->parsedDataRows,$collumnIndex));
}
}
// Returns range of row indexes from last decent
function getIndexRangeOfLastWinchDecent($timeCollumnIndex=0,$timeInputFormat='Y-m-d H:i:s'){
// if the we're in a position to make this calculation
if(count($this->parsedDataRows) && is_numeric($timeCollumnIndex)){
// get time collumn
$timeCollumn = array_column($this->parsedDataRows,$timeCollumnIndex);
// figure out the last decent range based on being in the same hour as the last entry
if(DateTime::createFromFormat($timeInputFormat, $timeCollumn[count($timeCollumn)-1] )){
$hourOfLastDecent = date('H',DateTime::createFromFormat($timeInputFormat, $timeCollumn[count($timeCollumn)-1])->getTimestamp());
// loop over entries till we find one that's in a different hour
for($i = count($timeCollumn)-1; $i >= 0; $i--){
if(date('H',DateTime::createFromFormat($timeInputFormat, $timeCollumn[$i])->getTimestamp()) != $hourOfLastDecent){
return array($i+1,count($timeCollumn)-1);
}
}
// if all entries were in the same hour then return the range of the whole thing
return array($i+1,count($timeCollumn)-1);
}
}
}
function getIndexRangeArrayOfWinchDecents($timeCollumnIndex=0,$timeInputFormat='Y-m-d H:i:s'){
// if the we're in a position to make this calculation
if(count($this->parsedDataRows) && is_numeric($timeCollumnIndex)){
// get time collumn
$timeCollumn = array_column($this->parsedDataRows,$timeCollumnIndex);
$decents = array();
foreach($timeCollumn as $timeIndexInCollumn => $timeString){
$thisDecentTimeIdentifier = date('n/d/y H',DateTime::createFromFormat($timeInputFormat, $timeString)->getTimestamp());
// If this is a new decent from the last row
if(!isset($decents[$thisDecentTimeIdentifier])){
// Set the start of this range
$decents[$thisDecentTimeIdentifier] = array($timeIndexInCollumn);
}
// Set end of this decent to this index, which will get overwritten if there's another
$decents[$thisDecentTimeIdentifier][1] = $timeIndexInCollumn;
}
return $decents;
}
}
}
?>