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

Adding relational data #5

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Changes from all commits
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
34 changes: 31 additions & 3 deletions commands/entity-store.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ENTITY_STORE_OPTIONS, generateNewSeed } from "../constants.mjs";
let client = getEsClient();
let EVENT_INDEX_NAME = "auditbeat-8.12.0-2024.01.18-000001";

const offset = () => Math.random() * 1000;
const offset = () => faker.number.int({ max: 1000 })

const ASSET_CRITICALITY = [
"very_important",
Expand All @@ -33,6 +33,8 @@ export const createRandomHost = () => {
};
};



export const createFactoryRandomEventForHost = (name) => () => {
return {
"@timestamp": moment().subtract(offset(), "h").format("yyyy-MM-DDTHH:mm:ss.SSSSSSZ"),
Expand Down Expand Up @@ -138,11 +140,14 @@ export const generateEntityStore = async ({ users = 10, hosts = 10, seed = gener
createFactoryRandomEventForHost
);

await ingestEvents(eventsForUsers);
const relational = matchUsersAndHosts(eventsForUsers, eventsForHosts)

await ingestEvents(relational.users);
console.log("Users events ingested");
await ingestEvents(eventsForHosts);
await ingestEvents(relational.hosts);
console.log("Hosts events ingested");


if (options.includes(ENTITY_STORE_OPTIONS.criticality)) {
await assignAssetCriticalityToEntities(generatedUsers, "user.name");
console.log("Assigned asset criticality to users");
Expand Down Expand Up @@ -197,3 +202,26 @@ export const cleanEntityStore = async () => {
console.log(error);
}
};


const matchUsersAndHosts = (users, hosts) => {
const splitIndex = faker.number.int({ max: users.length - 1 });

return {
users: users
.slice(0, splitIndex)
.map(user => {
const index = faker.number.int({ max: hosts.length - 1 });
return { ...user, host: hosts[index].host }
})
.concat(users.slice(splitIndex)),

hosts: hosts.
slice(0, splitIndex)
.map(host => {
const index = faker.number.int({ max: users.length - 1 });
return { ...host, user: users[index].user }
})
.concat(hosts.slice(splitIndex))
};
}