Created
November 16, 2017 22:30
-
-
Save goofmint/1906bdbbbd61d27713117829ce7eaf36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadScript(src) { | |
return new Promise(function(res, rej) { | |
try { | |
var done = false; | |
var head = document.getElementsByTagName('head')[0]; | |
var script = document.createElement('script'); | |
script.src = src; | |
head.appendChild(script); | |
// Attach handlers for all browsers | |
script.onload = script.onreadystatechange = function() { | |
if ( !done && (!this.readyState || | |
this.readyState === "loaded" || this.readyState === "complete") ) { | |
done = true; | |
res(); | |
// Handle memory leak in IE | |
script.onload = script.onreadystatechange = null; | |
if ( head && script.parentNode ) { | |
head.removeChild( script ); | |
} | |
} | |
}; | |
}catch(e) { | |
rej(e); | |
} | |
}); | |
} | |
function getCookie(name){ | |
var r = null; | |
var c = name + '='; | |
var allcookies = document.cookie; | |
var position = allcookies.indexOf( c ); | |
if( position != -1 ){ | |
var startIndex = position + c.length; | |
var endIndex = allcookies.indexOf( ';', startIndex ); | |
if( endIndex == -1 ){ | |
endIndex = allcookies.length; | |
} | |
r = decodeURIComponent(allcookies.substring( startIndex, endIndex ) ); | |
} | |
return r; | |
} | |
function sheet_from_array_of_arrays(data, opts) { | |
var ws = {}; | |
var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }}; | |
for(var R = 0; R != data.length; ++R) { | |
for(var C = 0; C != data[R].length; ++C) { | |
if(range.s.r > R) range.s.r = R; | |
if(range.s.c > C) range.s.c = C; | |
if(range.e.r < R) range.e.r = R; | |
if(range.e.c < C) range.e.c = C; | |
var cell = {v: data[R][C] }; | |
if(cell.v == null) continue; | |
var cell_ref = XLSX.utils.encode_cell({c:C,r:R}); | |
if(typeof cell.v === 'number') cell.t = 'n'; | |
else if(typeof cell.v === 'boolean') cell.t = 'b'; | |
else if(cell.v instanceof Date) { | |
cell.t = 'n'; cell.z = XLSX.SSF._table[14]; | |
cell.v = datenum(cell.v); | |
} | |
else cell.t = 's'; | |
ws[cell_ref] = cell; | |
} | |
} | |
if(range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range); | |
return ws; | |
} | |
function Workbook() { | |
if(!(this instanceof Workbook)) return new Workbook(); | |
this.SheetNames = []; | |
this.Sheets = {}; | |
} | |
var wb = new Workbook(); | |
function s2ab(s) { | |
var buf = new ArrayBuffer(s.length); | |
var view = new Uint8Array(buf); | |
for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; | |
return buf; | |
} | |
var app_id = location.href.replace(/.*applications\/(.*?)\/.*/, "$1"); | |
var session = JSON.parse(getCookie("session")).sessionToken; | |
let baseUrl = `https://console.mb.api.cloud.nifty.com/2013-09-01/applications/${app_id}/push`; | |
let limit = 100; | |
Promise.all([ | |
loadScript('https://cdn.rawgit.com/SheetJS/js-xlsx/master/dist/jszip.js'), | |
loadScript('https://code.jquery.com/jquery-3.2.1.min.js'), | |
loadScript('https://cdn.rawgit.com/SheetJS/js-xlsx/master/dist/xlsx.js'), | |
loadScript('https://cdn.rawgit.com/eligrey/Blob.js/master/Blob.js'), | |
loadScript('https://cdn.rawgit.com/eligrey/FileSaver.js/master/FileSaver.min.js') | |
]) | |
.then(function() { | |
return $.ajax({ | |
url: `${baseUrl}?limit=${limit}`, | |
headers: {"X-NCMB-Devs-Session-Token": session}, | |
type: "GET" | |
}) | |
}) | |
.then((pushes) => { | |
return getReport(pushes.results); | |
}) | |
.then((pushes) => { | |
let data = [ | |
['objectId', 'deliveryTime', 'target', 'title', 'message', 'deliveryNumber', 'openNumber'] | |
]; | |
for (let i = 0; i < pushes.length; i += 1) { | |
let push = pushes[i]; | |
data.push([ | |
push.objectId, | |
push.deliveryTime.iso, | |
push.target.join(','), | |
push.title, | |
push.message, | |
push.openNumber.openStatus.pushDeliveryNumber, | |
push.openNumber.openStatus.pushOpenNumber | |
]); | |
} | |
ws = sheet_from_array_of_arrays(data); | |
wb.SheetNames.push('Report'); | |
wb.Sheets['Report'] = ws; | |
var wbout = XLSX.write(wb, {bookType:'xlsx', bookSST:true, type: 'binary'}); | |
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "pushReport.xlsx") | |
}) | |
const getReport = (pushes) => { | |
return new Promise((res, rej) => { | |
let ary = []; | |
const loop = (index) => { | |
const push = pushes[index]; | |
if (!push) { | |
return res(pushes); | |
} | |
const date = new Date(push.deliveryTime.iso); | |
let url = `${baseUrl}/${push.objectId}/openNumber?`; | |
let where = { | |
"openDateTime": { | |
"$gte": `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}00` | |
} | |
}; | |
url += `where=${encodeURIComponent(JSON.stringify(where))}` | |
$.ajax({ | |
url: url, | |
headers: {"X-NCMB-Devs-Session-Token": session}, | |
type: "GET" | |
}) | |
.then((report) => { | |
pushes[index].openNumber = report; | |
loop(index + 1); | |
}) | |
} | |
loop(0); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment