Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cloud Security] Refactor D4C metering function #183814

Merged
merged 18 commits into from
May 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
handle pagination
  • Loading branch information
CohenIdo committed May 23, 2024
commit a4b6b5d1be22d192a427e342f3483044eca00856
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ const buildMeteringRecord = (
};
export const getHeartbeatRecords = async (
esClient: ElasticsearchClient,
searchFrom: Date
searchFrom: Date,
searchAfter?: any[]
): Promise<SearchResponse<CloudDefendHeartbeat, Record<string, AggregationsAggregate>>> => {
return await esClient.search<CloudDefendHeartbeat>(
{
index: CLOUD_DEFEND_HEARTBEAT_INDEX,
size: BATCH_SIZE,
sort: 'event.ingested',
sort: [{ 'event.ingested': 'asc' }, { _id: 'asc' }],
search_after: searchAfter,
query: {
bool: {
must: [
Expand Down Expand Up @@ -96,32 +98,44 @@ export const getCloudDefendUsageRecord = async ({
logger,
}: CloudSecurityMeteringCallbackInput): Promise<UsageRecord[] | undefined> => {
try {
const usageRecords = await getHeartbeatRecords(esClient, lastSuccessfulReport);
let allRecords: UsageRecord[] = [];
let searchAfter: any[] | undefined = undefined;
let fetchMore = true;

logger.error(`usage records: ${JSON.stringify(usageRecords)}`);
while (fetchMore) {
const usageRecords = await getHeartbeatRecords(esClient, lastSuccessfulReport, searchAfter);

if (!usageRecords?.hits?.hits?.length) {
return [];
}

const records = usageRecords.hits.hits.reduce((acc, { _source }) => {
if (!_source) {
return acc;
if (!usageRecords?.hits?.hits?.length) {
break;
}

const { event } = _source;
const record = buildMeteringRecord(
_source['agent.id'],
event.ingested,
taskId,
tier,
projectId
);
const records = usageRecords.hits.hits.reduce((acc, { _source }) => {
if (!_source) {
return acc;
}

const { event } = _source;
const record = buildMeteringRecord(
_source['agent.id'],
event.ingested,
taskId,
tier,
projectId
);

return [...acc, record];
}, [] as UsageRecord[]);

return [...acc, record];
}, [] as UsageRecord[]);
allRecords = [...allRecords, ...records];

if (usageRecords.hits.hits.length < BATCH_SIZE) {
fetchMore = false;
} else {
searchAfter = usageRecords.hits.hits[usageRecords.hits.hits.length - 1].sort;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this return the latest timestamp from the previous batch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep:)

}
}

return records;
return allRecords;
} catch (err) {
logger.error(`Failed to fetch ${cloudSecuritySolution} metering data ${err}`);
}
Expand Down