From 7de23ce68d786fb7671aa099ce8887c0db4ced77 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Thu, 5 Dec 2024 15:59:50 -0500 Subject: [PATCH] move: SmolUtil and ServerHealthService --- src/backend/src/CoreModule.js | 2 -- src/backend/src/modules/core/Core2Module.js | 3 +++ .../core}/ServerHealthService.js | 9 ++++++--- src/backend/src/modules/core/lib/__lib__.js | 1 + src/backend/src/{util => modules/core/lib}/linux.js | 7 ++++--- .../src/modules/puterai/OpenAICompletionService.js | 6 +++--- src/backend/src/services/TrackSpendingService.js | 4 ++-- src/putility/index.js | 1 + .../src/util/smolutil.js => putility/src/libs/smol.js} | 1 + 9 files changed, 21 insertions(+), 13 deletions(-) rename src/backend/src/{services/runtime-analysis => modules/core}/ServerHealthService.js (97%) rename src/backend/src/{util => modules/core/lib}/linux.js (84%) rename src/{backend/src/util/smolutil.js => putility/src/libs/smol.js} (99%) diff --git a/src/backend/src/CoreModule.js b/src/backend/src/CoreModule.js index b4e2a31e..d3dc5fe5 100644 --- a/src/backend/src/CoreModule.js +++ b/src/backend/src/CoreModule.js @@ -119,7 +119,6 @@ const install = async ({ services, app, useapi, modapi }) => { const { ESBuilder } = require('./om/entitystorage/ESBuilder'); const { Eq, Or } = require('./om/query/query'); const { TrackSpendingService } = require('./services/TrackSpendingService'); - const { ServerHealthService } = require('./services/runtime-analysis/ServerHealthService'); const { MakeProdDebuggingLessAwfulService } = require('./services/MakeProdDebuggingLessAwfulService'); const { ConfigurableCountingService } = require('./services/ConfigurableCountingService'); const { FSLockService } = require('./services/fs/FSLockService'); @@ -134,7 +133,6 @@ const install = async ({ services, app, useapi, modapi }) => { // === Services which extend BaseService === services.registerService('system-validation', SystemValidationService); - services.registerService('server-health', ServerHealthService); services.registerService('commands', CommandService); services.registerService('__api-filesystem', FilesystemAPIService); services.registerService('__api', PuterAPIService); diff --git a/src/backend/src/modules/core/Core2Module.js b/src/backend/src/modules/core/Core2Module.js index 458f2b6a..e4c70779 100644 --- a/src/backend/src/modules/core/Core2Module.js +++ b/src/backend/src/modules/core/Core2Module.js @@ -36,6 +36,9 @@ class Core2Module extends AdvancedBase { const { ProcessEventService } = require("./ProcessEventService.js"); services.registerService('process-event', ProcessEventService); + + const { ServerHealthService } = require("./ServerHealthService.js"); + services.registerService('server-health', ServerHealthService); } } diff --git a/src/backend/src/services/runtime-analysis/ServerHealthService.js b/src/backend/src/modules/core/ServerHealthService.js similarity index 97% rename from src/backend/src/services/runtime-analysis/ServerHealthService.js rename to src/backend/src/modules/core/ServerHealthService.js index 34fadf8b..11bdbd15 100644 --- a/src/backend/src/services/runtime-analysis/ServerHealthService.js +++ b/src/backend/src/modules/core/ServerHealthService.js @@ -17,9 +17,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -const BaseService = require("../BaseService"); +const BaseService = require("../../services/BaseService"); const { time, promise } = require("@heyputer/putility").libs; -const { parse_meminfo } = require("../../util/linux"); /** @@ -34,6 +33,10 @@ const { parse_meminfo } = require("../../util/linux"); * from `/proc/meminfo` and handling alarms via an external 'alarm' service. */ class ServerHealthService extends BaseService { + static USE = { + linuxutil: 'core.util.linuxutil' + }; + static MODULES = { fs: require('fs'), } @@ -95,7 +98,7 @@ class ServerHealthService extends BaseService { const meminfo_text = await this.modules.fs.promises.readFile( '/proc/meminfo', 'utf8' ); - const meminfo = parse_meminfo(meminfo_text); + const meminfo = this.linuxutil.parse_meminfo(meminfo_text); const alarm_fields = { mem_free: meminfo.MemFree, mem_available: meminfo.MemAvailable, diff --git a/src/backend/src/modules/core/lib/__lib__.js b/src/backend/src/modules/core/lib/__lib__.js index 30829984..25d503ea 100644 --- a/src/backend/src/modules/core/lib/__lib__.js +++ b/src/backend/src/modules/core/lib/__lib__.js @@ -3,6 +3,7 @@ module.exports = { logutil: require('./log.js'), identutil: require('./identifier.js'), stdioutil: require('./stdio.js'), + linuxutil: require('./linux.js'), }, expect: require('./expect.js'), }; diff --git a/src/backend/src/util/linux.js b/src/backend/src/modules/core/lib/linux.js similarity index 84% rename from src/backend/src/util/linux.js rename to src/backend/src/modules/core/lib/linux.js index 546be561..5695a5f1 100644 --- a/src/backend/src/util/linux.js +++ b/src/backend/src/modules/core/lib/linux.js @@ -16,7 +16,7 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -const SmolUtil = require("./smolutil"); +const smol = require('@heyputer/putility').libs.smol; const parse_meminfo = text => { const lines = text.split('\n'); @@ -26,8 +26,8 @@ const parse_meminfo = text => { for ( const line of lines ) { if ( line.trim().length == 0 ) continue; - const [key, value_and_unit] = SmolUtil.split(line, ':', { trim: true }); - const [value, _] = SmolUtil.split(value_and_unit, ' ', { trim: true }); + const [key, value_and_unit] = smol.split(line, ':', { trim: true }); + const [value, _] = smol.split(value_and_unit, ' ', { trim: true }); // note: unit is always 'kB' so we discard it meminfo[key] = Number.parseInt(value); } @@ -38,3 +38,4 @@ const parse_meminfo = text => { module.exports = { parse_meminfo, }; + diff --git a/src/backend/src/modules/puterai/OpenAICompletionService.js b/src/backend/src/modules/puterai/OpenAICompletionService.js index 881cd7f6..c5d95af5 100644 --- a/src/backend/src/modules/puterai/OpenAICompletionService.js +++ b/src/backend/src/modules/puterai/OpenAICompletionService.js @@ -4,7 +4,7 @@ const APIError = require('../../api/APIError'); const BaseService = require('../../services/BaseService'); const { TypedValue } = require('../../services/drivers/meta/Runtime'); const { Context } = require('../../util/context'); -const SmolUtil = require('../../util/smolutil'); +const smol = require('@heyputer/putility').libs.smol; const { nou } = require('../../util/langutil'); const { TeePromise } = require('@heyputer/putility').libs.promise; @@ -300,7 +300,7 @@ class OpenAICompletionService extends BaseService { if ( ! msg.content ) continue; if ( typeof msg.content !== 'object' ) continue; - const content = SmolUtil.ensure_array(msg.content); + const content = smol.ensure_array(msg.content); for ( const o of content ) { if ( ! o.hasOwnProperty('image_url') ) continue; @@ -326,7 +326,7 @@ class OpenAICompletionService extends BaseService { if ( ! msg.content ) continue; if ( typeof msg.content !== 'object' ) continue; - const content = SmolUtil.ensure_array(msg.content); + const content = smol.ensure_array(msg.content); for ( const o of content ) { // console.log('part of content', o); diff --git a/src/backend/src/services/TrackSpendingService.js b/src/backend/src/services/TrackSpendingService.js index 3c8e32af..e73bddb5 100644 --- a/src/backend/src/services/TrackSpendingService.js +++ b/src/backend/src/services/TrackSpendingService.js @@ -18,7 +18,7 @@ * along with this program. If not, see . */ const { TimeWindow } = require("../util/opmath"); -const SmolUtil = require("../util/smolutil"); +const smol = require('@heyputer/putility').libs.smol; const { format_as_usd } = require('@heyputer/putility').libs.string; const { MINUTE, SECOND } = require("@heyputer/putility").libs.time; const BaseService = require("./BaseService"); @@ -77,7 +77,7 @@ class TrackSpendingService extends BaseService { const input_tokens = data.count_tokens_input ?? 0; const output_tokens = data.count_tokens_output ?? 0; - const cost = SmolUtil.add( + const cost = smol.add( this.multiply_by_ratio_(input_tokens, cost_per_input_token), this.multiply_by_ratio_(output_tokens, cost_per_output_token), ); diff --git a/src/putility/index.js b/src/putility/index.js index 95e1deae..4390bd39 100644 --- a/src/putility/index.js +++ b/src/putility/index.js @@ -33,6 +33,7 @@ module.exports = { log: require('./src/libs/log'), string: require('./src/libs/string'), time: require('./src/libs/time'), + smol: require('./src/libs/smol'), }, concepts: { Service, diff --git a/src/backend/src/util/smolutil.js b/src/putility/src/libs/smol.js similarity index 99% rename from src/backend/src/util/smolutil.js rename to src/putility/src/libs/smol.js index 12942eaf..fb9c52e9 100644 --- a/src/backend/src/util/smolutil.js +++ b/src/putility/src/libs/smol.js @@ -49,3 +49,4 @@ class SmolUtil { } module.exports = SmolUtil; +