Refactor actionlog queries
This commit is contained in:
parent
c4d5b1cd1d
commit
b342be50a3
153
actionlog.js
153
actionlog.js
|
@ -9,13 +9,15 @@ The above copyright notice and this permission notice shall be included in all c
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Database = require("./database");
|
|
||||||
var Logger = require("./logger");
|
var Logger = require("./logger");
|
||||||
|
|
||||||
exports.record = function(ip, name, action, args) {
|
module.exports = function (Server) {
|
||||||
if(typeof args === "undefined" || args === null) {
|
var db = Server.db;
|
||||||
|
return {
|
||||||
|
record: function (ip, name, action, args) {
|
||||||
|
if(!args)
|
||||||
args = "";
|
args = "";
|
||||||
} else {
|
else {
|
||||||
try {
|
try {
|
||||||
args = JSON.stringify(args);
|
args = JSON.stringify(args);
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
@ -23,129 +25,34 @@ exports.record = function(ip, name, action, args) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var db = Database.getConnection();
|
db.recordAction(ip, name, action, args);
|
||||||
if(!db)
|
},
|
||||||
return false;
|
|
||||||
|
|
||||||
var query = Database.createQuery(
|
clear: function (actions) {
|
||||||
"INSERT INTO actionlog (ip, name, action, args, time) "+
|
db.clearActions(actions);
|
||||||
"VALUES (?, ?, ?, ?, ?)",
|
},
|
||||||
[ip, name, action, args, Date.now()]
|
|
||||||
);
|
|
||||||
|
|
||||||
var result = db.querySync(query);
|
clearOne: function (item) {
|
||||||
if(!result) {
|
db.clearSingleAction(item);
|
||||||
Logger.errlog.log("! Failed to record action");
|
},
|
||||||
|
|
||||||
|
throttleRegistrations: function (ip, callback) {
|
||||||
|
db.recentRegistrationCount(ip, function (err, count) {
|
||||||
|
if(err) {
|
||||||
|
callback(err, null);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
callback(null, count > 4);
|
||||||
}
|
});
|
||||||
|
},
|
||||||
|
|
||||||
exports.clear = function(actions) {
|
listActionTypes: function (callback) {
|
||||||
var db = Database.getConnection();
|
db.listActionTypes(callback);
|
||||||
if(!db)
|
},
|
||||||
return false;
|
|
||||||
|
|
||||||
var list = new Array(actions.length);
|
listActions: function (types, callback) {
|
||||||
for(var i = 0; i < actions.length; i++)
|
db.listActions(types, callback);
|
||||||
list[i] = "?";
|
|
||||||
|
|
||||||
var query = Database.createQuery(
|
|
||||||
"DELETE FROM actionlog WHERE action IN ("+
|
|
||||||
list.join(",")+
|
|
||||||
")",
|
|
||||||
actions
|
|
||||||
);
|
|
||||||
|
|
||||||
var result = db.querySync(query);
|
|
||||||
if(!result) {
|
|
||||||
Logger.errlog.log("! Failed to clear action log");
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
return result;
|
};
|
||||||
}
|
|
||||||
|
|
||||||
exports.clearOne = function(e) {
|
|
||||||
var db = Database.getConnection();
|
|
||||||
if(!db)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
var query = Database.createQuery(
|
|
||||||
"DELETE FROM actionlog WHERE ip=? AND time=?",
|
|
||||||
[e.ip, e.time]
|
|
||||||
);
|
|
||||||
|
|
||||||
var result = db.querySync(query);
|
|
||||||
if(!result) {
|
|
||||||
Logger.errlog.log("! Failed to clear action log");
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.tooManyRegistrations = function (ip) {
|
|
||||||
var db = Database.getConnection();
|
|
||||||
if(!db)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
var query = Database.createQuery(
|
|
||||||
"SELECT * FROM actionlog WHERE ip=? AND action='register-success'"+
|
|
||||||
"AND time > ?",
|
|
||||||
[ip, Date.now() - 48 * 3600 * 1000]
|
|
||||||
);
|
|
||||||
|
|
||||||
var results = db.querySync(query);
|
|
||||||
if(!results) {
|
|
||||||
Logger.errlog.log("! Failed to check tooManyRegistrations");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
var rows = results.fetchAllSync();
|
|
||||||
// TODO Config value for this
|
|
||||||
return rows.length > 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.getLogTypes = function () {
|
|
||||||
var db = Database.getConnection();
|
|
||||||
if(!db)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
var query = "SELECT DISTINCT action FROM actionlog";
|
|
||||||
var result = db.querySync(query);
|
|
||||||
if(!result) {
|
|
||||||
Logger.errlog.log("! Failed to read action log");
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
result = result.fetchAllSync();
|
|
||||||
var actions = [];
|
|
||||||
for(var i in result)
|
|
||||||
actions.push(result[i].action);
|
|
||||||
|
|
||||||
return actions;
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.readLog = function (actions) {
|
|
||||||
var db = Database.getConnection();
|
|
||||||
if(!db)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
var query = "SELECT * FROM actionlog";
|
|
||||||
if(actions !== undefined) {
|
|
||||||
var list = new Array(actions.length);
|
|
||||||
for(var i in actions)
|
|
||||||
list[i] = "?";
|
|
||||||
list = list.join(",");
|
|
||||||
query += Database.createQuery(
|
|
||||||
" WHERE action IN ("+list+")",
|
|
||||||
actions
|
|
||||||
);
|
|
||||||
}
|
|
||||||
var result = db.querySync(query);
|
|
||||||
if(!result) {
|
|
||||||
Logger.errlog.log("! Failed to read action log");
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.fetchAllSync();
|
|
||||||
}
|
|
||||||
|
|
96
database.js
96
database.js
|
@ -998,6 +998,100 @@ Database.prototype.listIPsForName = function (name, callback) {
|
||||||
|
|
||||||
/* END REGION */
|
/* END REGION */
|
||||||
|
|
||||||
|
/* REGION action log */
|
||||||
|
|
||||||
|
Database.prototype.recordAction = function (ip, name, action, args,
|
||||||
|
callback) {
|
||||||
|
var self = this;
|
||||||
|
if(typeof callback !== "function")
|
||||||
|
callback = blackHole;
|
||||||
|
|
||||||
|
var query = "INSERT INTO actionlog (ip, name, action, args, time) " +
|
||||||
|
"VALUES (?, ?, ?, ?, ?)";
|
||||||
|
|
||||||
|
self.query(query, [ip, name, action, args, Date.now()], callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Database.prototype.clearActions = function (actions, callback) {
|
||||||
|
var self = this;
|
||||||
|
if(typeof callback !== "function")
|
||||||
|
callback = blackHole;
|
||||||
|
|
||||||
|
var list = [];
|
||||||
|
for(var i in actions)
|
||||||
|
list.push("?");
|
||||||
|
|
||||||
|
var actionlist = "(" + list.join(",") + ")";
|
||||||
|
|
||||||
|
var query = "DELETE FROM actionlog WHERE action IN " + actionlist;
|
||||||
|
self.query(query, actions, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Database.prototype.clearSingleAction = function (item, callback) {
|
||||||
|
var self = this;
|
||||||
|
if(typeof callback !== "function")
|
||||||
|
callback = blackHole;
|
||||||
|
|
||||||
|
var query = "DELETE FROM actionlog WHERE ip=? AND time=?";
|
||||||
|
self.query(query, [item.ip, item.time], callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Database.prototype.recentRegistrationCount = function (ip, callback) {
|
||||||
|
var self = this;
|
||||||
|
if(typeof callback !== "function")
|
||||||
|
return;
|
||||||
|
|
||||||
|
var query = "SELECT * FROM actionlog WHERE ip=? " +
|
||||||
|
"AND action='register-success' AND time > ?";
|
||||||
|
|
||||||
|
self.query(query, [ip, Date.now() - 48 * 3600 * 1000],
|
||||||
|
function (err, res) {
|
||||||
|
if(err) {
|
||||||
|
callback(err, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null, res.length);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Database.prototype.listActionTypes = function (callback) {
|
||||||
|
var self = this;
|
||||||
|
if(typeof callback !== "function")
|
||||||
|
return;
|
||||||
|
|
||||||
|
var query = "SELECT DISTINCT action FROM actionlog";
|
||||||
|
self.query(query, function (err, res) {
|
||||||
|
if(err) {
|
||||||
|
callback(err, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var types = [];
|
||||||
|
res.forEach(function (row) {
|
||||||
|
types.push(row.action);
|
||||||
|
});
|
||||||
|
callback(null, types);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Database.prototype.listActions = function (types, callback) {
|
||||||
|
var self = this;
|
||||||
|
if(typeof callback !== "function")
|
||||||
|
return;
|
||||||
|
|
||||||
|
var list = [];
|
||||||
|
for(var i in types)
|
||||||
|
list.push("?");
|
||||||
|
|
||||||
|
var actionlist = "(" + list.join(",") + ")";
|
||||||
|
var query = "SELECT * FROM actionlog WHERE action IN " + actiontypes;
|
||||||
|
self.query(query, types, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
/* END REGION */
|
||||||
|
|
||||||
/* REGION stats */
|
/* REGION stats */
|
||||||
|
|
||||||
Database.prototype.listStats = function (callback) {
|
Database.prototype.listStats = function (callback) {
|
||||||
|
@ -1008,4 +1102,6 @@ Database.prototype.listStats = function (callback) {
|
||||||
var query = "SELECT * FROM stats ORDER BY time ASC";
|
var query = "SELECT * FROM stats ORDER BY time ASC";
|
||||||
self.query(query, callback);
|
self.query(query, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* END REGION */
|
||||||
module.exports = Database;
|
module.exports = Database;
|
||||||
|
|
Loading…
Reference in a new issue