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 aaad149 commit 7872b6f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 124 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
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 7872b6f

Please sign in to comment.