mirror of
https://github.com/HeyPuter/puter.git
synced 2026-05-29 21:01:27 +00:00
dev: update usages tab
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
const BaseService = require("../../services/BaseService");
|
||||
|
||||
/**
|
||||
* PermissiveCreditService listens to the event where DriverService asks
|
||||
* for a credit context, and always provides one that allows use of
|
||||
* cost-incurring services for no charge. This grants free use to
|
||||
* everyone to services that incur a cost, as long as the user has
|
||||
* permission to call the respective service.
|
||||
*/
|
||||
class PermissiveCreditService extends BaseService {
|
||||
static MODULES = {
|
||||
uuidv4: require('uuid').v4,
|
||||
}
|
||||
_init () {
|
||||
// Maps usernames to simulated credit amounts
|
||||
// (used when config.simulated_credit is set)
|
||||
this.simulated_credit_ = {};
|
||||
|
||||
const svc_event = this.services.get('event');
|
||||
svc_event.on(`credit.check-available`, (_, event) => {
|
||||
const username = event.actor.type.user.username;
|
||||
event.available = this.get_user_credit_(username);
|
||||
|
||||
// Useful for testing with Dall-E
|
||||
// event.available = 4 * Math.pow(10,6);
|
||||
|
||||
// Useful for testing with Polly
|
||||
// event.available = 9000;
|
||||
|
||||
// Useful for testing judge0
|
||||
// event.available = 50_000;
|
||||
// event.avaialble = 49_999;
|
||||
|
||||
// Useful for testing ConvertAPI
|
||||
// event.available = 4_500_000;
|
||||
// event.available = 4_499_999;
|
||||
|
||||
// Useful for testing with textract
|
||||
// event.available = 150_000;
|
||||
// event.available = 149_999;
|
||||
});
|
||||
|
||||
svc_event.on('credit.record-cost', (_, event) => {
|
||||
const username = event.actor.type.user.username;
|
||||
event.available = this.consume_user_credit_(
|
||||
username, event.cost);
|
||||
});
|
||||
|
||||
svc_event.on('usages.query', (_, event) => {
|
||||
const username = event.actor.type.user.username;
|
||||
if ( ! this.config.simulated_credit ) {
|
||||
event.usages.push({
|
||||
name: `Unlimited Credit`,
|
||||
used: 0,
|
||||
available: 1,
|
||||
});
|
||||
return;
|
||||
}
|
||||
event.usages.push({
|
||||
name: `Simulated Credit (${this.config.simulated_credit})`,
|
||||
used: this.config.simulated_credit -
|
||||
this.get_user_credit_(username),
|
||||
available: this.config.simulated_credit,
|
||||
});
|
||||
});
|
||||
}
|
||||
get_user_credit_ (username) {
|
||||
if ( ! this.config.simulated_credit ) {
|
||||
return Number.MAX_SAFE_INTEGER;
|
||||
}
|
||||
|
||||
return this.simulated_credit_[username] ??
|
||||
(this.simulated_credit_[username] = this.config.simulated_credit);
|
||||
|
||||
}
|
||||
consume_user_credit_ (username, amount) {
|
||||
if ( ! this.config.simulated_credit ) return;
|
||||
|
||||
if ( ! this.simulated_credit_[username] ) {
|
||||
this.simulated_credit_[username] = this.config.simulated_credit;
|
||||
}
|
||||
this.simulated_credit_[username] -= amount;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PermissiveCreditService;
|
||||
@@ -1,41 +0,0 @@
|
||||
const BaseService = require("../../services/BaseService");
|
||||
|
||||
/**
|
||||
* PermissiveCreditService listens to the event where DriverService asks
|
||||
* for a credit context, and always provides one that allows use of
|
||||
* cost-incurring services for no charge. This grants free use to
|
||||
* everyone to services that incur a cost, as long as the user has
|
||||
* permission to call the respective service.
|
||||
*/
|
||||
class PermissiveCreditService extends BaseService {
|
||||
static MODULES = {
|
||||
uuidv4: require('uuid').v4,
|
||||
}
|
||||
_init () {
|
||||
const svc_event = this.services.get('event');
|
||||
svc_event.on(`credit.check-available`, (_, event) => {
|
||||
// Useful for testing with Dall-E
|
||||
// event.available = 4 * Math.pow(10,6);
|
||||
|
||||
// Useful for testing with Polly
|
||||
// event.available = 9000;
|
||||
|
||||
// Useful for testing judge0
|
||||
// event.available = 50_000;
|
||||
// event.avaialble = 49_999;
|
||||
|
||||
// Useful for testing ConvertAPI
|
||||
// event.available = 4_500_000;
|
||||
// event.available = 4_499_999;
|
||||
|
||||
|
||||
// Useful for testing with textract
|
||||
// event.available = 150_000;
|
||||
// event.available = 149_999;
|
||||
|
||||
event.available = Number.MAX_SAFE_INTEGER;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PermissiveCreditService;
|
||||
@@ -35,8 +35,8 @@ class SelfHostedModule extends AdvancedBase {
|
||||
const DevWatcherService = require('./DevWatcherService');
|
||||
const path_ = require('path');
|
||||
|
||||
const PermissiveCreditService = require("./PermissiveCreditService");
|
||||
services.registerService('__permissive-credit', PermissiveCreditService);
|
||||
const DevCreditService = require("./DevCreditService");
|
||||
services.registerService('dev-credit', DevCreditService);
|
||||
|
||||
const { DBKVService } = require("../../services/DBKVService");
|
||||
services.registerService('puter-kvstore', DBKVService);
|
||||
|
||||
@@ -89,40 +89,6 @@ export default {
|
||||
`;
|
||||
});
|
||||
|
||||
// Loop through user services
|
||||
res.user.forEach(service => {
|
||||
const { monthly_limit, monthly_usage } = service;
|
||||
let usageDisplay = ``;
|
||||
|
||||
const first_identifier = false ||
|
||||
service.service['driver.implementation'] ||
|
||||
service.service['driver.interface'] ||
|
||||
'';
|
||||
|
||||
if (monthly_limit !== null) {
|
||||
let usage_percentage = (monthly_usage / monthly_limit * 100).toFixed(0);
|
||||
usage_percentage = usage_percentage > 100 ? 100 : usage_percentage; // Cap at 100%
|
||||
usageDisplay = `
|
||||
<div class="driver-usage" style="margin-bottom: 10px;">
|
||||
<h3 style="margin-bottom: 5px; font-size: 14px;">${html_encode(first_identifier)} (${html_encode(service.service['driver.method'])}):</h3>
|
||||
<span style="font-size: 13px; margin-bottom: 3px;">${monthly_usage} used of ${monthly_limit}</span>
|
||||
<div class="usage-progbar-wrapper" style="width: 100%;">
|
||||
<div class="usage-progbar" style="width: ${usage_percentage}%;"><span class="usage-progbar-percent">${usage_percentage}%</span></div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
else {
|
||||
usageDisplay = `
|
||||
<div class="driver-usage" style="margin-bottom: 10px;">
|
||||
<h3 style="margin-bottom: 5px; font-size: 14px;">${html_encode(first_identifier)} (${html_encode(service.service['driver.method'])}):</h3>
|
||||
<span style="font-size: 13px; margin-bottom: 3px;">${i18n('usage')}: ${monthly_usage} (${i18n('unlimited')})</span>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
h += usageDisplay;
|
||||
});
|
||||
|
||||
// Append driver usage bars to the container
|
||||
$('.settings-content[data-settings="usage"]').append(`<div class="driver-usage-container">${h}</div>`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user