Skip to content

Commit

Permalink
Merge pull request #55 from Flared/mahinse/update_saved_search
Browse files Browse the repository at this point in the history
Use the saved search for the button in completed + update the query when saving configuration
  • Loading branch information
TyMarc authored Nov 20, 2024
2 parents dafc78e + 8248d39 commit 2621d6c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { FC } from 'react';
import React, { FC, useEffect, useState } from 'react';
import Button from './Button';
import { getFlareDataUrl } from '../utils/setupConfiguration';
import { getFlareSearchDataUrl } from '../utils/setupConfiguration';
import ArrowRightIcon from './icons/ArrowRightIcon';

import './ConfigurationGlobalStep.css';
Expand All @@ -10,6 +10,12 @@ const ConfigurationCompletedStep: FC<{
tenantName: string;
onEditConfigurationClick: () => void;
}> = ({ show, tenantName, onEditConfigurationClick }) => {
const [flareSearchUrl, setFlareSearchUrl] = useState('');

useEffect(() => {
getFlareSearchDataUrl().then((url) => setFlareSearchUrl(url));
}, []);

return (
<div hidden={!show}>
<h5>
Expand All @@ -23,7 +29,7 @@ const ConfigurationCompletedStep: FC<{
Edit Configuration
</Button>
<div className="link">
<a href={getFlareDataUrl()}>View Flare Data</a>
<a href={flareSearchUrl}>View Flare Data</a>
<ArrowRightIcon remSize={1} />
</div>
</div>
Expand Down
14 changes: 14 additions & 0 deletions packages/react-components/src/models/splunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export interface SplunkIndex {
name: string;
}

export interface SplunkSavedSearch {
name: string;
qualifiedPath: string;
update: (properties: Record<string, string>) => void;
}

export interface SplunkAppAccessor {
reload: () => void;
}
Expand Down Expand Up @@ -66,6 +72,13 @@ export interface SplunkIndexesAccessor {
list: () => Array<SplunkIndex>;
}

export interface SplunkSavedSearchAccessor {
fetch: () => SplunkSavedSearchAccessor;
create: (indexName: string, data: any) => void;
item: (indexName: string) => SplunkSavedSearch;
list: () => Array<SplunkSavedSearch>;
}

export interface SplunkStoragePasswordAccessors {
fetch: () => SplunkStoragePasswordAccessors;
item: (applicationName: string) => SplunkAppAccessor;
Expand All @@ -79,6 +92,7 @@ export interface SplunkService {
apps: () => SplunkAppsAccessor;
storagePasswords: () => SplunkStoragePasswordAccessors;
indexes: () => SplunkIndexesAccessor;
savedSearches: () => SplunkSavedSearchAccessor;
get: (splunkUrlPath: string, data: any) => void;
post: (
splunkUrlPath: string,
Expand Down
30 changes: 26 additions & 4 deletions packages/react-components/src/utils/setupConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const applicationNameSpace: SplunkApplicationNamespace = {
app: appName,
sharing: 'app',
};
const flareSavedSearchName = 'Flare Search';

async function completeSetup(splunkService: SplunkService): Promise<void> {
await updateConfigurationFile(splunkService, 'app', 'install', {
Expand All @@ -35,8 +36,11 @@ function getRedirectUrl(): string {
return `/app/${appName}`;
}

function getFlareDataUrl(): string {
return `/app/${appName}/search?q=search%20source%3D"flare"`;
async function getFlareSearchDataUrl(): Promise<string> {
const service = createService();
const savedSearches = await promisify(service.savedSearches().fetch)();
const savedSearch = savedSearches.item(flareSavedSearchName);
return `/app/${appName}/@go?s=${savedSearch.qualifiedPath}`;
}

function redirectToHomepage(): void {
Expand Down Expand Up @@ -104,7 +108,6 @@ async function saveConfiguration(
isIngestingMetadataOnly: boolean
): Promise<void> {
const service = createService();

const storagePasswords = await promisify(service.storagePasswords().fetch)();
await savePassword(storagePasswords, PasswordKeys.API_KEY, apiKey);
await savePassword(storagePasswords, PasswordKeys.TENANT_ID, `${tenantId}`);
Expand All @@ -114,10 +117,29 @@ async function saveConfiguration(
`${isIngestingMetadataOnly}`
);
await saveIndexForIngestion(service, indexName);
await updateSavedSearchQuery(
service,
flareSavedSearchName,
`source=${appName} index=${indexName}`
);
await completeSetup(service);
await reloadApp(service);
}

async function updateSavedSearchQuery(
service: SplunkService,
savedSearchName: string,
query: string
): Promise<void> {
const savedSearches = await promisify(service.savedSearches().fetch)();
const savedSearch = savedSearches.item(savedSearchName);
if (savedSearch) {
await savedSearch.update({
search: query,
});
}
}

async function fetchCollectionItems(): Promise<SplunkCollectionItem[]> {
const service = createService();
return promisify(service.get)('storage/collections/data/event_ingestion_collection/', {})
Expand Down Expand Up @@ -242,7 +264,7 @@ export {
fetchIngestMetadataOnly,
redirectToHomepage,
getRedirectUrl,
getFlareDataUrl,
getFlareSearchDataUrl,
createFlareIndex,
fetchAvailableIndexNames,
fetchCurrentIndexName,
Expand Down

0 comments on commit 2621d6c

Please sign in to comment.