Giter Site home page Giter Site logo

accessd / models_stats Goto Github PK

View Code? Open in Web Editor NEW
42.0 7.0 2.0 778 KB

Charts for your rails models with MetricsGraphics.js and NVD3

License: MIT License

Ruby 81.43% CoffeeScript 12.12% JavaScript 2.53% CSS 3.92%
rails activerecord statistics collect-statistics metricsgraphics graphics nvd3 ruby

models_stats's Introduction

ModelsStats

Graphics for your rails models. It may show count(or average, or sum, or another sql agregate function) of models for each day with grouping, conditions.

For graphics it uses for your choice MetricsGraphics.js or/and NVD3.

Dependencies: Redis for store statistics. D3, jQuery, Bootstrap it's dependencies of MetricsGraphics.js.

Preview:

NVD3:

ScreenShot

ScreenShot

MetricsGraphics.js

ScreenShot

Installation

Add this line to your application's Gemfile:

gem 'models_stats', github: 'accessd/models_stats'

And then execute:

$ bundle

In your application.js manifest:

//= require models_stats/nvd3

or/and

//= require models_stats/metrics_graphics

if you want use MetricsGraphics.

In your application.css.scss manifest:

//= require models_stats/nvd3

or/and

//= require models_stats/metrics_graphics

if you want use MetricsGraphics.

Also if you use MetricsGraphics.js you must have jQuery and Bootstrap js/css included.

Usage

Configuration

Add config file config/models_stats.yml, for example:

minimal configuration:

  ---
  - total_users:
      description: "Total users"
      model: User

it would be calculate total users for day.

Enhanced configuration:

  ---
  - total_links_by_error_types: # Statistics alias, must be uniq
      description: "Total links by error types"
      model: Link
      datetime_attr: :created_at # Date or datetime attribute, allows to calculate the count of models per day
      group_by: :error_type_id
      conditions: "error_type_id != <%= Link::NO_ERROR %>"
      group_by_values_map: <%= ModelsStats.convert_hash_to_yaml(Link::ERROR_NAMES) %> # for example maping integer field to text representation
      graph_width: 430
      graph_height: 140
      graphic_lib: nvd3 # By default, or can be metrics_graphics
      graphic_type: stacked # It's can be using with nvd3, by default line
      date_tick: day # By default, or can be month or week
      date_format: '%d/%m' # By default is %x, more information about formattting time available at https://github.com/mbostock/d3/wiki/Time-Formatting
  - average_by_keyword_positions:
      description: "Average by keyword positions"
      select_statement: "AVG(google_position) AS count" # Right here you may specify select query, `count` alias for function required
      model: KeywordPosition

If you want using specific redis for store statistics, set it in config/initializers/models_stats.rb, for example:

ModelsStats.redis_connection = Redis.new(host: '127.0.0.1', port: 6379, db: 5)

Default graphics library can be configured through:

ModelsStats.default_lib_for_graphics = :nvd3 # Or metrics_graphics

Default graphics type:

ModelsStats.default_graphics_type = :line # Or stacked

Default graph width:

ModelsStats.default_graphics_width = 500

Default graph height:

ModelsStats.default_graphics_height = 120

Default date tick:

ModelsStats.default_date_tick = :day # Or month, week

Default date format:

ModelsStats.default_date_format = '%d/%m'

For the full list of directives for formatting time, refer to this list

Collecting statistics

Add to your crontab(may use whenever) rake task models_stats:collect_stat_for_yesterday, run it at 00:00 or later and it will collect statistics for yesterday. For collecting statistics for last month run rake task models_stats:collect_stat_for_last_month. Also you may collect statistics for specific date and config, for example:

  date = 2.days.ago.to_date
  statistics_alias = 'total_links_by_error_types' # statistic alias which you define in `config/models_stats.yml`
  ModelsStats::StatisticsCollector.new.collect(statistics_alias, date) # By default date is yestarday

Clear statistics

You may clear statistics data for particular alias.

  c = ModelsStats::StatisticsCollector.new
  c.clear_all_data('total_links_by_error_types')

Display graphics

In your views use helpers:

Render single graphic for total_links_by_error_types statistic alias(which you define in config/models_stats.yml) and last week:

= render_models_stats_graph('total_links_by_error_types', 1.week, 800, 200) # By default period is 1.month

With two last parameters you can customize width and height of the chart. In this case it will be 800px and 200px.

Render all defined graphics splited by two columns - first for new models count, second for total models count

= render_models_stats_dashboard

Contributing

  1. Fork it ( https://github.com/accessd/models_stats/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

models_stats's People

Contributors

accessd avatar stereodenis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

dammer jgomez1956

models_stats's Issues

statistics_collector.rb:15 — NoMethodError: undefined method `[]' for nil:NilClass

When using a very basic .yml file, the rake task for statistics collection fails with NoMethodError: undefined method []' for nil:NilClass in statistics_collector.rb:15:

config/models_stats.yml:

total_players:
  description: "Total players"
  model: Player
  datetime_attr: :created_at

rake task execution:

$ rake models_stats:collect_stat_for_yesterday
NoMethodError: undefined method `[]' for nil:NilClass
/Users/lukas/workspaces/models_stats/lib/models_stats/statistics_collector.rb:15:in `block in collect'
/Users/lukas/workspaces/models_stats/lib/models_stats/statistics_collector.rb:13:in `each'
/Users/lukas/workspaces/models_stats/lib/models_stats/statistics_collector.rb:13:in `collect'
/Users/lukas/workspaces/models_stats/lib/tasks/models_stats.rake:5:in `block (2 levels) in <top (required)>'
/Users/lukas/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `eval'
/Users/lukas/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => models_stats:collect_stat_for_yesterday
(See full trace by running task with --trace)

I’m not sure why this occurs, and tried messing around with the code a bit. It seems that the code assumes a Hash on multiple occurrences while I seem to be getting an Array with my config/models_stats.yml.

If this is indeed an error in the codebase, instead of an error on my side, I’d be willing to submit an according pull request.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.