snowman-io 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +3 -0
  3. data/README.md +4 -6
  4. data/bin/snowman +1 -1
  5. data/lib/config/mongoid.yml +5 -0
  6. data/lib/snowman-io.rb +62 -30
  7. data/lib/snowman-io/aggregate.rb +97 -0
  8. data/lib/snowman-io/api.rb +33 -65
  9. data/lib/snowman-io/api/agent.rb +27 -0
  10. data/lib/snowman-io/api/apps.rb +45 -0
  11. data/lib/snowman-io/api/auth_helpers.rb +76 -0
  12. data/lib/snowman-io/api/checks.rb +59 -0
  13. data/lib/snowman-io/api/extra/meteor.rb +45 -0
  14. data/lib/snowman-io/api/fridge.rb +14 -0
  15. data/lib/snowman-io/api/info.rb +30 -0
  16. data/lib/snowman-io/api/metrics.rb +115 -0
  17. data/lib/snowman-io/api/users.rb +231 -0
  18. data/lib/snowman-io/cli.rb +69 -0
  19. data/lib/snowman-io/launcher.rb +12 -11
  20. data/lib/snowman-io/loop/check_processor.rb +29 -0
  21. data/lib/snowman-io/loop/checks.rb +59 -0
  22. data/lib/snowman-io/loop/main.rb +43 -0
  23. data/lib/snowman-io/loop/ping.rb +25 -0
  24. data/lib/snowman-io/migration.rb +79 -0
  25. data/lib/snowman-io/models/aggregation.rb +15 -0
  26. data/lib/snowman-io/models/app.rb +61 -0
  27. data/lib/snowman-io/models/check.rb +48 -0
  28. data/lib/snowman-io/models/concerns/tokenable.rb +15 -0
  29. data/lib/snowman-io/models/data_point.rb +9 -0
  30. data/lib/snowman-io/models/deleted.rb +9 -0
  31. data/lib/snowman-io/models/following.rb +8 -0
  32. data/lib/snowman-io/models/metric.rb +36 -0
  33. data/lib/snowman-io/models/setting.rb +24 -0
  34. data/lib/snowman-io/models/user.rb +73 -0
  35. data/lib/snowman-io/options.rb +11 -3
  36. data/lib/snowman-io/report_mailer.rb +88 -0
  37. data/lib/snowman-io/reports.rb +16 -0
  38. data/lib/snowman-io/ui/AUTO_GENERATED_FOLDER +2 -0
  39. data/lib/snowman-io/ui/assets/ui-0e39dafcb798020fb855e325931c8451.css +1 -0
  40. data/lib/snowman-io/ui/assets/ui-d30809d0ae0a003d841fa95a352d624b.js +9 -0
  41. data/lib/snowman-io/ui/assets/vendor-7edfd1432c1bbd806306d5583c75b1fc.css +5 -0
  42. data/lib/snowman-io/ui/assets/vendor-c22e2ccc87c9bc7609b95939c308bc7f.js +24 -0
  43. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/css/bootstrap-theme.css +0 -0
  44. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/css/bootstrap-theme.css.map +0 -0
  45. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/css/bootstrap-theme.min.css +0 -0
  46. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/css/bootstrap.css +0 -0
  47. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/css/bootstrap.css.map +0 -0
  48. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/css/bootstrap.min.css +0 -0
  49. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/fonts/glyphicons-halflings-regular.eot +0 -0
  50. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/fonts/glyphicons-halflings-regular.svg +0 -0
  51. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/fonts/glyphicons-halflings-regular.ttf +0 -0
  52. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/fonts/glyphicons-halflings-regular.woff +0 -0
  53. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/js/bootstrap.js +0 -0
  54. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/js/bootstrap.min.js +0 -0
  55. data/lib/snowman-io/{api/public/bootstrap → ui/bootstrap-3.3.1}/js/npm.js +0 -0
  56. data/lib/snowman-io/ui/crossdomain.xml +15 -0
  57. data/lib/snowman-io/ui/fonts/glyphicons-halflings-regular.eot +0 -0
  58. data/lib/snowman-io/ui/fonts/glyphicons-halflings-regular.svg +229 -0
  59. data/lib/snowman-io/ui/fonts/glyphicons-halflings-regular.ttf +0 -0
  60. data/lib/snowman-io/ui/fonts/glyphicons-halflings-regular.woff +0 -0
  61. data/lib/snowman-io/ui/index.html +175 -0
  62. data/lib/snowman-io/ui/robots.txt +2 -0
  63. data/lib/snowman-io/utils.rb +66 -0
  64. data/lib/snowman-io/version.rb +1 -1
  65. data/lib/snowman-io/views/layouts/custom.css +42 -0
  66. data/lib/snowman-io/views/layouts/main.html.erb +38 -0
  67. data/lib/snowman-io/views/layouts/styles.css +264 -0
  68. data/lib/snowman-io/views/layouts/transactional-email-templates-LICENSE +21 -0
  69. data/lib/snowman-io/views/report_mailer/check_triggered.html.erb +16 -0
  70. data/lib/snowman-io/views/report_mailer/checks/_human_last_value_limit.html.erb +2 -0
  71. data/lib/snowman-io/views/report_mailer/checks/_human_prev_day_datapoints_limit.html.erb +2 -0
  72. data/lib/snowman-io/views/report_mailer/daily_report.html.erb +40 -0
  73. data/lib/snowman-io/views/report_mailer/restore_password.html.erb +28 -0
  74. data/lib/snowman-io/views/report_mailer/send_invite.html.erb +32 -0
  75. data/lib/snowman-io/web.rb +28 -0
  76. data/lib/snowman-io/web_server.rb +107 -0
  77. data/snowman-io.gemspec +15 -5
  78. metadata +220 -49
  79. data/lib/snowman-io/api/public/README.md +0 -36
  80. data/lib/snowman-io/api/public/css/normalize.css +0 -406
  81. data/lib/snowman-io/api/public/css/style.css +0 -4
  82. data/lib/snowman-io/api/public/js/app.js +0 -13
  83. data/lib/snowman-io/api/public/js/libs/ember-1.8.1.js +0 -49740
  84. data/lib/snowman-io/api/public/js/libs/handlebars-v1.3.0.js +0 -2746
  85. data/lib/snowman-io/api/public/js/libs/jquery-1.10.2.js +0 -9789
  86. data/lib/snowman-io/api/public/tests/runner.css +0 -14
  87. data/lib/snowman-io/api/public/tests/runner.js +0 -13
  88. data/lib/snowman-io/api/public/tests/tests.js +0 -30
  89. data/lib/snowman-io/api/public/tests/vendor/qunit-1.12.0.css +0 -244
  90. data/lib/snowman-io/api/public/tests/vendor/qunit-1.12.0.js +0 -2212
  91. data/lib/snowman-io/api/views/index.erb +0 -26
  92. data/lib/snowman-io/api/views/layout.erb +0 -24
  93. data/lib/snowman-io/api/views/login.erb +0 -21
  94. data/lib/snowman-io/api/views/unpacking.erb +0 -21
  95. data/lib/snowman-io/check.rb +0 -49
  96. data/lib/snowman-io/check_result.rb +0 -15
  97. data/lib/snowman-io/checks/hosted_graphite.rb +0 -23
  98. data/lib/snowman-io/handler.rb +0 -27
  99. data/lib/snowman-io/notifiers/slack.rb +0 -76
  100. data/lib/snowman-io/processor.rb +0 -32
  101. data/lib/snowman-io/scheduler.rb +0 -42
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a5124f50d680bd5e89354183e0e4cac5cbb3fe73
4
- data.tar.gz: 5780d7324eb1f25f1073b0eff05342257a941a3c
3
+ metadata.gz: 8591ccf2ab370cd2962171434bb77f454ebec94e
4
+ data.tar.gz: 5efbb7cf4889b13354864479cc5b32eece3b16bd
5
5
  SHA512:
