<?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>coderhs &#8211; redpanthers.co</title>
	<atom:link href="/author/coderhs/feed/" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>Red Panthers - Experts in Ruby on Rails, System Design and Vue.js</description>
	<lastBuildDate>Mon, 29 Apr 2019 11:28:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.2.7</generator>
	<item>
		<title>Red Panthers community report card, 2017</title>
		<link>/red-panthers-community-report-card-2017/</link>
				<pubDate>Mon, 29 Apr 2019 11:28:12 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=4266</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[2017 has been a wonderful year for us. We have been able to further contribute]]&gt;		</p>
]]></content:encoded>
										</item>
		<item>
		<title>Automate code formatting in your rails project</title>
		<link>/automate-code-formatting-in-your-rails-project/</link>
				<pubDate>Mon, 29 Apr 2019 11:28:11 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[Marketing]]></category>

		<guid isPermaLink="false">http://redpanthers.co/?p=16235</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[We give a lot of importance to how the code looks because we feel programming should be like an art. Anyone who sees the code in future should be inspired to work in it and not feel threatened so much as to run away from it. Code styling and writing test go hand in hand to improve the hygiene of your application.
In some of our projects, we use pronto (A flash talk by Tony one of our developer during Deccan Ruby Conf 2017 - <a href="https://www.youtube.com/watch?v=C759shnD97k)">https://www.youtube.com/watch?v=C759shnD97k)</a>. The problem here is that we are all still human and we make mistakes, accidentally breaking the standard here or there. So we decided to automate this and finally have a devised a standard way to automate code formatting in our rails project. We will be automating both our ruby code and our JS code, since the marriage between web packer and rails it&#8217;s now inevitable to keep JS out of our workflow. The idea devised is to run the auto code check right after we commit the code, in case if there are any issues that can’t be auto-corrected by the system the system should stop the commit. Also, it&#8217;s important that the auto-correction runs only on the diff so that we don’t end up confusing the reviewers.
&nbsp;
For a legacy project, I suggest doing `bundle exec rubocop –a` and fix the errors before adding this hook.
&nbsp;


<h2>How to implement</h2>


To implement this we are using husky (<a href="https://github.com/typicode/husky)">https://github.com/typicode/husky)</a> a really nice JS library that helps us build git hooks easily.


<pre class="lang:sh decode:true">yarn add --dev husky</pre>


&nbsp;
The second JS library we are going to add is `lint-staged` which limits our formatters to only run on the staged git files (ie. The changes).


<pre class="lang:sh decode:true">yarn add --dev lint-staged
</pre>


Once these two are installed we can set our system to autorun rubocop by adding the following lines to the package.json.


<pre class="lang:js decode:true">"scripts": {
    "precommit": "lint-staged"
  },
  "lint-staged": {
   "{app,spec,lib}/**/*.rb": [
      "bundle exec rubocop -a",
      "git add"
    ]
  }</pre>


&nbsp;
the above configuration would ensure that our formatters run only inside the app or spec or lib folders. Also<strong> please note that since we have the </strong>pre commit<strong> hook, this code </strong>is executed<strong> after you typed `git commit –m`</strong> but it will get executed before the actual commit happening.
<strong><sub> </sub></strong>


<h2><strong><sub>Auto Formatting Javascript</sub></strong></h2>


<a href="https://prettier.io/">https://prettier.io/</a> is an opinionated code formatter that supports many languages, in fact it’s also being extended to ruby. We will be using it to format our javascript code, which internally we are using es6. You can read more about it from its project page.


<pre class="lang:sh decode:true">yarn add --dev prettier</pre>


Following which we need to add to the script section of our code:


<pre class="lang:js decode:true">   "app/**/*.{js,es6,jsx,scss,css}": [
      "./node_modules/prettier/bin-prettier.js --trailing-comma es5 --write",
      "git add"
    ]</pre>


The final package.json inside a fresh project would look like this:


<pre class="lang:js decode:true">{
  "name": "starter",
  "private": true,
  "dependencies": {
    "@rails/webpacker": "^3.3.0"
  },
  "scripts": {
    "precommit": "lint-staged"
  },
  "lint-staged": {
    "app/**/*.{js,es6,jsx,scss,css}": [
      "./node_modules/prettier/bin-prettier.js --trailing-comma es5 --write",
      "git add"
    ],
    "{app,test}/**/*.rb": [
      "bundle exec rubocop -a",
      "git add"
    ]
  },
  "devDependencies": {
    "husky": "^0.13.4",
    "lint-staged": "^3.6.0",
    "prettier": "^1.4.2",
    "webpack-dev-server": "^2.11.1"
  }
}
</pre>


&nbsp;]]&gt;		</p>
]]></content:encoded>
										</item>
		<item>
		<title>Crystal tuples: the immutable data structure of crystal</title>
		<link>/tuples-immutable-data-crystal/</link>
				<pubDate>Wed, 06 Dec 2017 13:47:11 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[crystal]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[crystal 101]]></category>
		<category><![CDATA[fp]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[immutable datastructure]]></category>
		<category><![CDATA[newbie]]></category>
		<category><![CDATA[tuple]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=3985</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[<strong>Tuple</strong> is a data structure which has immutable elements and is of a fixed size. Its similar to an array, but unlike arrays in crystal and ruby which allows adding more values over time a tuple is of fixed and cannot change. (Disclaimer: This article is meant for Ruby developer and explaining what a tuple is to a ruby developer).
In crystal we have two types of tuple
1) Tuple <code>{1, "hello", :world}</code>
2) NamedTuple <code>{x: 1, y:2, z: 5}</code>
They are immutable, which means if you try changing the value of an element in a tuple you will get an exception. Since crystal programs are compiled before execution you will get to see these errors while you compile the program itself.
example:


