Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var tabletojson = require('tabletojson');
  2. var mysql = require("mysql");
  3. var striptag = require("striptags");
  4. var fs = require("fs");
  5. var path = require('path');
  6.  
  7. var startCollector;
  8. var iterations = 0;
  9. var insertions = 0;
  10. var duplicated = 0;
  11.  
  12. var datas = [];
  13.  
  14. var clients = ["ClientA", "ClientB", "ClientC", "ClientD", "ClientE", "ClientF", "ClientG", "ClientH"];
  15. var appDir = path.dirname(require.main.filename);
  16.  
  17. var errorList = ["err1", "err2", "err3", "err4", "err5", "err6"];
  18.  
  19. var con = mysql.createPool({
  20.     host: "localhost",
  21.     user: "User",
  22.     password: "Password",
  23.     database: "errors"
  24.   });
  25.  
  26. function CollectErrors() {
  27.     startCollector = new Date();
  28.     for(var a = 0; a < clients.length; a++) {
  29.         (function(a) {
  30.             tabletojson.convertUrl("http://example.com" + clients[a] + "/page.php?limit=100", { stripHtmlFromCells: false }, function(response) {
  31.             var rs = response[0];
  32.                 for(var l = rs.length-1; l > -1; l--) {
  33.                     var newDate = formatDate(striptag(rs[l]["Date"]), striptag(rs[l]["Time"]));
  34.                     var user = getUser(striptag(rs[l]["User"]));
  35.                     var msg = striptag(rs[l]["Error"]);
  36.                     var splitError = rs[l]["Error"].split("<a href=\"");
  37.                     var link = getUrl(splitError[1]);
  38.                     var id = getId(link);
  39.                     var type = getType(striptag(splitError[0]));
  40.                     var temp = [newDate, link, type, user, clients[a], id, msg];
  41.                     datas.push(temp);
  42.                 }
  43.                 });
  44.         })(a);
  45.     }
  46.     con.getConnection(function(err, connection) {
  47.         connection.query("INSERT IGNORE INTO entries (time, url, type, author, client, uid, message) VALUES ?", [datas], function(err, rows) {
  48.             console.log(err);
  49.         });
  50.         connection.release();
  51.         datas = [];
  52.     });
  53.     setTimeout(CollectErrors, 10000);
  54.  
  55. }
  56.  
  57.  
  58.  
  59. function formatDate(date, time) {
  60.     var newdate = date.split("/").reverse().join("-");
  61.     var newtime = time+":00";
  62.     return newdate + " " + newtime;
  63. }
  64.  
  65. function getUrl(uri) {
  66.     return "http://example.com/"+uri.split("\">Details")[0];
  67. }
  68.  
  69. function getId(url) {
  70.     return decodeURIComponent((new RegExp('[?|&]' + "id" + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [null, ''])[1].replace(/\+/g, '%20')) || null;
  71. }
  72.  
  73. function getType(error) {
  74.     for(var a = 0; a < errorList.length; a++) {
  75.         if(error.indexOf(errorList[a]) !== -1) {
  76.             return errorList[a];
  77.         }
  78.     }
  79.     return "Other";
  80. }
  81.  
  82. function getUser(user) {
  83.     if(user == "" || user == "&#xA0;" || user == null) {
  84.         return "System";
  85.     }
  86.     return user;
  87. }
  88.  
  89.  
  90. CollectErrors();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement