Skip to content

Commit

Permalink
Adding more comments and fixing type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Jun 26, 2020
1 parent 54f1a3f commit 92005dd
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import { MSearchQuery } from './multi_searcher';
/**
* ResolverQuery provides the base structure for queries to retrieve events when building a resolver graph.
*
* @param T the structured return type of a resolver query. This represents the type that is returned when translating
* Elasticsearch's SearchResponse<ResolverEvent> response.
* @param T the structured return type of a resolver query. This represents the final return type of the query after handling
* any aggregations.
* @param R the is the type after transforming ES's response. Making this definable let's us set whether it is a resolver event
* or something else.
*/
export abstract class ResolverQuery<T, R = ResolverEvent> implements MSearchQuery {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ export interface StatsResult {
events: Record<string, EventStats>;
}

interface CategoriesAgg {
interface AggBucket {
key: string;
doc_count: number;
}

interface CategoriesAgg extends AggBucket {
/**
* The reason categories is optional here is because if no data was returned in the query the categories aggregation
* will not be defined on the response (because it's a sub aggregation).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import {
ResolverAncestry,
ResolverEvent,
LifecycleNode,
ResolverLifecycleNode,
} from '../../../../../common/endpoint/types';
import { createAncestry, createLifecycle } from './node';
import { LifecycleQuery } from '../queries/lifecycle';
Expand All @@ -33,7 +33,7 @@ export class AncestryQueryHandler implements QueryHandler<ResolverAncestry> {
private levels: number,
indexPattern: string,
legacyEndpointID: string | undefined,
originNode: LifecycleNode | undefined
originNode: ResolverLifecycleNode | undefined
) {
this.ancestorsToFind = getAncestryAsArray(originNode?.lifecycle[0]).slice(0, levels);
this.query = new LifecycleQuery(indexPattern, legacyEndpointID);
Expand All @@ -46,7 +46,7 @@ export class AncestryQueryHandler implements QueryHandler<ResolverAncestry> {
}

private toMapOfNodes(results: ResolverEvent[]) {
return results.reduce((nodes: Map<string, LifecycleNode>, event: ResolverEvent) => {
return results.reduce((nodes: Map<string, ResolverLifecycleNode>, event: ResolverEvent) => {
const nodeId = entityId(event);
let node = nodes.get(nodeId);
if (!node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ export class ChildrenNodesHelper {
let rootNextChild = null;

if (rootNode) {
rootNextChild = rootNode.nextChild;
rootNextChild = rootNode.nextChild ?? null;
}

cacheCopy.delete(this.rootID);
return {
childNodes: Array.from(cacheCopy.values()),
nextChild: rootNextChild || null,
nextChild: rootNextChild,
};
}

/**
* Get the entity_ids of the nodes that are cached.
*/
getEntityIDs(): string[] {
const cacheCopy: Map<string, ChildNode> = new Map(this.entityToNodeCache);
const cacheCopy: Map<string, ResolverChildNode> = new Map(this.entityToNodeCache);
cacheCopy.delete(this.rootID);
return Array.from(cacheCopy.keys());
}
Expand Down Expand Up @@ -89,7 +89,7 @@ export class ChildrenNodesHelper {
addStartEvents(queriedNodes: Set<string>, startEvents: ResolverEvent[]): Set<string> | undefined {
let largestAncestryArray = 0;
const nodesToQueryNext: Map<number, Set<string>> = new Map();
const nonLeafNodes: Set<ChildNode> = new Set();
const nonLeafNodes: Set<ResolverChildNode> = new Set();

const isDistantGrandchild = (event: ResolverEvent) => {
const ancestry = getAncestryAsArray(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { SearchResponse } from 'elasticsearch';
import { IScopedClusterClient } from 'src/core/server';
import { ResolverEvent, LifecycleNode } from '../../../../../common/endpoint/types';
import { ResolverEvent, ResolverLifecycleNode } from '../../../../../common/endpoint/types';
import { LifecycleQuery } from '../queries/lifecycle';
import { QueryInfo } from '../queries/multi_searcher';
import { SingleQueryHandler } from './fetch';
Expand All @@ -15,8 +15,8 @@ import { createLifecycle } from './node';
/**
* Retrieve the lifecycle events for a node.
*/
export class LifecycleQueryHandler implements SingleQueryHandler<LifecycleNode> {
private lifecycle: LifecycleNode | undefined;
export class LifecycleQueryHandler implements SingleQueryHandler<ResolverLifecycleNode> {
private lifecycle: ResolverLifecycleNode | undefined;
private readonly query: LifecycleQuery;
constructor(
private readonly entityID: string,
Expand Down Expand Up @@ -51,7 +51,7 @@ export class LifecycleQueryHandler implements SingleQueryHandler<LifecycleNode>
/**
* Get the results from the msearch.
*/
getResults(): LifecycleNode | undefined {
getResults(): ResolverLifecycleNode | undefined {
return this.lifecycle;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function createLifecycle(
* @param nextChild the cursor for the response
*/
export function createChildren(
nodes: ChildNode[] = [],
nodes: ResolverChildNode[] = [],
nextChild: string | null = null
): ResolverChildren {
return { childNodes: nodes, nextChild };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Tree', () => {
// transform the generator's array of events into the format expected by the tree class
const ancestorInfo: ResolverAncestry = {
ancestors: generator
.createAlertEventAncestry(5, 0, 0)
.createAlertEventAncestry({ ancestors: 5, percentTerminated: 0, percentWithRelated: 0 })
.filter((event) => {
return event.event.kind === 'event';
})
Expand Down

0 comments on commit 92005dd

Please sign in to comment.