-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogViewer.sh
executable file
·232 lines (221 loc) · 8.58 KB
/
logViewer.sh
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#! /bin/sh -
# A working implementation for reading the logs generated by StreamToBuffer
actionLog="./actionLogs.log"
bufferSwitchTime=30
# keep both consistent with that in StreamToBuffer
getLastNCompleteLogs() {
local N=`expr "$1" \* 3`
# 3 lines of Pointer Action Log, Keyboard Action Log and EOA
local outputFile="$2"
local actionLogEndLine=`tail -1 $actionLog`
while [ "$actionLogEndLine" != "EOA" ]; do
sleep 0.2
# echo "$actionLogEndLine"
actionLogEndLine=`tail -1 $actionLog`
done
# Ensures that the Action Logs being read are complete in nature
echo "`tail -$N $actionLog`" > $outputFile
# tail will adjust for cases exceeding no of records
}
getSeperatedNLogs() {
# seperate out the File Contents for Pointer and Keyboard Logs, remove EOAs
# this will NOT just seperate any log files, only its $joinedLogs
local N="$1"
local joinedLogs="lastNCompleteLogs.log"
getLastNCompleteLogs $N $joinedLogs
local pointerLogs="$2"
local keyboardLogs="$3"
sed -n '1~3p' $joinedLogs > $pointerLogs
sed -n '2~3p' $joinedLogs > $keyboardLogs
rm $joinedLogs
}
getCumulativeOverNLogs() {
local N="$1"
getSeperatedNLogs $N PointerLogs.log KeyboardLogs.log
local PointerCount=0 KeyboardCount=0
while IFS= read -r -u 4 PointerLog && IFS= read -r -u 5 KeyboardLog; do
(( PointerCount+=PointerLog ))
(( KeyboardCount+=KeyboardLog ))
done 4<PointerLogs.log 5<KeyboardLogs.log
rm PointerLogs.log KeyboardLogs.log
echo "$PointerCount $KeyboardCount"
}
getAverageOverNLogs() {
local N="$1"
local cumulativeArrayOverNLogs=($(getCumulativeOverNLogs $N))
local pointerCumulativeOverNLogs=${cumulativeArrayOverNLogs[0]}
local keyboardCumulativeOverNLogs=${cumulativeArrayOverNLogs[1]}
local pointerAverageOverNLogs=$( echo "scale=3; $pointerCumulativeOverNLogs / $N" | bc -l)
local keyboardAverageOverNLogs=$( echo "scale=3; $keyboardCumulativeOverNLogs / $N" | bc -l)
echo "$pointerAverageOverNLogs $keyboardAverageOverNLogs"
}
getRatio() {
local N="$1"
local numerator="$2"
# distinguishes keyboard:pointer and pointer:keyboard
# k and K are keyboard:pointer, p and P are the inverse
local cumulativeArrayOverNLogs=($(getCumulativeOverNLogs $N))
local pointerCumulativeOverNLogs=${cumulativeArrayOverNLogs[0]}
local keyboardCumulativeOverNLogs=${cumulativeArrayOverNLogs[1]}
declare ratio
if [ $numerator = "k" ] || [ $numerator = "K" ]; then
ratio=$( echo "scale=3; $keyboardCumulativeOverNLogs / $pointerCumulativeOverNLogs" | bc -l)
elif [ $numerator = "p" ] || [ $numerator = "P" ]; then
ratio=$( echo "scale=3; $pointerCumulativeOverNLogs / $keyboardCumulativeOverNLogs" | bc -l)
else
printf "ILLEGAL args"
exit 1
fi
echo $ratio
}
getN() {
# provides an option to extract to be independent of logging frequency
local option="$1"
local valueParameter="$2"
declare N
case $option in
"-T" | "--time")
# the valueparameter now is assumed to have unit seconds
N=$(( valueParameter / bufferSwitchTime ))
;;
"-R" | "--record")
# -R is assumed default as well in viewMyFingers
N=$valueParameter
;;
esac
echo "$N"
}
viewMyFingers() {
# main function, gets the user input from STDIN
if ! [ -s $actionLog ]; then
echo "whereAreMyFingers daemon does not log to provided file" >> Error.log
exit 1
fi
# checks that the file is not empty / path is incorrect
local resultantView="$1"
local completeLogsDefaultPath="lastNCompleteLogs.log"
local pointerDefaultPath="PointerLogs.log"
local keyboardDefaultPath="KeyboardLogs.log"
case $resultantView in
"-c" | "--complete")
# case of getLastNCompleteLogs
case $2 in
"-T" | "--time" | "-R" | "--record")
Nparameter=($(getN $2 $3))
getLastNCompleteLogs $Nparameter $completeLogsDefaultPath
;;
*)
if test $# -eq 2; then
Nparameter=($(getN -R $2))
getLastNCompleteLogs $Nparameter $completeLogsDefaultPath
elif test $# -eq 3; then
completeLogsPath="$2"
Nparameter=($(getN -R $3))
getLastNCompleteLogs $Nparameter $completeLogsPath
else
completeLogsPath="$2"
Nparameter=($(getN $3 $4))
getLastNCompleteLogs $Nparameter $completeLogsPath
fi
;;
esac
;;
"-s" | "--seperated")
# case of getting seperated logs
case $2 in
"-T" | "--time" | "-R" | "--record")
Nparameter=($(getN $2 $3))
getSeperatedNLogs $Nparameter $pointerDefaultPath $keyboardDefaultPath
;;
*)
if test $# -eq 2; then
Nparameter=($(getN -R $2))
getSeperatedNLogs $Nparameter $pointerDefaultPath $keyboardDefaultPath
elif test $# -eq 4; then
# exclusively either paths makes no sense
pointerPath="$2"
keyboardPath="$3"
Nparameter=($(getN -R $4))
getSeperatedNLogs $Nparameter $pointerPath $keyboardPath
else
pointerPath="$2"
keyboardPath="$3"
Nparameter=($(getN $4 $5))
getSeperatedNLogs $Nparameter $pointerPath $keyboardPath
fi
;;
esac
;;
"-C" | "--cumulative")
# case of cumulative of seperated pointer and keyboard
case $2 in
"-T" | "--time" | "-R" | "--record")
Nparameter=($(getN $2 $3))
cumulativeLogs=($(getCumulativeOverNLogs $Nparameter))
echo ${cumulativeLogs[@]}
# order is PointerLogs and then KeyboardLogs
;;
*)
Nparameter=($(getN -R $2))
cumulativeLogs=($(getCumulativeOverNLogs $Nparameter))
echo ${cumulativeLogs[@]}
;;
esac
;;
"-a" | "--average")
# case of average of seperated pointer and keyboard
case $2 in
"-T" | "--time" | "-R" | "--record")
Nparameter=($(getN $2 $3))
averageLogs=($(getAverageOverNLogs $Nparameter))
echo ${averageLogs[@]}
# order is PointerLogs and then KeyboardLogs
;;
*)
Nparameter=($(getN -R $2))
averageLogs=($(getAverageOverNLogs $Nparameter))
echo ${averageLogs[@]}
;;
esac
;;
*)
# default case is -r --ratio
declare ratio
case $1 in
"-T" | "--time" | "-R" | "--record")
Nparameter=($(getN $1 $2))
ratio=($(getRatio $Nparameter K))
;;
"-r" | "--ratio")
case $2 in
"-T" | "--time" | "-R" | "--record")
Nparameter=($(getN $2 $3))
ratio=($(getRatio $Nparameter K))
;;
"-i" | "--inverse")
case $3 in
"-T" | "--time" | "-R" | "--record")
Nparameter=($(getN $3 $4))
ratio=($(getRatio $Nparameter P))
;;
*)
Nparameter=($(getN -R $3))
ratio=($(getRatio $Nparameter P))
;;
esac
;;
*)
Nparameter=($(getN -R $2))
ratio=($(getRatio $Nparameter K))
;;
esac
;;
*)
Nparameter=($(getN -R $1))
ratio=($(getRatio $Nparameter K))
;;
esac
echo $ratio
;;
esac
}