Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0176a1f

Browse filesBrowse files
committed
dev: update usages tab
1 parent b584801 commit 0176a1f
Copy full SHA for 0176a1f

File tree

4 files changed

+88
-77
lines changed
Filter options

4 files changed

+88
-77
lines changed
+86Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const BaseService = require("../../services/BaseService");
2+
3+
/**
4+
* PermissiveCreditService listens to the event where DriverService asks
5+
* for a credit context, and always provides one that allows use of
6+
* cost-incurring services for no charge. This grants free use to
7+
* everyone to services that incur a cost, as long as the user has
8+
* permission to call the respective service.
9+
*/
10+
class PermissiveCreditService extends BaseService {
11+
static MODULES = {
12+
uuidv4: require('uuid').v4,
13+
}
14+
_init () {
15+
// Maps usernames to simulated credit amounts
16+
// (used when config.simulated_credit is set)
17+
this.simulated_credit_ = {};
18+
19+
const svc_event = this.services.get('event');
20+
svc_event.on(`credit.check-available`, (_, event) => {
21+
const username = event.actor.type.user.username;
22+
event.available = this.get_user_credit_(username);
23+
24+
// Useful for testing with Dall-E
25+
// event.available = 4 * Math.pow(10,6);
26+
27+
// Useful for testing with Polly
28+
// event.available = 9000;
29+
30+
// Useful for testing judge0
31+
// event.available = 50_000;
32+
// event.avaialble = 49_999;
33+
34+
// Useful for testing ConvertAPI
35+
// event.available = 4_500_000;
36+
// event.available = 4_499_999;
37+
38+
// Useful for testing with textract
39+
// event.available = 150_000;
40+
// event.available = 149_999;
41+
});
42+
43+
svc_event.on('credit.record-cost', (_, event) => {
44+
const username = event.actor.type.user.username;
45+
event.available = this.consume_user_credit_(
46+
username, event.cost);
47+
});
48+
49+
svc_event.on('usages.query', (_, event) => {
50+
const username = event.actor.type.user.username;
51+
if ( ! this.config.simulated_credit ) {
52+
event.usages.push({
53+
name: `Unlimited Credit`,
54+
used: 0,
55+
available: 1,
56+
});
57+
return;
58+
}
59+
event.usages.push({
60+
name: `Simulated Credit (${this.config.simulated_credit})`,
61+
used: this.config.simulated_credit -
62+
this.get_user_credit_(username),
63+
available: this.config.simulated_credit,
64+
});
65+
});
66+
}
67+
get_user_credit_ (username) {
68+
if ( ! this.config.simulated_credit ) {
69+
return Number.MAX_SAFE_INTEGER;
70+
}
71+
72+
return this.simulated_credit_[username] ??
73+
(this.simulated_credit_[username] = this.config.simulated_credit);
74+
75+
}
76+
consume_user_credit_ (username, amount) {
77+
if ( ! this.config.simulated_credit ) return;
78+
79+
if ( ! this.simulated_credit_[username] ) {
80+
this.simulated_credit_[username] = this.config.simulated_credit;
81+
}
82+
this.simulated_credit_[username] -= amount;
83+
}
84+
}
85+
86+
module.exports = PermissiveCreditService;

‎src/backend/src/modules/selfhosted/PermissiveCreditService.js

Copy file name to clipboardExpand all lines: src/backend/src/modules/selfhosted/PermissiveCreditService.js
-41Lines changed: 0 additions & 41 deletions
This file was deleted.

‎src/backend/src/modules/selfhosted/SelfHostedModule.js

Copy file name to clipboardExpand all lines: src/backend/src/modules/selfhosted/SelfHostedModule.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class SelfHostedModule extends AdvancedBase {
3535
const DevWatcherService = require('./DevWatcherService');
3636
const path_ = require('path');
3737

38-
const PermissiveCreditService = require("./PermissiveCreditService");
39-
services.registerService('__permissive-credit', PermissiveCreditService);
38+
const DevCreditService = require("./DevCreditService");
39+
services.registerService('dev-credit', DevCreditService);
4040

4141
const { DBKVService } = require("../../services/DBKVService");
4242
services.registerService('puter-kvstore', DBKVService);

‎src/gui/src/UI/Settings/UITabUsage.js

Copy file name to clipboardExpand all lines: src/gui/src/UI/Settings/UITabUsage.js
-34Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -89,40 +89,6 @@ export default {
8989
`;
9090
});
9191

92-
// Loop through user services
93-
res.user.forEach(service => {
94-
const { monthly_limit, monthly_usage } = service;
95-
let usageDisplay = ``;
96-
97-
const first_identifier = false ||
98-
service.service['driver.implementation'] ||
99-
service.service['driver.interface'] ||
100-
'';
101-
102-
if (monthly_limit !== null) {
103-
let usage_percentage = (monthly_usage / monthly_limit * 100).toFixed(0);
104-
usage_percentage = usage_percentage > 100 ? 100 : usage_percentage; // Cap at 100%
105-
usageDisplay = `
106-
<div class="driver-usage" style="margin-bottom: 10px;">
107-
<h3 style="margin-bottom: 5px; font-size: 14px;">${html_encode(first_identifier)} (${html_encode(service.service['driver.method'])}):</h3>
108-
<span style="font-size: 13px; margin-bottom: 3px;">${monthly_usage} used of ${monthly_limit}</span>
109-
<div class="usage-progbar-wrapper" style="width: 100%;">
110-
<div class="usage-progbar" style="width: ${usage_percentage}%;"><span class="usage-progbar-percent">${usage_percentage}%</span></div>
111-
</div>
112-
</div>
113-
`;
114-
}
115-
else {
116-
usageDisplay = `
117-
<div class="driver-usage" style="margin-bottom: 10px;">
118-
<h3 style="margin-bottom: 5px; font-size: 14px;">${html_encode(first_identifier)} (${html_encode(service.service['driver.method'])}):</h3>
119-
<span style="font-size: 13px; margin-bottom: 3px;">${i18n('usage')}: ${monthly_usage} (${i18n('unlimited')})</span>
120-
</div>
121-
`;
122-
}
123-
h += usageDisplay;
124-
});
125-
12692
// Append driver usage bars to the container
12793
$('.settings-content[data-settings="usage"]').append(`<div class="driver-usage-container">${h}</div>`);
12894
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.