Saturday, 11 January 2014

Using Gist

Since I've been writing posts, one of the things I've been struggling with is writing code straight into the blog. Did a little research and found others facing the same problem. A lot of people recommended Gist, so this is me just trying it out.

Since I'm primarily focusing on Grails and SQL these days, I'll test out some sql, groovy and gsp code. First off the SQL:
WITH C1 AS (
SELECT date_created AS ts, type,
ROW_NUMBER() OVER( ORDER BY date_created) AS start_ordinal
FROM dst_audit_log where type = 'LOG_IN'
UNION ALL
SELECT date_created as endtime, type, NULL
FROM dst_audit_log where type = 'LOG_OUT'
),
C2 AS (
SELECT *,
ROW_NUMBER() OVER(ORDER BY ts, type) AS start_or_end_ordinal
FROM C1
)
SELECT MAX(2 * start_ordinal - start_or_end_ordinal) AS peakSessionCount, ts :: DATE as date_created
FROM C2
WHERE type = 'LOG_IN'
group by ts :: DATE
view raw gistfile1.sql hosted with ❤ by GitHub
Next the GSP:
<g:each in="${report}">
<tr>
<td><g:formatDate format="MMM dd, yyyy" date="${it?.date_created}"/></td>
<td>${it.customer_count}</td>
<td>${it.customer_with_spouse_count}</td>
</tr>
</g:each>
view raw gistfile1.gsp hosted with ❤ by GitHub
Finally some Groovy:
def sumavg
if(report && report.size() > 0) {
sumavg = [:]
sumavg.customer_count = report*.customer_count.findAll { it != null }.sum() ?: 0
}
The verdict: FULLY AWESOME!