<pre class="lang:ruby decode:true">x = {1, 2, 3}
# to get the value use
x[0]
</pre>


If you try assigning a value to it, like so


<pre class="lang:ruby decode:true">x[0] = 10</pre>


You will get an exception like bellow.


<pre class="lang:ruby decode:true">undefined method '[]=' for Tuple(Int32, Int32)</pre>


In crsytal, tuples are the preferred way to return a multiple results from a method.
<strong>For example</strong> inside the crystal core we have a method to get the minimum and maximum of an array.


<pre class="lang:ruby decode:true ">(1..100).minmax</pre>


the result would be <strong>{1, 100}</strong>


<blockquote>Note: Since we just mentioned <strong>minmax</strong>, have a look at <strong>minmax_by</strong> method as well.<b> </b>It would let you apply a block of code over your range and then return the minimum and maximum based on the returned collection.


<pre class="lang:ruby decode:true">["1234", "12", "123"].minmax_by { |i| i.size }
# =&gt; {"12", "1234"}</pre>


</blockquote>


Advantage of using tuple to return results instead of something like hash, is that we can be sure that our result cannot be altered accidentally. (since the data structure is immutable) ?
<strong>You can build a tuple from an array by using the <code>.from</code> method</strong>


<pre class="lang:ruby decode:true">Tuple(Int32, Int32).from([1, 2])</pre>


As a developer, the place where we use tuple the most in crystal are with splats(symbol: *). Passing arguments to method using splat and double splat operator is something we use widely in ruby keep our code small and readable. So if you wish to do the same in crystal you need to make a tuple not a hash or array.
If you use splat on an array directly like <code>test(*[1,2])</code> it would return an error


<pre class="lang:sh decode:true ">argument to splat must be a tuple, not Array(Int32)</pre>


So to achieve the same effect as a splat with array in crystal we would need to do <code>test(*{1,2})</code>


<h2>Named Tuple</h2>


Named Tuple are everything as above, but with a name for each element. Named Tuple looks like <code>{x: 1, y:2}</code> it gives more meaning to our tuple. Like the above you can access the values but not change them.


<pre class="lang:default decode:true">data = {x: 1, y: 2}
# to get the value
data[:x]
# raises errors when we try to change it
data[:x] = 1</pre>


Double splats are meant for Named Tuple where in we can pass in the values for a particular argument using named tuple and double splat.


<pre class="lang:default decode:true ">def print_date(year = nil, month = nil, day = nil)
  puts "#{year}/#{month}/#{day}"
end
birth_day = { year: 1990, month: 4, day: 3}
print_date(**birth_day)
card_expiry = { year: 2020, month: 1}
print_date(**card_expiry)</pre>


