Skip to content

Commit

Permalink
Merge pull request #5 from elastic/relational-data
Browse files Browse the repository at this point in the history
Adding relational data
  • Loading branch information
tiansivive authored Feb 14, 2024
2 parents c502f8a + 6463160 commit 54b14a4
Showing 1 changed file with 31 additions and 3 deletions.
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))
};
}

0 comments on commit 54b14a4

Please sign in to comment.