fix: only lookup disk temp when requested

This commit is contained in:
Alexis Tyler
2021-06-14 14:22:40 +09:30
parent 03e0f1cfdd
commit cd245c9063
5 changed files with 46 additions and 6 deletions

View File

@@ -43,7 +43,7 @@ const getTemperature = async (disk: Systeminformation.DiskLayoutData): Promise<n
return Number.parseInt(line[line.length - 1], 10);
};
const parseDisk = async (disk: Systeminformation.DiskLayoutData, partitionsToParse: Systeminformation.BlockDevicesData[]): Promise<Disk> => {
const parseDisk = async (disk: Systeminformation.DiskLayoutData, partitionsToParse: Systeminformation.BlockDevicesData[], temperature = false): Promise<Disk> => {
const partitions = partitionsToParse
// Only get partitions from this disk
.filter(partition => partition.name.startsWith(disk.device.split('/dev/')[1]))
@@ -58,7 +58,7 @@ const parseDisk = async (disk: Systeminformation.DiskLayoutData, partitionsToPar
...disk,
smartStatus: uppercaseFirstChar(disk.smartStatus.toLowerCase()),
interfaceType: disk.interfaceType || 'UNKNOWN',
temperature: await getTemperature(disk),
temperature: temperature ? await getTemperature(disk) : -1,
partitions
};
};
@@ -70,7 +70,7 @@ interface Result extends CoreResult {
/**
* Get all disks.
*/
export const getDisks = async (context: CoreContext): Promise<Result> => {
export const getDisks = async (context: CoreContext, options?: { temperature: boolean }): Promise<Result> => {
const { user } = context;
// Check permissions
@@ -80,8 +80,19 @@ export const getDisks = async (context: CoreContext): Promise<Result> => {
possession: 'any'
});
// Return all fields but temperature
if (options?.temperature === false) {
const partitions = await blockDevices().then(devices => devices.filter(device => device.type === 'part'));
const disks = await asyncMap(await diskLayout(), async disk => parseDisk(disk, partitions));
return {
text: `Disks: ${JSON.stringify(disks, null, 2)}`,
json: disks
};
}
const partitions = await blockDevices().then(devices => devices.filter(device => device.type === 'part'));
const disks = await asyncMap(await diskLayout(), async disk => parseDisk(disk, partitions));
const disks = await asyncMap(await diskLayout(), async disk => parseDisk(disk, partitions, true));
return {
text: `Disks: ${JSON.stringify(disks, null, 2)}`,

View File

@@ -0,0 +1,22 @@
/*!
* Copyright 2021 Lime Technology Inc. All rights reserved.
* Written by: Alexis Tyler
*/
import graphqlFields from 'graphql-fields';
import { getDisks } from '../../../core/modules/get-disks';
import { CoreContext } from '../../../core/types';
interface Context extends CoreContext {
params: {
username: string;
};
data: {
password: string;
};
}
export default async (_: unknown, args: unknown, context: Context, info: any) => {
const topLevelFields = Object.keys(graphqlFields(info));
return getDisks(context, { temperature: topLevelFields.includes('temperature') });
};

View File

@@ -2,7 +2,7 @@ type Query {
"""Single disk"""
disk(id: ID!): Disk @func(module: "getDisk")
"""Mulitiple disks"""
disks: [Disk]! @func(module: "getDisks")
disks: [Disk]!
}
type Disk {
# /dev/sdb

5
package-lock.json generated
View File

@@ -8073,6 +8073,11 @@
"apollo-server-types": "^0.6.3"
}
},
"graphql-fields": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/graphql-fields/-/graphql-fields-2.0.3.tgz",
"integrity": "sha512-x3VE5lUcR4XCOxPIqaO4CE+bTK8u6gVouOdpQX9+EKHr+scqtK5Pp/l8nIGqIpN1TUlkKE6jDCCycm/WtLRAwA=="
},
"graphql-iso-date": {
"version": "3.6.1",
"resolved": "https://registry.npmjs.org/graphql-iso-date/-/graphql-iso-date-3.6.1.tgz",

View File

@@ -81,6 +81,7 @@
"globby": "^11.0.3",
"graphql": "^15.5.0",
"graphql-directive": "^0.2.1",
"graphql-fields": "^2.0.3",
"graphql-iso-date": "^3.6.1",
"graphql-subscriptions-client": "OmgImAlexis/graphql-subscriptions-client#master",
"graphql-tag": "^2.11.0",
@@ -218,6 +219,7 @@
"globby",
"graphql",
"graphql-directive",
"graphql-fields",
"graphql-iso-date",
"graphql-subscriptions-client",
"graphql-tag",
@@ -271,4 +273,4 @@
"uuid-apikey",
"xhr2"
]
}
}