[upd] plankton
This commit is contained in:
parent
10e3862fc0
commit
f4811a43bf
2 changed files with 269 additions and 107 deletions
80
lib/plankton/plankton.d.ts
vendored
80
lib/plankton/plankton.d.ts
vendored
|
@ -1,11 +1,11 @@
|
|||
/**
|
||||
* @author fenris
|
||||
*/
|
||||
type int = number;
|
||||
declare type int = number;
|
||||
/**
|
||||
* @author fenris
|
||||
*/
|
||||
type float = number;
|
||||
declare type float = number;
|
||||
declare class Buffer {
|
||||
constructor(x: string, modifier?: string);
|
||||
toString(modifier?: string): string;
|
||||
|
@ -19,7 +19,7 @@ declare namespace lib_plankton.base {
|
|||
/**
|
||||
* @author fenris
|
||||
*/
|
||||
type type_pseudopointer<type_value> = {
|
||||
declare type type_pseudopointer<type_value> = {
|
||||
value: type_value;
|
||||
};
|
||||
/**
|
||||
|
@ -868,33 +868,11 @@ declare namespace lib_plankton.call {
|
|||
fallback?: (null | ((coproduct?: type_coproduct) => type_output));
|
||||
}): type_output;
|
||||
/**
|
||||
* for rate_limit_check
|
||||
*
|
||||
* @author fenris
|
||||
*/
|
||||
type type_mana_snapshot = {
|
||||
timestamp: float;
|
||||
value: float;
|
||||
function try_catch_wrap<type_value>(get_value: (() => type_value)): {
|
||||
value: (null | type_value);
|
||||
error: (null | any);
|
||||
};
|
||||
/**
|
||||
* rate limiting algorithm, based on the idea of mana (magic power) in video games:
|
||||
* - an actor has a fixed mana capacity, i.e. the maximum amount of available power
|
||||
* - an actor has a fixed rate of mana regeneration, i.e. how fast the power is filled up (linear growth)
|
||||
* - an action has a defined mana heft, i.e. how much power is required and deducted in order to execute it
|
||||
* - mana states are represented by snapshots, i.e. the amount of power at a certain point in time
|
||||
*
|
||||
* @author fenris
|
||||
*/
|
||||
function rate_limit_check(setup: {
|
||||
capacity: float;
|
||||
regeneration_rate: float;
|
||||
get_snapshot: (() => Promise<(null | type_mana_snapshot)>);
|
||||
set_snapshot: ((snapshot: type_mana_snapshot) => Promise<void>);
|
||||
update_snapshot: ((timestamp: float, value_increment: float) => Promise<void>);
|
||||
}, heft: float): Promise<{
|
||||
granted: boolean;
|
||||
seconds: (null | float);
|
||||
}>;
|
||||
}
|
||||
declare namespace lib_plankton.file {
|
||||
/**
|
||||
|
@ -1825,7 +1803,7 @@ declare namespace lib_plankton.storage.memory {
|
|||
clear(): Promise<void>;
|
||||
write(key: any, value: any): Promise<boolean>;
|
||||
delete(key: any): Promise<void>;
|
||||
read(key: any): Promise<Awaited<type_item>>;
|
||||
read(key: any): Promise<type_item>;
|
||||
search(term: any): Promise<{
|
||||
key: string;
|
||||
preview: string;
|
||||
|
@ -3014,7 +2992,7 @@ declare namespace lib_plankton.pit {
|
|||
/**
|
||||
* @todo test
|
||||
*/
|
||||
function to_datetime(pit: type_pit, options?: {
|
||||
function to_datetime(pit: type_pit, { timezone_shift, }?: {
|
||||
timezone_shift?: int;
|
||||
}): type_datetime;
|
||||
/**
|
||||
|
@ -3064,6 +3042,48 @@ declare namespace lib_plankton.pit {
|
|||
* @todo write tests
|
||||
*/
|
||||
function cest_switch_off(year: int): type_pit;
|
||||
/**
|
||||
*/
|
||||
function timezone_shift_ce(pit: type_pit): int;
|
||||
/**
|
||||
* [convenience]
|
||||
*/
|
||||
function to_datetime_ce(pit: type_pit): type_datetime;
|
||||
/**
|
||||
*/
|
||||
function datetime_translate(datetime: type_datetime, timezone_shift: int): type_datetime;
|
||||
/**
|
||||
* [convenience]
|
||||
*/
|
||||
function datetime_translate_ce(datetime: type_datetime): type_datetime;
|
||||
/**
|
||||
*/
|
||||
function timezone_shift_format(timezone_shift: int): string;
|
||||
/**
|
||||
*/
|
||||
function date_format(date: type_date): string;
|
||||
/**
|
||||
*/
|
||||
function time_format(time: type_time, { show_seconds, }?: {
|
||||
show_seconds?: boolean;
|
||||
}): string;
|
||||
/**
|
||||
* @todo show timezone
|
||||
*/
|
||||
function datetime_format(datetime: (null | type_datetime), { timezone_indicator, show_timezone, adjust_to_ce, omit_date, }?: {
|
||||
timezone_indicator?: string;
|
||||
show_timezone?: boolean;
|
||||
adjust_to_ce?: boolean;
|
||||
omit_date?: boolean;
|
||||
}): string;
|
||||
/**
|
||||
*/
|
||||
function timespan_format(from: type_datetime, to: (null | type_datetime), { timezone_indicator, show_timezone, adjust_to_ce, omit_date, }?: {
|
||||
timezone_indicator?: string;
|
||||
show_timezone?: boolean;
|
||||
adjust_to_ce?: boolean;
|
||||
omit_date?: boolean;
|
||||
}): string;
|
||||
}
|
||||
declare namespace lib_plankton.www_form {
|
||||
/**
|
||||
|
|
|
@ -2046,63 +2046,22 @@ var lib_plankton;
|
|||
}
|
||||
call.distinguish = distinguish;
|
||||
/**
|
||||
* rate limiting algorithm, based on the idea of mana (magic power) in video games:
|
||||
* - an actor has a fixed mana capacity, i.e. the maximum amount of available power
|
||||
* - an actor has a fixed rate of mana regeneration, i.e. how fast the power is filled up (linear growth)
|
||||
* - an action has a defined mana heft, i.e. how much power is required and deducted in order to execute it
|
||||
* - mana states are represented by snapshots, i.e. the amount of power at a certain point in time
|
||||
*
|
||||
* @author fenris
|
||||
*/
|
||||
async function rate_limit_check(setup, heft) {
|
||||
if (heft > setup.capacity) {
|
||||
return Promise.resolve({
|
||||
"granted": false,
|
||||
"seconds": null,
|
||||
});
|
||||
function try_catch_wrap(get_value) {
|
||||
try {
|
||||
return {
|
||||
"value": get_value(),
|
||||
"error": null,
|
||||
};
|
||||
}
|
||||
else {
|
||||
// get current value
|
||||
const current_timestamp = (Date.now() / 1000);
|
||||
const old_snapshot_raw = (await setup.get_snapshot());
|
||||
const old_snapshot = (old_snapshot_raw
|
||||
??
|
||||
{ "timestamp": current_timestamp, "value": setup.capacity });
|
||||
const seconds_passed = (current_timestamp - old_snapshot.timestamp);
|
||||
const current_value = Math.min(setup.capacity, (old_snapshot.value
|
||||
+
|
||||
(setup.regeneration_rate
|
||||
*
|
||||
seconds_passed)));
|
||||
// analyze
|
||||
if (current_value < heft) {
|
||||
// too less
|
||||
const seconds_needed = ((setup.regeneration_rate <= 0)
|
||||
? null
|
||||
: ((heft - old_snapshot.value) / setup.regeneration_rate));
|
||||
return Promise.resolve({
|
||||
"granted": false,
|
||||
"seconds": ((seconds_needed === null) ? null : (seconds_needed - seconds_passed)),
|
||||
});
|
||||
}
|
||||
else {
|
||||
// enough -> update snapshot
|
||||
const new_value = (current_value - heft);
|
||||
// set_snapshot
|
||||
if (old_snapshot_raw === null) {
|
||||
await setup.set_snapshot({ "timestamp": current_timestamp, "value": new_value });
|
||||
}
|
||||
else {
|
||||
await setup.update_snapshot(current_timestamp, (new_value - old_snapshot.value));
|
||||
}
|
||||
return Promise.resolve({
|
||||
"granted": true,
|
||||
"seconds": 0,
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
"value": null,
|
||||
"error": error,
|
||||
};
|
||||
}
|
||||
}
|
||||
call.rate_limit_check = rate_limit_check;
|
||||
call.try_catch_wrap = try_catch_wrap;
|
||||
})(call = lib_plankton.call || (lib_plankton.call = {}));
|
||||
})(lib_plankton || (lib_plankton = {}));
|
||||
/*
|
||||
|
@ -2225,7 +2184,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|||
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
||||
while (_) try {
|
||||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||
switch (op[0]) {
|
||||
|
@ -3824,7 +3783,7 @@ var lib_plankton;
|
|||
return {
|
||||
"reports": [
|
||||
{
|
||||
"incident": "no valid apaptions",
|
||||
"incident": "no valid adaptions",
|
||||
"details": {
|
||||
"sub_adaptions": sub_adaptions,
|
||||
}
|
||||
|
@ -4634,11 +4593,16 @@ var lib_plankton;
|
|||
if (options.legacy) {
|
||||
const value = args[key];
|
||||
const regexp_argument = new RegExp("\\${" + key + "}", "g");
|
||||
lib_plankton.log.debug("lib_plankton.string.coin", {
|
||||
"key": key,
|
||||
"regex": regexp_argument.toString(),
|
||||
"value": value,
|
||||
});
|
||||
/*
|
||||
lib_plankton.log.debug(
|
||||
"lib_plankton.string.coin",
|
||||
{
|
||||
"key": key,
|
||||
"regex": regexp_argument.toString(),
|
||||
"value": value,
|
||||
}
|
||||
);
|
||||
*/
|
||||
str = str.replace(regexp_argument, value);
|
||||
}
|
||||
}
|
||||
|
@ -4646,11 +4610,16 @@ var lib_plankton;
|
|||
{
|
||||
const value = args[key];
|
||||
const regexp_argument = new RegExp(options.open + key + options.close, "g");
|
||||
lib_plankton.log.debug("lib_plankton.string.coin", {
|
||||
"key": key,
|
||||
"regex": regexp_argument.toString(),
|
||||
"value": value,
|
||||
});
|
||||
/*
|
||||
lib_plankton.log.debug(
|
||||
"lib_plankton.string.coin",
|
||||
{
|
||||
"key": key,
|
||||
"regex": regexp_argument.toString(),
|
||||
"value": value,
|
||||
}
|
||||
);
|
||||
*/
|
||||
str = str.replace(regexp_argument, value);
|
||||
}
|
||||
});
|
||||
|
@ -6735,12 +6704,6 @@ var lib_plankton;
|
|||
* @author fenris
|
||||
*/
|
||||
class class_relation {
|
||||
/**
|
||||
* @author fenris
|
||||
*/
|
||||
check(value, reference) {
|
||||
return this.predicate(value, reference);
|
||||
}
|
||||
/**
|
||||
* @author fenris
|
||||
*/
|
||||
|
@ -6750,6 +6713,12 @@ var lib_plankton;
|
|||
this.name = name;
|
||||
this.predicate = predicate;
|
||||
}
|
||||
/**
|
||||
* @author fenris
|
||||
*/
|
||||
check(value, reference) {
|
||||
return this.predicate(value, reference);
|
||||
}
|
||||
/**
|
||||
* @author fenris
|
||||
*/
|
||||
|
@ -8421,18 +8390,15 @@ var lib_plankton;
|
|||
/**
|
||||
* @todo test
|
||||
*/
|
||||
function to_datetime(pit, options = {}) {
|
||||
options = Object.assign({
|
||||
"timezone_shift": 0,
|
||||
}, options);
|
||||
function to_datetime(pit, { timezone_shift = 0, } = {}) {
|
||||
return lib_plankton.call.convey(pit, [
|
||||
to_date_object,
|
||||
(x) => x.getTime(),
|
||||
(x) => (x + ((options.timezone_shift * (60 * 60)) * 1000)),
|
||||
(x) => (x + ((timezone_shift * (60 * 60)) * 1000)),
|
||||
(x) => new Date(x),
|
||||
x => x.toISOString(),
|
||||
x => ({
|
||||
"timezone_shift": options.timezone_shift,
|
||||
"timezone_shift": timezone_shift,
|
||||
"date": {
|
||||
"year": parseInt(x.slice(0, 4)),
|
||||
"month": parseInt(x.slice(5, 7)),
|
||||
|
@ -8729,6 +8695,182 @@ var lib_plankton;
|
|||
]);
|
||||
}
|
||||
pit_1.cest_switch_off = cest_switch_off;
|
||||
/**
|
||||
*/
|
||||
function timezone_shift_ce(pit) {
|
||||
const year = to_datetime(pit).date.year;
|
||||
return (is_between(pit, cest_switch_on(year), cest_switch_off(year))
|
||||
? +2
|
||||
: +1);
|
||||
}
|
||||
pit_1.timezone_shift_ce = timezone_shift_ce;
|
||||
/**
|
||||
* [convenience]
|
||||
*/
|
||||
function to_datetime_ce(pit) {
|
||||
return to_datetime(pit, {
|
||||
"timezone_shift": timezone_shift_ce(pit),
|
||||
});
|
||||
}
|
||||
pit_1.to_datetime_ce = to_datetime_ce;
|
||||
/**
|
||||
*/
|
||||
function datetime_translate(datetime, timezone_shift) {
|
||||
if (datetime.timezone_shift === timezone_shift) {
|
||||
return datetime;
|
||||
}
|
||||
else {
|
||||
const pit = from_datetime(datetime);
|
||||
const pit_shifted = shift_hour(pit, (timezone_shift - datetime.timezone_shift));
|
||||
const datetime_shifted = to_datetime(pit_shifted);
|
||||
return {
|
||||
"timezone_shift": timezone_shift,
|
||||
"date": datetime_shifted.date,
|
||||
"time": ((datetime.time === null)
|
||||
?
|
||||
null
|
||||
: datetime_shifted.time)
|
||||
};
|
||||
}
|
||||
}
|
||||
pit_1.datetime_translate = datetime_translate;
|
||||
/**
|
||||
* [convenience]
|
||||
*/
|
||||
function datetime_translate_ce(datetime) {
|
||||
return datetime_translate(datetime, timezone_shift_ce(from_datetime(datetime)));
|
||||
}
|
||||
pit_1.datetime_translate_ce = datetime_translate_ce;
|
||||
/**
|
||||
*/
|
||||
function timezone_shift_format(timezone_shift) {
|
||||
return lib_plankton.string.coin("{{sign}}{{value}}", {
|
||||
"sign": ((timezone_shift === 0)
|
||||
?
|
||||
" "
|
||||
:
|
||||
((timezone_shift > 0)
|
||||
?
|
||||
"+"
|
||||
:
|
||||
"-")),
|
||||
"value": Math.abs(timezone_shift).toFixed(0),
|
||||
});
|
||||
}
|
||||
pit_1.timezone_shift_format = timezone_shift_format;
|
||||
/**
|
||||
*/
|
||||
function date_format(date) {
|
||||
return lib_plankton.string.coin("{{year}}-{{month}}-{{day}}", {
|
||||
"year": date.year.toFixed(0).padStart(4, "0"),
|
||||
"month": date.month.toFixed(0).padStart(2, "0"),
|
||||
"day": date.day.toFixed(0).padStart(2, "0"),
|
||||
});
|
||||
}
|
||||
pit_1.date_format = date_format;
|
||||
/**
|
||||
*/
|
||||
function time_format(time, { show_seconds = false, } = {}) {
|
||||
return lib_plankton.string.coin((show_seconds
|
||||
?
|
||||
"{{hour}}:{{minute}}:{{seconds}}"
|
||||
:
|
||||
"{{hour}}:{{minute}}"), {
|
||||
"hour": time.hour.toFixed(0).padStart(2, "0"),
|
||||
"minute": time.minute.toFixed(0).padStart(2, "0"),
|
||||
"second": time.second.toFixed(0).padStart(2, "0"),
|
||||
});
|
||||
}
|
||||
pit_1.time_format = time_format;
|
||||
/**
|
||||
* @todo show timezone
|
||||
*/
|
||||
function datetime_format(datetime, { timezone_indicator = "", show_timezone = false, adjust_to_ce = false, omit_date = false, } = {}) {
|
||||
if (datetime === null) {
|
||||
return "-";
|
||||
}
|
||||
else {
|
||||
const datetime_adjusted = (adjust_to_ce
|
||||
?
|
||||
to_datetime_ce(from_datetime(datetime))
|
||||
:
|
||||
datetime);
|
||||
return lib_plankton.string.coin("{{macro_date_and_time}}{{macro_timezone}}", {
|
||||
"macro_date_and_time": ([
|
||||
// date
|
||||
(omit_date
|
||||
?
|
||||
null
|
||||
:
|
||||
date_format(datetime_adjusted.date)),
|
||||
// time
|
||||
((datetime_adjusted.time === null)
|
||||
?
|
||||
null
|
||||
:
|
||||
time_format(datetime_adjusted.time)),
|
||||
]
|
||||
.filter(x => (x !== null))
|
||||
.join(", ")),
|
||||
"macro_timezone": (show_timezone
|
||||
?
|
||||
lib_plankton.string.coin(" [{{timezone_indicator}}{{timezone_value}}]", {
|
||||
"timezone_indicator": timezone_indicator,
|
||||
"timezone_value": timezone_shift_format(datetime_adjusted.timezone_shift),
|
||||
})
|
||||
:
|
||||
""),
|
||||
});
|
||||
}
|
||||
}
|
||||
pit_1.datetime_format = datetime_format;
|
||||
/**
|
||||
*/
|
||||
function timespan_format(from, to, { timezone_indicator = "", show_timezone = false, adjust_to_ce = false, omit_date = false, } = {}) {
|
||||
const from_adjusted = (adjust_to_ce
|
||||
?
|
||||
datetime_translate_ce(from)
|
||||
:
|
||||
from);
|
||||
const to_adjusted = ((to === null)
|
||||
?
|
||||
null
|
||||
:
|
||||
(adjust_to_ce
|
||||
?
|
||||
datetime_translate_ce(to)
|
||||
:
|
||||
to));
|
||||
return lib_plankton.string.coin("{{from}}{{to_macro}}{{macro_timezone}}", {
|
||||
"from": datetime_format(from_adjusted, {
|
||||
"show_timezone": false,
|
||||
"adjust_to_ce": false,
|
||||
"omit_date": omit_date,
|
||||
}),
|
||||
"to_macro": ((to_adjusted === null)
|
||||
?
|
||||
""
|
||||
:
|
||||
lib_plankton.string.coin(" - {{to}}", {
|
||||
"to": datetime_format(to_adjusted, {
|
||||
"omit_date": ((from_adjusted.date.year === to_adjusted.date.year)
|
||||
&&
|
||||
(from_adjusted.date.month === to_adjusted.date.month)
|
||||
&&
|
||||
(from_adjusted.date.day === to_adjusted.date.day))
|
||||
}),
|
||||
})),
|
||||
"macro_timezone": (show_timezone
|
||||
?
|
||||
lib_plankton.string.coin(" [{{timezone_indicator}}{{timezone_value}}]", {
|
||||
"timezone_indicator": timezone_indicator,
|
||||
"timezone_value": timezone_shift_format(from_adjusted.timezone_shift),
|
||||
})
|
||||
:
|
||||
""),
|
||||
});
|
||||
}
|
||||
pit_1.timespan_format = timespan_format;
|
||||
})(pit = lib_plankton.pit || (lib_plankton.pit = {}));
|
||||
})(lib_plankton || (lib_plankton = {}));
|
||||
/*
|
||||
|
|
Loading…
Add table
Reference in a new issue