Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/telemetry accounts metric #872

Merged
merged 2 commits into from
Jul 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ gem 'mini_magick'
gem 'activerecord-import'
gem 'active_model_serializers'
gem 'includes-count'
gem 'poirot_rails', git: "https://github.com/instedd/poirot_rails.git", branch: 'master' unless ENV['CI']
gem 'poirot_rails', git: "https://github.com/instedd/poirot_rails.git", branch: 'master' unless ENV['CI'] || (ENV['POIROT_ENABLED'] == 'false')
gem 'instedd_telemetry', git: "https://github.com/instedd/telemetry_rails", branch: 'master'
gem 'paranoia', '~> 2.0'

Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ Start the Jasmine server with:
And open a browser tab in [http://localhost:8888](http://localhost:8888)


### Docker development

`docker-compose.yml` file build a development environment mounting the current folder and running rails in development environment.

Run the following commands to have a stable development environment.

```
$ docker-compose run --rm --no-deps web bundle install
$ ./on-web rake db:create db:schema:load db:seed
$ docker-compose up
```

To setup and run test, once the web container is running:

```
$ docker exec -it resourcemap_web_1 bash
root@45ccfa697a3a:/app# RAILS_ENV=test rake db:create db:schema:load
$ ./on-web rake
$ ./on-web rake spec SPEC=spec/models/user_spec.rb
```

## Deployment

Resource Map uses [Capistrano 3](http://capistranorb.com) to deploy. Capistrano
Expand Down
13 changes: 13 additions & 0 deletions app/models/telemetry/accounts_collector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Telemetry::AccountsCollector

def self.collect_stats(period)
{
"counters" => [{
"metric" => "accounts",
"key" => { },
"value" => User.where("created_at < ?", period.end).count,
}]
}
end

end
1 change: 1 addition & 0 deletions config/initializers/instedd_telemetry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
config.add_collector Telemetry::NewCollectionsCollector
config.add_collector Telemetry::SitesCollector
config.add_collector Telemetry::FieldsCollector
config.add_collector Telemetry::AccountsCollector

end
14 changes: 14 additions & 0 deletions docker-compose-base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '2.0'

services:
rails:
image: instedd/nginx-rails:2.1
environment:
RAILS_ENV: development
ELASTICSEARCH_URL: 'elasticsearch:9200'
REDIS_URL: 'redis://redis:6379'
DATABASE_URL: 'mysql2://root@db'
POIROT_ENABLED: 'false'
volumes:
- .:/app
- bundle:/usr/local/bundle
56 changes: 56 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
version: '2.0'

services:
db:
image: mysql:5.6
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- db:/var/lib/mysql

elasticsearch:
image: elasticsearch:1.7
command: elasticsearch -Des.network.host=0.0.0.0
volumes:
- elastic:/usr/share/elasticsearch/data

redis:
image: redis

web:
extends:
file: docker-compose-base.yml
service: rails
command: bundle exec rails server -b 0.0.0.0
links:
- db
- elasticsearch
- redis
ports:
- 3000:3000

resque:
extends:
file: docker-compose-base.yml
service: rails
command: bundle exec rake resque:work TERM_CHILD=1 FORK_PER_JOB=false
links:
- db
- elasticsearch
- redis

resque_scheduler:
extends:
file: docker-compose-base.yml
service: rails
command: bundle exec rake resque:scheduler
links:
- db
- elasticsearch
- redis

volumes:
db:
elastic:
redis:
bundle:
2 changes: 2 additions & 0 deletions on-web
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
docker-compose run --rm web "$@"
48 changes: 48 additions & 0 deletions spec/models/telemetry/accounts_collector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'spec_helper'

describe Telemetry::AccountsCollector do

it "counts accounts for current period" do
3.times { User.make }
period = InsteddTelemetry::Period.current

stats = Telemetry::AccountsCollector.collect_stats(period)

assert_equal stats, {
"counters" => [
{
"metric" => "accounts",
"key" => {},
"value" => 3
}
]
}
end

it "takes into account period date" do
Timecop.freeze(Time.now)
3.times { User.make }
p0 = InsteddTelemetry::Period.current

Timecop.freeze(Time.now + InsteddTelemetry::Period.span)
2.times { User.make }
p1 = InsteddTelemetry::Period.current

assert_equal Telemetry::AccountsCollector.collect_stats(p0), {
"counters" => [{
"metric" => "accounts",
"key" => {},
"value" => 3
}]
}

assert_equal Telemetry::AccountsCollector.collect_stats(p1), {
"counters" => [{
"metric" => "accounts",
"key" => {},
"value" => 5
}]
}
end

end