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

const util = api.import(Fossil.file.dirPart(__FILE)+'reports-util')
assert 'object' === typename util
util.initReportsView2()


$out {<h2>Repository activity (} util.getEventTypeLabel() ')'
if(util.user) {
    $out ' for user ' util.user 
}else{
    $out ' by user'
}
$out {</h2>}

util.generateRemoveUserLink()



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)
}
stmt.reset()
if(!maxEvents){
    $out {<div>No activity found based on the current filters.</div>}
}else{
    const userLink = proc(user){
        user || return ''
        const param = Fossil.extendProperties(util.linkProps,object{user: user})
        const appPath = CGI.getAppUrlPath('timeline')
        return CGI.html.createAnchor( appPath + {?} + CGI.objToUrlParams(param),
                                      user );
    }
    $out <<<EOF
        <table class='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 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='reports-graph-line' style=}
        $out {'width:%1$d%%;'}.applyFormat(nSize) {>&nbsp;</div>}
        $out {</td></tr>}
    }
}
stmt.finalize()
$out <<<EOF
    </tbody></table>
EOF
