const CGI = api.cgi
const F = CGI.getFossilInstance()
const out = api.io.output

const util = import(Fossil.file.dirPart(__FILE)+'reports-util.fossi1ish')
assert 'object' === typename util
const eventType = util.initReportsView(false)
CGI.handleShowSource(__FILE) && return

// has no effect here: util.generateYearSelection()

$out <<<EOF
    <table class='page-reports-table-events' border='0' cellpadding='2' cellspacing='0'>
    <thead><tr>
    <th>User</th>
    <th>Events</th>
    <th width='90%%'><!-- relative commits graph --></th>
    </tr></thead><tbody>
EOF


var stmt = F.db.prepare({
    SELECT user, COUNT(*) AS eventCount
    FROM v_reports
    GROUP BY user ORDER BY eventCount DESC
    })

var maxEvents = 0, nCount
while(stmt.step()){
    nCount = stmt.get(1)
    (nCount>maxEvents) && (maxEvents = nCount)
}
assert maxEvents>0
stmt.reset()

const userLink = proc(user){
    user || return ''
    const appPath = CGI.getAppUrlPath('reports/by-week')
    return "<a href='".concat(
        appPath, '?user=', CGI.urlencode(user),
        "'>", user, {</a>}
    )
}

var user, rowNumber = 0, nSize
while(stmt.step()){
    rowNumber += 1
    user = stmt.get(0)
    nCount = stmt.get(1)
    nCount || continue
    nSize = nCount ? (100 * nCount / maxEvents) : 0
    nSize || (nSize=1)
    $out {<tr class=} ((rowNumber%2) ? {'odd'} : {'even'}) {>}
    $out {<td>} userLink(user) {</td>}
    $out {<td>} nCount {</td>}
    $out {<td><div class='page-reports-graph-line' style=}
    $out {'width:%1$d%%;'}.applyFormat(nSize) {>&nbsp;</div>}
    $out {</td></tr>}
}
stmt.finalize()
$out <<<EOF
    </tbody></table>
EOF
