Skip to content

Commit

Permalink
refactor: eliminate fetchings logs without logging client
Browse files Browse the repository at this point in the history
  • Loading branch information
rabi-siddique committed Nov 22, 2024
1 parent cfa27e6 commit c19857e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 174 deletions.
12 changes: 2 additions & 10 deletions controllers/dateRangeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,12 @@ export const handleDateRange = 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"
`;

console.log(`Fetching data from GCP for...`);
console.log(`Fetching data from GCP...`);
const isSuccessful = await fetchAndStoreLogsFromGCP({
startTime: formattedStartDate,
endTime: formattedEndDate,
inputFile,
queryfilter,
network,
});

if (!isSuccessful) {
Expand Down
33 changes: 11 additions & 22 deletions public/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ html {
border-bottom: 2px solid #ddd;
}

.formDateRange {
.formDateRange,
.formBlockHeight {
display: flex;
flex-direction: column;
align-items: start;
Expand Down Expand Up @@ -47,7 +48,7 @@ html {
}
}

#submitSearchButton,
#submitHeightButton,
#uploadFileButton,
#submitDateButton {
background-color: #007bff;
Expand Down Expand Up @@ -95,16 +96,18 @@ select {
font-weight: 500;
}

#dateForm > #fileHelperText > p {
#dateForm > #fileHelperText > p,
#blockHeightForm > #fileHelperText > p {
margin: 0;
}

@media (max-width: 599px) {
@media (max-width: 649px) {
.topbar {
flex-direction: column;
gap: 20px;
}
.formDateRange {
.formDateRange,
.formBlockHeight {
display: block;
}

Expand All @@ -127,22 +130,8 @@ select {
#networkForm > #fileHelperText > p {
display: none;
}
}

#txHashInput,
#searchTermInput {
display: none;
}

#searchForm {
display: flex;
flex-direction: column-reverse;
gap: 8px;
margin-bottom: 20px;
}

#searchFormTop,
#searchFormBottom {
display: flex;
gap: 8px;
#blockHeightForm > #fileHelperText > p {
padding-bottom: 8px;
}
}
39 changes: 11 additions & 28 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,45 +46,28 @@
</div>
</form>

<form id="searchForm">
<div id="searchFormTop">
<select id="searchType" name="searchType">
<option value="blockHeight">Block Height</option>
<option value="txHash">Transaction Hash</option>
<option value="searchTerm">Search Term</option>
</select>
<form id="blockHeightForm" class="formBlockHeight">
<div id="inputBlock">
<input type="number" id="blockHeight" name="blockHeight" required>
<div id="spinnerHeightForm" class="spinner"></div>
<button id="submitHeightButton" type="submit">Submit Height</button>
</div>

<div id="searchFormBottom">
<div class="searchInput" id="blockHeightInput">
<input type="number" id="blockHeight" name="blockHeight" required>
<div class="spinner"></div>
</div>

<div class="searchInput" id="txHashInput">
<input type="text" id="txHash" name="txHash" required>
</div>

<div class="searchInput" id="searchTermInput">
<input type="text" id="searchTerm" name="searchTerm" required>
</div>

<div id="spinnerSearchForm" class="spinner"></div>
<button id="submitSearchButton" type="submit">Search</button>
<div id="fileHelperText">
<p id="helperText">Enter a Block Height</p>
</div>
</form>




</div>

<div class="mainBody">
<img id="svgDisplay" src="" alt="Causeway SVG">
</div>

<script src="/scripts/ses/ses.umd.min.js" charset="utf-8"></script>
<script type="module" src="./scripts/index.js"></script>
<script src="./scripts/lockdown.js"></script>
<script src="scripts/upload.js"></script>
<script src="scripts/submitDateRange.js"></script>
<script src="scripts/submitHeight.js"></script>

</body>

Expand Down
38 changes: 38 additions & 0 deletions public/scripts/submitHeight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
document
.getElementById('blockHeightForm')
.addEventListener('submit', async (e) => {
e.preventDefault();

const height = document.getElementById('blockHeight').value;
const spinner = document.getElementById('spinnerHeightForm');
const submitButton = document.getElementById('submitHeightButton');
const network = document.getElementById('networkSelect').value;

spinner.style.display = 'inline-block';
submitButton.style.visibility = 'hidden';
try {
const response = await fetch('/submit-height', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ height, network }),
});

if (response.ok) {
const svgContent = await response.blob();
const url = URL.createObjectURL(svgContent);

const svgElement = document.getElementById('svgDisplay');
svgElement.src = url;
svgElement.style.display = 'inline-block';
} else {
console.error('Failed to upload file');
}
} catch (error) {
console.error('Error:', error);
} finally {
spinner.style.display = 'none';
submitButton.style.visibility = 'visible';
}
});
20 changes: 12 additions & 8 deletions services/fetchAndStoreHeightLogs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// @ts-check
import { fetchGCPLogs } from './fetchGCPLogs.js';
import {
findEntryWithTimestamp,
calculateDaysDifference,
fetchLogsInBatches,
fetchLogs,
} from '../helpers/utils.js';
import { fs } from 'zx';
import { ADDITIONAL_QUERY_FILTERS } from '../helpers/constants.js';

const fetchLogsByBlockEvents = async ({
network,
Expand Down Expand Up @@ -36,7 +37,6 @@ const fetchLogsByBlockEvents = async ({
export const fetchAndStoreHeightLogs = async ({
blockHeight,
inputFile,
queryfilter = '',
network,
}) => {
try {
Expand Down Expand Up @@ -87,19 +87,23 @@ export const fetchAndStoreHeightLogs = async ({

let allEntries = [];

const { entries } = await fetchGCPLogs({
const searchQuery = `
${ADDITIONAL_QUERY_FILTERS}
`;

const entries = await fetchLogs({
startTime,
endTime,
filter: queryfilter,
pageSize: 1000,
searchQuery,
network,
});

console.log('Fetched page size: ' + entries.length);
allEntries = allEntries.concat(entries);

const logEntries = allEntries.map((entry) =>
JSON.stringify(entry.jsonPayload)
);
const logEntries = allEntries.map((entry) => {
return JSON.stringify(entry.data);
});

if (!logEntries) {
throw Error('No Entries found for the given Height');
Expand Down
21 changes: 14 additions & 7 deletions services/fetchAndStoreLogsFromGCP.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
// @ts-check
import { fs } from 'zx';
import { fetchGCPLogs } from './fetchGCPLogs.js';
import { fetchLogs } from '../helpers/utils.js';
import { ADDITIONAL_QUERY_FILTERS } from '../helpers/constants.js';

export const fetchAndStoreLogsFromGCP = async ({
startTime,
endTime,
inputFile,
network,
queryfilter = '',
}) => {
try {
let allEntries = [];

const { entries } = await fetchGCPLogs({
const searchQuery = `
${queryfilter}
${ADDITIONAL_QUERY_FILTERS}
`;

const entries = await fetchLogs({
startTime,
endTime,
filter: queryfilter,
pageSize: 1000,
searchQuery,
network,
});

if (!entries) {
Expand All @@ -25,9 +32,9 @@ export const fetchAndStoreLogsFromGCP = async ({
console.log('Fetched page size: ' + entries.length);
allEntries = allEntries.concat(entries);

const logEntries = allEntries.map((entry) =>
JSON.stringify(entry.jsonPayload)
);
const logEntries = allEntries.map((entry) => {
return JSON.stringify(entry.data);
});

fs.writeFile(inputFile, logEntries.join('\n'), (err) => {
if (err) {
Expand Down
99 changes: 0 additions & 99 deletions services/fetchGCPLogs.js

This file was deleted.

0 comments on commit c19857e

Please sign in to comment.