Skip to content

Commit

Permalink
refactoring..
Browse files Browse the repository at this point in the history
  • Loading branch information
aang7 committed Apr 27, 2020
1 parent 714dcd4 commit d63c2d5
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 78 deletions.
11 changes: 3 additions & 8 deletions bash/logextract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ DIRECTORY="mediciones" # this is the directory where the log files are in
# filter lines getting the line number
#



mkdir -p $DIR # create folder if does not exists
# create a for loop to cycle through desired files.
# read the files of directory
Expand All @@ -38,17 +36,14 @@ do
delimeter='-'
SplitLineByDelimeter line delimeter split_result
line_number="${split_result[0]}"
GeneratedCSV="${split_result[-1]}"
#echo "$GeneratedCSV"
data="${split_result[-1]}"

re='^[0-9a-zA-Z]+$' # regex to check if variable is a number
if ! [[ $data =~ $re ]] ;
then
#echo "error: Not a number in file $file";
# continue
#else

date_line_number=$(($line_number - 2))
# printf " line number: $line_number - 2 : $date_line_number\n"

# show that line
date_line=$(sed -n "${date_line_number}p" "$file")
date_line_clean=$(echo ${date_line%demos*}) # remove from word demos to the end of line
Expand Down
74 changes: 74 additions & 0 deletions bash/logextract_ver2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/bin/bash
. ./utils.lib

# NOTE: This file does the same as logextract.sh
# but was modified to parse just one file at a time.
# the file has to be passed through as the first argument
FILE_EXTENSION=".log"
DIRECTORY="mediciones" # this is the directory where the log files are in
file="$DIRECTORY/$1" # first argument: the file name in the folder named as $DIRECTORY
DIR="../GeneratedData"
TEMP_FILE_NAME="temp_$1"
echo "$TEMP_FILE_NAME"
# ALGORITHM
# read files
# create temp file to save grep result
# filter lines getting the line number

echo "$file"
echo "$DIR"

mkdir -p $DIR # create folder if does not exists


