Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uploadData #2

Open
jadenben01 opened this issue Apr 4, 2019 · 0 comments
Open

uploadData #2

jadenben01 opened this issue Apr 4, 2019 · 0 comments

Comments

@jadenben01
Copy link
Owner

var Fingerprint2 = require('fingerprintjs2');
var nanoajax = require('nanoajax')

var product = process.env.PRODUCT;
var productModule = process.env.MODULE;
var pushDataUrl = 'https://3s.sreanalyze.com/api/v1/stats/collect';

window.THREEDATA = {
  'db': product,
  'global': {
    
  },
  'data': {
    't_3s_html_ajax': [],
    't_3s_html_error': []
  }
};
window.ISACTIVE = false;

//error callback
//window.onerror = function (msg, url, lineNo, columnNo, error) {
//  var errorData = {
//    'path': url,
//    'msg': msg
//  };
//  window.THREEDATA.data.t_3s_html_error.push(errorData);
//  console.log(errorData);
//  return false;
//};

//monkey patching xhr
catchXHR()
function catchXHR(){
  if (!XMLHttpRequest && !window.ActiveXObject) {
    return
  }
  // patch
  var XHR = XMLHttpRequest || window.ActiveXObject
  var open = XHR.prototype.open
  try {
    XHR.prototype.open = function (method, url) {
    this.RequestMethod = method;
    this.RequestUrlString = url;
    open.apply(this, arguments)
    }
  } catch (err) {
    console.log(err)
  }
  
  var send = XHR.prototype.send
  XHR.prototype.send = function (data) {
    try {
    this.RequestStartTime = Date.now();
    var self = this;
    this.addEventListener('readystatechange', function(){
      if (self.readyState === 4) {
        self.RequestEndTime = Date.now();
        var xhrdata = {
          'url': self.RequestUrlString,
          'time': self.RequestStartTime,
          'method': self.RequestMethod,
          'rsptime': self.RequestEndTime - self.RequestStartTime,
          'rspcode': self.status,
          'rspsize': getResponseTextSize(self.responseType)
        }
        if(!window.ISACTIVE){
          window.THREEDATA.data.t_3s_html_ajax.push(xhrdata);
        }else{
          window.ACTIVEDATA.data.t_3s_html_ajax.push(xhrdata);
        }
        
        function getResponseTextSize(type){
          if(type == 'json' || type == 'text' || type == 'document' || type == ''){
            return (self.responseText + '').length;
          }else if(type == 'arraybuffer'){
            return new Uint8Array(self.response).byteLength;
          }else{
            return 0;
          }
        }
      }
    }, false)
    send.apply(this, arguments)
    } catch (err) {
    console.log(err)
    }
  }
}
var transferCount = 0;
window.ThreeInitLock = 0;
transferData();
function transferData(){
  if (!window.performance) return;
  if(!window.performance.timing.loadEventEnd){
    setTimeout(transferData, 3*1000);
  }else if(!window.FirstScreenTime && transferCount < 2){
    transferCount++;
    setTimeout(transferData, 3*1000);
  }else{
    if (window.requestIdleCallback) {
      requestIdleCallback(function () {
        initData();
      });
    } else {
      setTimeout(function(){
        initData();
      }, 1*1000);
    };
    setTimeout(function(){
      initData();
    }, 3*1000);
  }
  function initData(){
    if (window.ThreeInitLock !== 0) {
        return;
    }
    window.ThreeInitLock = 1
    var timing = performance.timing;
    var entries = performance.getEntries()
    //id
    Fingerprint2.get(function(components) {
      var uuid = Fingerprint2.x64hash128(components.map(function (pair) { return pair.value }).join(), 31);
      window.THREEDATA.global['id'] = uuid;
      //product
      //module
      //domain
      var global = window.THREEDATA.global;
      global['product'] = product;
      global['module'] = productModule;
      global['domain'] = window.location.host;
      global['page'] = window.location.pathname;
      
      var data = window.THREEDATA.data;
      data.t_3s_html_info = [{
        'title': document.title,
        'referer': document.referrer,
        'user-agent': navigator.userAgent
      }]
      data.t_3s_html_load = [{
        'time': timing.navigationStart || Date.now(),
        'dns': timing.domainLookupEnd - timing.domainLookupStart || 0,
        'tcp': timing.connectEnd - timing.connectStart || 0,
        'ttfb': timing.responseStart - timing.requestStart || 0,
        'download': timing.responseEnd - timing.responseStart || 0,
        'processing': timing.domComplete - timing.domLoading || 0,
        'load': timing.loadEventEnd - timing.loadEventStart || 0,
        'domcontentloaded': timing.domContentLoadedEventEnd - timing.navigationStart || 0,
        'onload': timing.loadEventEnd - timing.navigationStart,
        'firstscreen': window.FirstScreenTime || 0,
        'size': entries[0]['transferSize'],
        'pingcdn': window.PingCdnTime || 0,
        'fcp': (window.performance.getEntriesByType("paint").length > 1) && parseInt(window.performance.getEntriesByType("paint")[1].startTime)
      }];
      uploadData();
    })
    
    function uploadData(){
      window.THREEDATA.time = Date.now();
      console.log(window.THREEDATA);
      //console.log(JSON.stringify(window.THREEDATA, null, 1).replace(/"/g, '\''))
      var code = btoa(unescape(encodeURIComponent(JSON.stringify(window.THREEDATA))));
      nanoajax.ajax({url: pushDataUrl, method: 'POST', body: 'code=' + code}, function (code, responseText, request) {
        
      });
      window.ACTIVEDATA = {
        'db': product,
        'global': window.THREEDATA.global,
        'data': {
          't_3s_html_live': [],
          't_3s_html_ajax': []
        }
      }
      window.ISACTIVE = true;
      if (productModule == 'gc') {
        setTimeout(activeData, 60*1000);
      } else {
        setTimeout(activeData, 30*1000);
      }
     // window.addEventListener("beforeunload", function (event) {
     //   event.preventDefault();
     //   activeData();
     // });
    }
    window.ACTIVEDATAuploadCount = 0; 
    function activeData(){
      if(window.ACTIVEDATAuploadCount > 0 && productModule == 'gc') return;
      if(window.ACTIVEDATAuploadCount > 19) return;
      window.ACTIVEDATAuploadCount++;
      window.ACTIVEDATA.data.t_3s_html_live = [{'live': window.ACTIVEDATAuploadCount}];
      window.ACTIVEDATA.time = Date.now();
      if(productModule == 'gc'){
        var isCompleted = false;
        var is97percentage = false;
        window.performance.getEntries().forEach(function(e){
          if(e.initiatorType === 'xmlhttprequest' && e.name.indexOf('data_gather') !== -1){
            if(!is97percentage && e.name.indexOf('data_gather?loadCompleted=yes') == -1){
              is97percentage = true;
              window.ACTIVEDATA.data.t_3s_html_ajax.push({                                      
                                'url': e.name,                                                  
                                'time': window.performance.timing.navigationStart + parseInt(e.startTime),                                                              
                                'method': 'OPTIONS',                                                                          
                                'rsptime': parseInt(e.duration),
                                'rspcode': 200,  
                                'rspsize': 0                                                                              
              }); 
            }
            if(!isCompleted && e.name.indexOf('data_gather?loadCompleted=yes') !== -1){
                isCompleted = true;
                window.ACTIVEDATA.data.t_3s_html_ajax.push({
                  'url': e.name,
                  'time': window.performance.timing.navigationStart + parseInt(e.startTime),
                  'method': 'OPTIONS',
                  'rsptime': parseInt(e.duration),
                  'rspcode': 200,
                  'rspsize': 0
                });
            }
          }
        });
      }
      if(window.ACTIVEDATAuploadCount==1){
        //console.log("test");
        var resource = [],
        //    size0 = [],
        //    size5 = [],
        //    size50 = [],
        //    size100 = [],
        //    size200 = [],
            size0 = {'name': 'size0', 'data': []},
            size5 = {'name': 'size5', 'data': []},
            size50 = {'name': 'size50', 'data': []},
            size100 = {'name': 'size100', 'data': []},
            size200 = {'name': 'size200', 'data': []},
            triggerDnsResource = undefined,
            cdn = window.sessionStorage.getItem('cdn') || undefined,
            domainMap = {};
        window.performance.getEntriesByType("resource").forEach(function(e){
          //console.log("all");
          //console.log(e);
            var urlParts = e.name.split("/");
            var url = urlParts[0] + "//" + urlParts[2];
            if(pushDataUrl.indexOf(url) == -1){
              if(domainMap[url] === undefined){
                domainMap[url] = [e];
              }else{
                domainMap[url].push(e);
              }
            }
        });
        window.domainMap = domainMap;
        for(var d in domainMap){
          if(domainMap.hasOwnProperty(d)){
            if(domainMap[d].length > resource.length){
              resource = domainMap[d];
              if (d.split('/').length >= 3){
                cdn = d.split('/')[2];
              }else{
                cdn = d;
              }
            }
          }
        };
        resource = resource.filter(function(e){
          if(productModule !== 'gc'){
            return e.initiatorType !== "xmlhttprequest" && e.decodedBodySize !== 0 && e.duration !== 0 && e.transferSize !== 0;
          }else{
            return e.decodedBodySize !== 0 && e.duration !== 0 && e.transferSize !== 0;
          }
        });
        triggerDnsResource = resource[0];
        window.cdnResource = resource;
        window.triggerDnsResource = resource[0];
        if(resource.length !== 0 && triggerDnsResource.name.indexOf(location.host) === -1){
          resource.forEach(splitResource);
          function splitResource(e){
            if(e.decodedBodySize > 0 && e.decodedBodySize <= 5000){
              size0.data.push(e);
            }else if(e.decodedBodySize > 5000 && e.decodedBodySize <= 50000){
              size5.data.push(e);
            }else if(e.decodedBodySize > 50000 && e.decodedBodySize <= 100000){
              size50.data.push(e);
            }else if(e.decodedBodySize > 100000 && e.decodedBodySize <= 200000){
              size100.data.push(e);
            }else{
              size200.data.push(e);
            }
          };
          [size0, size5, size50, size100, size200].forEach(function(e){
            e.data.sort(compare);
          });
          function compare(a, b){
            if(a.decodedBodySize > b.decodedBodySize){
              return 1;
            }else if(a.decodedBodySize === b.decodedBodySize){
              return 0;
            }else{
              return -1;
            }
          };
          function getMedianResource(e){
            if(e.data.length === 0){
              return "noresource"
            }else{
              //return e[Math.floor(e.length / 2)]
              return e.data[e.data.length-1]
            }
          }
          window.ACTIVEDATA.data.t_3s_cdn_speed = []
          var dnsTime = parseInt(triggerDnsResource.domainLookupEnd - triggerDnsResource.domainLookupStart);
          var tcpTime = parseInt(triggerDnsResource.connectEnd - triggerDnsResource.connectStart);
          [size0, size5, size50, size100, size200].forEach(function(e){
            var medianResource = getMedianResource(e);
            if(medianResource !== "noresource"){
              window.ACTIVEDATA.data.t_3s_cdn_speed.push({
                //domain: cdn,
                host: cdn,
                type: e.name,
                dns: dnsTime,
                tcp: tcpTime,
                ttfb: parseInt(medianResource.responseStart - medianResource.requestStart),
                download: parseInt(medianResource.responseEnd - medianResource.responseStart),
                size: medianResource.decodedBodySize,
                resource: medianResource.name
              });
            }
          });
        };
      }

      if(window.THREEUPLOAD === undefined){
        window.THREEUPLOAD = {};
      }else{
        for(var i in window.THREEUPLOAD){
          if(window.THREEUPLOAD.hasOwnProperty(i)){
            if(Object.prototype.toString.call(window.THREEUPLOAD[i]) !== "[object Array]"){
              continue;
            }
            window.ACTIVEDATA.data[i] = window.THREEUPLOAD[i];
            window.THREEUPLOAD[i] = [];
          }
        }
      }
      //var outputData = Object.assign({},window.ACTIVEDATA);
      var outputData = window.ACTIVEDATA;
      console.log(outputData)
      var code = btoa(unescape(encodeURIComponent(JSON.stringify(outputData))));
      nanoajax.ajax({url: pushDataUrl, method: 'POST', body: 'code=' + code}, function (code, responseText, request) {
        
      })
      window.ACTIVEDATA.data = {
        't_3s_html_live':[],
        't_3s_html_ajax':[]
      };
      setTimeout(activeData, 30*1000)
    }
  }
}


