girishramnani / polly

A social poll application
MIT License
45 stars 5 forks source link

Polly

Polly is a liveview based polling application. It allows the users to

Demo

A small demo of showing the vote counts increasing in real-time

https://github.com/girishramnani/polly/assets/6551988/6219da0c-1f7b-443d-9df0-c856e4ff2d3b

Things to know

Architecture

This application from state management standpoint has 2 main components

Polly.PollsManager

PollsManager takes care of all the state related to Polls. Essentially PollsManager stores data using 3 read and write concurrency enabled ets table. They are as

Polly.VoteManager

VoteManager

VoteManager is a gen-server responsible for holding and managing votes for a user. A VoteManager process holds votes casted by a single user. Each instance is registered to the VoteRegistry for fast and convinient discovery and is created under a DynamicSupervisor.

This process design works well here because the act of "storing a vote" and checking if a user has "already cast a vote" are independent operation on a user level.

The state of the gen server is of the format map(poll_id => %Vote{}). This has been done to provide O(1) lookups.

The VoteRegistry used here is also using partitioned storage to allow for more performance.

Decisions

TradeOff

Tests and Credo

I have tried to add as many test as I can, definitely could have added more but felt the ones added were good. Also added credo with a .credo.exs (added a few more checks that I personally like).

Output of mix test --cover


27 tests, 0 failures

Randomized with seed 992261

Generating cover results ...

Percentage | Module
-----------|--------------------------
     0.00% | Polly.Constants
     0.00% | Polly.Schema.Vote
     0.00% | PollyWeb.Layouts
     0.00% | PollyWeb.PageHTML
    50.00% | PollyWeb.ErrorHTML
    63.33% | PollyWeb.UserAuth
    70.73% | PollyWeb.PollLive.Show
    71.43% | PollyWeb.PollLive.FormComponent
    80.00% | Polly.Application
    80.00% | Polly.Schema.Option
    80.00% | Polly.VoteSupervisor
    80.00% | PollyWeb.Telemetry
    81.41% | PollyWeb.CoreComponents
    83.33% | Polly.Factory
    87.50% | Polly.Polls
    90.00% | PollyWeb.Router
    92.86% | Polly.VoteManager
   100.00% | Polly
   100.00% | Polly.Mailer
   100.00% | Polly.PollsManager
   100.00% | Polly.Schema.Poll
   100.00% | PollyWeb
   100.00% | PollyWeb.ConnCase
   100.00% | PollyWeb.Endpoint
   100.00% | PollyWeb.ErrorJSON
   100.00% | PollyWeb.Gettext
   100.00% | PollyWeb.LoginLive.Index
   100.00% | PollyWeb.PollLive.Index
   100.00% | PollyWeb.PollyComponents
   100.00% | PollyWeb.UserSessionController
-----------|--------------------------
    81.52% | Total

How to start

To start your Phoenix server:

Now you can visit localhost:4000 from your browser.