UNPKG

unifi-client

Version:

NodeJs client for Unifi products (https://www.ui.com/)

383 lines (382 loc) 17 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; 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 { 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]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Stats = void 0; var _ObjectSubSite_1 = require("../commons/_ObjectSubSite"); var Validate_1 = require("../commons/Validate"); var EStatsPeriod_1 = require("./EStatsPeriod"); var Errors_1 = require("../Errors"); var Stats = /** @class */ (function (_super) { __extends(Stats, _super); function Stats() { return _super !== null && _super.apply(this, arguments) || this; } /** * Return a range of hours, default to the last 12 hours, or 12h before end * @param pStart - the start date * @param pEnd - the end date * @param hours - number of hours */ Stats.prototype.getHoursRange = function (pStart, pEnd, hours) { if (hours === void 0) { hours = 12; } var end = new Date(pEnd !== null && pEnd !== void 0 ? pEnd : Date.now()); var start; if (!Validate_1.Validate.isDate(pStart, true)) { start = new Date(end); start.setHours(end.getHours() - hours); } else { start = new Date(pStart); } if (start > end) { throw new Errors_1.ClientError("end date can't be before start date"); } return { start: start, end: end }; }; /** * Return default number of hours returned when getting stats : * - 5 minutes stats = 12 hours ~90 results * - hourly stats = 7 * 24 hours ~170 results * - daily / monthly = 52 * 7 * 24 ~365/12 results * * number results always depends of the results stored on the controller * * @param period - the stats period */ Stats.prototype.getDefaultHourRange = function (period) { switch (period) { case EStatsPeriod_1.EStatsPeriod.FIVE_MINUTES: // 12 hours return 12; case EStatsPeriod_1.EStatsPeriod.HOURLY: // a week return 7 * 24; case EStatsPeriod_1.EStatsPeriod.DAILY: case EStatsPeriod_1.EStatsPeriod.MONTHLY: // 1 year return 52 * 7 * 24; default: return 24; } }; /** * Fetch site stats * * returns an array stats objects for the current site * * FIVE_MINUTES need version gte 5.5.* */ Stats.prototype.getSiteStats = function () { return __awaiter(this, arguments, void 0, function (period, pStart, pEnd, attribs) { var _a, start, end; if (period === void 0) { period = EStatsPeriod_1.EStatsPeriod.DAILY; } if (attribs === void 0) { attribs = [ 'bytes', 'wan-tx_bytes', 'wan-rx_bytes', 'wan2-tx_bytes', 'wan2-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta', 'wlan-num_sta', 'time' ]; } return __generator(this, function (_b) { switch (_b.label) { case 0: if (period === EStatsPeriod_1.EStatsPeriod.FIVE_MINUTES) { this.checkNeedVersion('5.5.0'); } _a = this.getHoursRange(pStart, pEnd, this.getDefaultHourRange(period)), start = _a.start, end = _a.end; return [4 /*yield*/, this.instance.post("/stat/report/".concat(period, ".site"), { attrs: attribs, start: start.getTime(), end: end.getTime() })]; case 1: return [2 /*return*/, (_b.sent()).data.data]; } }); }); }; /** * Fetch APs stats * * returns an array stats objects for the current Aps (or AP with mac) * * FIVE_MINUTES need version gte 5.5.* */ Stats.prototype.getAPsStats = function (APMac_1) { return __awaiter(this, arguments, void 0, function (APMac, period, pStart, pEnd, attribs) { var _a, start, end, payload; if (period === void 0) { period = EStatsPeriod_1.EStatsPeriod.DAILY; } if (attribs === void 0) { attribs = ['bytes', 'num_sta', 'time']; } return __generator(this, function (_b) { switch (_b.label) { case 0: if (period === EStatsPeriod_1.EStatsPeriod.FIVE_MINUTES) { this.checkNeedVersion('5.5.0'); } _a = this.getHoursRange(pStart, pEnd, this.getDefaultHourRange(period)), start = _a.start, end = _a.end; payload = { attrs: attribs, start: start.getTime(), end: end.getTime() }; if (APMac) { payload.mac = APMac.toLowerCase(); } return [4 /*yield*/, this.instance.post("/stat/report/".concat(period, ".ap"), payload)]; case 1: return [2 /*return*/, (_b.sent()).data.data]; } }); }); }; /** * Fetch Users stats * * returns an array stats objects for the current Aps (or AP with mac) * * - only supported with UniFi controller versions 5.8.X and higher * - make sure that the retention policy for 5 minutes stats is set to the correct value in * the controller settings * - make sure that "Clients Historical Data" has been enabled in the UniFi controller settings in the Maintenance section */ Stats.prototype.getUsersStats = function (userMac_1) { return __awaiter(this, arguments, void 0, function (userMac, period, pStart, pEnd, attribs) { var _a, start, end, payload; if (period === void 0) { period = EStatsPeriod_1.EStatsPeriod.DAILY; } if (attribs === void 0) { attribs = ['rx_bytes', 'tx_bytes', 'time']; } return __generator(this, function (_b) { switch (_b.label) { case 0: this.checkNeedVersion('5.8.0'); _a = this.getHoursRange(pStart, pEnd, this.getDefaultHourRange(period)), start = _a.start, end = _a.end; payload = { attrs: attribs, start: start.getTime(), end: end.getTime() }; if (userMac) { payload.mac = (!Array.isArray(userMac) ? [userMac] : userMac).map(function (mac) { return mac.toLowerCase(); }); } return [4 /*yield*/, this.instance.post("/stat/report/".concat(period, ".user"), payload)]; case 1: return [2 /*return*/, (_b.sent()).data.data]; } }); }); }; /** * Fetch gateway stats * * NOTES: * - requires a USG * */ Stats.prototype.getGatewayStats = function () { return __awaiter(this, arguments, void 0, function (period, pStart, pEnd, attribs) { var _a, start, end; if (period === void 0) { period = EStatsPeriod_1.EStatsPeriod.DAILY; } if (attribs === void 0) { attribs = ['time', 'mem', 'cpu', 'loadavg_5']; } return __generator(this, function (_b) { switch (_b.label) { case 0: if (period === EStatsPeriod_1.EStatsPeriod.FIVE_MINUTES) { this.checkNeedVersion('5.5.0'); } _a = this.getHoursRange(pStart, pEnd, this.getDefaultHourRange(period)), start = _a.start, end = _a.end; return [4 /*yield*/, this.instance.post("/stat/report/".concat(period, ".gw"), { attrs: attribs, start: start.getTime(), end: end.getTime() })]; case 1: return [2 /*return*/, (_b.sent()).data.data]; } }); }); }; /** * Fetch IPS/IDS events * * NOTES: * - defaults to the past 24 hours * - requires a USG * - supported in UniFi controller versions 5.9.X and higher */ Stats.prototype.getIPSEvents = function (pStart_1, pEnd_1) { return __awaiter(this, arguments, void 0, function (pStart, pEnd, limit) { var _a, start, end; if (limit === void 0) { limit = 10000; } return __generator(this, function (_b) { switch (_b.label) { case 0: // return is unknown doesn't hesitate to open a PR this.checkNeedVersion('5.9.0'); _a = this.getHoursRange(pStart, pEnd, 24), start = _a.start, end = _a.end; return [4 /*yield*/, this.instance.post("/stat/ips/event", { start: start.getTime(), end: end.getTime(), _limit: limit })]; case 1: return [2 /*return*/, (_b.sent()).data.data]; } }); }); }; /** * Fetch login sessions * * NOTES: * - defaults to the past 7*24 hours * * ```typescript * // get all session between date * getSession({ * start: '2021-08-03T00:00:00.000Z', * end: '2021-01-03T00:00:00.000Z', * type: 'all' * }); * ``` * * ```typescript * // get 5 last sessions for mac address * getSession({ * mac: '00:0a:95:9d:68:16', * _limit: 5, * _sort: '-assoc_time' * }); * ``` * */ Stats.prototype.getSession = function () { return __awaiter(this, arguments, void 0, function (options) { var pStart, pEnd, mac, _a, type, _limit, _sort, _b, start, end, payload; if (options === void 0) { options = {}; } return __generator(this, function (_c) { switch (_c.label) { case 0: pStart = options.start, pEnd = options.end, mac = options.mac, _a = options.type, type = _a === void 0 ? 'all' : _a, _limit = options._limit, _sort = options._sort; _b = this.getHoursRange(pStart, pEnd, 7 * 24), start = _b.start, end = _b.end; payload = { start: start.getTime(), end: end.getTime(), type: type }; if (mac) { payload._limit = _limit !== null && _limit !== void 0 ? _limit : 5; payload._sort = _sort !== null && _sort !== void 0 ? _sort : '-assoc_time'; payload.mac = mac.toLowerCase(); } return [4 /*yield*/, this.instance.post("/stat/session", payload)]; case 1: return [2 /*return*/, (_c.sent()).data.data]; } }); }); }; /** * Fetch authorizations * * NOTES: * - defaults to the past 7*24 hours */ Stats.prototype.getAuths = function (pStart, pEnd) { return __awaiter(this, void 0, void 0, function () { var _a, start, end; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = this.getHoursRange(pStart, pEnd, 7 * 24), start = _a.start, end = _a.end; return [4 /*yield*/, this.instance.post("/stat/session", { start: start.getTime(), end: end.getTime() })]; case 1: return [2 /*return*/, (_b.sent()).data.data]; } }); }); }; /** * Fetch client devices that connected to the site within given timeframe * * NOTES: * - <within> is only used to select clients that were online within that period, * the returned stats per client are all-time totals, irrespective of the value of <within> * * @param type - unknown need documentation (open PR) * @param conn - unknown need documentation (open PR) * @param within - optional, hours to go back (default is 8760 hours or 1 year) * */ Stats.prototype.getAllUsers = function () { return __awaiter(this, arguments, void 0, function (type, conn, within) { if (type === void 0) { type = 'all'; } if (conn === void 0) { conn = 'all'; } if (within === void 0) { within = 8760; } return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.instance.post("/stat/alluser", { type: type, conn: conn, within: within })]; case 1: return [2 /*return*/, (_a.sent()).data.data]; } }); }); }; return Stats; }(_ObjectSubSite_1._ObjectSubSite)); exports.Stats = Stats;