6
- metadata.gz: f9de1539ff5b8ff4c24ed3c51f50358dac3e813499a080efc86a1676493966871590a76ef800f7719570f079e5ce9090dad270e3a2cdc6515a139a3cef94ba9c
7
- data.tar.gz: 86125496a5fb7781cba02bdf34489530184e4473e314194fa1269bdb4669d057aa3735db6d0c924b1c543b5347db31658cb03c767c16f87a678b3202a783d0dd
6
+ metadata.gz: 781a22f544cde6adf1108d0a56d8ac4b002a3cab6e07f9baeed197d76a50b2ef40b65c32f99c40f999a1530c3130a3aae5fac509f4e98a29f6de5a8e372b6fc4
7
+ data.tar.gz: f5b85766a6f5702f73baf8948279c861ce84a89caf858c7966465164c665b8d23910de0ae9ecc544a31df627ad83f03d05a383d781a5a9eeb468f1f9bce371e4
@@ -1,5 +1,8 @@
1
1
  # SnowmanIO
2
2
 
3
+ Version 0.0.5 - 2015.06.08
4
+ - First ponential usefull version: user management, 3 metrics kinds, 2 check templates
5
+
3
6
  Version 0.0.4 - 2014.12.4
4
7
  - First kernel version
5
8
  - Very simple web UI
