I want to access the timestamp.log file and fetch the last timestamp corresponding to an index name.
For example: if I pass logIndex = index-7.11.1-frontend1-endpoint-2021.02.24
as argument to fetchIndexTimeStamp
it should return last timestamp corresponding to this logIndex i.e 3rd last line timestamp (2021-02-24T00:24:43.659Z
)
timestamp.log looks as given below:
index-7.11.1-frontend2-endpoint-2021.02.24::2021-02-24T00:24:31.187Z
index-7.10.1-backend2-recommendation-2021.02.24::2021-02-24T00:23:36.793Z
index-7.10.1-backend1-config-mgmt-2021.02.24::2021-02-24T00:23:41.523Z
index-7.10.1-backend1-recommendation-2021.02.24::2021-02-24T00:23:41.523Z
index-7.10.1-backend2-migration-2021.02.24::2021-02-24T00:23:36.793Z
index-7.10.1-backend1-migration-2021.02.24::2021-02-24T00:23:41.523Z
index-7.11.1-frontend1-endpoint-2021.02.24::2021-02-24T00:24:29.659Z
index-7.11.1-frontend2-merlinui-2021.02.24::2021-02-24T00:03:08.988Z
index-7.11.1-frontend1-merlinui-2021.02.24::2021-02-24T00:02:07.705Z
index-7.11.1-frontend2-endpoint-2021.02.24::2021-02-24T00:24:31.187Z
index-7.10.1-backend2-recommendation-2021.02.24::2021-02-24T00:23:36.793Z
index-7.10.1-backend1-recommendation-2021.02.24::2021-02-24T00:23:41.523Z
index-7.10.1-backend1-config-mgmt-2021.02.24::2021-02-24T00:23:41.523Z
index-7.10.1-backend2-migration-2021.02.24::2021-02-24T00:23:36.793Z
index-7.10.1-backend1-migration-2021.02.24::2021-02-24T00:23:41.523Z
index-7.11.1-frontend1-endpoint-2021.02.24::2021-02-24T00:24:43.659Z
index-7.11.1-frontend1-merlinui-2021.02.24::2021-02-24T00:02:07.705Z
index-7.11.1-frontend2-merlinui-2021.02.24::2021-02-24T00:03:08.988Z
Here is my function to get the timestamp, but it's returning undefined:
const fs = require('fs')
module.exports = {
fetchIndexTimeStamp: (logIndex) => {
const dir = './logs/timestamp.log'
fs.access(`${dir}`, fs.R_OK, (err) => {
if(err) {
console.log(err);
if(err.code === 'ENOENT') {
// file does not exists
const writableStream = fs.createWriteStream(`${dir}`);
console.log('File does not exists !!')
const message = `${logIndex}::default`
writableStream.write(message + '\r\n')
}
}
else {
// file exists
let ans = undefined;
const file = fs.readFileSync(`${dir}`, 'utf-8').split(/\r?\n/);
file.forEach((line) => {
let temp = line.split("::");
if(temp[0].toString() === logIndex) {
ans = temp[1];
}
})
// console.log('data ', ans); // this is printing correct output
return ans;
}
})
}
}
const tmpstp = fetchIndexTimeStamp(logIndex); // gives undefined ??
Please let me know why the function is returning undefined??