I have lists i can export of detailed call records for customers and it shows up in 3 columns, col1 end time, col2 call-id, col3 start time.
some lists are 800k lines long.
I’ve written a script that does the job but it’s slow… very slow.
I’m looking at a way to improve the run speed.
This works, but it’s very slow.
#!/bin/bash
IFS=$'n';
starts=();
ends=();
cids=();
#check if cdr.tmp is the same as cdr.txt, if not, condition the .txt file for use.
if [ "$(cat cdr.tmp)" != "$(cat cdr.txt)" ]; then
>cdr.tmp
if [ ! -f cdr.tmp ]; then
touch cdr.tmp
fi;
line=0;
for i in $(cat cdr.txt); do
echo working on line $line;
random=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 36; echo);
echo $i | sed "s/missing_externaltrackingid/$random/g" >> cdr.tmp;
((++line));
done;
sed -i 's/ 2024,//g' cdr.tmp;
cat cdr.tmp > cdr.txt;
fi;
#timestamp to epoch
ts2epoch() {
orig="$1"
orig="$(echo $orig | sed 's/th//g')";
orig="$(echo $orig | sed 's/rd//g')";
orig="$(echo $orig | sed 's/nd//g')";
orig="$(echo $orig | sed 's/1st/1/g')";
orig="$(echo $orig | rev | cut -d '.' -f2 | rev)";
orig="$(echo $orig | tr -d '"')";
epoch=$(date -d "${orig}" +"%s")
echo "$epoch"
};
#epoch to timestamp
epoch2ts() {
echo $(date -d @$1 +%Y-%m-%d_%H:%M:%S)
};
#add pertinent values to arrays.
for line in $(tac ~/cdr.txt); do
id=$(echo "$line" | cut -d ',' -f2);
start=$(echo "$line" | cut -d ',' -f3);
end=$(echo "$line" | cut -d ',' -f1);
cids+=("$id");
echo adding $id to cids array >> ~/bin/concurrent.log;
starts+=($(ts2epoch "$start"));
echo adding $(ts2epoch "$start") to start times >> ~/bin/concurrent.log;
ends+=($(ts2epoch "$end"));
echo adding $(ts2epoch "$end") to end times >> ~/bin/concurrent.log;
done;
#calculate earliest time and latest time
begin_ts="$(echo ${starts[0]})";
end_ts="$(echo ${ends[-1]})";
#create a sequence of times with 30 second intervals, then for each interval, check to see how many calls were active during that instance
for time in $(seq $begin_ts 30 $end_ts); do
active=0;
for i in ${!cids[@]}; do
if [ $time -gt ${starts[i]} ] && [ $time -lt ${ends[i]} ]; then
((++active));
fi;
done;
echo $(epoch2ts $time):$active active. >> ~/bin/concurrent.log
echo $(epoch2ts $time):$active active. >> cdr-active.txt;
done;
4