diff --git a/controllers/searchLogsController.js b/controllers/searchLogsController.js index 21a5a02..9cf2f70 100644 --- a/controllers/searchLogsController.js +++ b/controllers/searchLogsController.js @@ -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) => { @@ -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 }); }; diff --git a/helpers/utils.js b/helpers/utils.js index 973169b..5be1d6c 100644 --- a/helpers/utils.js +++ b/helpers/utils.js @@ -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); diff --git a/public/scripts/submitDateRange.js b/public/scripts/submitDateRange.js index 7d2b246..0edf309 100644 --- a/public/scripts/submitDateRange.js +++ b/public/scripts/submitDateRange.js @@ -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',