Best JavaScript code snippet using playwright-internal
Database.js
Source:Database.js
1var Q = require('q');2var mysql = require('mysql');3var Rhizoma_Logger = require('./Logger');4var Rhizoma_Cache_LRUCache = require('./Cache/LRUCache');5var DatabaseException = require('./../DatabaseException');6var InstallationException = require('./../InstallationException');7/**8 * An object representing a single Rhizoma database.9 *10 * WARNING: THIS API IS IN FLUX. PLUGIN AUTHORS SHOULD NOT USE. See lib/database.js instead.11 *12 * @param {Rhizoma_Database_Config} config Database configuration13 * @param {Rhizoma_Logger} logger The logger14 * 15 * @private16 *17 */18function Rhizoma_Database(config, logger) {19 /** @property {String} tablePrefix Prefix for database tables */20 this._tablePrefix;21 /** @property {Array} dbLinks Database connection resources */22 this._dbLinks = [];23 /** @property {Number} queryCount The number of queries made */24 this._queryCount = 0;25 /**26 * Query cache for select queries.27 *28 * Queries and their results are stored in this cache as:29 * 30 * <pre>this._queryCache[query hash] => [result1, result2, ... resultN]</pre>31 * 32 * {@link Rhizoma_Database#_getResults} for details on the hash.33 *34 * @property {Rhizoma_Cache_LRUCache} queryCache35 */36 this._queryCache = null;37 /**38 * @property {Number} queryCacheSize The number of queries to cache39 */40 this._queryCacheSize = 50;41 /**42 * Queries are saved to an array and executed using43 * a function registered by register_shutdown_function().44 *45 * Queries are saved as an array in the format:46 * <pre>47 * delayedQueries.push({48 * 'q': string query,49 * 'l': string query_type,50 * 'h': function handler // a callback function51 * });52 * </pre>53 *54 * @property {Array} delayedQueries Queries to be run during shutdown55 */56 this._delayedQueries = [];57 /** @property {Boolean} installed Is the database installed? */58 this._installed = false;59 /** @property {Rhizoma_Database_Config} config Database configuration */60 this._config;61 /** @property {Rhizoma_Logger} logger The logger */62 this._logger;63 this._logger = logger;64 this._config = config;65 this._tablePrefix = config.getTablePrefix();66 this.enableQueryCache();67}68Rhizoma_Database.prototype = {69 /**70 * Gets (if required, also creates) a database link resource.71 *72 * The database link resources are created by73 * {@link Rhizoma_Database#setupConnections}, which is called if no links exist.74 *75 * @param {String} type The type of link we want: "read", "write" or "readwrite".76 * @param {Function} cb Callback that runs when the link is available77 *78 * @return {Object} Database link79 * @throws {DatabaseException}80 */81 getLink: function(type, cb) {82 var deferred = Q.defer();83 84 if (typeof this._dbLinks[type] != 'undefined') {85 deferred.resolve(this._dbLinks[type]);86 } else if (typeof this._dbLinks['readwrite'] != 'undefined') {87 deferred.resolve(this._dbLinks['readwrite']);88 } else {89 this.setupConnections(function(err, dblink) {90 if (err) {91 deferred.reject(err);92 } else {93 deferred.resolve(dblink);94 }95 });96 }97 return deferred.promise.nodeify(cb);98 },99 /**100 * Establish database connections101 *102 * If the configuration has been set up for multiple read/write databases, set those103 * links up separately; otherwise just create the one database link.104 * 105 * @param {Function} cb Callback that runs when connection is established106 *107 * @throws {DatabaseException}108 */109 setupConnections: function(cb) {110 if (this._config.isDatabaseSplit()) {111 this.establishLink('read', cb);112 this.establishLink('write', cb);113 } else {114 this.establishLink('readwrite', cb);115 }116 },117 /**118 * Establish a connection to the database server119 *120 * Connect to the database server and use the Rhizoma database for a particular database link121 *122 * @param {String} dblinkname The type of database connection. Used to identify the123 * resource: "read", "write", or "readwrite".124 * @param {Function} cb Callback that runs when link is established125 *126 * @throws {DatabaseException}127 */128 establishLink: function(dblinkname, cb) {129 dblinkname = dblinkname || "readwrite";130 var conf = this._config.getConnectionConfig(dblinkname);131 var self = this;132 133 this._dbLinks[dblinkname] = mysql.createConnection({134 host: conf.host,135 user: conf.user,136 password: conf.password,137 database: conf.database138 });139 140 // Set DB for UTF8 (connection is implicitly established invoking a query).141 this._dbLinks[dblinkname].query("SET NAMES utf8", function(err) {142 if (err) {143 var msg = "Rhizoma couldn't connect to the database using the given credentials. Check the settings file.";144 err = new DatabaseException(msg);145 }146 cb(err, self._dbLinks[dblinkname]);147 });148 },149 /**150 * Retrieve rows from the database.151 *152 * Queries are executed with {@link Rhizoma_Database#executeQuery} and result153 * is an array containing the rows of the query. If a callback154 * function callback is defined, each row will be passed as a single155 * argument to callback. If no callback function is defined, the156 * entire result set is returned as an array.157 *158 * @param {String} query The query being passed.159 * @param {Function} cb Callback that returns the result160 * @param {Function} [transform] Optionally, the function to call back to on each row161 *162 * @return {Array} An array of database result objects or transform function results. If the query163 * returned nothing, an empty array.164 * @throws {DatabaseException}165 */166 getData: function(query, cb, transform) {167 return this._getResults(query, cb, transform, false);168 },169 /**170 * Retrieve a single row from the database.171 *172 * Similar to {@link Rhizoma_Database#getData} but returns only the first row173 * matched. If a callback function callback is specified, the row will be passed174 * as the only argument to callback.175 *176 * @param {String} query The query to execute.177 * @param {Function} cb Callback that runs when the data is available178 * @param {Function} [transform] A callback function179 *180 * @return A single database result object or the result of the transform function.181 * @throws {DatabaseException}182 */183 getDataRow: function(query, cb, transform) {184 return this._getResults(query, cb, transform, true);185 },186 /**187 * Insert a row into the database.188 *189 * *Note: Altering the DB invalidates all queries in the query cache.*190 *191 * @param {String} query The query to execute.192 * @param {Function} cb Callback that runs when the data is inserted193 *194 * @return {Number/Boolean} The database id of the inserted row if a AUTO_INCREMENT field is195 * defined, 0 if not, and false on failure.196 * @throws {DatabaseException}197 */198 insertData: function(query, cb) {199 200 var deferred = Q.defer();201 var self = this;202 this._logger.log("DB query " + query, Rhizoma_Logger.INFO);203 var dblink = this.getLink('write');204 self._invalidateQueryCache();205 self.executeQuery(query, dblink, function(err, result) {206 if (err) {207 deferred.reject(err);208 } else {209 deferred.resolve(result.insertId);210 }211 });212 213 return deferred.promise.nodeify(cb);214 },215 /**216 * Update the database.217 *218 * *Note: Altering the DB invalidates all queries in the query cache.*219 *220 * @param {String} query The query to run.221 * @param {Function} cb Callback that runs when the data is updated222 * @param {Boolean} [getNumRows=false] Return the number of rows affected223 *224 * @return {Boolean/Number}225 * @throws {DatabaseException}226 */227 updateData: function(query, cb, getNumRows) {228 229 var deferred = Q.defer();230 var self = this;231 this._logger.log("DB query " + query, Rhizoma_Logger.INFO);232 var dblink = this.getLink('write');233 234 self._invalidateQueryCache();235 236 self.executeQuery(query, dblink, function(err, result) {237 if (err) {238 deferred.reject(err);239 } else if (getNumRows) {240 deferred.resolve(result.affectedRows);241 } else {242 deferred.resolve(true);243 }244 }); 245 246 return deferred.promise.nodeify(cb);247 },248 /**249 * Delete data from the database250 *251 * *Note: Altering the DB invalidates all queries in query cache.*252 *253 * @param {String} query The SQL query to run254 * @param {Function} cb Callback that runs when the data is deleted255 *256 * @return {Number} The number of affected rows257 * @throws {DatabaseException}258 */259 deleteData: function(query, cb) {260 261 var deferred = Q.defer();262 var self = this;263 this._logger.log("DB query " + query, Rhizoma_Logger.INFO);264 var dblink = this.getLink('write');265 266 self._invalidateQueryCache();267 self.executeQuery(query, dblink, function(err, result) {268 if (err) {269 deferred.reject(err);270 } else {271 deferred.resolve(result.affectedRows);272 }273 }); 274 275 return deferred.promise.nodeify(cb);276 },277 /**278 * Handles queries that return results, running the results through a279 * an optional callback function. This is for R queries (from CRUD).280 *281 * @param {String} query The select query to execute282 * @param {Function} cb Callback that runs when the data is fetched283 * @param {String} [transform] An optional callback function to run on each row284 * @param {Boolean} [single=false] Return only a single result?285 * 286 * @private287 * @return {Array} An array of database result objects or transform function results. If the query288 * returned nothing, an empty array.289 * @throws {DatabaseException}290 */291 _getResults: function(query, cb, transform, single) {292 293 var deferred = Q.defer();294 var self = this;295 // Since we want to cache results of running the callback, we need to296 // need to namespace the query with the callback and single result request.297 // http://trac.elgg.org/ticket/4049298 var transform_hash = transform ? String(transform) : "";299 var hash = transform_hash + (single ? 1 : 0) + query;300 // Is cached?301 if (this._queryCache) {302 if (typeof this._queryCache[hash] != "undefined") {303 this._logger.log("DB query " + query + " results returned from cache (hash: " + hash + ")", Rhizoma_Logger.INFO);304 deferred.resolve(this._queryCache[hash]);305 return deferred.promise.nodeify(cb);306 }307 }308 var dblink = this.getLink('read');309 310 var _return = [];311 self.executeQuery(query, dblink, function(err, result) {312 313 if (err) {314 deferred.reject(err);315 return;316 }317 // test for callback once instead of on each iteration.318 var is_callable = typeof transform === 'function';319 320 321 result.some(function(row) {322 if (is_callable) {323 row = transform(row);324 }325 if (single) {326 _return = row;327 return true; // break328 } else {329 _return.push(row);330 }331 });332 333 if (!_return.length) {334 self._logger.log("DB query " + query + " returned no results.", Rhizoma_Logger.INFO);335 }336 // Cache result337 if (self._queryCache) {338 self._queryCache[hash] = _return;339 self._logger.log("DB query " + query + " results cached (hash: " + hash + ")", Rhizoma_Logger.INFO);340 }341 deferred.resolve(_return);342 });343 344 return deferred.promise.nodeify(cb);345 },346 /**347 * Execute a query.348 *349 * query is executed via mysql.query. If there is an SQL error,350 * a {@link DatabaseException} is thrown.351 *352 * @param {String} query The query353 * @param {Object} dblink The DB link354 * @param {Function} cb Callback that runs when the query is completed355 *356 * @return {Array} The result of mysql.query357 * @throws {DatabaseException}358 */359 executeQuery: function(query, dblink, cb) {360 361 var deferred = Q.defer();362 if (query === null || dblink === null) {363 deferred.reject(new DatabaseException("Query and dblink cannot be null"));364 }365 this._queryCount++;366 367 var onQuery = function(err, result) {368 if (err) {369 if (err.code == 'PROTOCOL_CONNECTION_LOST' || err.code == 'ECONNREFUSED') {370 err = new DatabaseException("Connection to database was lost.");371 } else {372 err = new DatabaseException(err.message + "\n\n QUERY: " + query);373 }374 deferred.reject(err);375 } else {376 deferred.resolve(result);377 }378 }379 // If dblink is promise380 if (dblink.then) {381 dblink.then(function(dblink) {382 dblink.query(query, onQuery);383 })384 .catch(function(err) {385 onQuery(err);386 });387 } else {388 dblink.query(query, onQuery);389 }390 391 return deferred.promise.nodeify(cb);392 },393 /**394 * Runs a full database script from disk.395 *396 * The file specified should be a standard SQL file as created by397 * mysqldump or similar. Statements must be terminated with ;398 * and a newline character (\n or \r\n) with only one statement per line.399 *400 * The special string 'prefix_' is replaced with the database prefix401 * as defined in {@link #tablePrefix}.402 *403 * **Warning:** Errors do not halt execution of the script. If a line404 * generates an error, the error message is saved and the405 * next line is executed. After the file is run, any errors406 * are displayed as a {@link DatabaseException}407 *408 * @param {String} scriptlocation The full path to the script409 * @param {Function} cb Callback that runs when the script is complete410 *411 * @throws {DatabaseException}412 */413 runSqlScript: function(scriptlocation, cb) {414 var fs = require('fs');415 var self = this;416 417 fs.readFile(scriptlocation, 'utf8', function (err, script) {418 if (err) {419 err.message = "Rhizoma couldn't find the requested database script at " + scriptlocation + ".";420 cb(err);421 return;422 }423 var errors = [];424 var promises = [];425 426 // Remove MySQL -- style comments427 script = script.replace(/\-\-.*[\n\r]+/g, '');428 429 // Statements must end with ; and a newline430 var sql_statements = script.split(/;[\n\r]+/);431 432 sql_statements.forEach(function(statement) {433 statement = statement.trim();434 statement = statement.replace("prefix_", self._tablePrefix);435 if (statement.length) {436 var deferred = Q.defer();437 self.updateData(statement, function(err) {438 if (err) {439 errors.push(err.message);440 }441 deferred.resolve();442 });443 promises.push(deferred.promise);444 }445 });446 Q.all(promises).then(function() {447 if (errors.length) {448 var errortxt = "";449 errors.forEach(function(error) {450 errortxt += " {" + error.message + "};";451 });452 453 var msg = "There were a number of issues: " + errortxt;454 err = new DatabaseException(msg);455 } else {456 err = null;457 }458 cb(err);459 });460 });461 },462 /**463 * Queue a query for execution upon shutdown.464 *465 * You can specify a handler function if you care about the result. This function will accept466 * the array from mysql.query.467 *468 * @param {String} query The query to execute469 * @param {String} type The query type ('read' or 'write')470 * @param {Function} [handler] A callback function to pass the results array to471 *472 * @return {Boolean} Whether registering was successful.473 */474 registerDelayedQuery: function(query, type, handler) {475 if (typeof type != 'object' && type != 'read' && type != 'write') {476 return false;477 }478 // Construct delayed query479 var delayed_query = [];480 delayed_query['q'] = query;481 delayed_query['l'] = type;482 delayed_query['h'] = handler;483 this._delayedQueries.push(delayed_query);484 return true;485 },486 /**487 * Trigger all queries that were registered as "delayed" queries. This is488 * called by the system automatically on shutdown.489 *490 * @private491 */492 executeDelayedQueries: function() {493 494 var self = this;495 496 this._delayedQueries.forEach(function(query_details) {497 var link = query_details['l'];498 if (link == 'read' || link == 'write') {499 link = self.getLink(link);500 } else if (typeof link != 'object') {501 var msg = "Link for delayed query not valid resource or db_link type. Query: " + query_details['q'];502 self._logger.log(msg, Rhizoma_Logger.WARNING);503 }504 505 self.executeQuery(query_details['q'], link, function(err, result) {506 if (err) {507 // Suppress all exceptions since page already sent to requestor508 self._logger.log(err.message, Rhizoma_Logger.ERROR);509 }510 if (typeof query_details['h'] === 'function') {511 query_details['h'](result);512 }513 });514 });515 },516 /**517 * Enable the query cache518 * 519 * This does not take precedence over the Rhizoma_Database_Config setting.520 * 521 */522 enableQueryCache: function() {523 if (this._config.isQueryCacheEnabled() && this._queryCache === null) {524 this._queryCache = new Rhizoma_Cache_LRUCache(this._queryCacheSize);525 }526 },527 /**528 * Disable the query cache529 * 530 * This is useful for special scripts that pull large amounts of data back531 * in single queries.532 * 533 */534 disableQueryCache: function() {535 this._queryCache = null;536 },537 /**538 * Invalidate the query cache539 */540 _invalidateQueryCache: function() {541 if (this._queryCache) {542 this._queryCache.clear();543 this._logger.log("Query cache invalidated", Rhizoma_Logger.INFO);544 }545 },546 /**547 * Test that the Rhizoma database is installed548 * 549 * @param {Function} cb Callback that runs when the result is available550 *551 * @throws {InstallationException}552 */553 assertInstalled: function(cb) {554 if (this._installed) {555 cb();556 return;557 }558 var dblink = this.getLink('read');559 var self = this;560 dblink.query("SELECT value FROM " + self._tablePrefix + "datalists WHERE name = 'installed'", function(err) {561 if (err) {562 err = new InstallationException("Unable to handle this request. This site is not configured or the database is down.");563 }564 self._installed = true;565 cb(err);566 });567 },568 /**569 * Get the number of queries made to the database570 *571 * @return {Number}572 */573 getQueryCount: function() {574 return this._queryCount;575 },576 /**577 * Get the prefix for Rhizoma's tables578 *579 * @return {String}580 */581 getTablePrefix: function() {582 return this._tablePrefix;583 },584 /**585 * Sanitizes an integer value for use in a query586 *587 * @param {Number} value Value to sanitize588 * @param {Boolean} [signed=true] Whether negative values are allowed589 * @return {Number}590 */591 sanitizeInt: function(value, signed) {592 value = parseInt(value, 10);593 if (signed === false) {594 if (value < 0) {595 value = 0;596 }597 }598 return value;599 },600 /**601 * Sanitizes a string for use in a query602 *603 * @param {String} value Value to escape604 * @return {String}605 */606 sanitizeString: function(value) {607 return mysql.escape(value);608 }609};...
TimerGPU.js
Source:TimerGPU.js
1import notify from 'osg/notify';2import WebGLCaps from 'osg/WebGLCaps';3/*4use EXT_disjoint_timer_queryto time webgl calls GPU side average over multiple frames5If timestamp feature is not supported, we virtualize the query by splitting and adding6dummy queries, that way it should handle both nested and interleaved queries.7Also, if you time the same queryID multiple time in the same frame, it will sum the different8queries, that way you can track a particular of gl command for examples9*/10var TimerGPU = function(gl) {11 this._enabled = false;12 this.reset(gl);13};14TimerGPU.FRAME_COUNT = 0;15TimerGPU.instance = function(gl, force) {16 if (!TimerGPU._instance) {17 TimerGPU._instance = new TimerGPU(gl);18 } else if (gl && (TimerGPU._instance.getContext() !== gl || force)) {19 TimerGPU._instance.setContext(gl);20 TimerGPU._instance.reset(gl);21 }22 return TimerGPU._instance;23};24TimerGPU.prototype = {25 reset: function(gl) {26 if (gl) {27 var ext = WebGLCaps.instance(gl).getDisjointTimerQuery();28 if (!ext) return this;29 // webgl1 to webgl230 if (!gl.getQueryParameter) gl.getQueryParameter = ext.getQueryObjectEXT.bind(ext);31 // https://github.com/KhronosGroup/WebGL/blob/master/sdk/tests/conformance/extensions/ext-disjoint-timer-query.html#L10232 // run the page if strange results33 // to validate you gpu/browser has correct gpu queries support34 this._hasTimeElapsed =35 gl.getQuery(ext.TIME_ELAPSED_EXT, ext.QUERY_COUNTER_BITS_EXT) >= 30;36 this._hasTimeStamp = gl.getQuery(ext.TIMESTAMP_EXT, ext.QUERY_COUNTER_BITS_EXT) >= 30;37 if (!this._hasTimeElapsed && !this._hasTimeStamp) {38 return this;39 }40 // no timestamp means not start/end absolute time41 // which means each start must be followed by a end42 // BEFORE any other start (of other queryID)43 if (!this._hasTimeStamp) notify.debug('Warning: do not use interleaved GPU query');44 this._ext = ext;45 this._gl = gl;46 this._enabled = true;47 }48 this._frameAverageCount = 10;49 this._glQueries = [];50 this._queriesByID = {};51 this._userQueries = []; // for timestamp, it's the same as _glQueries52 // stuffs used to virtualize query (no timestamp)53 this._queryCount = 0;54 this._nbOpened = 0;55 },56 getContext: function() {57 return this._gl;58 },59 setContext: function(gl) {60 this._gl = gl;61 },62 setFrameAverageCount: function(val) {63 this._frameAverageCount = val;64 },65 clearQueries: function() {66 var glQueries = this._glQueries;67 for (var i = 0, nbQueries = glQueries.length; i < nbQueries; ++i) {68 var query = glQueries[i];69 this._gl.deleteQuery(query._pollingStartQuery);70 if (query._pollingEndQuery) this._gl.deleteQuery(query);71 }72 this._userQueries.length = 0;73 this._glQueries.length = 0;74 this._queriesByID = {};75 },76 supportTimeStamp: function() {77 return this._hasTimeStamp;78 },79 // many browser doesn't yet have80 // the marvellous gpu timers81 enable: function() {82 // enable only if we have the extension83 this._enabled = !!this._ext;84 },85 disable: function() {86 this._enabled = false;87 },88 isEnabled: function() {89 return this._enabled;90 },91 setCallback: function(cb) {92 this._callback = cb;93 },94 createUserQuery: function(queryID) {95 var query;96 if (this._hasTimeStamp) {97 query = this.createGLQuery();98 } else {99 query = {100 _startIndex: 0,101 _endIndex: 0102 };103 }104 query._id = queryID;105 query._frame = TimerGPU.FRAME_COUNT;106 query._isOpened = true;107 query._siblings = []; // if the query is called multiple time in the same frame108 return query;109 },110 createGLQuery: function() {111 var query = {};112 query._isWaiting = false; // wait typically 1 or 2 frames113 query._pollingStartQuery = undefined; // gl query object114 query._pollingEndQuery = undefined; // gl query object (timestamp only)115 query._averageTimer = 0.0; // cumulative average time116 query._resultCount = 0; // cumulative average count117 if (this._hasTimeStamp) query._pollingEndQuery = this._gl.createQuery();118 query._pollingStartQuery = this._gl.createQuery();119 this._glQueries.push(query);120 return query;121 },122 getOrCreateLastGLQuery: function() {123 var query = this._glQueries[this._queryCount - 1];124 if (query) return query;125 query = this._glQueries[this._queryCount - 1] = this.createGLQuery();126 return query;127 },128 beginCurrentQuery: function() {129 if (this._nbOpened === 0) return;130 this._queryCount++;131 var query = this.getOrCreateLastGLQuery();132 if (!query._isWaiting) {133 this._gl.beginQuery(this._ext.TIME_ELAPSED_EXT, query._pollingStartQuery);134 }135 },136 endCurrentQuery: function() {137 if (this._nbOpened === 0) return;138 if (!this.getOrCreateLastGLQuery()._isWaiting) {139 this._gl.endQuery(this._ext.TIME_ELAPSED_EXT);140 }141 },142 getAvailableQueryByID: function(queryID) {143 var query = this._queriesByID[queryID];144 if (!query) {145 query = this._queriesByID[queryID] = this.createUserQuery(queryID);146 this._userQueries.push(query);147 return query;148 }149 if (query._frame === TimerGPU.FRAME_COUNT) {150 if (query._isOpened) return query;151 var siblings = query._siblings;152 for (var i = 0, nbSiblings = siblings.length; i < nbSiblings; ++i) {153 var qsib = siblings[i];154 if (qsib._frame !== TimerGPU.FRAME_COUNT || qsib._isOpened) {155 qsib._frame = TimerGPU.FRAME_COUNT;156 return qsib;157 }158 }159 var newQuery = this.createUserQuery();160 siblings.push(newQuery);161 return newQuery;162 }163 query._frame = TimerGPU.FRAME_COUNT;164 return query;165 },166 // start recording time if query already exist, don't recreate167 start: function(queryID) {168 // If timing currently disabled or glTimer does not exist, exit early.169 if (!this._enabled) {170 return undefined;171 }172 var query = this.getAvailableQueryByID(queryID);173 query._isOpened = true;174 if (this._hasTimeStamp) {175 if (!query._isWaiting)176 this._ext.queryCounterEXT(query._pollingStartQuery, this._ext.TIMESTAMP_EXT);177 } else {178 this.endCurrentQuery();179 this._nbOpened++;180 query._startIndex = this._queryCount;181 this.beginCurrentQuery();182 }183 },184 // stop query recording (if running) polls for results185 end: function(queryID) {186 if (!this._enabled) {187 return;188 }189 var query = this.getAvailableQueryByID(queryID);190 query._isOpened = false;191 if (this._hasTimeStamp) {192 if (!query._isWaiting)193 this._ext.queryCounterEXT(query._pollingEndQuery, this._ext.TIMESTAMP_EXT);194 } else {195 this.endCurrentQuery();196 query._endIndex = this._queryCount;197 this._nbOpened--;198 this.beginCurrentQuery();199 }200 },201 computeQueryAverageTime: function(query) {202 var average = 0;203 var glQueries = this._glQueries;204 for (var i = query._startIndex; i < query._endIndex; ++i) {205 var glAvg = glQueries[i]._averageTimer;206 if (glAvg < 0) return -1;207 average += glAvg;208 }209 return average;210 },211 computeFullAverageTime: function(query) {212 var average = this.computeQueryAverageTime(query);213 if (average < 0) return -1;214 var siblings = query._siblings;215 for (var i = 0, nbSiblings = siblings.length; i < nbSiblings; ++i) {216 var qsib = siblings[i];217 if (qsib._frame !== TimerGPU.FRAME_COUNT - 1) continue;218 var sibAvg = this.computeQueryAverageTime(qsib);219 if (sibAvg < 0) return -1;220 average += sibAvg;221 }222 return average;223 },224 pollQueries: function() {225 TimerGPU.FRAME_COUNT++;226 this._queryCount = 0;227 this._nbOpened = 0;228 if (!this._enabled || !this._callback) {229 return;230 }231 var glQueries = this._glQueries;232 var nbGlQueries = glQueries.length;233 var i;234 // all timer are corrupted, clear the queries235 var disjoint = this._gl.getParameter(this._ext.GPU_DISJOINT_EXT);236 if (disjoint) {237 for (i = 0; i < nbGlQueries; ++i) {238 glQueries[i]._isWaiting = false;239 }240 return;241 }242 // update average time for each queries243 for (i = 0; i < nbGlQueries; ++i) {244 this.pollQuery(glQueries[i]);245 }246 var userQueries = this._userQueries;247 var nbUserQueries = userQueries.length;248 for (i = 0; i < nbUserQueries; ++i) {249 var query = userQueries[i];250 var average = this.computeFullAverageTime(query);251 if (average > 0) {252 this._callback(average, query._id);253 }254 }255 },256 pollQuery: function(query) {257 query._isWaiting = false;258 // last to be queried259 var lastQuery = this._hasTimeStamp ? query._pollingEndQuery : query._pollingStartQuery;260 // wait till results are ready261 var available = this._gl.getQueryParameter(lastQuery, this._gl.QUERY_RESULT_AVAILABLE);262 if (!available) {263 query._isWaiting = true;264 return 0;265 }266 var timeElapsed;267 if (this._hasTimeStamp) {268 var startTime = this._gl.getQueryParameter(269 query._pollingStartQuery,270 this._gl.QUERY_RESULT271 );272 var endTime = this._gl.getQueryParameter(lastQuery, this._gl.QUERY_RESULT);273 timeElapsed = endTime - startTime;274 } else {275 timeElapsed = this._gl.getQueryParameter(lastQuery, this._gl.QUERY_RESULT);276 }277 query._resultCount++;278 // restart cumulative average every frameAveragecount frames279 if (query._resultCount > this._frameAverageCount) {280 query._averageTimer = 0.0;281 query._resultCount = 1;282 }283 // https://en.wikipedia.org/wiki/Moving_average#Cumulative_moving_average284 query._averageTimer =285 query._averageTimer + (timeElapsed - query._averageTimer) / query._resultCount;286 return query._averageTimer;287 }288};...
StatusUtils.js
Source:StatusUtils.js
1/******************************************************************************2 * Copyright (c) Dworek 2017. All rights reserved. *3 * *4 * @author Tim Visee *5 * @website http://timvisee.com/ *6 * *7 * Open Source != No Copyright *8 * *9 * Permission is hereby granted, free of charge, to any person obtaining a *10 * copy of this software and associated documentation files (the "Software"), *11 * to deal in the Software without restriction, including without limitation *12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, *13 * and/or sell copies of the Software, and to permit persons to whom the *14 * Software is furnished to do so, subject to the following conditions: *15 * *16 * The above copyright notice and this permission notice shall be included *17 * in all copies or substantial portions of the Software. *18 * *19 * You should have received a copy of The MIT License (MIT) along with this *20 * program. If not, see <http://opensource.org/licenses/MIT/>. *21 ******************************************************************************/22var _= require('lodash');23var cluster = require('cluster');24var express = require('express');25var os = require('os');26var process = require('process');27var percentile = require('stats-percentile');28var config = require('../../config');29var appInfo = require('../../appInfo');30var Core = require('../../Core');31var LayoutRenderer = require('../layout/LayoutRenderer');32var RedisUtils = require('../redis/RedisUtils');33var CallbackLatch = require('../util/CallbackLatch');34var Formatter = require('../format/Formatter');35/**36 * StatusUtils class.37 *38 * @class39 * @constructor40 */41var StatusUtils = function() {};42/**43 * Get an object containing all relevant status.44 *45 * @param {StatusUtils~getStatusCallback} callback Callback containing the status object.46 */47StatusUtils.getStatus = function(callback) {48 // Do not fetch the status when the callback isn't a function49 if(!_.isFunction(callback))50 return;51 // Create a callback latch52 var latch = new CallbackLatch();53 // Get the CPU load average54 const loadAvg = os.loadavg();55 // Get the memory usage data56 var memoryUsage = process.memoryUsage();57 // Determine whether this is the master thread, and get the PID58 var isMaster = cluster.isMaster;59 var workerId = !isMaster ? cluster.worker.id : '?';60 // Layout options object61 var status = {62 cluster: {63 serverCount: 1,64 isMaster: isMaster,65 workerCount: os.cpus().length,66 },67 worker: {68 id: workerId,69 pid: process.pid,70 uptime: Math.floor(process.uptime())71 },72 server: {73 os: os.type(),74 platform: os.platform(),75 arch: os.arch(),76 loadavg: [77 loadAvg[0] >= 0 ? parseFloat(loadAvg[0]) : '?',78 loadAvg[1] >= 0 ? parseFloat(loadAvg[1]) : '?',79 loadAvg[2] >= 0 ? parseFloat(loadAvg[2]) : '?',80 ],81 cpus: os.cpus(),82 memory_system: {83 free: os.freemem(),84 used: os.totalmem() - os.freemem(),85 total: os.totalmem()86 },87 memory_app: {88 heapFree: memoryUsage.heapTotal - memoryUsage.heapUsed,89 heapUsed: memoryUsage.heapUsed,90 heapTotal: memoryUsage.heapTotal,91 rss: memoryUsage.rss,92 external: memoryUsage.external93 }94 },95 web: {96 online: true,97 uptime: Math.round(os.uptime())98 },99 live: {100 gameCount: Core.gameManager.getLoadedGameCount()101 },102 realtime: {103 online: Core.realTime.isOnline(),104 connections: Core.realTime.getConnectionCount()105 },106 mongo: {107 online: true108 },109 redis: {110 online: RedisUtils.isReady()111 },112 cache: {113 queryCount: Core.status.internalCache.queryCount,114 objectCount: 0,115 fieldCount: 0116 }117 };118 // Fetch and process latency data119 const latencyList = Core.eventLoopMonitor.countLatency();120 status.server.latency = [121 Math.max.apply(null, latencyList),122 Math.min.apply(null, latencyList),123 percentile(latencyList, 50),124 percentile(latencyList, 90),125 percentile(latencyList, 99)126 ];127 // Make sure the minimum value is valid128 var minVal = status.server.latency[1];129 if(minVal === null || minVal === undefined || minVal < 0)130 status.server.latency[1] = 0;131 // Get the redis status if ready132 if(RedisUtils.isReady()) {133 // Get the Redis connection134 var redis = RedisUtils.getConnection();135 // Call the Redis info command136 latch.add();137 redis.info(function(err) {138 // Call back errors139 if(err !== null) {140 next(err);141 return;142 }143 // Get the info section of the current Redis database144 var redisInfo = redis.server_info;145 var redisDbInfo = redisInfo['db' + config.redis.dbNumber];146 // Get the data147 status.redis.uptime = parseInt(redisInfo.uptime_in_seconds);148 status.redis.commandCount = parseInt(redisInfo.total_commands_processed);149 status.redis.keyCount = redisDbInfo != undefined ? redisDbInfo.keys : 0;150 status.redis.memory = parseInt(redisInfo.used_memory);151 status.redis.memoryHuman = Formatter.formatBytes(status.redis.memory);152 status.redis.memoryLua = parseInt(redisInfo.used_memory_lua);153 status.redis.memoryLuaHuman = Formatter.formatBytes(status.redis.memoryLua);154 status.redis.memoryRss = parseInt(redisInfo.used_memory_rss);155 status.redis.memoryRssHuman = Formatter.formatBytes(status.redis.memoryRss);156 status.redis.memoryPeak = parseInt(redisInfo.used_memory_peak);157 status.redis.memoryPeakHuman = Formatter.formatBytes(status.redis.memoryPeak);158 // Resolve the latch159 latch.resolve();160 });161 }162 // Go through the internal cache163 for(var modelManagerName in Core.model) {164 // make sure the manager is in Core.model165 if(!Core.model.hasOwnProperty(modelManagerName))166 continue;167 // Get the model manager168 var modelManager = Core.model[modelManagerName];169 // Append the number of queries on the objects, and the objects itself170 //status.cache.queryCount += modelManager._instanceManager._queryCount;171 status.cache.objectCount += modelManager._instanceManager.count();172 // Iterate through the objects173 for(var object of modelManager._instanceManager._instances.values()) {174 //status.cache.queryCount += object._baseModel._cache._queryCount;175 status.cache.fieldCount += object._baseModel._cache.getCacheCount();176 }177 }178 //status.cache.queryCount = 123;179 // Add humanly formatted properties180 status.server.latencyHuman = status.server.latency.map(function(val) {181 return Formatter.formatNano(val)182 });183 status.server.loadavgHuman = status.server.loadavg.map(function(val) {184 if(typeof val === 'number')185 return val.toFixed(3);186 else187 return val;188 });189 status.server.memory_system.freeHuman = Formatter.formatBytes(status.server.memory_system.free);190 status.server.memory_system.usedHuman = Formatter.formatBytes(status.server.memory_system.used);191 status.server.memory_system.totalHuman = Formatter.formatBytes(status.server.memory_system.total);192 status.server.memory_app.heapFreeHuman = Formatter.formatBytes(status.server.memory_app.heapFree);193 status.server.memory_app.heapUsedHuman = Formatter.formatBytes(status.server.memory_app.heapUsed);194 status.server.memory_app.heapTotalHuman = Formatter.formatBytes(status.server.memory_app.heapTotal);195 status.server.memory_app.rssHuman = Formatter.formatBytes(status.server.memory_app.rss);196 status.server.memory_app.externalHuman = Formatter.formatBytes(status.server.memory_app.external);197 // Call back when all status is fetched198 latch.then(function () {199 callback(null, status);200 });201};202/**203 * @callback StatusUtils~getStatusCallback204 * @param {Error|null} Error instance if an error occurred, null otherwise.205 * @param {object} Object containing all status properties.206 */207// Export the class...
dashboard.js
Source:dashboard.js
1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18'use strict';19KylinApp.controller('DashBoardCtrl', function ($scope, DashBoardService, $log, $q) {20 $scope.stastic = {21 countUser: 0,22 last30DayPercentile: 0,23 cubeStorage: 0,24 avgDayQuery: 0,25 cubesCount: 026 }27 $scope.eachDayPercentileData=[];28 $scope.reduceCubeSourceTicks=false;29 // each day percentile chart30 $scope.cubeInfo = function () {31 var cubeSourceRecords ={"key":"Cube Source Records","values":[],"sizes":[]};32 $scope.cubeInfoPromise = DashBoardService.listCubes({},33 function (data) {34 if(data.length>30){35 $scope.reduceCubeSourceTicks=true;36 }37 for (var i = 0; i < data.length; i++) {38 cubeSourceRecords.values.push([data[i].name,parseInt(data[i].input_records_count)]);39 cubeSourceRecords.sizes.push((data[i].size_kb));40 $log.info(data[i]);41 }42 $scope.cubeUsageData = [cubeSourceRecords];43 }, function (result) {44 $log.error(result);45 }).$promise;46 }();47 $scope.cubeSourceYAxisTickFormat = function(){48 return d3.format('0');49 }50 $scope.avgDayQuery = function () {51 $scope.avgDayQueryPromise = DashBoardService.avgDayQuery({},52 function (data) {53 if (!isNaN(data[0][0])) {54 $scope.stastic.avgDayQuery = Math.round(data[0][0]);55 } else {56 $log.info("No data available.");57 }58 $log.info("avg day query:" + data);59 }, function (result) {60 $log.error(result);61 }).$promise;62 }();63 $scope.totalQueryUser = function () {64 $scope.queryUserPromise = DashBoardService.totalQueryUser({},65 function (data) {66 if (!isNaN(data[0][0])) {67 $scope.stastic.userCount = data[0][0];68 } else {69 $log.info("No data available.");70 }71 $log.info("total query user:" + data);72 }, function (result) {73 $log.error(result);74 }).$promise;75 }();76 $scope.last30DayPercentile = function () {77 $scope.last30DayPercentilePromise = DashBoardService.last30DayPercentile({},78 function (data) {79 if (!isNaN(data[0][0])) {80 $scope.stastic.last30DayPercentile = Math.round(data[0][0] * 100) / 100;81 } else {82 $log.info("No data available.");83 }84 $log.info("last 30 Day 90th Percentile:" + data);85 }, function (result) {86 $log.error(result);87 }).$promise;88 }();89 //daily query num90 $scope.dailyQueryData = [];91 // last 30 days92 $scope.dailyQueryCount = function () {93 var queryCount ={"key":"Query Count","bar":true,"values":[]};94 $scope.dailyQueryCountPromise = DashBoardService.dailyQueryCount({},95 function (data) {96 for (var i = 0; i < data.length; i++) {97 $scope.dailyQueryData.push(parseInt(data[i][1]));98 queryCount.values.push([new Date(data[i][0]).getTime()/1000,data[i][1]]);99 }100 console.log("daily query count");101 $scope.eachDayPercentileData.push(queryCount);102 }, function (result) {103 $log.error(result);104 }).$promise;105 };106 $scope.eachDayPercentile = function () {107 var percenTile90 ={"key":"90%-ile","values":[]},percenTile95 = {"key":"95%-ile","values":[]},queryCount ={"key":"Query Count","bar":true,"values":[]};;108 $scope.eachDayPercentilePromise = DashBoardService.eachDayPercentile({},109 function (data) {110 for (var i = 0; i < data.length; i++) {111 var _latency90 = data[i][1].split(",")[0].substr(1);112 var _latency95 = data[i][1].split(",")[1].substr(0, data[i][1].length - 1);113 var _querycount = data[i][2];114 percenTile90.values.push([new Date(data[i][0]).getTime()/1000,Math.round(parseFloat(_latency90) * 100) / 100]);115 percenTile95.values.push([new Date(data[i][0]).getTime()/1000,Math.round(parseFloat(_latency95) * 100) / 100]);116 queryCount.values.push([new Date(data[i][0]).getTime()/1000,Math.round(parseInt(_querycount) * 100) / 100]);117 }118 $scope.eachDayPercentileData=[percenTile90,percenTile95,queryCount];119 }, function (result) {120 $log.error(result);121 }).$promise;122 }();123 $scope.dailyLatencyReportRightYaxis = function(){124 return "Query Latency";125 }126 $scope.xAxisTickFormatFunction = function(){127 return function(d){128 return d3.time.format('%Y-%m-%d')(moment.unix(d).toDate());129 }130 };131 $scope.legendColorFunction = function(){132 return function(d){133 return '#E01B5D';134 }135 };136 $scope.reduceProjectPercentileTicks=false;137 $scope.projectPercentile = function () {138 var percenTile90 ={"key":"90%-ile","values":[]},percenTile95 = {"key":"95%-ile","values":[]};139 $scope.projectPercentilePromise = DashBoardService.projectPercentile({},140 function (data) {141 if(data.length>30){142 $scope.reduceProjectPercentileTicks=true;143 }144 for (var i = 0; i < data.length; i++) {145 var _latency90 = data[i][1].split(",")[0].substr(1);146 var _latency95 = data[i][1].split(",")[1].substr(0, data[i][1].length - 1);147 percenTile90.values.push([data[i][0],Math.round(parseFloat(_latency90) * 100) / 100]);148 percenTile95.values.push([data[i][0],Math.round(parseFloat(_latency95) * 100) / 100]);149 }150 $scope.eachProjectPercentileData=[percenTile90,percenTile95];151 }, function (result) {152 $log.error(result);153 }).$promise;154 }();155 $scope.projectPercentileToolTipContentFunction = function(key, x, y, e, graph) {156 return '<table>'+'<tr>'+'<td>'+'<strong>Latency</strong>'+'</td>'+'<td>'+' : '+y+'(S)'+'</td>'+'</tr>'+'<tr>'+'<td>'+'<strong>Percentile</strong>'+'</td>'+'<td>'+' : '+key+'</td>'+'</tr>'+'<tr>'+'<td>'+'<strong>Project</strong>'+'</td>'+'<td>'+' : '+x+'</td>'+'</tr>'+'</table>';157 }158 $scope.dailyPercentileToolTip = function(key, x, y, e, graph) {159 var suffix ='';160 if(key.indexOf('ile')!=-1){161 suffix = '(S)';162 }163 return '<table>'+'<tr>'+'<td>'+'<strong>'+key+'</strong>'+'</td>'+'<td>'+' : '+y+suffix+'</td>'+'</tr>'+'<tr>'+'<td>'+'<strong>Date</strong>'+'</td>'+'<td>'+' : '+x+'</td>'+'</tr>'+'</table>';164 }165 $scope.cubeToolTipContentFunction = function(key, x, y, e, graph) {166 return '<table>'+'<tr>'+'<td>'+'<strong>Source Records</strong>'+'</td>'+'<td>'+' : '+parseInt(y)+'</td>'+'</tr>'+'<tr>'+'<td>'+'<strong>Source Size</strong>'+'</td>'+'<td>'+' : '+$scope.dataSize(e.series.sizes[e.pointIndex]*1024)+'</td>'+'</tr>'+'<tr>'+'<td>'+'<strong>Cube Name</strong>'+'</td>'+'<td>'+' : '+x+'</td>'+'</tr>'+'</table>';167 }168 $scope.commonToolTipContentFunction = function(key, x, y, e, graph) {169 return '<p>' + x +':'+y+ '</p>';170 }171 $scope.cubesStorage = function () {172 $scope.cubesStoragePromise = DashBoardService.cubesStorage({},173 function (cubes) {174 $scope.stastic.cubesCount = cubes.length;175 var _cubeStorage = $scope.getTotalSize(cubes);176 $scope.stastic.cubeStorage = _cubeStorage;177 }, function (result) {178 $log.error(result);179 }).$promise;180 }();181 $scope.getTotalSize = function (cubes) {182 var size = 0;183 if (!cubes) {184 return 0;185 }186 else {187 for (var i = 0; i < cubes.length; i++) {188 size += cubes[i].size_kb;189 }190 return $scope.dataSize(size * 1024);191 }192 };...
MongoDBProducer.js
Source:MongoDBProducer.js
...112 case MongoDBFunctions.AGGREGATE:113 response = await this._queryAggregate(config);114 break;115 case MongoDBFunctions.COUNT:116 response = await this._queryCount(config);117 break;118 default:119 response = await this._queryFindASync(config);120 }121 return new RequestResponse(response, requestId);122 }123 /**124 * @param collection125 * @param query JSON with the MongoDB Query as JSON126 * @param (optional) resultkeys (def=[]) - the projection fields127 * @param (optional) targetRange Cells TargetRange (if ommited then METADATA and128 * DATA is included in the result, otherwise the fieldnames below DATA are returned129 * and placed into the Targetrange according to Resultkeys-Template. This requires result130 * @param (optional) page - page id (if omitted or 0 then all documents are returned)131 * @returns {Promise<Promise<*>|*>}132 * @constructor133 */134 async _queryDelete(config) {135 const { collection, query = {} } = config;136 const resultMessage = {137 Metadata: {138 collection139 },140 Data: []141 };142 try {143 const client = await this.connect();144 const db = client.db(this.dbName);145 const { result } = await db.collection(collection).remove(query);146 resultMessage.Data = result;147 } catch (e) {148 resultMessage.Metadata.error = e.message;149 this.handleWarningOnce(e);150 }151 return resultMessage;152 }153 async _queryCount({ collection, query }) {154 const result = {155 Metadata: {156 collection157 },158 Data: {}159 };160 try {161 const client = await this.connect();162 const db = client.db(this.dbName);163 result.Data.count = await db164 .collection(collection)165 .find(query)166 .count();167 } catch (err) {...
ModelInstanceManager.js
Source:ModelInstanceManager.js
1/******************************************************************************2 * Copyright (c) Dworek 2016. All rights reserved. *3 * *4 * @author Tim Visee *5 * @website http://timvisee.com/ *6 * *7 * Open Source != No Copyright *8 * *9 * Permission is hereby granted, free of charge, to any person obtaining a *10 * copy of this software and associated documentation files (the "Software"), *11 * to deal in the Software without restriction, including without limitation *12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, *13 * and/or sell copies of the Software, and to permit persons to whom the *14 * Software is furnished to do so, subject to the following conditions: *15 * *16 * The above copyright notice and this permission notice shall be included *17 * in all copies or substantial portions of the Software. *18 * *19 * You should have received a copy of The MIT License (MIT) along with this *20 * program. If not, see <http://opensource.org/licenses/MIT/>. *21 ******************************************************************************/22var _ = require('lodash');23var ObjectId = require('mongodb').ObjectId;24var Core = require('../../Core');25/**26 * ModelInstanceManager class.27 *28 * @param {Function} modelConstructor Constructor for the model.29 *30 * @class31 * @constructor32 */33var ModelInstanceManager = function(modelConstructor) {34 /**35 * Weak map of model instances.36 *37 * @type {Map}38 * @private39 */40 this._instances = new Map();41 /**42 * Model constructor.43 *44 * @type {Function}45 * @private46 */47 this._modelConstructor = modelConstructor;48 /**49 * Number of queries executed.50 */51 this._queryCount = 0;52};53/**54 * Create a new model instance for the given model object ID, or return the already existing one.55 *56 * @param {ObjectId|String} id Object ID.57 * @param {Object} [localCache] Object with fields and values to cache locally, which greatly benefits performance.58 * @return {Object} Model object instance.59 */60ModelInstanceManager.prototype.create = function(id, localCache) {61 // Parse the ID62 id = this._parseId(id);63 // Update the query count64 this._increaseQueryCount();65 // Return the instance if it's known66 if(this.has(id))67 return this._instances.get(id);68 // Create a new model instance69 var model = new this._modelConstructor(new ObjectId(id));70 // Add local cache if configured71 if(localCache !== undefined && _.isObject(localCache))72 model._baseModel.cacheSetFields(localCache);73 // Put the object in the instances map74 this._instances.set(id, model);75 // Return the instance76 return model;77};78/**79 * Get the instance for a given object ID.80 *81 * @param {ObjectId|String} id Object ID.82 * @return {Object|undefined} Model object instance or undefined if no instance with the given ID is known.83 */84ModelInstanceManager.prototype.get = function(id) {85 // Update the query count86 this._increaseQueryCount();87 // Get the value88 return this._instances.get(this._parseId(id));89};90/**91 * Check whether an instance for the given object ID exists.92 *93 * @param {ObjectId|String} id Object ID.94 * @return {boolean} True if an instance exists, false if not.95 */96ModelInstanceManager.prototype.has = function(id) {97 // Update the query count98 this._increaseQueryCount();99 // Get the result100 return this._instances.has(this._parseId(id));101};102/**103 * Parse an object ID to make it usable as key in the instances map.104 *105 * @param {ObjectId|String} id Object ID.106 * @return {string} Parsed object ID.107 *108 * @private109 */110ModelInstanceManager.prototype._parseId = function(id) {111 // Convert the ID to a string if it isn't112 if(!_.isString(id))113 id = id.toString();114 // Lowercase and return the string115 return id.toLowerCase();116};117/**118 * Count the number of instances.119 *120 * @return {Number} Number of instances.121 */122ModelInstanceManager.prototype.count = function() {123 // Update the query count124 this._increaseQueryCount();125 // Return the count126 return this._instances.size;127};128/**129 * Clear the list of instances.130 *131 * @param {boolean} [clearModelCache=true] True to also clear the internal cache of the managed models, false to ignore this.132 */133ModelInstanceManager.prototype.clear = function(clearModelCache) {134 // Update the query count135 this._increaseQueryCount();136 // Clear the model cache137 if(clearModelCache || clearModelCache === undefined)138 this.clearModelCache();139 140 // Reset the instances map141 this._instances = new Map();142};143/**144 * Clear the cache for the managed model instances.145 */146ModelInstanceManager.prototype.clearModelCache = function() {147 // Update the query count148 this._increaseQueryCount();149 // Clear150 this._instances.forEach((instance) => instance._baseModel.cacheFlush());151};152/**153 * Increase the query count by one.154 *155 * @private156 */157ModelInstanceManager.prototype._increaseQueryCount = function() {158 this._queryCount++;159 Core.status.internalCache.queryCount++;160};161// Export the class...
ObjectCache.js
Source:ObjectCache.js
1/******************************************************************************2 * Copyright (c) Dworek 2016. All rights reserved. *3 * *4 * @author Tim Visee *5 * @website http://timvisee.com/ *6 * *7 * Open Source != No Copyright *8 * *9 * Permission is hereby granted, free of charge, to any person obtaining a *10 * copy of this software and associated documentation files (the "Software"), *11 * to deal in the Software without restriction, including without limitation *12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, *13 * and/or sell copies of the Software, and to permit persons to whom the *14 * Software is furnished to do so, subject to the following conditions: *15 * *16 * The above copyright notice and this permission notice shall be included *17 * in all copies or substantial portions of the Software. *18 * *19 * You should have received a copy of The MIT License (MIT) along with this *20 * program. If not, see <http://opensource.org/licenses/MIT/>. *21 ******************************************************************************/22var Core = require('../../Core');23/**24 * Constructor.25 *26 * @returns {ObjectCache} ObjectCache instance.27 */28var ObjectCache = function() {29 /**30 * Map of cached data.31 *32 * @type {Map}33 * @private34 */35 this._cache = new Map();36 /**37 * Number of queries executed.38 */39 this._queryCount = 0;40};41/**42 * Check whether the cache of this object has the given field cached.43 *44 * @param {string} field Cached field.45 *46 * @returns {boolean} True if this field is available, false if not.47 */48ObjectCache.prototype.hasCache = function(field) {49 // Increase the query count50 this._increaseQueryCount();51 // Return true if the field is cached52 return this._cache.has(field);53};54/**55 * Get the cached value for the given field.56 *57 * @param {String} field Field.58 * @returns {*|undefined} Cached value, or undefined if the field is unknown.59 */60ObjectCache.prototype.getCache = function(field) {61 // Increase the query count62 this._increaseQueryCount();63 // Get the cached value64 return this._cache.get(field);65};66/**67 * Get the number of cached fields.68 *69 * @returns {Number} Total number of cached fields.70 */71ObjectCache.prototype.getCacheCount = function() {72 // Increase the query count73 this._increaseQueryCount();74 // Return the size75 return this._cache.size;76};77/**78 * Set a cache field.79 *80 * @param {string} field Cache field name.81 * @param {*} value Value to cache.82 */83ObjectCache.prototype.setCache = function(field, value) {84 // Increase the query count85 this._increaseQueryCount();86 // Set the cache value87 this._cache.set(field, value);88};89/**90 * Cache multiple fields from the given JSON object.91 *92 * @param {object} values Object with fields to cache.93 */94ObjectCache.prototype.setCacheMultiple = function(values) {95 // Make sure the given value isn't null or undefined96 if(values === undefined || values === null)97 return;98 // Loop through all keys in the object, and add them individually99 for(var key in values)100 // Make sure we aren't handling prototype fields101 if(values.hasOwnProperty(key))102 this.setCache(key, values[key]);103};104/**105 * Flush the data that is currently cached in this object.106 * All cache will be flushed unless specific fields are given.107 *108 * @param {Array|string} [fields] Array of fields or a specific field as a string to flush.109 */110ObjectCache.prototype.flushCache = function(fields) {111 // Increase the query count112 this._increaseQueryCount();113 // Flush all if no field is given114 if(fields === undefined) {115 this._cache = new Map();116 return;117 }118 // Convert the fields parameter to an array, if it isn't in array format119 if(!Array.isArray(fields))120 fields = [fields];121 // Loop through the list of fields, and delete them one by one122 for(var i = 0, fieldCount = fields.length; i < fieldCount; i++)123 this._cache.delete(fields[i]);124};125/**126 * Increase the query count by one.127 *128 * @private129 */130ObjectCache.prototype._increaseQueryCount = function() {131 this._queryCount++;132 Core.status.internalCache.queryCount++;133};134// Export the user class...
index.js
Source:index.js
1// äºå½æ°å
¥å£æ件2const cloud = require('wx-server-sdk')3cloud.init({4 env: 'test-f3c86f'5})6// äºå½æ°å
¥å£å½æ°7exports.main = async (event, context) => {8 const wxContext = cloud.getWXContext()9 var queryNum = 20;10 var difficultList = [11 {12 difficultLevel:1,13 num:614 },15 {16 difficultLevel: 2,17 num: 818 },19 {20 difficultLevel: 3,21 num: 622 }23 ];24 var result = [];25 var functionList = [];26 for (var i = 0; i < difficultList.length; i++){27 functionList.push(queryParactice(difficultList[i].difficultLevel,difficultList[i].num));28 }29 Promise.all(functionList).then(res =>{30 console.log(res);31 for(var j=0; j<res.length; j++){32 result.push(j);33 }34 })35 return {36 event,37 openid: wxContext.OPENID,38 appid: wxContext.APPID,39 unionid: wxContext.UNIONID,40 result: result41 }42}43function queryParactice(level,num){ 44 //æ¥è¯¢æ°æ®åºæ¬¡æ°45 var queryCount = 4;46 //ä¸æ¬¡æ¥è¯¢æ°é47 var queryNum = 2;48 if (num > queryNum * queryCount){49 queryNum = num / queryCount;50 if (num % queryCount != 0){51 queryNum++;52 }53 }else{54 queryCount = num / queryNum;55 if(queryCount == 0){56 queryCount++;57 }58 }59 const db = cloud.database();60 const _ = db.command;61 var querySql = db.collection('paper').where({62 level: _.eq(level)63 });64 querySql.count()65 .then(res => {66 var count = res.total;67 if(count <= num){68 querySql.get()69 .then(res => {70 console.log(res);71 return res.data;72 })73 }else{74 var result = [];75 for(var i=0; i<queryCount; i++){76 var step = count / queryCount;77 var randomSkip = randomNum(step * i, step * (i + 1)) - queryCount;78 if (randomSkip < step * i){79 randomSkip = step * i;80 }81 var _queryCount = queryCount > num - result.length ? num - result.length : queryCount;82 querySql.skip(randomSkip).limit(_queryCount).get()83 .then(res => {84 console.log(res);85 result.push(res.data);86 if(result.length == num){87 return result;88 }89 })90 }91 }92 })93}94function queryPracticeLimit(queryCount,queryNum){95}96//çæä»minNumå°maxNumçéæºæ°97function randomNum(minNum, maxNum) {98 switch (arguments.length) {99 case 1:100 return parseInt(Math.random() * minNum + 1, 10);101 break;102 case 2:103 if (maxNum == minNum) return maxNum;104 return parseInt(Math.random() * (maxNum - minNum) + minNum, 10);105 break;106 default:107 return 0;108 break;109 }...
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.screenshot({ path: 'example.png' });7 console.log(page._queryCount());8 await browser.close();9})();10import { Page } from './page';11import { Browser } from './browser';12import { assert } from '../../utils/utils';13import { Events } from '../../utils/events';14import { BrowserContextOptions, BrowserContextInitializer, BrowserContextEvents } from '../../server/channels';15import { BrowserContextBase } from './browserContextBase';16import { PageProxy } from './pageProxy';17import { Selectors } from '../../server/selectors';18import { ViewportSize } from '../../server/page';19import { TimeoutSettings } from '../../utils/timeoutSettings';20import { Download } from './download';21import { CRBrowser, CRBrowserContext } from '../chromium/crBrowser';22import { Protocol } from 'devtools-protocol';23export class BrowserContext extends BrowserContextBase {24 _browser: Browser;25 _options: BrowserContextOptions;26 _timeoutSettings: TimeoutSettings;27 _pageProxy: PageProxy;28 _downloads = new Set<Download>();29 _browserContextId: string;30 _crBrowserContext: CRBrowserContext | undefined;31 _closePromise: Promise<void>;32 _closePromiseFulfill: () => void;33 _closePromiseReject: (error: Error) => void;34 constructor(browser: Browser, options: BrowserContextOptions) {35 super();36 this._browser = browser;37 this._options = options;38 this._timeoutSettings = new TimeoutSettings();39 this._pageProxy = new PageProxy(this);40 this._closePromise = new Promise((f, r) => {41 this._closePromiseFulfill = f;42 this._closePromiseReject = r;43 });44 this._browserContextId = browser._browserContextIds.create().toString();45 this._browser._contexts.add(this);46 this._browser._connection.on('Browser.close', () => this._didClose());47 this._browser._connection.on('Browser.downloadCreated', ({ downloadId, url,
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.fill('input[name="q"]', 'Playwright');7 await page.click('text=Google Search');8 await page.waitForSelector('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');9 await page.click('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');10 await page.waitForSelector('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');11 await page.click('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');12 await page.waitForSelector('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');13 await page.click('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');14 await page.waitForSelector('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');15 await page.click('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');16 await page.waitForSelector('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');17 await page.click('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');18 await page.waitForSelector('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');19 await page.click('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');20 await page.waitForSelector('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');21 await page.click('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');22 await page.waitForSelector('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API');23 await page.click('text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.fill('input[aria-label="Search"]', 'playwright');7 await page.keyboard.press('Enter');8 await page.waitForNavigation();9 const count = await page._queryCount('text=Playwright - Node.js library to automate Chromium, Firefox and WebKit with a single API');10 console.log(count);11 await browser.close();12})();13const { chromium } = require('playwright');14(async () => {15 const browser = await chromium.launch();16 const context = await browser.newContext();17 const page = await context.newPage();18 await page.fill('input[aria-label="Search"]', 'playwright');19 await page.keyboard.press('Enter');20 await page.waitForNavigation();21 const count = await page._queryCount('text=Playwright - Node.js library to automate Chromium, Firefox and WebKit with a single API');22 console.log(count);23 await browser.close();24})();
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 console.log('Number of queries: ' + page._queryCount);7 await browser.close();8})();9const { chromium } = require('playwright');10(async () => {11 const browser = await chromium.launch();12 const context = await browser.newContext();13 const page = await context.newPage();14 const userAgent = await page.$eval('#detected_value', (el) => el.textContent);15 console.log('User Agent: ' + userAgent);16 await browser.close();17})();18const { chromium } = require('playwright');19(async () => {20 const browser = await chromium.launch();21 const context = await browser.newContext();22 const page = await context.newPage();23 const userAgent = await page.$('#detected_value');24 console.log('User Agent: ' + userAgent);25 await browser.close();26})();27const { chromium } = require('playwright');28(async () => {29 const browser = await chromium.launch();30 const context = await browser.newContext();31 const page = await context.newPage();32 await page.waitForSelector('#detected_value');33 const userAgent = await page.$('#detected_value');34 console.log('User Agent: ' + userAgent);35 await browser.close();36})();
Using AI Code Generation
1const { _queryCount } = require('@playwright/test/lib/server/frames');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const page = await browser.newPage();6 const count = await _queryCount(page, 'css=div');7 console.log(count);8 await browser.close();9})();
Using AI Code Generation
1const playwright = require('playwright');2const { _queryCount } = require('playwright/lib/server/chromium/crPage');3(async () => {4 const browser = await playwright.chromium.launch({5 });6 const context = await browser.newContext();7 const page = await context.newPage();8 const result = await _queryCount(page, 'text="Google"', 'css');9 console.log(result);10 await browser.close();11})();
Using AI Code Generation
1const { _queryCount } = require('playwright/lib/server/supplements/recorderSupplement');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 await page.click('text=Google apps');8 await page.click('text=Mail');9 await page.click('text=Sign in');10 await page.fill('#identifierId', 'xxxxxxxxxxxx');11 await page.click('text=Next');12 await page.fill('[name="password"]', 'xxxxxxxxxxxx');13 await page.click('text=Next');14 await page.click('text=Compose');15 await page.fill('[placeholder="To"]', 'xxxxxxxxxxxx');16 await page.click('text=Next');17 await page.fill('[placeholder="Subject"]', 'test');18 await page.click('text=Next');19 await page.fill('[placeholder="Message body"]', 'test');20 await page.click('text=Send');21 await page.click('text=Inbox');22 await page.click('text=Sent');23 await page.click('text=Inbox');24 await page.click('text=Google apps');25 await page.click('text=Account');26 await page.click('text=Sign out');27 await browser.close();28})();29const { _queryCount } = require('playwright/lib/server/supplements/recorderSupplement');30const { chromium } = require('playwright');31(async () => {32 const browser = await chromium.launch();33 const context = await browser.newContext();34 const page = await context.newPage();35 await page.click('text=Google apps');36 await page.click('text=Mail');37 await page.click('text=Sign in');38 await page.fill('#identifierId', 'xxxxxxxxxxxx');39 await page.click('text=Next');40 await page.fill('[name="password"]', 'xxxxxxxxxxxx');41 await page.click('text=Next');42 await page.click('text=Compose');43 await page.fill('[placeholder="To"]', 'xxxxxxxxxxxx');44 await page.click('text=Next');45 await page.fill('[placeholder="Subject"]', 'test');
Using AI Code Generation
1const { _queryCount } = require('playwright/lib/server/supplements/recorder/recorderSupplement.js');2const { _querySelector } = require('playwright/lib/server/supplements/recorder/recorderSupplement.js');3const { chromium } = require('playwright');4(async () => {5 const browser = await chromium.launch();6 const context = await browser.newContext();7 const page = await context.newPage();8 await page.click('text=News');9 await page.click('text=World');
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!