Skip to content

Commit

Permalink
HOSTSD-246 Fix data (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fosol authored Feb 12, 2024
1 parent be03138 commit 1d76f65
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 63 deletions.
2 changes: 2 additions & 0 deletions src/api/Areas/Admin/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public IActionResult GetForId(int id, bool includePermissions)
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[ClientRoleAuthorize(ClientRole.SystemAdministrator)]
[HttpPost(Name = "AddUser-SystemAdmin")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(UserModel), (int)HttpStatusCode.Created)]
Expand Down Expand Up @@ -151,6 +152,7 @@ public async Task<IActionResult> UpdateAsync(UserModel model)
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[ClientRoleAuthorize(ClientRole.SystemAdministrator)]
[HttpDelete("{id}", Name = "RemoveUser-SystemAdmin")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(UserModel), (int)HttpStatusCode.OK)]
Expand Down
48 changes: 26 additions & 22 deletions src/dashboard/src/app/client/admin/users/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,32 @@ export default function Page() {
>
Search
</Button>
<Select
options={tenantOptions}
placeholder="Select Tenant"
value={tenantOptions.find((o) => o.value == selectedTenant?.id)?.value ?? ''}
onChange={(value) => {
const tenant = userinfo?.tenants.find((t) => t.id == value);
setSelectedTenant(tenant);
doSearch(formUsers, filter, tenant, selectedOrganization);
}}
/>
<Select
options={organizationOptions}
placeholder="Select Organization"
value={
organizationOptions.find((o) => o.value == selectedOrganization?.id)?.value ?? ''
}
onChange={(value) => {
const organization = userinfo?.organizations.find((t) => t.id == value);
setSelectedOrganization(organization);
doSearch(formUsers, filter, selectedTenant, organization);
}}
/>
{!!tenantOptions.length && (
<Select
options={tenantOptions}
placeholder="Select Tenant"
value={tenantOptions.find((o) => o.value == selectedTenant?.id)?.value ?? ''}
onChange={(value) => {
const tenant = userinfo?.tenants.find((t) => t.id == value);
setSelectedTenant(tenant);
doSearch(formUsers, filter, tenant, selectedOrganization);
}}
/>
)}
{!!organizationOptions.length && (
<Select
options={organizationOptions}
placeholder="Select Organization"
value={
organizationOptions.find((o) => o.value == selectedOrganization?.id)?.value ?? ''
}
onChange={(value) => {
const organization = userinfo?.organizations.find((t) => t.id == value);
setSelectedOrganization(organization);
doSearch(formUsers, filter, selectedTenant, organization);
}}
/>
)}
</div>
<Info>
Enable user access to tenants and/or organizations, and make users administrators.
Expand Down
3 changes: 2 additions & 1 deletion src/dashboard/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { useAuth } from '@/hooks';
import { redirect } from 'next/navigation';

export default function Page() {
const { isClient, isHSB } = useAuth();
const { isClient, isHSB, isAuthorized } = useAuth();

// Redirect to default page for each type of user.
if (isClient) redirect('/client/dashboard');
else if (isHSB) redirect('/hsb/dashboard');
else if (isAuthorized) redirect('/welcome');

return (
<main>
Expand Down
12 changes: 8 additions & 4 deletions src/dashboard/src/components/charts/allocationTable/TableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export const TableRow: React.FC<TableRowProps> = ({
onClick,
}) => {
const percentageUsed = capacity ? Math.round(((capacity - available) / capacity) * 100) : 0;
const capacityValue = convertToStorageSize<string>(capacity, 'MB', 'TB');
const availableValue = convertToStorageSize<string>(available, 'MB', 'TB');
const capacityValue = convertToStorageSize<string>(capacity, 'B', 'TB');
const availableValue = convertToStorageSize<string>(available, 'B', 'TB');

return (
<div className={styles.row}>
Expand All @@ -39,8 +39,12 @@ export const TableRow: React.FC<TableRowProps> = ({
</p>
{showTenant ? <p title={tenant}>{tenant}</p> : ''}
<p title={os}>{os}</p>
<p className={styles.centered} title={capacityValue}>{capacityValue}</p>
<p className={styles.centered} title={availableValue}>{availableValue}</p>
<p className={styles.centered} title={capacityValue}>
{capacityValue}
</p>
<p className={styles.centered} title={availableValue}>
{availableValue}
</p>
</div>
<div className={styles.barChart}>
<div className={styles.bar}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export const BarChart: React.FC<IBarChartProps> = ({
? Math.min(100, Math.max(0, Math.round(((totalStorage - availableSpace) / totalStorage) * 100)))
: 0;
const usedStorage = totalStorage - availableSpace;
const usedStorageLabel = convertToStorageSize(usedStorage, 'MB', 'TB', {
const usedStorageLabel = convertToStorageSize(usedStorage, 'B', 'TB', {
formula: (value) => Number(value.toFixed(2)),
});
const totalStorageLabel = convertToStorageSize(totalStorage, 'MB', 'TB', {
const totalStorageLabel = convertToStorageSize(totalStorage, 'B', 'TB', {
formula: (value) => Number(value.toFixed(2)),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ export const useStorageTrendsData = (): ((
const items = group.items.filter((i) => i.serviceNowKey === volume.serviceNowKey);
const capacity = convertToStorageSize<number>(
items.length ? items[items.length - 1].capacity : 0,
'MB',
'B',
'GB',
{ type: 'number' },
);
const available = convertToStorageSize<number>(
items.length ? items[items.length - 1].availableSpace : 0,
'MB',
'B',
'GB',
{ type: 'number' },
);
Expand All @@ -141,23 +141,23 @@ export const useStorageTrendsData = (): ((
// The second dataset is an array of unused space grouped by month.
return [
{
label: `Used ${volume.name} (${convertToStorageSize(volume.capacity, 'MB', 'GB', {
label: `Used ${volume.name} (${convertToStorageSize(volume.capacity, 'B', 'GB', {
formula: (value) => Number(value.toFixed(1)),
})})`,
name: volume.name,
capacity: convertToStorageSize(volume.capacity, 'MB', 'GB', {
capacity: convertToStorageSize(volume.capacity, 'B', 'GB', {
formula: (value) => Number(value.toFixed(1)),
}),
data: groupData.map((group) => group.used), // Record of the volume data for each group (month).
backgroundColor: colors[0],
stack: `Stack ${index - 1}`,
},
{
label: `Unused ${volume.name} (${convertToStorageSize(volume.capacity, 'MB', 'GB', {
label: `Unused ${volume.name} (${convertToStorageSize(volume.capacity, 'B', 'GB', {
formula: (value) => Number(value.toFixed(1)),
})})`,
name: volume.name,
capacity: convertToStorageSize(volume.capacity, 'MB', 'GB', {
capacity: convertToStorageSize(volume.capacity, 'B', 'GB', {
formula: (value) => Number(value.toFixed(1)),
}),
data: groupData.map((group) => group.available), // Record of the volume data for each group (month).
Expand Down
6 changes: 3 additions & 3 deletions src/dashboard/src/components/charts/bar/smallBar/BarRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ interface IBarRowProps extends Omit<IBarChartRowData<unknown>, 'label'> {
export const BarRow: React.FC<IBarRowProps> = ({ label, capacity, available }) => {
const used = capacity - available;
const percentageUsed = capacity ? Math.round((used / capacity) * 100) : 0;
const capacityValue = convertToStorageSize(capacity, 'MB', 'TB');
const usedValue = convertToStorageSize(used, 'MB', 'TB');
const availableValue = convertToStorageSize(available, 'MB', 'TB');
const capacityValue = convertToStorageSize(capacity, 'B', 'TB');
const usedValue = convertToStorageSize(used, 'B', 'TB');
const availableValue = convertToStorageSize(available, 'B', 'TB');

return (
<div className={styles.row}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export const generateDoughnutChart = (
const availableCir = availablePercent ? (360 * availablePercent) / 100 : 0;

return {
space: convertToStorageSize<string>(space, 'MB', 'TB', {
space: convertToStorageSize<string>(space, 'B', 'TB', {
formula: Math.trunc,
}),
used: convertToStorageSize<string>(used, 'MB', 'TB', {
used: convertToStorageSize<string>(used, 'B', 'TB', {
formula: Math.trunc,
}),
available: convertToStorageSize<string>(available, 'MB', 'TB', {
available: convertToStorageSize<string>(available, 'B', 'TB', {
formula: Math.trunc,
}),
usedPercent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const TotalStorage = ({ serverItems, loading }: ITotalStorageProps) => {
let labelIndex = context.dataIndex;
let label = data.chart.labels?.[labelIndex] || '';
let value = context.parsed;
return `${label}: ${convertToStorageSize(value, 'MB', 'TB')}`;
return `${label}: ${convertToStorageSize(value, 'B', 'TB')}`;
},
title: function () {
return ''; // Return an empty string to remove the title for tooltips
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ export const generateDoughnutChart = (
const availablePercent = space ? (available / space) * 100 : 0;

return {
space: convertToStorageSize<string>(space, 'MB', 'TB', {
space: convertToStorageSize<string>(space, 'B', 'TB', {
formula: Math.trunc,
}),
used: convertToStorageSize<string>(used, 'MB', 'TB', {
used: convertToStorageSize<string>(used, 'B', 'TB', {
formula: Math.trunc,
}),
available: convertToStorageSize<string>(available, 'MB', 'TB', {
available: convertToStorageSize<string>(available, 'B', 'TB', {
formula: Math.trunc,
}),
usedPercent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ export const useStorageTrendsData = (): ((
values
.map((i) => i.capacity)
.reduce((result, value) => (result ?? 0) + (value ?? 0), 0) ?? 0,
'MB',
'B',
'TB',
{ type: 'number' },
);
group.availableSpace = convertToStorageSize<number>(
values
.map((i) => i.availableSpace)
.reduce((result, value) => (result ?? 0) + (value ?? 0), 0) ?? 0,
'MB',
'B',
'TB',
{ type: 'number' },
);
Expand Down
13 changes: 11 additions & 2 deletions src/dashboard/src/components/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ export const Dashboard = () => {
}

setValues((state) => ({ operatingSystemItem }));
await updateDashboard({ operatingSystemItem, applyFilter: true });
await updateDashboard({
operatingSystemItem,
serverItems: filteredServerItems,
applyFilter: true,
});
} else {
if (serverItems.length) {
const filteredServerItems = serverItems.filter(
Expand Down Expand Up @@ -241,7 +245,12 @@ export const Dashboard = () => {
: undefined;

setValues((state) => ({ organization, operatingSystemItem, serverItem }));
await updateDashboard({ organization, applyFilter: true });
await updateDashboard({
organization,
serverItems: filteredServerItems,
operatingSystemItems: filteredOperatingSystemItems,
applyFilter: true,
});
} else {
setFilteredOperatingSystemItems(operatingSystemItems);
if (serverItems.length) {
Expand Down
17 changes: 13 additions & 4 deletions src/dashboard/src/utils/convertToStorageSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/
export const convertToStorageSize = <T extends string | number>(
value: number,
input: 'TB' | 'GB' | 'MB' | 'KB' | '' = '',
output: 'TB' | 'GB' | 'MB' | 'KB' | '' = '',
input: 'TB' | 'GB' | 'MB' | 'KB' | 'B' | '' = '',
output: 'TB' | 'GB' | 'MB' | 'KB' | 'B' | '' = '',
options?:
| ({
formula?: (value: number) => number;
Expand All @@ -24,22 +24,31 @@ export const convertToStorageSize = <T extends string | number>(
if (output === 'GB') result = value * 1024;
else if (output === 'MB') result = value * Math.pow(1024, 2);
else if (output === 'KB') result = value * Math.pow(1024, 3);
else if (output === 'B') result = value * Math.pow(1024, 4);
else if (output === '') result = value * Math.pow(1024, 4);
} else if (input === 'GB') {
if (output === 'TB') result = value / 1024;
else if (output === 'MB') result = value * Math.pow(1024, 2);
else if (output === 'KB') result = value * Math.pow(1024, 3);
else if (output === 'B') result = value * Math.pow(1024, 4);
else if (output === '') result = value * Math.pow(1024, 4);
} else if (input === 'MB') {
if (output === 'TB') result = value / Math.pow(1024, 2);
else if (output === 'GB') result = value / 1024;
else if (output === 'KB') result = value * Math.pow(1024, 3);
else if (output === 'B') result = value * Math.pow(1024, 4);
else if (output === '') result = value * Math.pow(1024, 4);
} else if (input === 'KB') {
if (output === 'TB') result = value / Math.pow(1024, 3);
else if (output === 'GB') result = value / Math.pow(1024, 2);
else if (output === 'MB') result = value / 1024;
else if (output === 'B') result = value * Math.pow(1024, 4);
else if (output === '') result = value * Math.pow(1024, 4);
} else if (input === 'B') {
if (output === 'TB') result = value / Math.pow(1024, 4);
else if (output === 'GB') result = value / Math.pow(1024, 3);
else if (output === 'MB') result = value / Math.pow(1024, 2);
else if (output === 'KB') result = value / 1024;
} else if (input === '') {
if (output === 'TB') result = value / Math.pow(1024, 4);
else if (output === 'GB') result = value / Math.pow(1024, 3);
Expand Down Expand Up @@ -68,8 +77,8 @@ export const convertToStorageSize = <T extends string | number>(

export const reduceToType = (
value: number,
type: 'TB' | 'GB' | 'MB' | 'KB' | '' = '',
): { value: number; type: 'TB' | 'GB' | 'MB' | 'KB' | '' } => {
type: 'TB' | 'GB' | 'MB' | 'KB' | 'B' | '' = '',
): { value: number; type: 'TB' | 'GB' | 'MB' | 'KB' | 'B' | '' } => {
if (value >= 1) return { value, type };
// Downsize to the smaller type.
if (type === 'TB') {
Expand Down
1 change: 1 addition & 0 deletions src/libs/dal/HSB.DAL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ProjectReference Include="..\core\HSB.Core.csproj" />
<ProjectReference Include="..\entities\HSB.Entities.csproj" />
<ProjectReference Include="..\models\HSB.Models.csproj" />
<ProjectReference Include="..\keycloak\HSB.Keycloak.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions src/libs/dal/Services/FileSystemItemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public override EntityEntry<FileSystemItem> Add(FileSystemItem entity)
{
// TODO: File system items need to be removed otherwise this formula will be invalid over time.
var volumes = this.Context.FileSystemItems.AsNoTracking().Where(fsi => fsi.ServerItemServiceNowKey == entity.ServerItemServiceNowKey).ToArray();
server.Capacity = volumes.Sum(v => v.SizeBytes / 1024 / 1024) + (entity.SizeBytes / 1024 / 1024);
server.AvailableSpace = volumes.Sum(v => v.FreeSpaceBytes / 1024 / 1024) + (entity.FreeSpaceBytes / 1024 / 1024);
server.Capacity = volumes.Sum(v => v.SizeBytes) + entity.SizeBytes;
server.AvailableSpace = volumes.Sum(v => v.FreeSpaceBytes) + entity.FreeSpaceBytes;
this.Context.Entry(server).State = EntityState.Modified;

// Update current historical record too.
Expand Down Expand Up @@ -137,8 +137,8 @@ public override EntityEntry<FileSystemItem> Update(FileSystemItem entity)
{
// TODO: File system items need to be removed otherwise this formula will be invalid over time.
var volumes = this.Context.FileSystemItems.AsNoTracking().Where(fsi => fsi.ServerItemServiceNowKey == entity.ServerItemServiceNowKey).ToArray();
server.Capacity = volumes.Sum(v => v.SizeBytes / 1024 / 1024) + (entity.SizeBytes / 1024 / 1024);
server.AvailableSpace = volumes.Sum(v => v.FreeSpaceBytes / 1024 / 1024) + (entity.FreeSpaceBytes / 1024 / 1024);
server.Capacity = volumes.Sum(v => v.SizeBytes) + entity.SizeBytes;
server.AvailableSpace = volumes.Sum(v => v.FreeSpaceBytes) + entity.FreeSpaceBytes;
this.Context.Entry(server).State = EntityState.Modified;

// Update current historical record too.
Expand Down
Loading

0 comments on commit 1d76f65

Please sign in to comment.