From cd245c9063f6aac65d7cd4ffec73f61b33c2e9e3 Mon Sep 17 00:00:00 2001 From: Alexis Tyler Date: Mon, 14 Jun 2021 14:22:40 +0930 Subject: [PATCH] fix: only lookup disk temp when requested --- app/core/modules/get-disks.ts | 19 ++++++++++++++---- app/graphql/resolvers/query/disks.ts | 22 +++++++++++++++++++++ app/graphql/schema/types/disks/disk.graphql | 2 +- package-lock.json | 5 +++++ package.json | 4 +++- 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 app/graphql/resolvers/query/disks.ts diff --git a/app/core/modules/get-disks.ts b/app/core/modules/get-disks.ts index 2301d5b22..2d362d042 100644 --- a/app/core/modules/get-disks.ts +++ b/app/core/modules/get-disks.ts @@ -43,7 +43,7 @@ const getTemperature = async (disk: Systeminformation.DiskLayoutData): Promise => { +const parseDisk = async (disk: Systeminformation.DiskLayoutData, partitionsToParse: Systeminformation.BlockDevicesData[], temperature = false): Promise => { 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 => { +export const getDisks = async (context: CoreContext, options?: { temperature: boolean }): Promise => { const { user } = context; // Check permissions @@ -80,8 +80,19 @@ export const getDisks = async (context: CoreContext): Promise => { 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)}`, diff --git a/app/graphql/resolvers/query/disks.ts b/app/graphql/resolvers/query/disks.ts new file mode 100644 index 000000000..b665e0525 --- /dev/null +++ b/app/graphql/resolvers/query/disks.ts @@ -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') }); +}; diff --git a/app/graphql/schema/types/disks/disk.graphql b/app/graphql/schema/types/disks/disk.graphql index 7bbb6568c..a4b691b1c 100644 --- a/app/graphql/schema/types/disks/disk.graphql +++ b/app/graphql/schema/types/disks/disk.graphql @@ -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 diff --git a/package-lock.json b/package-lock.json index 01a12c3d8..0ea2f4b22 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index c97f419e7..4a66dd6a8 100644 --- a/package.json +++ b/package.json @@ -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" ] -} +} \ No newline at end of file