- 
                Notifications
    You must be signed in to change notification settings 
- Fork 8.5k
[Security_Solution][Resolver] Resolver loading and error state #75600
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
          
     Merged
      
      
            michaelolo24
  merged 12 commits into
  elastic:master
from
michaelolo24:resolver-loading-states
  
      
      
   
  Aug 27, 2020 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            12 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      6c2519c
              
                WIP: need better control flow
              
              
                michaelolo24 544de76
              
                WIP: added error data access layer
              
              
                michaelolo24 2da7c52
              
                cleaned up tests
              
              
                michaelolo24 b841c2a
              
                fix comment
              
              
                michaelolo24 0f03731
              
                improved pattern
              
              
                michaelolo24 062454f
              
                order fix
              
              
                michaelolo24 1a69f70
              
                types fix
              
              
                michaelolo24 0485a73
              
                make pause and empty composable
              
              
                michaelolo24 62aead1
              
                updated comment & fix pausify
              
              
                michaelolo24 f75a0f9
              
                pr feedback cleanup
              
              
                michaelolo24 26dfa65
              
                remove ts-ignore
              
              
                michaelolo24 389f912
              
                make emptify arg required
              
              
                michaelolo24 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            88 changes: 88 additions & 0 deletions
          
          88 
        
  x-pack/plugins/security_solution/public/resolver/data_access_layer/mocks/emptify_mock.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|  | ||
| import { | ||
| ResolverRelatedEvents, | ||
| ResolverTree, | ||
| ResolverEntityIndex, | ||
| } from '../../../../common/endpoint/types'; | ||
| import { mockTreeWithNoProcessEvents } from '../../mocks/resolver_tree'; | ||
| import { DataAccessLayer } from '../../types'; | ||
|  | ||
| type EmptiableRequests = 'relatedEvents' | 'resolverTree' | 'entities' | 'indexPatterns'; | ||
|  | ||
| interface Metadata<T> { | ||
| /** | ||
| * The `_id` of the document being analyzed. | ||
| */ | ||
| databaseDocumentID: string; | ||
| /** | ||
| * A record of entityIDs to be used in tests assertions. | ||
| */ | ||
| entityIDs: T; | ||
| } | ||
|  | ||
| /** | ||
| * A simple mock dataAccessLayer that allows you to control whether a request comes back with data or empty. | ||
| */ | ||
| export function emptifyMock<T>( | ||
| { | ||
| metadata, | ||
| dataAccessLayer, | ||
| }: { | ||
| dataAccessLayer: DataAccessLayer; | ||
| metadata: Metadata<T>; | ||
| }, | ||
| dataShouldBeEmpty: EmptiableRequests[] | ||
| ): { | ||
| dataAccessLayer: DataAccessLayer; | ||
| metadata: Metadata<T>; | ||
| } { | ||
| return { | ||
| metadata, | ||
| dataAccessLayer: { | ||
| /** | ||
| * Fetch related events for an entity ID | ||
| */ | ||
| async relatedEvents(...args): Promise<ResolverRelatedEvents> { | ||
| return dataShouldBeEmpty.includes('relatedEvents') | ||
| ? Promise.resolve({ | ||
| entityID: args[0], | ||
| events: [], | ||
| nextEvent: null, | ||
| }) | ||
| : dataAccessLayer.relatedEvents(...args); | ||
| }, | ||
|  | ||
| /** | ||
| * Fetch a ResolverTree for a entityID | ||
| */ | ||
| async resolverTree(...args): Promise<ResolverTree> { | ||
| return dataShouldBeEmpty.includes('resolverTree') | ||
| ? Promise.resolve(mockTreeWithNoProcessEvents()) | ||
| : dataAccessLayer.resolverTree(...args); | ||
| }, | ||
|  | ||
| /** | ||
| * Get an array of index patterns that contain events. | ||
| */ | ||
| indexPatterns(...args): string[] { | ||
| return dataShouldBeEmpty.includes('indexPatterns') | ||
| ? [] | ||
| : dataAccessLayer.indexPatterns(...args); | ||
| }, | ||
|  | ||
| /** | ||
| * Get entities matching a document. | ||
| */ | ||
| async entities(...args): Promise<ResolverEntityIndex> { | ||
| return dataShouldBeEmpty.includes('entities') | ||
| ? Promise.resolve([]) | ||
| : dataAccessLayer.entities(...args); | ||
| }, | ||
| }, | ||
| }; | ||
| } | 
        
          
          
            124 changes: 124 additions & 0 deletions
          
          124 
        
  x-pack/plugins/security_solution/public/resolver/data_access_layer/mocks/pausify_mock.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|  | ||
