Skip to content

Commit

Permalink
feat: analyze on create (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kav91 committed Dec 19, 2022
1 parent bad91a5 commit 2cd41f6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
70 changes: 65 additions & 5 deletions nerdlets/optimizer-beta/components/collectionCreate/index.js
Expand Up @@ -10,7 +10,8 @@ import {
Checkbox,
CheckboxGroup,
TextField,
AccountStorageMutation
AccountStorageMutation,
Toast
} from 'nr1';
import DataContext from '../../context/data';
import { v4 as uuidv4 } from 'uuid';
Expand All @@ -25,9 +26,18 @@ export default function CollectionCreateModal(props) {
workloads,
updateDataState,
fetchingAccessibleWorkloads,
fetchWorkloadCollections
fetchWorkloadCollections,
optimizerKey,
apiUrlDev,
apiUrlProd,
uuid
} = dataContext;

const isLocal =
!window.location.href.includes('https://one.newrelic.com') &&
!window.location.href.includes('https://one.eu.newrelic.com');
const apiUrl = isLocal ? apiUrlDev : apiUrlProd;

const [writingDocument, setWriteState] = useState(false);
const [name, setName] = useState('');
const [searchText, setSearch] = useState('');
Expand All @@ -53,20 +63,45 @@ export default function CollectionCreateModal(props) {
lastEditedBy: email
};

const documentId = uuidv4();

AccountStorageMutation.mutate({
accountId: selectedAccount.id,
actionType: AccountStorageMutation.ACTION_TYPE.WRITE_DOCUMENT,
collection: 'workloadCollections',
documentId: uuidv4(),
documentId,
document
}).then(value => {
}).then(config => {
// eslint-disable-next-line no-console
console.log('wrote document', value);
console.log('wrote document', config);

setWriteState(false);
fetchWorkloadCollections();
setCheckBoxValues([]);
updateDataState({ createCollectionOpen: false });

postData(`${apiUrl}/optimize`, optimizerKey.key, {
workloadGuids: document.workloads.map(w => w.guid),
accountId: selectedAccount.id,
nerdpackUUID: uuid,
collectionId: documentId,
config
}).then(data => {
if (data?.success) {
Toast.showToast({
title: 'Job sent successfully',
description: 'Processing... can take up to 15m',
type: Toast.TYPE.NORMAL
});
} else {
Toast.showToast({
title: 'Job failed to send',
description:
data?.message || 'Check... console & network logs for errors',
type: Toast.TYPE.CRITICAL
});
}
});
});
};

Expand Down Expand Up @@ -158,3 +193,28 @@ export default function CollectionCreateModal(props) {
</Modal>
);
}

function postData(url = '', key, data = {}) {
return new Promise(resolve => {
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'NR-API-KEY': key,
'NR-REGION': (window?.location?.host || '').includes('.eu.')
? 'EU'
: undefined
},
body: JSON.stringify(data)
})
.then(async response => {
const responseData = await response.json();
resolve(responseData);
})
.catch(error => {
// eslint-disable-next-line no-console
resolve({ success: false, error });
resolve();
});
});
}
2 changes: 1 addition & 1 deletion nr1.json
@@ -1,6 +1,6 @@
{
"schemaType": "NERDPACK",
"id": "ec734258-b5ca-41b2-84ef-d97352e0ea19",
"id": "7e874b6e-c010-4933-a15d-ea5b2e54d029",
"displayName": "Cloud Optimize",
"description": "Identify right-sizing opportunities and potential savings of your AWS, GCP, Azure & Alibaba instances across your cloud environment"
}

0 comments on commit 2cd41f6

Please sign in to comment.