| 4 | import { ILifeCycleTask } from "../../instance/life_cycle"; |
| 5 | |
| 6 | export default class DockerStatsTask implements ILifeCycleTask { |
| 7 | private static defaultDocker = new DefaultDocker(); |
| 8 | |
| 9 | public status: number = 0; |
| 10 | public name: string = "DockerStats"; |
| 11 | private task: NodeJS.Timeout | null = null; |
| 12 | private isUpdating = false; |
| 13 | private lastStatsMap: Map<string, { [key: string]: number | undefined; timestamp: number }> = |
| 14 | new Map(); |
| 15 | |
| 16 | private calculateRealTimeRate<T extends Record<string, number | undefined>>( |
| 17 | currentValues: T, |
| 18 | statsName: string |
| 19 | ): T { |
| 20 | const currentTimestamp = Date.now(); |
| 21 | const result = {} as T; |
| 22 | const lastStats = this.lastStatsMap.get(statsName); |
| 23 | |
| 24 | if (lastStats) { |
| 25 | const timeDelta = (currentTimestamp - lastStats.timestamp) / 1000; |
| 26 | |
| 27 | if (timeDelta > 0) { |
| 28 | for (const key in currentValues) { |
| 29 | const currentValue = currentValues[key]; |
| 30 | const lastValue = lastStats[key]; |
| 31 | |
| 32 | if (currentValue !== undefined && lastValue !== undefined) { |
| 33 | result[key] = Math.max(0, (currentValue - lastValue) / timeDelta) as T[Extract< |
| 34 | keyof T, |
| 35 | string |
| 36 | >]; |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | this.lastStatsMap.set(statsName, { |
| 43 | ...currentValues, |
| 44 | timestamp: currentTimestamp |
| 45 | }); |
| 46 | |
| 47 | return result; |
| 48 | } |
| 49 | |
| 50 | private getDockerNetworkCumulative(networks?: Dockerode.NetworkStats) { |
| 51 | if (!networks || typeof networks !== "object") { |
| 52 | return { |
| 53 | rxBytes: undefined, |
| 54 | txBytes: undefined, |
| 55 | interfaceNames: undefined, |
| 56 | source: "docker" as const |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | let rxBytes = 0; |
| 61 | let txBytes = 0; |
| 62 | let hasNetworkData = false; |
| 63 | const interfaceNames: string[] = []; |
nothing calls this directly
no outgoing calls
no test coverage detected