| import { | ||
| ResolverRelatedEvents, | ||
| ResolverTree, | ||
| ResolverEntityIndex, | ||
| } from '../../../../common/endpoint/types'; | ||
| import { DataAccessLayer } from '../../types'; | ||
|  | ||
| type PausableRequests = 'relatedEvents' | 'resolverTree' | 'entities'; | ||
|  | ||
| interface Metadata<T> { | ||
| /** | ||
| * The `_id` of the document being analyzed. | ||
| */ | ||
| databaseDocumentID: string; | ||
| /** | ||
| * A record of entityIDs to be used in tests assertions. | ||
| */ | ||
| entityIDs: T; | ||
| } | ||
|  | ||
| /** | ||
| * A simple mock dataAccessLayer that allows you to manually pause and resume a request. | ||
| */ | ||
| export function pausifyMock<T>({ | ||
| metadata, | ||
| dataAccessLayer, | ||
| }: { | ||
| dataAccessLayer: DataAccessLayer; | ||
| metadata: Metadata<T>; | ||
| }): { | ||
| dataAccessLayer: DataAccessLayer; | ||
| metadata: Metadata<T>; | ||
| pause: (pausableRequests: PausableRequests[]) => void; | ||
| resume: (pausableRequests: PausableRequests[]) => void; | ||
| } { | ||
| let relatedEventsPromise = Promise.resolve(); | ||
| let resolverTreePromise = Promise.resolve(); | ||
| let entitiesPromise = Promise.resolve(); | ||
|  | ||
| let relatedEventsResolver: (() => void) | null; | ||
| let resolverTreeResolver: (() => void) | null; | ||
| let entitiesResolver: (() => void) | null; | ||
|  | ||
| return { | ||
| metadata, | ||
| pause: (pausableRequests: PausableRequests[]) => { | ||
| const pauseRelatedEventsRequest = pausableRequests.includes('relatedEvents'); | ||
| const pauseResolverTreeRequest = pausableRequests.includes('resolverTree'); | ||
| const pauseEntitiesRequest = pausableRequests.includes('entities'); | ||
|  | ||
| if (pauseRelatedEventsRequest && !relatedEventsResolver) { | ||
| relatedEventsPromise = new Promise((resolve) => { | ||
| relatedEventsResolver = resolve; | ||
| }); | ||
| } | ||
| if (pauseResolverTreeRequest && !resolverTreeResolver) { | ||
| resolverTreePromise = new Promise((resolve) => { | ||
| resolverTreeResolver = resolve; | ||
| }); | ||
| } | ||
| if (pauseEntitiesRequest && !entitiesResolver) { | ||
| entitiesPromise = new Promise((resolve) => { | ||
| entitiesResolver = resolve; | ||
| }); | ||
| } | ||
| }, | ||
| resume: (pausableRequests: PausableRequests[]) => { | ||
| const resumeEntitiesRequest = pausableRequests.includes('entities'); | ||
| const resumeResolverTreeRequest = pausableRequests.includes('resolverTree'); | ||
| const resumeRelatedEventsRequest = pausableRequests.includes('relatedEvents'); | ||
|  | ||
| if (resumeEntitiesRequest && entitiesResolver) { | ||
| entitiesResolver(); | ||
| entitiesResolver = null; | ||
| } | ||
| if (resumeResolverTreeRequest && resolverTreeResolver) { | ||
| resolverTreeResolver(); | ||
| resolverTreeResolver = null; | ||
| } | ||
| if (resumeRelatedEventsRequest && relatedEventsResolver) { | ||
| relatedEventsResolver(); | ||
| relatedEventsResolver = null; | ||
| } | ||
| }, | ||
| dataAccessLayer: { | ||
| /** | ||
| * Fetch related events for an entity ID | ||
| */ | ||
| async relatedEvents(...args): Promise<ResolverRelatedEvents> { | ||
| await relatedEventsPromise; | ||
| return dataAccessLayer.relatedEvents(...args); | ||
| }, | ||
|  | ||
| /** | ||
| * Fetch a ResolverTree for a entityID | ||
| */ | ||
| async resolverTree(...args): Promise<ResolverTree> { | ||
| await resolverTreePromise; | ||
| return dataAccessLayer.resolverTree(...args); | ||
| }, | ||
|  | ||
| /** | ||
| * Get an array of index patterns that contain events. | ||
| */ | ||
| indexPatterns(...args): string[] { | ||
| return dataAccessLayer.indexPatterns(...args); | ||
| }, | ||
|  | ||
| /** | ||
| * Get entities matching a document. | ||
| */ | ||
| async entities(...args): Promise<ResolverEntityIndex> { | ||
| await entitiesPromise; | ||
| return dataAccessLayer.entities(...args); | ||
| }, | ||
| }, | ||
| }; | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.