Skip to content

Commit

Permalink
feat: fetch logs based on search term and txhash
Browse files Browse the repository at this point in the history
  • Loading branch information
rabi-siddique committed Nov 22, 2024
1 parent c602163 commit 9f2a3bc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
50 changes: 35 additions & 15 deletions controllers/searchLogsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import { fileURLToPath } from 'url';
import { processAndConvert } from '../services/fileProcessor.js';
import { fetchAndStoreHeightLogs } from '../services/fetchAndStoreHeightLogs.js';
import { networks } from '../helpers/constants.js';
import { fetchAndStoreLogsFromGCP } from '../services/fetchAndStoreLogsFromGCP.js';
import { getTimeStamps } from '../helpers/utils.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const uploadDir = 'uploads';

const validStrategies = new Set(['blockHeight', 'txHash', 'searchTerm']);
const strategies = ['blockHeight', 'txHash', 'searchTerm'];
const isValidStrategy = (strategy) => {
return validStrategies.has(strategy);
return strategies.includes(strategy);
};

export const handleSearchLogs = async (req, res) => {
Expand Down Expand Up @@ -47,19 +49,37 @@ export const handleSearchLogs = async (req, res) => {
console.log(`Namespace Name: ${networks[network].namespace_name}`);
console.log(`Pod Name: ${networks[network].pod_name}`);

const queryfilter = `
resource.labels.container_name="${networks[network].container_name}" AND
resource.labels.cluster_name="${networks[network].cluster_name}" AND
resource.labels.namespace_name="${networks[network].namespace_name}" AND
resource.labels.pod_name="${networks[network].pod_name}" AND
resource.type="k8s_container"
`;
if (strategy === strategies[0]) {
await fetchAndStoreHeightLogs({
blockHeight: search,
inputFile,
network,
});
} else if (strategy === strategies[1]) {
const { startTime, endTime } = getTimeStamps();
const queryfilter = `
jsonPayload.txHash="${search}" AND`;

await fetchAndStoreLogsFromGCP({
startTime,
endTime,
inputFile,
network,
queryfilter,
});
} else if (strategy === strategies[2]) {
const { startTime, endTime } = getTimeStamps();
const queryfilter = `
jsonPayload.txHash="${search}" AND`;

await fetchAndStoreLogsFromGCP({
startTime,
endTime,
inputFile,
network,
queryfilter,
});
}

await fetchAndStoreHeightLogs({
blockHeight: search,
inputFile,
network,
queryfilter,
});
await processAndConvert({ inputFile, res });
};
13 changes: 13 additions & 0 deletions helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ export const formatDateString = (dateString) => {
return date.toISOString();
};

export const getTimeStamps = () => {
const endDate = new Date();

// Get the date 10 days before today
const startDate = new Date();
startDate.setDate(endDate.getDate() - 10);

return {
startTime: startDate.toISOString(),
endTime: endDate.toISOString(),
};
};

export const getDaysDifference = (startDate, endDate) => {
const start = new Date(startDate);
const end = new Date(endDate);
Expand Down
2 changes: 1 addition & 1 deletion public/scripts/submitDateRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ document.getElementById('dateForm').addEventListener('submit', async (e) => {
spinner.style.display = 'inline-block';
submitButton.style.visibility = 'hidden';
try {
const response = await fetch('/search-logs', {
const response = await fetch('/submit-date-range', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down

0 comments on commit 9f2a3bc

Please sign in to comment.