move: SmolUtil and ServerHealthService

This commit is contained in:
KernelDeimos 2024-12-05 15:59:50 -05:00
parent f9c6f82440
commit 7de23ce68d
9 changed files with 21 additions and 13 deletions

View File

@ -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);

View File

@ -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);
}
}

View File

@ -17,9 +17,8 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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,

View File

@ -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'),
};

View File

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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,
};

View File

@ -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);

View File

@ -18,7 +18,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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),
);

View File

@ -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,

View File

@ -49,3 +49,4 @@ class SmolUtil {
}
module.exports = SmolUtil;