Controllers.prototype.dailyStats = (function(){ const tmpl = new Template(), downloadDropdownOpts = new DropdownObj([ { title: 'Download', value: '-', placeholder: true }, { title: 'CSV', value: 'csv' }, { title: 'JSON', value: 'json' }, ], '-', 'cool', 'cool', 'cool', true); return newDailyStats; function newDailyStats(core, target, rcb){ return new DailyStats(core, target, rcb) } function DailyStats(core, target, rcb){ let events = new EventManager(), ele = getElementFromString(tmpl.main), elements = new Elements(ele, { //'index' : {}, 'fromInput': { 'selector': '.fromInput' }, 'toInput': { 'selector': '.toInput' }, 'fromDate': {}, 'toDate': {}, 'downloadAnchor': { 'target': new Dropdown(ele.querySelector('.downloadAnchor'), downloadDropdownOpts, downloadReport) } }), dataTbl = null, tables = [], data = null, reqData = { from: '', to: '', fmt: 'json', }, inited = false, closed = false; elements.fromDate = new CalendarInput(elements.fromInput, null, updateFromDate); elements.toDate = new CalendarInput(elements.toInput, null, updateToDate); rcb(); target.appendChild(ele); //new ApiManager(new APIRequest(reqData), core.userID, init); init(null, {}); this.exit = exit; function init(err, resp){ data = new APIResponse(resp); removeChild(ele, ele.querySelector('.loading')); dataTbl = ele.querySelector('#data-table'); initTable(data); inited = true; } function initTable(data) { tables.forEach((t) => t.exit()); tables = []; dataTbl.innerHTML = ''; Object.keys(data.stats).forEach((advID) => { const st = data.stats[advID] || {}, advName = st.advName; if(advName !== "") { const e = getElementFromString(`

${advName} (${advID}):

`); dataTbl.appendChild(e); } (st.stats||[]).forEach((st) => { const e = getElementFromString(tmpl.statsItems, st), tbl = NewMTable(e.querySelector('div'), getTable(st || {}), tableUpdate); dataTbl.appendChild(e); tables.push(tbl); }); }); } function downloadReport(e) { preventDefault(e); if (e === '-') return; reqData.fmt = e; reload(); } function getTable(d) { const dt = (d.campaigns||[]); d.total.campaignName = "Total:"; dt.push(d.total); return { columns : { campaignID : { "title" : "CmpID", "valueType" : "string", "sortable" : true, "width" : "100" }, campaignName : { "title" : "Campaign", "valueType" : "string", "sortable" : true, "width" : "400" }, spend : { "title" : "Spend", "valueType" : "number", "sortable" : true, "width" : "80" }, clicks : { "title" : "Clicks", "valueType" : "number", "sortable" : true, "width" : "80" }, impressions : { "title" : "imps", "valueType" : "number", "sortable" : true, "width" : "80" }, }, rows : dt, rowSelect: false, maxPerPage : null }; } function tableUpdate() {} function updateFromDate(e) { if (!e) return; reqData.from = e.year + '-' + getFormattedDate(e.monthValue) + '-' + getFormattedDate(e.date); } function updateToDate(e) { if (!e) return; reqData.to = e.year + '-' + getFormattedDate(e.monthValue) + '-' + getFormattedDate(e.date); } function getFormattedDate(v) { if (v > 9) return v; else return '0' + v; } function reload() { if (!inited) return; new ApiManager(new APIRequest(reqData), core.userID, (_, resp) => { pop('Pop!', 'Downloads', '/dashboard/downloads'); // keeping this in case go back to the old system? // data = new APIResponse(resp); // initTable(data); }); } function exit(){ if(closed){ return } elements.exit(); removeChild(target, ele); ele.innerHTML = ''; dataTbl = null; closed = true } } function APIRequest(req){ let start = req.from, end = req.to, fmt = req.fmt; if (!start || !end) { start = end = formatISODate(new Date(), true); } const ag = isAgency(); this.stats = { source: `dailyStats${ag ? 'ByAgency' : ''}/${core.userID}/${start}/${end}?fmt=${fmt}`, noID: true }; } function APIResponse(data){ if (isAgency) { this.stats = data.stats || {}; } else { this.stats = {}; this.stats[core.userID] = data.stats || {}; } } function Template(){ var header = '

Daily Stats

', loading = '

One moment please...

', fromDate = 'from:
', toDate = 'to:
', downloadReport = '
Report Range:

' + fromDate + toDate + '

'; this.main = '
' + header + downloadReport + loading + '
'; this.statsItems = '

Date: {{date}}




'; this.empty = '

Oh! It appears that your billing stats is empty.

'; function getstatsItem(){ return `

Date: {{ date}}

Campaign: {{ name }} [{{ cid }}]

Spend: {{ usd }}

Clicks: {{ clicks }}

Impressions: {{ clicks }}

`; } } function comparestatsItems(a, b){ if(a.ts < b.ts){ return -1; } if(a.ts > b.ts){ return 1; } return 0; } function getLastWeek() { var d = new Date(); d.setHours(-(24 * 7) + d.getHours()); return d; } function isAgency() { return core.userType === 'agency' } })();