if [[ ${file: -${#FILE_EXTENSION}} == $FILE_EXTENSION ]]; # checks if file has the FILE_EXTENSION criteria
then
# get lines with specified lenght (min: 510 chars max: 511)
grep -anx '.\{510,511\}' $file > "$DIR/$TEMP_FILE_NAME"

# replace ':' with '-'
sed -ri 's/[:]+/-/g' "$DIR/$TEMP_FILE_NAME"

# read temp file and filter the desired GeneratedCSV
input="$DIR/$TEMP_FILE_NAME"

i=0
while IFS= read -r line
do

i=$((i+1))
delimeter='-'
SplitLineByDelimeter line delimeter split_result
line_number="${split_result[0]}"
data="${split_result[-1]}" # encoded data from the ekm metering

re='^[0-9a-zA-Z]+$' # regex to check if variable is alphanumeric
if [[ $data =~ $re ]] ;
then
date_line_number=$(($line_number - 2))
# printf " line number: $line_number - 2 : $date_line_number\n"
# show that line
date_line=$(sed -n "${date_line_number}p" "$file")
date_line_clean=$(echo ${date_line%demos*}) # remove from word demos to the end of line
# add date to temp file
sed -i "${i}s/$/-${date_line_clean}/" "$input"

fi


done < "$input"


# create new file with temp info
delimeter='/'
SplitLineByDelimeter file delimeter split_result

new_file_name=$(echo "$DIR/${split_result[-1]}.filtered")
cp $input $new_file_name

echo "file: $file has $i rows of data"
fi

rm $input

exit 0
69 changes: 11 additions & 58 deletions cleandata.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,29 @@ const readline = require('readline');
const converter_to_csv = require('json-2-csv');
const DATA_DIR = 'GeneratedData';
const DIR_TO_PUT_CSV = 'GeneratedCSV';
const helper = require('./helper');

function filter_data_in_line(line, array_to_fill) {

//Remove all the characters in even position in the string
splitted_data = line.split('-');
data = splitted_data[1];
data = data.replace(/.(.)/g, '$1');
//Get the GeneratedCSV from the each position
var decoded = {
"name": data.substr(8 , 8),
"kwhtot":data.substr(16, 8),
"volt1": data.substr(23 + 73, 3) + "." + data.substr(26 + 73, 1),
"volt2": data.substr(27 + 73, 3) + "." + data.substr(30 + 73, 1),
"volt3": data.substr(31 + 73, 3) + "." + data.substr(34 + 73, 1),
"amp1": data.substr(35 + 73, 4) + "." + data.substr(39 + 73, 1),
"amp2": data.substr(40 + 73, 4) + "." + data.substr(44 + 73, 1),
"amp3": data.substr(45 + 73, 4) + "." + data.substr(49 + 73, 1),
"watts1":data.substr(50 + 73, 6) + "." + data.substr(56 + 73, 1),
"watts2":data.substr(57 + 73, 6) + "." + data.substr(63 + 73, 1),
"watts3":data.substr(64 + 73, 6) + "." + data.substr(70 + 73, 1),
"date": splitted_data[2]
};
array_to_fill.push(decoded)
}


async function processLineByLine(file) {
const fileStream = fs.createReadStream(file);

// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});

let decoded_data = [];

for await (const line of rl) {
// Each line in input.txt will be successively available here as `line`.
console.log(`Line from file: ${line}`);
filter_data_in_line(line, decoded_data)
}

return decoded_data;
}

// read dir
fs.readdirSync(DATA_DIR).forEach(async (file) => {
if (file.split('.').pop() === 'filtered') {
await processLineByLine(DATA_DIR + '/' + file)
await helper.processLineByLine(DATA_DIR + '/' + file)
.then(decoded_data => {
// create the new csv file which will contains the decoded GeneratedCSV
converter_to_csv.json2csv(decoded_data, function (err, csv) {
// once we received the created csv we want to write a new file with it
converter_to_csv.json2csv(decoded_data, function (err, csv) {
if (err) throw err;
fs.writeFile(DIR_TO_PUT_CSV + '/' + file + '.csv', csv, 'utf8', (err) =>
{
if (err) {
console.log('Some error occured - file either not saved or corrupted file saved.');
} else {
console.log('It\'s saved!');
}
});
if (err) throw err;
fs.writeFile(DIR_TO_PUT_CSV + '/' + file + '.csv', csv, 'utf8', (err) =>
{
if (err) {
console.log('Some error occured - file either not saved or corrupted file saved.');
} else {
console.log('It\'s saved!');
}
});

});

})
.catch(reason => console.log(reason));
}
});

57 changes: 45 additions & 12 deletions exec_command.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
const { exec } = require('child_process');

const dir = exec("ls -la", function(err, stdout, stderr) {
if (err) {
// should have err.code here?
console.log('error ', err)
}
console.log(stdout);
});

dir.on('exit', function (code) {
// exit code is code
console.log('exit ', code);
const { exec, execSync } = require('child_process');
const fs = require('fs');
const converter_to_csv = require('json-2-csv');

const DATA_DIR = 'GeneratedData';
const DIR_TO_PUT_CSV = 'GeneratedCSV';
const DIR_TO_LOG_DATA = 'bash/mediciones';
const helper = require('./helper.js');


fs.readdirSync(DIR_TO_LOG_DATA).forEach( async (file) => {
console.log(file);
const command = exec("bash logextract_ver2.sh " + file, { cwd: './bash' }, function(err, stdout, stderr) {
if (err)
Promise.reject('error');

console.log(stdout);
helper.processLineByLine(DATA_DIR + `/${file}` + '.filtered')
.then(decoded_data => {

// once we received the created csv we want to write a new file with it
converter_to_csv.json2csv(decoded_data, function (err, csv) {
if (err) throw err;
fs.writeFile(DIR_TO_PUT_CSV + '/' + file + '.csv', csv, 'utf8', (err) =>
{
if (err) {
console.log('Some error occured - file either not saved or corrupted file saved.');
console.log(err);
} else {
console.log('It\'s saved!');
}
});
});

});

});

command.on('exit', async (code) => {
// exit code is code
// once finish the bash script to be processed
console.log(`exit with code on file: ${file}`, code);
});
});

return 0;
52 changes: 52 additions & 0 deletions helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const fs = require('fs');
const readline = require('readline');


function filter_data_in_line(line, array_to_fill) {

//Remove all the characters in even position in the string
splitted_data = line.split('-');
data = splitted_data[1];
data = data.replace(/.(.)/g, '$1');
//Get the GeneratedCSV from the each position
var decoded = {
"name": data.substr(8 , 8),
"kwhtot":data.substr(16, 8),
"volt1": data.substr(23 + 73, 3) + "." + data.substr(26 + 73, 1),
"volt2": data.substr(27 + 73, 3) + "." + data.substr(30 + 73, 1),
"volt3": data.substr(31 + 73, 3) + "." + data.substr(34 + 73, 1),
"amp1": data.substr(35 + 73, 4) + "." + data.substr(39 + 73, 1),
"amp2": data.substr(40 + 73, 4) + "." + data.substr(44 + 73, 1),
"amp3": data.substr(45 + 73, 4) + "." + data.substr(49 + 73, 1),
"watts1":data.substr(50 + 73, 6) + "." + data.substr(56 + 73, 1),
"watts2":data.substr(57 + 73, 6) + "." + data.substr(63 + 73, 1),
"watts3":data.substr(64 + 73, 6) + "." + data.substr(70 + 73, 1),
"date": splitted_data[2]
};
array_to_fill.push(decoded)
}

async function processLineByLine(file) {
const fileStream = fs.createReadStream(file);

// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});

let decoded_data = [];

for await (const line of rl) {
// Each line in input.txt will be successively available here as `line`.
console.log(`Line from file: ${line}`);
filter_data_in_line(line, decoded_data)
}

return decoded_data;
}

module.exports = {
processLineByLine
};

0 comments on commit d63c2d5

Please sign in to comment.