2021-11-19 14:42:27 +00:00
|
|
|
Ext.define('PBS.window.TrafficControlEdit', {
|
|
|
|
extend: 'Proxmox.window.Edit',
|
|
|
|
alias: 'widget.pbsTrafficControlEdit',
|
|
|
|
mixins: ['Proxmox.Mixin.CBind'],
|
|
|
|
|
|
|
|
onlineHelp: 'sysadmin_traffic_control',
|
|
|
|
width: 800,
|
2021-11-20 21:09:39 +00:00
|
|
|
height: 600,
|
2021-11-19 14:42:27 +00:00
|
|
|
|
|
|
|
isAdd: true,
|
|
|
|
|
|
|
|
subject: gettext('Traffic Control Rule'),
|
|
|
|
|
|
|
|
fieldDefaults: { labelWidth: 120 },
|
|
|
|
|
|
|
|
cbindData: function(initialConfig) {
|
|
|
|
let me = this;
|
|
|
|
|
|
|
|
let baseurl = '/api2/extjs/config/traffic-control';
|
|
|
|
let name = initialConfig.name;
|
|
|
|
|
|
|
|
me.isCreate = !name;
|
|
|
|
me.url = name ? `${baseurl}/${name}` : baseurl;
|
|
|
|
me.method = name ? 'PUT' : 'POST';
|
|
|
|
return { };
|
|
|
|
},
|
|
|
|
|
|
|
|
controller: {
|
|
|
|
xclass: 'Ext.app.ViewController',
|
|
|
|
|
|
|
|
weekdays: ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'],
|
|
|
|
|
|
|
|
dowChanged: function(field, value) {
|
|
|
|
let me = this;
|
|
|
|
let record = field.getWidgetRecord();
|
|
|
|
if (record === undefined) {
|
|
|
|
// this is sometimes called before a record/column is initialized
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let col = field.getWidgetColumn();
|
|
|
|
record.set(col.dataIndex, value);
|
|
|
|
record.commit();
|
|
|
|
|
|
|
|
me.updateTimeframeField();
|
|
|
|
},
|
|
|
|
|
|
|
|
timeChanged: function(field, value) {
|
|
|
|
let me = this;
|
|
|
|
if (value === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let record = field.getWidgetRecord();
|
|
|
|
if (record === undefined) {
|
|
|
|
// this is sometimes called before a record/column is initialized
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let col = field.getWidgetColumn();
|
|
|
|
let hours = value.getHours().toString().padStart(2, '0');
|
|
|
|
let minutes = value.getMinutes().toString().padStart(2, '0');
|
|
|
|
record.set(col.dataIndex, `${hours}:${minutes}`);
|
|
|
|
record.commit();
|
|
|
|
|
|
|
|
me.updateTimeframeField();
|
|
|
|
},
|
|
|
|
|
|
|
|
addTimeframe: function() {
|
|
|
|
let me = this;
|
|
|
|
me.lookup('timeframes').getStore().add({
|
|
|
|
start: "00:00",
|
|
|
|
end: "23:59",
|
|
|
|
mon: true,
|
|
|
|
tue: true,
|
|
|
|
wed: true,
|
|
|
|
thu: true,
|
|
|
|
fri: true,
|
|
|
|
sat: true,
|
|
|
|
sun: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
me.updateTimeframeField();
|
|
|
|
},
|
|
|
|
|
|
|
|
updateTimeframeField: function() {
|
|
|
|
let me = this;
|
|
|
|
|
|
|
|
let timeframes = [];
|
|
|
|
me.lookup('timeframes').getStore().each((rec) => {
|
|
|
|
let timeframe = '';
|
|
|
|
let days = me.weekdays.filter(day => rec.data[day]);
|
|
|
|
if (days.length < 7 && days.length > 0) {
|
|
|
|
timeframe += days.join(',') + ' ';
|
|
|
|
}
|
|
|
|
let { start, end } = rec.data;
|
|
|
|
|
|
|
|
timeframe += `${start}-${end}`;
|
|
|
|
timeframes.push(timeframe);
|
|
|
|
});
|
|
|
|
|
|
|
|
let field = me.lookup('timeframe');
|
|
|
|
field.suspendEvent('change');
|
|
|
|
field.setValue(timeframes.join(';'));
|
|
|
|
field.resumeEvent('change');
|
|
|
|
},
|
|
|
|
|
|
|
|
removeTimeFrame: function(field) {
|
|
|
|
let me = this;
|
|
|
|
let record = field.getWidgetRecord();
|
|
|
|
if (record === undefined) {
|
|
|
|
// this is sometimes called before a record/column is initialized
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
me.lookup('timeframes').getStore().remove(record);
|
|
|
|
me.updateTimeframeField();
|
|
|
|
},
|
|
|
|
|
|
|
|
parseTimeframe: function(timeframe) {
|
|
|
|
let me = this;
|
|
|
|
let [, days, start, end] = /^(?:(\S*)\s+)?([0-9:]+)-([0-9:]+)$/.exec(timeframe) || [];
|
|
|
|
|
|
|
|
if (start === '0') {
|
|
|
|
start = "00:00";
|
|
|
|
}
|
|
|
|
|
|
|
|
let record = {
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!days) {
|
|
|
|
days = 'mon..sun';
|
|
|
|
}
|
|
|
|
|
|
|
|
days = days.split(',');
|
|
|
|
days.forEach((day) => {
|
|
|
|
if (record[day]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (me.weekdays.indexOf(day) !== -1) {
|
|
|
|
record[day] = true;
|
|
|
|
} else {
|
|
|
|
// we have a range 'xxx..yyy'
|
|
|
|
let [startDay, endDay] = day.split('..');
|
|
|
|
let startIdx = me.weekdays.indexOf(startDay);
|
|
|
|
let endIdx = me.weekdays.indexOf(endDay);
|
|
|
|
|
|
|
|
if (endIdx < startIdx) {
|
|
|
|
endIdx += me.weekdays.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let dayIdx = startIdx; dayIdx <= endIdx; dayIdx++) {
|
|
|
|
let curDay = me.weekdays[dayIdx%me.weekdays.length];
|
|
|
|
if (!record[curDay]) {
|
|
|
|
record[curDay] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return record;
|
|
|
|
},
|
|
|
|
|
|
|
|
setGridData: function(field, value) {
|
|
|
|
let me = this;
|
|
|
|
if (!value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = value.split(';');
|
|
|
|
let records = value.map((timeframe) => me.parseTimeframe(timeframe));
|
|
|
|
me.lookup('timeframes').getStore().setData(records);
|
|
|
|
},
|
|
|
|
|
|
|
|
control: {
|
|
|
|
'grid checkbox': {
|
|
|
|
change: 'dowChanged',
|
|
|
|
},
|
|
|
|
'grid timefield': {
|
|
|
|
change: 'timeChanged',
|
|
|
|
},
|
|
|
|
'grid button': {
|
|
|
|
click: 'removeTimeFrame',
|
|
|
|
},
|
|
|
|
'field[name=timeframe]': {
|
|
|
|
change: 'setGridData',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
items: {
|
|
|
|
xtype: 'inputpanel',
|
|
|
|
onGetValues: function(values) {
|
|
|
|
let me = this;
|
|
|
|
let isCreate = me.up('window').isCreate;
|
|
|
|
|
2021-11-22 08:09:34 +00:00
|
|
|
if (!values.network) {
|
2021-11-22 08:09:35 +00:00
|
|
|
values.network = ['0.0.0.0/0', '::/0'];
|
2021-11-22 08:09:34 +00:00
|
|
|
} else {
|
2021-11-22 09:26:13 +00:00
|
|
|
values.network = [...new Set(values.network.split(/\s*,\s*/))];
|
2021-11-19 14:42:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 20:56:12 +00:00
|
|
|
if ('timeframe' in values && !values.timeframe) {
|
|
|
|
delete values.timeframe;
|
|
|
|
}
|
|
|
|
if (values.timeframe && !Ext.isArray(values.timeframe)) {
|
2021-11-22 09:25:38 +00:00
|
|
|
values.timeframe = [...new Set(values.timeframe.split(';'))];
|
2021-11-19 14:42:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isCreate) {
|
2021-11-20 20:56:12 +00:00
|
|
|
PBS.Utils.delete_if_default(values, 'timeframe');
|
2021-11-19 14:42:27 +00:00
|
|
|
PBS.Utils.delete_if_default(values, 'rate-in');
|
|
|
|
PBS.Utils.delete_if_default(values, 'rate-out');
|
|
|
|
PBS.Utils.delete_if_default(values, 'burst-in');
|
|
|
|
PBS.Utils.delete_if_default(values, 'burst-out');
|
|
|
|
if (typeof values.delete === 'string') {
|
|
|
|
values.delete = values.delete.split(',');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
|
|
|
},
|
|
|
|
column1: [
|
|
|
|
{
|
|
|
|
xtype: 'pmxDisplayEditField',
|
|
|
|
name: 'name',
|
|
|
|
fieldLabel: gettext('Name'),
|
|
|
|
renderer: Ext.htmlEncode,
|
|
|
|
allowBlank: false,
|
2021-11-20 21:33:32 +00:00
|
|
|
minLength: 3,
|
2021-11-19 14:42:27 +00:00
|
|
|
cbind: {
|
|
|
|
editable: '{isCreate}',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xtype: 'pmxBandwidthField',
|
|
|
|
name: 'rate-in',
|
2021-11-20 21:08:22 +00:00
|
|
|
fieldLabel: gettext('Rate In'),
|
|
|
|
emptyText: gettext('Unlimited'),
|
|
|
|
submitAutoScaledSizeUnit: true,
|
2021-11-19 14:42:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
xtype: 'pmxBandwidthField',
|
|
|
|
name: 'rate-out',
|
2021-11-20 21:08:22 +00:00
|
|
|
fieldLabel: gettext('Rate Out'),
|
|
|
|
emptyText: gettext('Unlimited'),
|
|
|
|
submitAutoScaledSizeUnit: true,
|
2021-11-19 14:42:27 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
|
|
|
|
column2: [
|
|
|
|
{
|
|
|
|
xtype: 'proxmoxtextfield',
|
|
|
|
name: 'comment',
|
|
|
|
cbind: {
|
|
|
|
deleteEmpty: '{!isCreate}',
|
|
|
|
},
|
|
|
|
fieldLabel: gettext('Comment'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xtype: 'pmxBandwidthField',
|
|
|
|
name: 'burst-in',
|
2021-11-20 21:08:22 +00:00
|
|
|
fieldLabel: gettext('Burst In'),
|
|
|
|
emptyText: gettext('Same as Rate'),
|
|
|
|
submitAutoScaledSizeUnit: true,
|
2021-11-19 14:42:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
xtype: 'pmxBandwidthField',
|
|
|
|
name: 'burst-out',
|
2021-11-20 21:08:22 +00:00
|
|
|
fieldLabel: gettext('Burst Out'),
|
|
|
|
emptyText: gettext('Same as Rate'),
|
|
|
|
submitAutoScaledSizeUnit: true,
|
2021-11-19 14:42:27 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
|
|
|
|
columnB: [
|
|
|
|
{
|
2021-11-20 21:04:57 +00:00
|
|
|
xtype: 'proxmoxtextfield',
|
|
|
|
fieldLabel: gettext('Network(s)'),
|
|
|
|
name: 'network',
|
2021-11-22 08:21:43 +00:00
|
|
|
emptyText: `0.0.0.0/0, ::/0 (${gettext('Apply on all Networks')})`,
|
2021-11-20 21:04:57 +00:00
|
|
|
autoEl: {
|
2021-11-22 08:09:33 +00:00
|
|
|
tag: 'div',
|
|
|
|
'data-qtip': gettext('A comma-separated list of networks to apply the (shared) limit.'),
|
2021-11-19 14:42:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xtype: 'displayfield',
|
|
|
|
fieldLabel: gettext('Timeframes'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xtype: 'fieldcontainer',
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
xtype: 'grid',
|
2021-11-20 21:09:39 +00:00
|
|
|
height: 300,
|
2021-11-19 14:42:27 +00:00
|
|
|
scrollable: true,
|
|
|
|
reference: 'timeframes',
|
2021-11-20 21:09:39 +00:00
|
|
|
viewConfig: {
|
|
|
|
emptyText: gettext('Apply Always'),
|
|
|
|
},
|
2021-11-19 14:42:27 +00:00
|
|
|
store: {
|
|
|
|
fields: ['start', 'end', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'],
|
|
|
|
data: [],
|
|
|
|
},
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
text: gettext('Time Start'),
|
|
|
|
xtype: 'widgetcolumn',
|
|
|
|
dataIndex: 'start',
|
|
|
|
widget: {
|
|
|
|
xtype: 'timefield',
|
|
|
|
isFormField: false,
|
|
|
|
format: 'H:i',
|
|
|
|
formatText: 'HH:MM',
|
|
|
|
},
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: gettext('Time End'),
|
|
|
|
xtype: 'widgetcolumn',
|
|
|
|
dataIndex: 'end',
|
|
|
|
widget: {
|
|
|
|
xtype: 'timefield',
|
|
|
|
isFormField: false,
|
|
|
|
format: 'H:i',
|
|
|
|
formatText: 'HH:MM',
|
|
|
|
maxValue: '23:59',
|
|
|
|
},
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: gettext('Mon'),
|
|
|
|
xtype: 'widgetcolumn',
|
|
|
|
dataIndex: 'mon',
|
|
|
|
width: 60,
|
|
|
|
widget: {
|
|
|
|
xtype: 'checkbox',
|
|
|
|
isFormField: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: gettext('Tue'),
|
|
|
|
xtype: 'widgetcolumn',
|
|
|
|
dataIndex: 'tue',
|
|
|
|
width: 60,
|
|
|
|
widget: {
|
|
|
|
xtype: 'checkbox',
|
|
|
|
isFormField: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: gettext('Wed'),
|
|
|
|
xtype: 'widgetcolumn',
|
|
|
|
dataIndex: 'wed',
|
|
|
|
width: 60,
|
|
|
|
widget: {
|
|
|
|
xtype: 'checkbox',
|
|
|
|
isFormField: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: gettext('Thu'),
|
|
|
|
xtype: 'widgetcolumn',
|
|
|
|
dataIndex: 'thu',
|
|
|
|
width: 60,
|
|
|
|
widget: {
|
|
|
|
xtype: 'checkbox',
|
|
|
|
isFormField: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: gettext('Fri'),
|
|
|
|
xtype: 'widgetcolumn',
|
|
|
|
dataIndex: 'fri',
|
|
|
|
width: 60,
|
|
|
|
widget: {
|
|
|
|
xtype: 'checkbox',
|
|
|
|
isFormField: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: gettext('Sat'),
|
|
|
|
xtype: 'widgetcolumn',
|
|
|
|
dataIndex: 'sat',
|
|
|
|
width: 60,
|
|
|
|
widget: {
|
|
|
|
xtype: 'checkbox',
|
|
|
|
isFormField: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: gettext('Sun'),
|
|
|
|
xtype: 'widgetcolumn',
|
|
|
|
dataIndex: 'sun',
|
|
|
|
width: 60,
|
|
|
|
widget: {
|
|
|
|
xtype: 'checkbox',
|
|
|
|
isFormField: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xtype: 'widgetcolumn',
|
|
|
|
width: 40,
|
|
|
|
widget: {
|
|
|
|
xtype: 'button',
|
|
|
|
iconCls: 'fa fa-trash-o',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xtype: 'button',
|
|
|
|
text: gettext('Add'),
|
|
|
|
iconCls: 'fa fa-plus-circle',
|
|
|
|
handler: 'addTimeframe',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xtype: 'hidden',
|
|
|
|
reference: 'timeframe',
|
|
|
|
name: 'timeframe',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
|
2021-11-22 09:25:06 +00:00
|
|
|
doSetValues: function(data) {
|
|
|
|
let me = this;
|
|
|
|
|
|
|
|
// NOTE: it can make sense to have any-ip (::/0 and 0/0) and specific ones in the same set
|
|
|
|
// so only check for "is default" when there really just two networks
|
|
|
|
if (data.network?.length === 2) {
|
|
|
|
let nets = [...new Set(data.network)]; // only the set of unique networks
|
|
|
|
if (nets.find(net => net === '0.0.0.0/0') && nets.find(net => net === '::/0')) {
|
|
|
|
delete data.network;
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 09:45:29 +00:00
|
|
|
if (data.network?.length) {
|
|
|
|
data.network = data.network.join(', ');
|
|
|
|
}
|
2021-11-22 09:25:06 +00:00
|
|
|
|
|
|
|
if (Ext.isArray(data.timeframe)) {
|
|
|
|
data.timeframe = data.timeframe.join(';');
|
|
|
|
}
|
|
|
|
|
|
|
|
me.setValues(data);
|
|
|
|
},
|
|
|
|
|
2021-11-19 14:42:27 +00:00
|
|
|
initComponent: function() {
|
|
|
|
let me = this;
|
2021-11-22 09:25:06 +00:00
|
|
|
|
2021-11-19 14:42:27 +00:00
|
|
|
me.callParent();
|
2021-11-22 09:25:06 +00:00
|
|
|
|
2021-11-19 14:42:27 +00:00
|
|
|
if (!me.isCreate) {
|
|
|
|
me.load({
|
2021-11-22 09:25:06 +00:00
|
|
|
success: ({ result: { data } }) => me.doSetValues(data),
|
2021-11-19 14:42:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|