smc-hub
Version:
CoCalc: Backend webserver component
119 lines (106 loc) • 3.85 kB
JavaScript
// Generated by CoffeeScript 2.5.1
(function() {
//########################################################################
// This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
// License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
//########################################################################
/*
Class to track (and report in a log) stats about user_queries by the Rethink interface.
*/
var defaults, misc, required;
({defaults} = misc = require('smc-util/misc'));
required = defaults.required;
exports.UserQueryStats = class UserQueryStats {
constructor(dbg) {
this._cnt = this._cnt.bind(this);
this.report = this.report.bind(this);
this.set_query = this.set_query.bind(this);
this.get_query = this.get_query.bind(this);
this.changefeed = this.changefeed.bind(this);
this.cancel_changefeed = this.cancel_changefeed.bind(this);
this.dbg = dbg;
this._accounts = {};
this._projects = {};
this._feeds = {};
}
_cnt(account_id, project_id, table, op, eps = 1) {
var base, base1, s, t;
if (account_id != null) {
t = (base = this._accounts)[account_id] != null ? base[account_id] : base[account_id] = {};
} else if (project_id != null) {
t = (base1 = this._projects)[project_id] != null ? base1[project_id] : base1[project_id] = {};
} else {
return;
}
s = t[table] != null ? t[table] : t[table] = {};
if (s[op] == null) {
s[op] = 0;
}
return s[op] += eps;
}
report(opts) {
var head, t;
if (opts.account_id != null) {
t = this._accounts[opts.account_id];
head = `account_id='${opts.account_id}'`;
} else if (opts.project_id != null) {
t = this._projects[opts.project_id];
head = `project_id='${opts.project_id}'`;
} else {
return;
}
return this.dbg(`${head}: ${misc.to_json(t)}`);
}
set_query(opts) {
opts = defaults(opts, {
account_id: void 0,
project_id: void 0,
table: required
});
//@dbg("set_query(account_id='#{opts.account_id}',project_id='#{opts.project_id}',table='#{opts.table}')")
this._cnt(opts.account_id, opts.project_id, opts.table, 'set');
return this.report(opts);
}
get_query(opts) {
opts = defaults(opts, {
account_id: void 0,
project_id: void 0,
table: required
});
//@dbg("get_query(account_id='#{opts.account_id}',project_id='#{opts.project_id}',table='#{opts.table}')")
this._cnt(opts.account_id, opts.project_id, opts.table, 'get');
return this.report(opts);
}
changefeed(opts) {
opts = defaults(opts, {
account_id: void 0,
project_id: void 0,
table: required,
changefeed_id: required
});
//@dbg("changefeed(account_id='#{opts.account_id}',project_id='#{opts.project_id}',table='#{opts.table}')")
this._cnt(opts.account_id, opts.project_id, opts.table, 'feed');
this._feeds[opts.changefeed_id] = opts;
return this.report(opts);
}
cancel_changefeed(opts) {
var account_id, project_id, table, x;
opts = defaults(opts, {
changefeed_id: required
});
this.dbg(`cancel_changefeed(changefeed_id='${opts.changefeed_id}')`);
x = this._feeds[opts.changefeed_id];
if (x == null) {
this.dbg(`no such changefeed_id='${opts.changefeed_id}'`);
return;
}
({account_id, project_id, table} = this._feeds[opts.changefeed_id]);
this._cnt(account_id, project_id, table, 'feed', -1);
return this.report({
account_id: account_id,
project_id: project_id
});
}
};
}).call(this);
//# sourceMappingURL=user-query-stats.js.map