-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement connection to the perf monitoring service
- Loading branch information
Showing
11 changed files
with
358 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
schema: | ||
- 'http://localhost:8082' | ||
documents: | ||
- './src/external-api/performance-monitoring-graphql.ts' | ||
generates: | ||
src/__generated__/perf-monitoring.graphql.ts: | ||
plugins: | ||
- 'typescript' | ||
- 'typescript-operations' | ||
config: | ||
enumsAsTypes: true | ||
avoidOptionals: | ||
field: true | ||
object: false | ||
inputValue: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { gql, GraphQLClient } from 'graphql-request'; | ||
import config from '../config'; | ||
import { | ||
CurrentCpuUsageQuery, | ||
CurrentCpuUsageQueryVariables, | ||
CurrentCpuUsagesQuery, | ||
CurrentCpuUsagesQueryVariables, | ||
CurrentMemoryUsageQuery, | ||
CurrentMemoryUsageQueryVariables, | ||
CurrentMemoryUsagesQuery, | ||
CurrentMemoryUsagesQueryVariables, | ||
} from '../__generated__/perf-monitoring.graphql'; | ||
|
||
export type DeviceLoadUsage = { | ||
cpuUsage: number | null; | ||
memoryUsage: number | null; | ||
}; | ||
|
||
const DEVICE_MEMORY_USAGES = gql` | ||
query CurrentMemoryUsages($names: [String!]!) { | ||
currentMemoryUsages(devices: $names) { | ||
device | ||
usage | ||
} | ||
} | ||
`; | ||
|
||
const DEVICE_CPU_USAGES = gql` | ||
query CurrentCpuUsages($names: [String!]!) { | ||
currentCpuUsages(devices: $names) { | ||
device | ||
usage | ||
} | ||
} | ||
`; | ||
|
||
const DEVICE_MEMORY_USAGE = gql` | ||
query CurrentMemoryUsage($name: String!) { | ||
currentMemoryUsage(device: $name) { | ||
device | ||
usage | ||
} | ||
} | ||
`; | ||
|
||
const DEVICE_CPU_USAGE = gql` | ||
query CurrentCpuUsage($name: String!) { | ||
currentCpuUsage(device: $name) { | ||
device | ||
usage | ||
} | ||
} | ||
`; | ||
|
||
function getPerformanceMonitoringAPI() { | ||
if (config.topologyEnabled === false) { | ||
return undefined; | ||
} | ||
|
||
const client = new GraphQLClient(config.performanceMonitoringGraphqlURL); | ||
|
||
async function getDeviceCpuUsage(deviceName: string): Promise<CurrentCpuUsageQuery> { | ||
const result = await client.request<CurrentCpuUsageQuery, CurrentCpuUsageQueryVariables>(DEVICE_CPU_USAGE, { | ||
name: deviceName, | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
async function getDeviceMemoryUsage(deviceName: string): Promise<CurrentMemoryUsageQuery> { | ||
const result = await client.request<CurrentMemoryUsageQuery, CurrentMemoryUsageQueryVariables>( | ||
DEVICE_MEMORY_USAGE, | ||
{ name: deviceName }, | ||
); | ||
|
||
return result; | ||
} | ||
|
||
async function getDeviceCpuUsages(deviceNames: string[]): Promise<CurrentCpuUsagesQuery> { | ||
const result = await client.request<CurrentCpuUsagesQuery, CurrentCpuUsagesQueryVariables>(DEVICE_CPU_USAGES, { | ||
names: deviceNames, | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
async function getDeviceMemoryUsages(deviceNames: string[]): Promise<CurrentMemoryUsagesQuery> { | ||
const result = await client.request<CurrentMemoryUsagesQuery, CurrentMemoryUsagesQueryVariables>( | ||
DEVICE_MEMORY_USAGES, | ||
{ names: deviceNames }, | ||
); | ||
|
||
return result; | ||
} | ||
|
||
async function getDeviceLoadUsage(deviceName: string): Promise<DeviceLoadUsage> { | ||
const [cpuUsage, memoryUsage] = await Promise.all([ | ||
getDeviceCpuUsage(deviceName), | ||
getDeviceMemoryUsage(deviceName), | ||
]); | ||
|
||
return { cpuUsage: cpuUsage.currentCpuUsage.usage, memoryUsage: memoryUsage.currentMemoryUsage.usage }; | ||
} | ||
|
||
async function getDeviceLoadUsages(deviceNames: string[]): Promise<(DeviceLoadUsage & { deviceName: string })[]> { | ||
const [cpuUsages, memoryUsages] = await Promise.all([ | ||
getDeviceCpuUsages(deviceNames), | ||
getDeviceMemoryUsages(deviceNames), | ||
]); | ||
|
||
const map = new Map<string, DeviceLoadUsage & { deviceName: string }>(); | ||
|
||
cpuUsages.currentCpuUsages?.forEach((cpuUsage) => { | ||
map.set(cpuUsage.device, { deviceName: cpuUsage.device, cpuUsage: cpuUsage.usage, memoryUsage: null }); | ||
}); | ||
|
||
memoryUsages.currentMemoryUsages?.forEach((memoryUsage) => { | ||
const deviceLoadUsage = map.get(memoryUsage.device); | ||
if (deviceLoadUsage) { | ||
deviceLoadUsage.memoryUsage = memoryUsage.usage; | ||
} else { | ||
map.set(memoryUsage.device, { deviceName: memoryUsage.device, cpuUsage: null, memoryUsage: memoryUsage.usage }); | ||
} | ||
}); | ||
|
||
return Array.from(map.values()); | ||
} | ||
|
||
return { getDeviceLoadUsage, getDeviceLoadUsages }; | ||
} | ||
|
||
export type PerformanceMonitoringAPI = ReturnType<typeof getPerformanceMonitoringAPI>; | ||
export default getPerformanceMonitoringAPI; |
Oops, something went wrong.