<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>netCUBED Blog &#187; postgresql</title>
	<atom:link href="http://blog.netcubed.de/tag/postgresql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.netcubed.de</link>
	<description>Just another web developer's weblog</description>
	<lastBuildDate>Mon, 29 Jun 2009 20:58:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Reports with DBIC and Postgres</title>
		<link>http://blog.netcubed.de/2009/05/reports-with-dbic-and-postgres/</link>
		<comments>http://blog.netcubed.de/2009/05/reports-with-dbic-and-postgres/#comments</comments>
		<pubDate>Sat, 23 May 2009 10:52:47 +0000</pubDate>
		<dc:creator>Moritz Onken</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[dbic]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[reports]]></category>

		<guid isPermaLink="false">http://blog.netcubed.de/?p=91</guid>
		<description><![CDATA[Currently I use plain SQL statements to create reports which, for instance, show me the number of logins of all users on a 5 minutes scale.
One of those statements looks similar to this:

INSERT INTO report_item 
    &#40;
        SELECT '2009-05-23 02:30:00+0200'::timestamp WITH time zone + &#40;s.a [...]]]></description>
			<content:encoded><![CDATA[<p>Currently I use plain SQL statements to create reports which, for instance, show me the number of logins of all users on a 5 minutes scale.</p>
<p>One of those statements looks similar to this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> report_item 
    <span style="color: #66cc66;">&#40;</span>
        <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'2009-05-23 02:30:00+0200'</span>::timestamp <span style="color: #993333; font-weight: bold;">WITH</span> time zone <span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">.</span>a <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' minute'</span><span style="color: #66cc66;">&#41;</span>::interval <span style="color: #993333; font-weight: bold;">AS</span> timestamp<span style="color: #66cc66;">,</span> 
               COUNT<span style="color: #66cc66;">&#40;</span>source<span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot;created_on&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> value
        <span style="color: #993333; font-weight: bold;">FROM</span> generate_series<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">595</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> s<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> 
          <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">&quot;created_on&quot;</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">&quot;session&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> source
          <span style="color: #993333; font-weight: bold;">ON</span> source<span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot;created_on&quot;</span> <span style="color: #66cc66;">&gt;=</span> <span style="color: #ff0000;">'2009-05-23 02:30:00+0200'</span>::timestamp <span style="color: #993333; font-weight: bold;">WITH</span> time zone <span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">.</span>a  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' minute'</span><span style="color: #66cc66;">&#41;</span>::interval 
             <span style="color: #993333; font-weight: bold;">AND</span> source<span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot;created_on&quot;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff0000;">'2009-05-23 02:30:00+0200'</span>::timestamp <span style="color: #993333; font-weight: bold;">WITH</span> time zone <span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">.</span>a  <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' minute'</span><span style="color: #66cc66;">&#41;</span>::interval
&nbsp;
        <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> a
    <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This gives me the number of logins (or new sessions) per 5 minute time slot. Now it&#8217;s pretty easy to read the data from <code>report_item</code> and create a graph or do other nice things with it.</p>
<p>I was thinking about a more generic way to do these reports so that the user can choose, what kind of report he wants.</p>
<p>So I started to create a result class <code>Report</code> with the following columns:</p>
<ul>
<li>group_by<br/>The column which is used to group rows together. This is always a timestamp column, e.g. <code>created_on</code></li>
<li>interval_type<br/>Group by minutes, hours, days, weeks etc. (defaults to minutes)</li>
<li>interval_length<br/>Group by that amount of <code>interval_type</code> (defaults to 5)</li>
<li>aggregate_by<br/>Which SQL function should be used to aggregate data in a time slot (defaults to <code>COUNT</code>)</li>
<li>aggregate<br/>Which column should be aggregated</li>
<li>query<br/>Which data should be aggregated? This accepts a DBIC result set or a plain SQL statement
</ul>
<p>After you created a report in that table you call <code>$report->create_report</code> on that row. This will execute the query above with the correct values filled in and the result will be stored in a table <code>result_item</code>, which has a foreign key <code>report_id</code> to the report.</p>
<p>It does a decent job so far and I was wondering if anyone else did something like this before?</p>
<p>If anyone is interested in the code I can make a dist and upload it to github. Since the SQL is Postgres only I don&#8217;t want to push it to the CPAN.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netcubed.de/2009/05/reports-with-dbic-and-postgres/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
