Skip to content

Commit

Permalink
Added a partial update to the organization's inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrho committed Aug 15, 2024
1 parent ac5fbeb commit dbd1f3d
Show file tree
Hide file tree
Showing 10 changed files with 761 additions and 60 deletions.
3 changes: 3 additions & 0 deletions jccm/src/Frontend/Common/StateStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const useStore = create((set, get) => ({
isUserLoggedIn: false,
setIsUserLoggedIn: (isUserLoggedIn) => set(() => ({ isUserLoggedIn })),

isInventoryLoading: false,
setIsInventoryLoading: (isInventoryLoading) => set(() => ({ isInventoryLoading })),

user: null,
orgs: {},
setUser: (user) =>
Expand Down
8 changes: 4 additions & 4 deletions jccm/src/Frontend/Components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,14 @@ export const Login = ({ isOpen, onClose }) => {
return { status: 'two_factor' };
} else {
console.log('Login successful!');
await eventBus.emit('cloud-inventory-refresh');

return { status: 'success', message: 'Login successful!', data: data };
}
} else {
console.log('Login failed!');
await eventBus.emit('cloud-inventory-refresh');

return { status: 'error', message: 'Login failed!' };
}
} catch (error) {
Expand All @@ -234,8 +238,6 @@ export const Login = ({ isOpen, onClose }) => {

setIsUserLoggedIn(true);
setCurrentActiveThemeName(Constants.getActiveThemeName(data?.user?.theme));
setCloudInventory(data.inventory);
setCloudInventoryFilterApplied(data.isFilterApplied);

onClose();
} else if (response.status === 'two_factor') {
Expand Down Expand Up @@ -315,8 +317,6 @@ export const Login = ({ isOpen, onClose }) => {

setIsUserLoggedIn(true);
setCurrentActiveThemeName(Constants.getActiveThemeName(data?.user?.theme));
setCloudInventory(data.inventory);
setCloudInventoryFilterApplied(data.isFilterApplied);

onClose();
} else {
Expand Down
2 changes: 1 addition & 1 deletion jccm/src/Frontend/Components/UserAvatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const UserAvatar = () => {

useEffect(() => {
const intervalId = setInterval(async () => {
await eventBus.emit('user-session-check', { message: ':Periodic user session aliveness check' });
await eventBus.emit('user-session-check', { message: 'Periodic user session aliveness check' });
}, 30000);
return () => clearInterval(intervalId);
}, []);
Expand Down
16 changes: 14 additions & 2 deletions jccm/src/Frontend/Layout/InventoryTreeMenuLocal.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,12 @@ const InventoryTreeMenuLocal = () => {
return siteExists && device.path.startsWith(node.value) && !!!cloudDevices[serialNumber];
});

const targetOrgs = new Set();

targetDevices.forEach((device) => {
targetOrgs.add(device.organization);
});

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const rateLimit = 1000 / rate; // Rate in calls per second
const maxConcurrentCalls = 100; // Maximum number of concurrent async calls
Expand Down Expand Up @@ -946,7 +952,7 @@ const InventoryTreeMenuLocal = () => {
await adoptDeviceFactsWithRateLimit();

setTimeout(async () => {
await eventBus.emit('cloud-inventory-refresh', { notification: false });
await eventBus.emit('cloud-inventory-refresh', { targetOrgs: Array.from(targetOrgs), notification: false });
}, 3000);
};

Expand Down Expand Up @@ -1006,6 +1012,12 @@ const InventoryTreeMenuLocal = () => {
return device.path.startsWith(node.value) && !!cloudDevices[serialNumber];
});

const targetOrgs = new Set();

targetDevices.forEach((device) => {
targetOrgs.add(device.organization);
});

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const rateLimit = 1000 / rate; // Rate in calls per second
const maxConcurrentCalls = 100; // Maximum number of concurrent async calls
Expand Down Expand Up @@ -1042,7 +1054,7 @@ const InventoryTreeMenuLocal = () => {

await releaseDeviceFactsWithRateLimit();
setTimeout(async () => {
await eventBus.emit('cloud-inventory-refresh', { notification: false });
await eventBus.emit('cloud-inventory-refresh', { targetOrgs: Array.from(targetOrgs), notification: false });
}, 3000);
};

Expand Down
41 changes: 35 additions & 6 deletions jccm/src/Frontend/Layout/LeftSide.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import InventoryTreeMenuCloud from './InventoryTreeMenuCloud';
import OrgFilterMenu from './OrgFilterMenu';
import InventoryTreeMenuLocal from './InventoryTreeMenuLocal';
import eventBus from '../Common/eventBus';
import { RotatingIcon } from './ChangeIcon';

const StackIcon = bundleIcon(StackFilled, StackRegular);
const CloudIcon = bundleIcon(CloudFilled, CloudRegular);
Expand All @@ -75,6 +76,7 @@ const LeftSide = () => {
cloudInventoryFilterApplied,
setCloudInventoryFilterApplied,
currentActiveThemeName,
isInventoryLoading,
} = useStore();

const [selectedTree, setSelectedTree] = useState('local');
Expand Down Expand Up @@ -142,13 +144,40 @@ const LeftSide = () => {
Local
</Tab>
{isUserLoggedIn ? (
<Tab
id='cloud'
value='cloud'
icon={<CloudIcon fontSize='15px' />}
<div
style={{
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
}}
>
Cloud
</Tab>
<Tab
id='cloud'
value='cloud'
icon={<CloudIcon fontSize='15px' />}
>
Cloud
</Tab>
{isInventoryLoading && (
<div
style={{
display: 'flex',
flexDirection: 'row',
gap: '5px',
alignItems: 'center',
leftMargin: '10px',
}}
>
<RotatingIcon
Icon={ArrowSyncCircleRegular}
size={12}
rotationDuration='1000ms'
color={tokens.colorNeutralForeground2BrandHover}
/>
<Text size={100}>Loading large cloud inventory...</Text>
</div>
)}
</div>
) : (
<Tooltip
content='Please login to a cloud service for the cloud inventory display'
Expand Down
Empty file.
33 changes: 19 additions & 14 deletions jccm/src/Frontend/MainEventProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const MainEventProcessor = () => {

const { importSettings, settings } = useStore();

const { isUserLoggedIn, setIsUserLoggedIn, user, setUser } = useStore();
const { isUserLoggedIn, setIsUserLoggedIn, user, setUser, setIsInventoryLoading } = useStore();
const { inventory, setInventory } = useStore();
const { cloudInventory, setCloudInventory } = useStore();
const { deviceFacts, setDeviceFactsAll, cleanUpDeviceFacts, zeroDeviceFacts } = useStore();
Expand Down Expand Up @@ -62,14 +62,22 @@ export const MainEventProcessor = () => {
}
};

const handleCloudInventoryRefresh = async ({ notification = false } = {}) => {
const handleCloudInventoryRefresh = async ({ targetOrgs = null, notification = false } = {}) => {
console.log('Event: "cloud-inventory-refresh"');

const response = await electronAPI.saGetCloudInventory();
// Initialize a timeout for setting the loading state
const loadingTimeout = setTimeout(() => {
setIsInventoryLoading(true);
}, 3000);

const response = await electronAPI.saGetCloudInventory({targetOrgs});

clearTimeout(loadingTimeout);
setIsInventoryLoading(false);

if (response.cloudInventory) {
if (!_.isEqual(cloudInventoryRef.current, response.inventory)) {
console.log('>>>cloudInventory', response.inventory);
// console.log('>>>cloudInventory', response.inventory);
setCloudInventory(response.inventory);
setCloudInventoryFilterApplied(response.isFilterApplied);
}
Expand Down Expand Up @@ -119,38 +127,35 @@ export const MainEventProcessor = () => {
};

const handleUserSessionCheck = async ({ message = '' } = {}) => {
console.log(`Event: "user-session-check" ${message}`);
console.log(`Event: "user-session-check" ${message.length > 0 ? `-> "${message}"` : ''}`);
try {
const data = await electronAPI.saWhoamiUser();
if (data.sessionValid) {
// console.log('user-session-check: data', data);
if (!_.isEqual(userRef.current, data.user)) {
setUser(data.user);
setIsUserLoggedIn(true);
}
if (currentActiveThemeNameRef.current !== data?.user?.theme) {
setCurrentActiveThemeName(data.user.theme);
}
if (!_.isEqual(cloudInventoryRef.current, data?.inventory)) {
setCloudInventory(data.inventory);
setCloudInventoryFilterApplied(data.isFilterApplied);
}

await handleCloudInventoryRefresh();
} else {
setUser(null);
setCloudInventory([])
setCloudInventory([]);
setIsUserLoggedIn(false);
setCurrentActiveThemeName(data.theme);
}
} catch (error) {
setUser(null);
setCloudInventory([])
setCloudInventory([]);
setIsUserLoggedIn(false);
console.error('Session check error:', error);
}
};

const handleDeviceFactsRefresh = async () => {
console.log('handleDeviceFactsRefresh');
console.log('Event: "device-facts-refresh"');
const data = await electronAPI.saLoadDeviceFacts();

if (data.deviceFacts) {
Expand All @@ -161,7 +166,7 @@ export const MainEventProcessor = () => {
};

const handleDeviceFactsCleanup = async () => {
console.log('handleDeviceFactsCleanup');
console.log('Event: "device-facts-cleanup"');
cleanUpDeviceFacts();
};

Expand Down
Loading

0 comments on commit dbd1f3d

Please sign in to comment.