data/README.md CHANGED
@@ -1,14 +1,12 @@
1
1
  # SnowmanIO
2
2
 
3
- Zero maintenance power parameters checks.
3
+ Your personal assistent.
4
4
 
5
- [![Build Status](https://travis-ci.org/snowman-io/snowman-io.svg)](https://travis-ci.org/snowman-io/snowman-io)
5
+ [![Gem Version](https://badge.fury.io/rb/snowman-io.svg)](http://badge.fury.io/rb/snowman-io) [![Build Status](https://travis-ci.org/snowman-io/snowman-io.svg)](https://travis-ci.org/snowman-io/snowman-io) [![Code Climate](https://codeclimate.com/github/snowman-io/snowman-io/badges/gpa.svg)](https://codeclimate.com/github/snowman-io/snowman-io) [![Coverage Status](https://coveralls.io/repos/snowman-io/snowman-io/badge.png)](https://coveralls.io/r/snowman-io/snowman-io)
6
6
 
7
7
  ## Installation
8
8
 
9
- Press a button below to install your private Heroku SnowmanIO instance for free.
10
-
11
- [![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy?template=https://github.com/snowman-io/sample-app)
9
+ Please follow instructions on project site http://snowman.io.
12
10
 
13
11
  ## Authors
14
12
 
@@ -17,7 +15,7 @@ Press a button below to install your private Heroku SnowmanIO instance for free.
17
15
 
18
16
  ## Contributing
19
17
 
20
- 1. Fork it ( https://github.com/[my-github-username]/snowman-io/fork )
18
+ 1. Fork it ( https://github.com/snowman-io/snowman-io/fork )
21
19
  2. Create your feature branch (`git checkout -b my-new-feature`)
22
20
  3. Commit your changes (`git commit -am 'Add some feature'`)
23
21
  4. Push to the branch (`git push origin my-new-feature`)
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  require "snowman-io"
3
3
 
4
- SnowmanIO.start
4
+ SnowmanIO::CLI.run
@@ -0,0 +1,5 @@
1
+ production:
2
+ sessions:
3
+ default:
4
+ # Try different mongo urls one after another
5
+ uri: <%= ENV["MONGOHQ_URL"] || ENV["MONGOLAB_URI"] || ENV["MONGOSOUP_URL"] || ENV["MONGO_URL"] || "mongodb://127.0.0.1:27017/db" %>
@@ -1,46 +1,78 @@
1
1
  require 'logger'
2
- require 'redis'
3
2
  require 'bcrypt'
4
3
  require 'celluloid/autostart'
4
+ require 'action_mailer'
5
+ require 'premailer'
6
+ require 'mongoid'
7
+ require 'active_support/core_ext/object/blank'
8
+ require 'active_support/core_ext/string/strip'
9
+ require 'active_support/core_ext/hash/slice'
10
+ require 'active_support/core_ext/hash/except'
11
+ require 'active_support/core_ext/date/calculations'
12
+ require 'active_support/core_ext/time/calculations'
13
+ require 'active_support/core_ext/numeric/time'
14
+ require 'active_support/core_ext/enumerable'
5
15
 
6
16
  require "snowman-io/version"
17
+ require "snowman-io/utils"
18
+ require "snowman-io/web"
7
19
  require "snowman-io/api"
8
20
  require "snowman-io/options"
9
- require "snowman-io/checks/hosted_graphite"
10
- require "snowman-io/check"
11
- require "snowman-io/check_result"
12
-
13
21
  require "snowman-io/launcher"
22
+ require "snowman-io/cli"
23
+ require "snowman-io/web_server"
24
+ require "snowman-io/aggregate"
25
+ require "snowman-io/reports"
26
+ require "snowman-io/loop/ping"
27
+ require "snowman-io/loop/main"
28
+ require "snowman-io/loop/checks"
29
+ require "snowman-io/report_mailer"
30
+ require "snowman-io/migration"
14
31
 
15
- module SnowmanIO
16
- def self.start
17
- # parse options
18
- options = Options.new.parse!(ARGV)
19
-
20
- Celluloid.logger = (options[:verbose] ? SnowmanIO.logger : nil)
21
-
22
- launcher = Launcher.new(load_checks)
23
- launcher.start
32
+ require "snowman-io/models/concerns/tokenable"
33
+ require "snowman-io/models/user"
34
+ require "snowman-io/models/app"
35
+ require "snowman-io/models/metric"
36
+ require "snowman-io/models/check"
37
+ require "snowman-io/models/data_point"
38
+ require "snowman-io/models/aggregation"
39
+ require "snowman-io/models/setting"
40
+ require "snowman-io/models/deleted"
41
+ require "snowman-io/models/following"
24
42
 
25
- # start web server on main thread
26
- API.start(options)
27
- end
28
-
29
- def self.load_checks
30
- checks = []
31
- Dir[Dir.pwd + "/**/*_check.rb"].each do |path|
32
- require path
33
- klass = path.sub(Dir.pwd + "/checks" + "/", "").sub(/\.rb$/, '').camelize
34
- checks.push Kernel.const_get(klass)
35
- end
36
- checks
37
- end
43
+ ActionMailer::Base.raise_delivery_errors = true
44
+ ActionMailer::Base.view_paths = File.dirname(__FILE__) + "/snowman-io/views"
45
+ if ENV["DEV_MODE"].to_i == 1
46
+ Object.send(:remove_const, :Rails)
47
+ require "letter_opener"
48
+ ActionMailer::Base.add_delivery_method :letter_opener,
49
+ LetterOpener::DeliveryMethod,
50
+ :location => File.expand_path('../../tmp/letter_opener', __FILE__)
51
+ ActionMailer::Base.delivery_method = :letter_opener
52
+ else
53
+ ActionMailer::Base.delivery_method = :smtp
54
+ ActionMailer::Base.smtp_settings = {
55
+ :address => ENV["MAILGUN_SMTP_SERVER"],
56
+ :port => ENV["MAILGUN_SMTP_PORT"],
57
+ :authentication => :plain,
58
+ :user_name => ENV["MAILGUN_SMTP_LOGIN"],
59
+ :password => ENV["MAILGUN_SMTP_PASSWORD"],
60
+ :enable_starttls_auto => true
61
+ }
62
+ end
38
63
 
39
- def self.redis
40
- @redis ||= Redis.new(url: ENV["REDIS_URL"])
41
- end
64
+ module SnowmanIO
65
+ BASE_URL_KEY = "base_url"
66
+ FORCE_SSL_KEY = "force_ssl"
67
+ NEXT_REPORT_DATE_KEY = "next_report_date"
42
68
 
43
69
  def self.logger
44
70
  @logger ||= Logger.new(STDERR)
45
71
  end
72
+
73
+ def self.unpacked?
74
+ User.count > 0
75
+ end
46
76
  end
77
+
78
+ Mongoid.load!(File.expand_path("../config/mongoid.yml", __FILE__), :production)
@@ -0,0 +1,97 @@
1
+ module SnowmanIO
2
+ module Aggregate
3
+ # Aggregate 5mins metrics
4
+ def self.metrics_aggregate_5min
5
+ Metric.all.each do |metric|
6
+ aggr = {}
7
+
8
+ # collect
9
+ metric.data_points.each do |point|
10
+ key = Utils.floor_5min(point.at)
11
+ aggr[key] ||= []
12
+ aggr[key] << point.value
13
+ end
14
+
15
+ # aggregrate
16
+ aggr.each do |at, values|
17
+ metric.aggregations.where(precision: "5min", at: at).first_or_create!.update_attributes!(
18
+ count: values.length,
19
+ min: values.min,
20
+ avg: Utils.avg(values),
21
+ up: Utils.up(values),
22
+ max: values.max,
23
+ sum: values.inject(&:+)
24
+ )
25
+ end
26
+ end
27
+ end
28
+
29
+ # Aggregate hour metrics
30
+ def self.metrics_aggregate_hour
31
+ Metric.all.each do |metric|
32
+ out = {}
33
+ metric.aggregations.where(precision: "5min").each do |aggr|
34
+ key = aggr.at.beginning_of_hour
35
+ out[key] ||= {"count" => 0, "avg" => 0, "up" => 0, "sum" => 0}
36
+ out[key]["count"] += aggr["count"]
37
+ out[key]["min"] = aggr["min"] if !out[key]["min"] || out[key]["min"] > aggr["min"]
38
+ out[key]["avg"] += aggr["avg"]*aggr["count"]
39
+ out[key]["up"] += aggr["up"]*aggr["count"]
40
+ out[key]["max"] = aggr["max"] if !out[key]["max"] || out[key]["max"] < aggr["max"]
41
+ out[key]["sum"] += aggr["sum"]
42
+ end
43
+
44
+ # normalize
45
+ out.each do |__, chunk|
46
+ chunk["avg"] /= chunk["count"]
47
+ chunk["up"] /= chunk["count"]
48
+ end
49
+
50
+ # store
51
+ out.each do |at, chunk|
52
+ metric.aggregations.where(precision: "hour", at: at).first_or_create!.update_attributes!(chunk)
53
+ end
54
+ end
55
+ end
56
+
57
+ # Aggregate daily metrics
58
+ def self.metrics_aggregate_daily
59
+ Metric.all.each do |metric|
60
+ out = {}
61
+ metric.aggregations.where(precision: "hour").each do |aggr|
62
+ key = aggr.at.beginning_of_day
63
+ out[key] ||= {"count" => 0, "avg" => 0, "up" => 0, "sum" => 0}
64
+ out[key]["count"] += aggr["count"]
65
+ out[key]["min"] = aggr["min"] if !out[key]["min"] || out[key]["min"] > aggr["min"]
66
+ out[key]["avg"] += aggr["avg"]*aggr["count"]
67
+ out[key]["up"] += aggr["up"]*aggr["count"]
68
+ out[key]["max"] = aggr["max"] if !out[key]["max"] || out[key]["max"] < aggr["max"]
69
+ out[key]["sum"] += aggr["sum"]
70
+ end
71
+
72
+ # normalize
73
+ out.each do |__, chunk|
74
+ chunk["avg"] /= chunk["count"]
75
+ chunk["up"] /= chunk["count"]
76
+ end
77
+
78
+ # store
79
+ out.each do |at, chunk|
80
+ metric.aggregations.where(precision: "daily", at: at).first_or_create!.update_attributes!(chunk)
81
+ end
82
+ end
83
+
84
+ # for meteor
85
+ App.all.each(&:touch)
86
+ end
87
+
88
+ # Clean old records
89
+ def self.metrics_clean_old
90
+ now = Time.now
91
+ DataPoint.where(:at.lt => Utils.floor_5min(now - 6.minutes)).delete_all
92
+ SnowmanIO::Aggregation.where(precision: "5min", :at.lt => (now - 2.hours).beginning_of_hour).delete_all
93
+ SnowmanIO::Aggregation.where(precision: "hour", :at.lt => (now - 25.hours).beginning_of_day).delete_all
94
+ SnowmanIO::Aggregation.where(precision: "daily", :at.lt => (now - 365.days).beginning_of_day).delete_all
95
+ end
96
+ end
97
+ end
@@ -1,75 +1,43 @@
1
- require 'sinatra'
2
- require 'sinatra/content_for'
1
+ require 'grape'
2
+
3
+ require 'snowman-io/api/extra/meteor'
4
+ require 'snowman-io/api/auth_helpers'
5
+ require 'snowman-io/api/users'
6
+ require 'snowman-io/api/apps'
7
+ require 'snowman-io/api/info'
8
+ require 'snowman-io/api/metrics'
9
+ require 'snowman-io/api/checks'
10
+ require 'snowman-io/api/agent'
11
+ require 'snowman-io/api/fridge'
3
12
 
4
13
  module SnowmanIO
5
- class API < Sinatra::Base
6
- def self.start(options)
7
- sinatra_options = {}
8
- sinatra_options[:port] = options[:port]
9
- run!(sinatra_options)
10
- end
11
-
12
- enable :sessions
13
- helpers Sinatra::ContentFor
14
- set :public_folder, File.dirname(__FILE__) + "/api/public"
15
- set :views, File.dirname(__FILE__) + "/api/views"
16
- # TODO: fix it
17
- set :session_secret, 'super secret'
18
-
19
- ADMIN_PASSWORD_KEY = "admin_password_hash"
20
-
21
- def admin_exists?
22
- !!SnowmanIO.redis.get(ADMIN_PASSWORD_KEY)
23
- end
24
-
25
- def admin_authenticated?
26
- admin_exists? && !!session[:user]
27
- end
28
-
29
- before do
30
- if !admin_exists?
31
- redirect to('/unpacking') if request.path_info != '/unpacking'
32
- elsif !admin_authenticated?
33
- redirect to('/login') if request.path_info != '/login'
14
+ module API
15
+ class Root < Grape::API
16
+ include AuthHelpers
17
+ default_format :json
18
+ format :json
19
+ default_error_formatter :json
20
+
21
+ rescue_from Mongoid::Errors::Validations do |e|
22
+ response = {errors: e.document.errors}
23
+ rack_response response.to_json, 400
34
24
  end
35
- end
36
25
 
37
- get "/" do
38
- erb :index
39
- end
40
-
41
- get "/login" do
42
- erb :login
43
- end
44
-
45
- post "/login" do
46
- if BCrypt::Password.new(SnowmanIO.redis.get(ADMIN_PASSWORD_KEY)) == params["password"]
47
- session[:user] = "admin"
48
- redirect to('/')
49
- else
50
- @wrong_password_warning = true
51
- erb :login
26
+ helpers do
27
+ def permitted_params
28
+ @permitted_params ||= declared(params, include_missing: false)
29
+ end
52
30
  end
53
- end
54
31
 
55
- get "/logout" do
56
- session[:user] = nil
57
- redirect to("/login")
58
- end
59
-
60
- get "/unpacking" do
61
- erb :unpacking
62
- end
32
+ mount Agent
63
33
 
64
- post "/unpacking" do
65
- if params["password"].empty?
66
- @empty_password_warning = true
67
- erb :unpacking
68
- else
69
- session[:user] = "admin"
70
- SnowmanIO.redis.set(ADMIN_PASSWORD_KEY, BCrypt::Password.create(params["password"]))
71
- redirect to('/')
72
- end
34
+ prefix :api
35
+ mount Users
36
+ mount Apps
37
+ mount Info
38
+ mount Metrics
39
+ mount Checks
40
+ mount Fridge
73
41
  end
74
42
  end
75
43
  end
@@ -0,0 +1,27 @@
1
+ module SnowmanIO
2
+ module API
3
+ class Agent < Grape::API
4
+ namespace :agent do
5
+
6
+ desc "Report metrics from agent"
7
+ params do
8
+ requires :token, type: String
9
+ optional :metrics, type: Array
10
+ end
11
+ post "metrics" do
12
+ if app = App.where(token: params[:token]).first
13
+ params[:metrics].each do |metric|
14
+ if Metric.supported?(metric["kind"])
15
+ app.register_metric_value(metric["name"], metric["kind"], metric["value"].to_f, Time.now)
16
+ end
17
+ end
18
+ "OK"
19
+ else
20
+ "WRONG APP"
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,45 @@
1
+ module SnowmanIO
2
+ module API
3
+ class Apps < Grape::API
4
+ before(&:authenticate!)
5
+
6
+ namespace :apps do
7
+ desc "Creates app"
8
+ params do
9
+ requires :app, type: Hash do
10
+ requires :name, type: String
11
+ end
12
+ end
13
+ post do
14
+ { app: App.create!(permitted_params[:app].to_h) }
15
+ end
16
+
17
+ route_param :id do
18
+ before do
19
+ @app = App.find(params[:id])
20
+ end
21
+
22
+ desc "Returns app"
23
+ get do
24
+ { app: @app }
25
+ end
26
+
27
+ desc "Updates app"
28
+ params do
29
+ requires :app, type: Hash do
30
+ requires :name, type: String
31
+ end
32
+ end
33
+ put do
34
+ { app: @app.tap { |app| app.update_attributes!(permitted_params[:app].to_h) } }
35
+ end
36
+
37
+ desc "Deletes app"
38
+ delete do
39
+ Extra::Meteor.model_destroy(App, @app)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end