You can build a NamedTuple from a hash.


<pre class="lang:ruby decode:true">NamedTuple(name: String, val: String).from({"name" =&gt; "number", "val" =&gt; "Harisankar P S"}</pre>


<strong>Note:</strong> Crystal has a nifty feature called Union types (a variable can store data of multiple data types), so if it happen to pass such a variable to a named tuple/tuple, it will still check for the exact type that we want if the data is not in that variable then an exception would be raised
Example


<pre class="lang:default decode:true ">k = 42.as(Int32 | String)
NamedTuple(name: String, val: String).from({"name" =&gt; "number", "val" =&gt; K}
</pre>


Exception:


<pre class="lang:sh decode:true ">cast from Int32 to String failed, at /usr/local/Cellar/crystal-lang/0.23.1_1/src/class.cr:41:5:41 (TypeCastError)
0x10e8f1085: *CallStack::unwind:Array(Pointer(Void)) at ??
0x10e8f1021: *CallStack#initialize:Array(Pointer(Void)) at ??
0x10e8f0ff8: *CallStack::new:CallStack at ??
0x10e8ec295: *raise&lt;TypeCastError&gt;:NoReturn at ??
0x10e90feb8: *String@Object::cast&lt;(Int32 | String)&gt;:String at ??
0x10e95faa3: *NamedTuple(name: String:Class, val: String:Class)@NamedTuple(T)#from&lt;Hash(String, Int32 | String)&gt;:NamedTuple(name: String, val: String) at ??
0x10e95f787: *NamedTuple(name: String, val: String)@NamedTuple(T)::from&lt;Hash(String, Int32 | String)&gt;:NamedTuple(name: String, val: String) at ??
0x10e8ef8a6: *__icr_exec__:NamedTuple(name: String, val: String) at ??
0x10e8db130: __crystal_main at ??
0x10e8ee578: main at ??</pre>




<h2><strong>Extra Note:</strong></h2>


<span style="font-size: 16px;">If you put a splat before method argument and pass in arguments, they will be converted to a tuple</span>


<pre class="lang:ruby decode:true ">def a_method(*data)
  puts data
end
a_method(1,2,3)
#=&gt; {1,2,3}</pre>


If you put a double splat before method argument and pass in data as keyword argument it gets converted to a NamedTuple


<pre class="lang:default decode:true ">def a_method(**data)
  puts data
end
a_method(x: 1, y: 10)
#=&gt; {x: 1, y: 10}</pre>


&nbsp;


<h3>To summarize:</h3>




<ul>
 	

<li>Tuples are immutable data structure</li>


 	

<li>Regular tuple is like a frozen array</li>


 	

<li>You can use splat only with a tuple</li>


 	

<li>NamedTuple is like a frozen hash</li>


 	

<li>Double splat can only be used with NamedTuple</li>


</ul>

]]&gt;		</p>
]]></content:encoded>
										</item>
		<item>
		<title>Using mina to deploy a particular commit</title>
		<link>/using-mina-deploy-particular-commit/</link>
				<pubDate>Sun, 10 Sep 2017 06:24:45 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[DevOps]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=3522</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[Mina is our tool of choice when it comes to automating our deployment. It might not be the best tool out there, but it is enough to get us started. Single server or multi server, we love Mina.
In most of uses cases if something fail in production we just need to rollback to one or two version before to it. But recently we had a case in which we had to revert to version order than the previous 5 version (5 is default no of versions stored). The quick thing that we did at that point is copy of master called temp-master branch and changed master to the version that we wanted and had the new master deployed.
We didn't like that solution much, so we decided to look for a cleaner solution. Looking at the source code of the git task inside mina, we found that mina had an option to deploy a particular git commit. The config was <code>set :commit, HASH</code> (Thee cheers to the contributors to mina for having that).
Now that made our life easier for the future. We just added the following line to our deploy.rb
<code>set :commit, ENV['COMMIT']</code>
so now we can deploy a particular hash by passing that hash as an environment variable.
<code>mina deploy COMMIT=thegitcommithash</code>
note: If no hash is passed (eg: <code>mina deploy</code>), it would just deploy the master.
&nbsp;]]&gt;		</p>
]]></content:encoded>
										</item>
		<item>
		<title>securing and showing a redis server to the world</title>
		<link>/securing-and-showing-a-redis-server-to-the-world/</link>
				<pubDate>Fri, 25 Aug 2017 13:05:34 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=3193</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[[caption id="" align="aligncenter" width="1280"]<img class="size-full" src="https://upload.wikimedia.org/wikipedia/en/thumb/6/6b/Redis_Logo.svg/1280px-Redis_Logo.svg.png" alt="redis logo" width="1280" height="428" /> Redis[/caption]
Redis is an in memory based key value data structure server. They keywords here are in-memory (RAM) and Key &#8211; Value (Hash). RAM being the easily accessible memory location for your CPU, and hash being the most accissable data-structure a combination of both makes it lethal. It was developed by an Italian developer named <a href="http://antirez.com/">Salvatore Sanfilippo</a>, in 2009.
Such a system is useful in multiple scenarios. Especially in cases where key is readily available, constant and not changing. Some example use cases being. In a rails projects Redis is used at multiple places for example:


<ul>
 	

<li>Backend of action cable which is used to provide notifications (pub/sub)</li>


 	

<li>Queue system used by background workers (Sidekiq, Resque)</li>


 	

<li>Web Caching</li>


 	

<li>Session Store &#8211; sharing user session across all the load balanced servers</li>


 	

<li>Fast accessible meta data catalog for your inventory or tool</li>


 	

<li>Counting &#8211; Redis offers a fast method to increment and decrement value. Being an in-memory storage does add</li>


</ul>


&nbsp;
Coming back to the topic of this article, most self hosted rails applications starts off by installing redis in the same server as your rails application. In fact sidekiq, crontab (for scheduling tasks) would all be on the same server. Over time as your projects grows with users you would see that your application is slowing dow, CPU spiking to 100%, etc. We can start fixing that by moving sidekiq and the cron to another server (like how its done in heroku). But the first thing we need to make is the redis on our main server open to the second server and secure it.
The steps to be followed are:
<strong>To open up your redis to the world.</strong>
Go to <code>/etc/redis/redis.conf</code> find the following line <code>bind 127.0.0.1</code>, by removing that line you will make your redis application accessable to the world through the default port <code>6379</code> . You can also set for the world by placing the ip as <code>0.0.0.0</code>. Lets remove it for now, as we will be securing it in our firewall (since my example is the AWS i would be doing that in the security group). If you don&#8217;t have a security group then add the ip of the servers connecting to it after a space <code>bind 127.0.0.1 192.168.1.1</code>
<strong>Disable Protective mod</strong>
Since 3.2.0 redis comes with a default <code>protective-mode yes</code> that make it accepts query and request only from loopback (the machine itself). It was enables so as most people would install redis and have it exposed to the world (redis has limited security in itself, its expected for the system admin to take care of the necessary arrangements). So we need to turn it off. Find the line saying <code>protective-mode</code> and the set it to <code>no</code>
<strong>Set a password for your instance</strong>
As a final step we can set a password to access redis. To set that find the like that says <code>requirepass</code> and after that word provide your password : <code>requirepass iwouldbeafooltosharemyrealpassword</code>
&nbsp;
<strong>SET the IP of your secondary server in your security group</strong>
For all those who uses AWS, like us. You need to open up this particular port in your machine, but do not make it accessable to all the IPs. Redis doesn&#8217;t have user permissions so basically if anyone were to get access they could just do a FLUSHALL and you will loose all your data.
So open the port <code>6379</code> in your security group or IP tables (if you are in just a linux server) to the IP of the clients you want to connect. You can read about security group here -&gt; <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html">http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html</a>
With the above 4 steps you would have opened up your existing redis installed in your main server to the secondary new server. You can further add more machines by adding their IP to security group/ IP Tables.
&nbsp;]]&gt;		</p>
]]></content:encoded>
										</item>
		<item>
		<title>[Tip] Mina: Find last git commit released</title>
		<link>/tip-mina-find-last-git-commit-released/</link>
				<pubDate>Wed, 24 May 2017 07:43:31 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[DevOps]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=2499</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[A project that we were handling hadn't had a deployment in some time. So we were confused on what all commits were going to go to production ( bad karma for us for not keeping a release or change log). We use <a href="https://github.com/mina-deploy/mina">mina</a> for all our deployment as we found it to be faster that Capistrano. So we were sure that there would be something on our server to help us settle this dilemma. We finally solved this by going through the various folders and files.
Edit the file inside the file with your branch name located at <strong>/path/to/project/</strong>scm<strong>/refs/heads</strong>. Like for example if you have your project in <strong>/var/www</strong> folder and you are deploying master then you should edit the file called master found at the following location.`<strong>/var/www/project/</strong>scm<strong>/refs/heads/master</strong>` that file would have only one line and that&#8217;s the last git commit released.
&nbsp;]]&gt;		</p>
]]></content:encoded>
										</item>
		<item>
		<title>Materialized Views: Caching database query</title>
		<link>/materialized-views-caching-database-query/</link>
				<comments>/materialized-views-caching-database-query/#comments</comments>
				<pubDate>Wed, 30 Nov 2016 09:22:46 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Materialized]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Views]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=728</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[As a part of our database optimization series, this article is related to creating materializing views in the database.
[caption id="" align="alignnone" width="1142"]<img class="size-full" src="https://i-msdn.sec.s-msft.com/dynimg/IC709528.png" alt="Materialzied View" width="1142" height="487" /> Materialized View Purpose[/caption]
Before starting with a materialized view, let&#8217;s talk about database views.


<h2>What is a database view?</h2>


A database view is a stored set of queries, which gets executed whenever a view is called or evoked. Unlike the regular tables, the view doesn&#8217;t occupy any physical space in your hard disk but its schema and everything is stored in the system memory. It helps abstract away the underlying tables and makes it easier to work with.
They can also be called as pseudo tables.
Quoted from the PostgerSQL documentation.


<blockquote>Making liberal use of views is a key aspect of good SQL database design. Views allow you to encapsulate the details of the structure of your tables, which might change as your application evolves, behind consistent interfaces.
&nbsp;</blockquote>




<pre class="lang:pgsql decode:true">CREATE VIEW company_manager AS
SELECT id, name, email
FROM  companies
WHERE role='manager';</pre>


Now to access all the managers


<pre class="lang:pgsql decode:true">SELECT * FROM company_managers;</pre>


&nbsp;
Making more use of views makes your DB design much cleaner, but here we are talking more about using Materializing views. As that would lead to the more direct performance boost.


<h2>So what is a Materialized view?</h2>


The materializing view was first introduced in oracle. But now you can find it in most database systems like PostgreSQL, MicrosoftSQL server, IBM DB2, Sybase. MySQL doesn&#8217;t have native support for it, but you can find extensions for it which would help achieve this
<strong>Materialized view </strong>is also called <strong>Matview. </strong>It is a form of database view that also has the result of the query as well. Which speeds up the results because now, you don&#8217;t have to run the query to get the results, as its already there, calculated. Of course, there are cases where we can&#8217;t have this, where we need more real-time information. But while generating reports you create a matview and then later refresh the matview to get the updated reports.
Things to note about matview are:


<ol>
 	

<li>It&#8217;s read-only (pseudo-table) so you can&#8217;t update it.</li>


 	

<li>You need to refresh the table to get the latest data.</li>


 	

<li>While refreshing, it would block other connections to access the existing data from the material view, so you need to make the refresh run concurrently</li>


</ol>




<h2>So why use Materialized views in Rails?</h2>




<ul>
 	

<li>Capture commonly used joins &amp; filters.</li>


 	

<li>Push data intensive processing from Ruby to Database.</li>


 	

<li>Allow fast and live filtering of complex associations or calculation fields.</li>


</ul>




<h2>How do you use it in Rails?</h2>


Well thanks to active record, it&#8217;s quite easy to use this in our code. But we need a bit of SQL as well.
First, we add the migration to create the materialized views.


<pre class="lang:sh decode:true">bundle exec rails g migration create_all_time_sales_mat_view</pre>


In the migration file, we add the SQL


<pre class="lang:ruby decode:true">class CreateAllTimesSalesMatView &lt; ActiveRecord::Migration
  def up
    execute &lt;&lt;-SQL
      CREATE MATERIALIZED VIEW all_time_sales_mat_view AS
        SELECT sum(amount) as total_sale,
        DATE_TRUNC('day', invoice_adte) as date_of_sale
      FROM sales
      GROUP BY DATE_TRUNC('day', invoice_adte)
    SQL
  end
  def down
    execute("DROP MATERIALIZED VIEW IF EXISTS all_time_sales_view")
  end
end</pre>


Once the view is ready , we can create the model for this at <code>app/models/all_time_sales_mat_view.rb</code>


<pre class="lang:default decode:true">class AllTimeSalesMatView &lt; ActiveRecord::Base
  self.table_name = 'all_time_sales_mat_view'
  def readonly?
    true
  end
  def self.refresh
    ActiveRecord::Base.connection.execute('REFRESH MATERIALIZED VIEW CONCURRENTLY all_time_sales_mat_view')
  end
end</pre>


Now we select and query the model as usual.


<pre class="lang:ruby decode:true">AllTimeSalesMatView.select(:date_of_sale)
AllTimeSalesMatView.sum(:total_sale)</pre>


We can&#8217;t do any <code>create</code>, <code>save</code> or <code>update</code>. As its a read-only table.
Creating a table with a total of million sales record for every date in the last year, gave us the following speed improvement.


<pre class="lang:sh decode:true ">Regular
       user     system      total        real
     (976.4ms)  0.020000   0.000000   0.020000 (  0.990258)
MatiView
     (2.3ms)    0.000000   0.010000   0.010000 (  0.012010)</pre>


Over 10 times speed improvement, yay!!


<h2>Summarize</h2>




<h3>Good Points</h3>




<ul>
 	

<li>Faster to fetch data.</li>


 	

<li>Capture commonly used joins &amp; filters.</li>


 	

<li>Push data intensive processing from Ruby to Database.</li>


 	

<li>Allow fast and live filtering of complex associations or calculation .fields.</li>


</ul>




<h3>Pain Points</h3>




<ul>
 	

<li>To alter table we need to write SQL</li>


 	

<li>We will be using more RAM and Storage</li>


 	

<li>Requires Postgres 9.3 for MatView</li>


 	

<li>Requires Postgres 9.4 to refresh concurrently</li>


 	

<li>Can&#8217;t have Live data


<ul>
 	

<li>You can fix this by creating your own MatViewTable and updating it with the latest information</li>


</ul>


</li>


</ul>




<h2>References</h2>




<ul>
 	

<li>https://www.postgresql.org/docs/9.3/static/rules-materializedviews.html</li>


 	

<li>http://en.wikipedia.org/wiki/Materialized_view</li>


 	

<li>http://dev.mysql.com/doc/refman/5.7/en/create-view.html</li>


 	

<li>https://blog.pivotal.io/labs/labs/database-views-performance-rails</li>


 	

<li>https://www.sitepoint.com/speed-up-with-materialized-views-on-postgresql-and-rails/</li>


</ul>

]]&gt;		</p>
]]></content:encoded>
							<wfw:commentRss>/materialized-views-caching-database-query/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
							</item>
		<item>
		<title>Introduction to generating JSON using PostgreSQL</title>
		<link>/create-json-response-using-postgresql-instead-rails/</link>
				<comments>/create-json-response-using-postgresql-instead-rails/#comments</comments>
				<pubDate>Thu, 24 Nov 2016 04:38:33 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[benchmarking]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Faster]]></category>
		<category><![CDATA[generation]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[standards]]></category>
		<category><![CDATA[web api]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=663</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[

<h2>Introduction</h2>


One of the major requirements for any online business is to have a backend that either provides or can be extended to provide an API response. Building  websites with static HTML and simple jquery ajax is coming to an end. In this era, Javascript frameworks rules the market. Hence, it is a good decision for the database to support JSON, as JSON is becoming the glue that connects the frontend and backend.
Rails have an inbuilt support for generating JSON, as it&#8217;s our swiss army knife of web development, and encourages the REST URL structure . And its a good choice for building API. It is good enough to a particular point of growth. Very soon you will reach bottlenecks, where you have more requests than you can handle and you have to either spawn up more servers or use some concurrent languages like elixir, go, etc. Before we go to that scale and burn down the existing codebase, we can use database to generate JSON responses for us, which is 10 times faster in generating JSON than Rails (though more verbose).
Since PostgreSQL 9.2, the database has taken a major leap in supporting JSON. The support that PostgreSQL provides can be divided into two


<ul>
 	

<li>Storing data in JSON and JSONB format</li>


 	

<li>Generating JSON results from the query itself</li>


</ul>


In this article, we will talk about generating JSON(an introduction) from the query itself.


<h2>Getting Started</h2>


One of the advantages of using a database to generate JSON is that I have found it fast while generating smaller JSON but much more faster in generating complex JSON. (Note: The speed is in comparison with rails not with respect to the database itself)


<h3><strong>How to generate JSON</strong></h3>




<div></div>




<div>Simplest way to do that is row_to_json()
For example: Query to return user with id 1 as JSON</div>




<div>


<pre class="lang:pgsql decode:true">select row_to_json(users) from users where id = 1;
</pre>


Result:


<pre class="lang:js decode:true">{"id":1,"email":"hsps@redpanthers.co","encrypted_password":"iwillbecrazytodisplaythat",
"reset_password_token":null,"reset_password_sent_at":null,
"remember_created_at":"2016-11-06T08:39:47.983222",
"sign_in_count":11,"current_sign_in_at":"2016-11-18T11:47:01.946542",
"last_sign_in_at":"2016-11-16T20:46:31.110257",
"current_sign_in_ip":"::1","last_sign_in_ip":"::1",
"created_at":"2016-11-06T08:38:46.193417",
"updated_at":"2016-11-18T11:47:01.956152",
"first_name":"Super","last_name":"Admin","role":3}</pre>


if you want to send only some specific fields
</div>




<pre class="lang:pgsql decode:true">select row_to_json(results)
from (
  select id, email from users
) as results</pre>


Result


<pre class="lang:js decode:true">{"id":1,"email":"hsps@redpanthers.co"}</pre>


Now let&#8217;s see how to generate more complex JSON with sub JSON, and arrays.


<pre class="lang:pgsql decode:true">select row_to_json(result)
from (
  select id, email,
    (
      select array_to_json(array_agg(row_to_json(user_projects)))
      from (
        select id, name
        from projects
        where user_id=users.id
        order by created_at asc
      ) user_projects
    ) as projects
  from users
  where id = 1
) result</pre>


This would return the JSON response


<pre class="lang:default decode:true ">{"id":1,"email":"hsps@redpanthers.co", "project":["id": 3, "name": "CSnipp"]}</pre>


The issue with the above code is that it is more verbose (has more text)  when compared to a ruby code. We need to make sure that while we do a bit of sacrifice there, is worthwhile. So while working with API&#8217;s  use it only where you see a delay in JSON generation.
Similarly ,to the <strong>&#8216;array_agg&#8217;</strong> method that we used above to aggregate values to an array then to JSON, we aggregate them as JSON using <code>json_agg</code>.


<pre class="lang:pgsql decode:true">array_to_json(array_agg(row_to_json(user_projects)))</pre>


can be shortened to


<pre class="lang:pgsql decode:true">json_agg(user_projects)</pre>


&nbsp;
Since the above method of array generation can be tedious, in PostgreSQL 9.4, they have introduced a new method called <code>json_build_object</code>. Simple usage of the function can be as below


<pre class="lang:pgsql decode:true ">json_build_object('foo',1,'bar',2)</pre>


which will output


<pre class="lang:js decode:true ">{"foo": 1, "bar": 2}</pre>


Also, we can use it to build complex JSON tree by creating functions within the PostgreSQL database. Of course, as we do that, we are moving more and more logic of the code into the DB and we would need to run migrations every time when we want to update a function. So as I said before, we are sacrificing our convenience here .So we should only use this, as the complexity of our JSON generation increases.
I will be covering how to write PostgreSQL functions to help generate more complex JSON structure easier in the second part of this particle.


<h2>References</h2>


https://www.postgresql.org/docs/current/static/functions-json.html
http://bytefish.de/blog/postgresql_json/]]&gt;		</p>
]]></content:encoded>
							<wfw:commentRss>/create-json-response-using-postgresql-instead-rails/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
							</item>
		<item>
		<title>New binding.irb introduced in ruby 2.4</title>
		<link>/new-binding-irb-introduced-ruby-2-4/</link>
				<pubDate>Tue, 22 Nov 2016 05:23:21 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[Ruby 2.4]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[irb]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[REPL]]></category>
		<category><![CDATA[session]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=636</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[Ruby 2.4 will have the feature to introduce a REPL session, using IRB, in between your code execution for better debugging. <a href="http://ruby-doc.org/stdlib-2.3.0/libdoc/irb/rdoc/IRB.html">IRB</a>, which stands for Interactive Ruby, is the standard REPL which is bundled along with ruby. <a href="http://pryrepl.org/">Pry</a> is a popular alternative for IRB, which has many developer-friendly features like tab compilation and syntax highlighting. One of the most heavily used features of pry is the ability to introduce a REPL session in between your code execution for better debugging. Instead of using <code>p</code> or <code>puts</code> to print the result and various variables, this helps us try out various codes and fixes in between the code to find the right solution.
[caption id="" align="aligncenter" width="625"]<img src="http://www.alanmacdougall.com/post_content/2012-06-08-interactive-debugging-with-pry/pry_debugging_002.png" width="625" height="377" /> binding.pry being used.[/caption]
&nbsp;
To use <code>binding.irb</code> in your code, you need to require the IRB library to your code and call <code>binding.irb</code> where you want to introduce the REPL.


<pre class="lang:ruby decode:true ">require 'irb'
# all the codes before the binding
binding.irb
# all the codes after the binding</pre>


and you will see a REPL like below.
<a href="https://redpanthers.co/wp-content/uploads/2016/11/Screen-Shot-2016-11-22-at-10.47.27-AM.png"><img class="alignnone size-full wp-image-637" src="https://redpanthers.co/wp-content/uploads/2016/11/Screen-Shot-2016-11-22-at-10.47.27-AM.png" alt="screen-shot-2016-11-22-at-10-47-27-am" width="715" height="59" /></a>
&nbsp;]]&gt;		</p>
]]></content:encoded>
										</item>
		<item>
		<title>Deploying Sidekiq to Ubuntu 16.04</title>
		<link>/deploying-sidekiq-ubuntu-16-04/</link>
				<pubDate>Mon, 07 Nov 2016 13:56:02 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[DevOps]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[deployment]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[RVM]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=605</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[Sidekiq is a popular background processing tool available in Ruby. It's fast, robust and reliable compared to other solutions out there. Sidekiq run as a process outside of rails (but including the rails environment), which means it doesn't start when you start your rails application. During development, we start sidekiq in another terminal (or tab) using the command


<pre class="lang:ruby decode:true">bundle exec sidekiq</pre>


to run it as a daemon we use the -d option


<pre class="lang:ruby decode:true">bundle exec sidekiq -d</pre>


To kill a sidekiq daemon, you need to do  the PID of the sidekiq process. When a sidekiq process starts it enters its pid to file which can be found at


<pre class="lang:sh decode:true">/path/to/rails/app/tmp/pid/sidekiq.pid</pre>


So the command to stop it would be


<pre class="lang:sh decode:true">pkill -F /path/to/rails/app/tmp/pid/sidekiq.pid</pre>


But making it a daemon is not a good idea, as there is no code from sidekiq to restart the process when it fails or exits on its own. So in ubuntu, which is our favorite OS for the production server, we make sidekiq a <strong>systemd</strong> process.
Before we make it into a service and if you are using rvm you need to create a wrapper for systemd so that ruby with all the gems are available for it.


<pre class="lang:sh decode:true code bash">rvm wrapper 2.3.1 systemd bundle</pre>


Once that is done you need to create a <strong>sidekiq.service</strong> file under your &#8216;<strong>/etc/systemd/system/</strong>&#8216;. You can find the configuration file with default settings <a href="https://github.com/redpanthers/config-files/blob/master/sidekiq/sidekiq.service">here</a>.
&nbsp;
So that was a bit  from me on Sidekiq.Hope it helps you some way in further understanding sidekiq.]]&gt;		</p>
]]></content:encoded>
										</item>
	</channel>
</rss>