module.exports.clickInGame = function(gameName){
  var now = Date.now();
  Fingerprint2.get(function(components) {
    var uuid = Fingerprint2.x64hash128(components.map(function (pair) { return pair.value }).join(), 31);
    window.GAMEDATA = {
      'db': product,
      'global': {},
      'data': {
        't_3s_html_game': []
      }
    };
    window.GAMEDATA.global['id'] = uuid;
    var global = window.GAMEDATA.global;
    global['product'] = product;
    global['module'] = productModule;
    global['domain'] = window.location.host;
    global['page'] = window.location.pathname;
    
    var data = window.GAMEDATA.data;
    data.t_3s_html_game = [{
      'game': gameName
    }]
    gameData();
    function gameData(){
      window.GAMEDATA.time = now;
      //window.GAMEDATA.time = Date.now();
      console.log(window.GAMEDATA);
      var code = btoa(unescape(encodeURIComponent(JSON.stringify(window.GAMEDATA))));
      nanoajax.ajax({url: pushDataUrl, method: 'POST', body: 'code=' + code}, function (code, responseText, request) {
      });
    }
  });
}

module.exports.clickAction = function(action){
  var now = Date.now();
  Fingerprint2.get(function(components) {
    var uuid = Fingerprint2.x64hash128(components.map(function (pair) { return pair.value }).join(), 31);
    window.ACTIONDATA = {
      'db': product,
      'global': {},
      'data': {
        't_3s_html_action': []
      }
    };
    window.ACTIONDATA.global['id'] = uuid;
    var global = window.ACTIONDATA.global;
    global['product'] = product;
    global['module'] = productModule;
    global['domain'] = window.location.host;
    global['page'] = window.location.pathname;

    var data = window.ACTIONDATA.data;
    data.t_3s_html_action = [{
      'action': action
    }]
    actionData();
    function actionData(){
      window.ACTIONDATA.time = now;
      //window.ACTIONDATA.time = Date.now();
      console.log(window.ACTIONDATA);
      var code = btoa(unescape(encodeURIComponent(JSON.stringify(window.ACTIONDATA))));
      nanoajax.ajax({url: pushDataUrl, method: 'POST', body: 'code=' + code}, function (code, responseText, request) {
      });
    }
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant