Skip to content

Commit

Permalink
Use index pattern id instead of title (#6746)
Browse files Browse the repository at this point in the history
* Use index pattern id instead title

* Update CHANGELOG

* Fix index pattern in cluster statistics
  • Loading branch information
lucianogorza committed Jun 7, 2024
1 parent 1c73ab3 commit 30ff6ee
Show file tree
Hide file tree
Showing 14 changed files with 330 additions and 316 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ All notable changes to the Wazuh app project will be documented in this file.

### Changed

- Removed embedded discover [#6120](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6120) [#6235](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6235) [#6254](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6254) [#6285](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6285) [#6288](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6288) [#6290](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6290) [#6289](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6289) [#6286](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6286) [#6275](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6275) [#6287](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6287) [#6297](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6297) [#6291](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6287) [#6459](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6459) [#6434](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6434) [#6504](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6504) [#6649](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6649) [#6506](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6506) [#6537](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6537) [#6528](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6528) [#6675](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6675) [#6674](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6674) [#6558](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6558) [#6685](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6685) [#6691](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6691) [#6712](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6712) [#6734](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6734)
- Removed embedded discover [#6120](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6120) [#6235](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6235) [#6254](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6254) [#6285](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6285) [#6288](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6288) [#6290](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6290) [#6289](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6289) [#6286](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6286) [#6275](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6275) [#6287](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6287) [#6297](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6297) [#6291](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6287) [#6459](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6459) [#6434](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6434) [#6504](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6504) [#6649](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6649) [#6506](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6506) [#6537](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6537) [#6528](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6528) [#6675](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6675) [#6674](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6674) [#6558](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6558) [#6685](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6685) [#6691](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6691) [#6712](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6712) [#6734](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6734) [#6746](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6746)
- Allow editing groups for an agent from Endpoints Summary [#6250](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6250)
- Change how the configuration is managed in the backend side [#6337](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6337) [#6519](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6519) [#6573](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6573)
- Change the view of API is down and check connection to Server APIs application [#6337](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6337)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class AlertsDataSource extends PatternDataSource {

getClusterManagerFilters() {
return PatternDataSourceFilterManager.getClusterManagerFilters(
this.title,
this.id,
DATA_SOURCE_FILTER_CONTROLLED_CLUSTER_MANAGER,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ const AGENT_ID_KEY = 'agent.id';

/**
* Get the filter that excludes the data related to Wazuh servers
* @param indexPatternTitle Index pattern title
* @param indexPatternId Index pattern id
* @returns
*/
export function getFilterExcludeManager(indexPatternTitle: string) {
export function getFilterExcludeManager(indexPatternId: string) {
return {
meta: {
alias: null,
Expand All @@ -31,7 +31,7 @@ export function getFilterExcludeManager(indexPatternTitle: string) {
negate: true,
params: { query: MANAGER_AGENT_ID },
type: 'phrase',
index: indexPatternTitle,
index: indexPatternId,
controlledBy: DATA_SOURCE_FILTER_CONTROLLED_EXCLUDE_SERVER,
},
query: { match_phrase: { [AGENT_ID_KEY]: MANAGER_AGENT_ID } },
Expand All @@ -42,17 +42,17 @@ export function getFilterExcludeManager(indexPatternTitle: string) {
/**
* Get the filter that restrict the search to the allowed agents
* @param agentsIds
* @param indexPatternTitle
* @param indexPatternId
* @returns
*/
export function getFilterAllowedAgents(
agentsIds: string[],
indexPatternTitle: string,
indexPatternId: string,
) {
const field = AGENT_ID_KEY;
return {
meta: {
index: indexPatternTitle,
index: indexPatternId,
type: 'phrases',
key: field,
value: agentsIds.toString(),
Expand Down Expand Up @@ -297,7 +297,7 @@ export class PatternDataSourceFilterManager
* Return the filter when the cluster or manager are enabled
*/
static getClusterManagerFilters(
indexPatternTitle: string,
indexPatternId: string,
controlledByValue: string,
key?: string,
): tFilter[] {
Expand All @@ -313,7 +313,7 @@ export class PatternDataSourceFilterManager
managerFilter.meta = {
...managerFilter.meta,
controlledBy: controlledByValue,
index: indexPatternTitle,
index: indexPatternId,
};
//@ts-ignore
managerFilter.$state = {
Expand All @@ -326,7 +326,7 @@ export class PatternDataSourceFilterManager
/**
* Returns the filter when the an agent is pinned (saved in the session storage or redux store)
*/
static getPinnedAgentFilter(indexPatternTitle: string): tFilter[] {
static getPinnedAgentFilter(indexPatternId: string): tFilter[] {
const pinnedAgentManager = new PinnedAgentManager();
const isPinnedAgent = pinnedAgentManager.isPinnedAgent();
if (!isPinnedAgent) {
Expand All @@ -342,7 +342,7 @@ export class PatternDataSourceFilterManager
negate: false,
params: { query: currentPinnedAgent.id },
type: 'phrase',
index: indexPatternTitle,
index: indexPatternId,
controlledBy: PinnedAgentManager.FILTER_CONTROLLED_PINNED_AGENT_KEY,
},
query: {
Expand All @@ -363,10 +363,10 @@ export class PatternDataSourceFilterManager
/**
* Return the filter to exclude the data related to servers (managers) due to the setting hideManagerAlerts is enabled
*/
static getExcludeManagerFilter(indexPatternTitle: string): tFilter[] {
static getExcludeManagerFilter(indexPatternId: string): tFilter[] {
if (store.getState().appConfig?.data?.hideManagerAlerts) {
let excludeManagerFilter = getFilterExcludeManager(
indexPatternTitle,
indexPatternId,
) as tFilter;
return [excludeManagerFilter];
}
Expand All @@ -377,13 +377,13 @@ export class PatternDataSourceFilterManager
* Return the allowed agents related to the user permissions to read data from agents in the
API server
*/
static getAllowAgentsFilter(indexPatternTitle: string): tFilter[] {
static getAllowAgentsFilter(indexPatternId: string): tFilter[] {
const allowedAgents =
store.getState().appStateReducers?.allowedAgents || [];
if (allowedAgents.length > 0) {
const allowAgentsFilter = getFilterAllowedAgents(
allowedAgents,
indexPatternTitle,
indexPatternId,
) as tFilter;
return [allowAgentsFilter];
}
Expand Down Expand Up @@ -529,12 +529,12 @@ export class PatternDataSourceFilterManager
*
* @param field
* @param value
* @param indexPatternTitle
* @param indexPatternId
*/
private generateFilter(
field: string,
value: string | string[],
indexPatternTitle: string,
indexPatternId: string,
query: tFilter['query'] | tFilter['exists'],
controlledBy?: string,
negate: boolean = false,
Expand All @@ -548,7 +548,7 @@ export class PatternDataSourceFilterManager
params: value,
negate,
type: 'phrases',
index: indexPatternTitle,
index: indexPatternId,
controlledBy,
},
...query,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,125 +1,133 @@
import { tDataSource, tSearchParams, tFilter, tParsedIndexPattern } from "../index";
import {
tDataSource,
tSearchParams,
tFilter,
tParsedIndexPattern,
} from '../index';
import { getDataPlugin } from '../../../../kibana-services';
import { IndexPatternsContract, IndexPattern } from "../../../../../../../src/plugins/data/public";
import {
IndexPatternsContract,
IndexPattern,
} from '../../../../../../../src/plugins/data/public';
import { search } from '../../search-bar/search-bar-service';
import { PatternDataSourceFilterManager } from '../index';

export class PatternDataSource implements tDataSource {
id: string;
title: string;
fields: any[];
patternService: IndexPatternsContract;
indexPattern: IndexPattern;
export class PatternDataSource implements tDataSource {
id: string;
title: string;
fields: any[];
patternService: IndexPatternsContract;
indexPattern: IndexPattern;

constructor(id: string, title: string) {
this.id = id;
this.title = title;
}
/**
* Initialize the data source
*/
async init(){
this.patternService = await getDataPlugin().indexPatterns;
this.indexPattern = await this.patternService.get(this.id);
}
constructor(id: string, title: string) {
this.id = id;
this.title = title;
}
/**
* Initialize the data source
*/
async init() {
this.patternService = await getDataPlugin().indexPatterns;
this.indexPattern = await this.patternService.get(this.id);
}

getFields(){
return this.fields;
}
getFields() {
return this.fields;
}

getFixedFilters(): tFilter[]{
// return all filters
return [
...this.getPinnedAgentFilter(),
];
}
getFixedFilters(): tFilter[] {
// return all filters
return [...this.getPinnedAgentFilter()];
}

getFetchFilters(): tFilter[]{
return [
...this.getAllowAgentsFilter(),
...this.getExcludeManagerFilter()
];
}
getFetchFilters(): tFilter[] {
return [...this.getAllowAgentsFilter(), ...this.getExcludeManagerFilter()];
}

async select(){
try {
const pattern = await this.patternService.get(this.id);
if(pattern){
const fields = await this.patternService.getFieldsForIndexPattern(
pattern,
);
const scripted = pattern.getScriptedFields().map(field => field.spec);
pattern.fields.replaceAll([...fields, ...scripted]);
await this.patternService.updateSavedObject(pattern);
}else{
throw new Error('Error selecting index pattern: pattern not found');
}
}catch(error){
throw new Error(`Error selecting index pattern: ${error}`);
}
async select() {
try {
const pattern = await this.patternService.get(this.id);
if (pattern) {
const fields = await this.patternService.getFieldsForIndexPattern(
pattern,
);
const scripted = pattern.getScriptedFields().map(field => field.spec);
pattern.fields.replaceAll([...fields, ...scripted]);
await this.patternService.updateSavedObject(pattern);
} else {
throw new Error('Error selecting index pattern: pattern not found');
}
} catch (error) {
throw new Error(`Error selecting index pattern: ${error}`);
}
}

async fetch(params: tSearchParams){
const indexPattern = await this.patternService.get(this.id);
const { filters: defaultFilters = [], query, pagination, sorting, fields, dateRange, aggs } = params;
if(!indexPattern){
return;
}

try {
const results = await search({
indexPattern,
filters: defaultFilters,
query,
pagination,
sorting,
fields: fields,
dateRange,
aggs
}
);

return results;
}catch(error){
throw new Error(`Error fetching data: ${error}`);
}

async fetch(params: tSearchParams) {
const indexPattern = await this.patternService.get(this.id);
const {
filters: defaultFilters = [],
query,
pagination,
sorting,
fields,
dateRange,
aggs,
} = params;
if (!indexPattern) {
return;
}

toJSON(): tParsedIndexPattern {
return {
attributes: {
fields: JSON.stringify(this.fields),
title: this.title
},
title: this.title,
id: this.id,
migrationVersion: {
'index-pattern': '7.10.0'
},
namespace: [],
references: [],
score: 0,
type: 'index-pattern',
updated_at: new Date().toISOString(),
version: 'WzPatternDataSource',
_fields: this.fields
}
}
try {
const results = await search({
indexPattern,
filters: defaultFilters,
query,
pagination,
sorting,
fields: fields,
dateRange,
aggs,
});

/**
* Returns the filter when the an agent is pinned (saved in the session storage or redux store)
*/
getPinnedAgentFilter(): tFilter[] {
return PatternDataSourceFilterManager.getPinnedAgentFilter(this.title);
return results;
} catch (error) {
throw new Error(`Error fetching data: ${error}`);
}
}

getAllowAgentsFilter(): tFilter[] {
return PatternDataSourceFilterManager.getAllowAgentsFilter(this.title);
}
toJSON(): tParsedIndexPattern {
return {
attributes: {
fields: JSON.stringify(this.fields),
title: this.title,
},
title: this.title,
id: this.id,
migrationVersion: {
'index-pattern': '7.10.0',
},
namespace: [],
references: [],
score: 0,
type: 'index-pattern',
updated_at: new Date().toISOString(),
version: 'WzPatternDataSource',
_fields: this.fields,
};
}

getExcludeManagerFilter(): tFilter[] {
return PatternDataSourceFilterManager.getExcludeManagerFilter(this.title);
}
/**
* Returns the filter when the an agent is pinned (saved in the session storage or redux store)
*/
getPinnedAgentFilter(): tFilter[] {
return PatternDataSourceFilterManager.getPinnedAgentFilter(this.id);
}

getAllowAgentsFilter(): tFilter[] {
return PatternDataSourceFilterManager.getAllowAgentsFilter(this.id);
}

}
getExcludeManagerFilter(): tFilter[] {
return PatternDataSourceFilterManager.getExcludeManagerFilter(this.id);
}
}
Loading

0 comments on commit 30ff6ee

Please sign in to comment.