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
@@ -0,0 +1,76 @@
1
+ module SnowmanIO
2
+ module API
3
+ module AuthHelpers
4
+ extend ActiveSupport::Concern
5
+ TOKEN_REGEX = /^Token /
6
+ AUTHN_PAIR_DELIMITERS = /(?:,|;|\t+)/
7
+
8
+ included do
9
+ helpers do
10
+ def authenticate!
11
+ current_user || render_unauthorized
12
+ end
13
+
14
+ def render_unauthorized
15
+ error! 'Unauthorized', 401, 'WWW-Authenticate' => 'Token realm="Application"'
16
+ end
17
+
18
+ def current_user
19
+ @current_user ||= authenticate_user_from_token
20
+ end
21
+
22
+ # Code below grabbed from [Ruby on Rails](https://github.com/rails/rails)
23
+ def authenticate_user_from_token
24
+ authenticate_with_http_token do |token, options|
25
+ # # Let use only token for authentication
26
+ # user_email = options[:email]
27
+ # user_email && User.where(email: user_email, authentication_token: token).first
28
+ User.where(authentication_token: token).first
29
+ end
30
+ end
31
+
32
+ def authenticate_with_http_token(&login_procedure)
33
+ token, options = token_and_options
34
+ unless token.blank?
35
+ login_procedure.call(token, options)
36
+ end
37
+ end
38
+
39
+ def token_and_options
40
+ return if authorization_request.blank?
41
+
42
+ if authorization_request.to_s[TOKEN_REGEX]
43
+ params = token_params_from authorization_request
44
+ [params.shift.last, Hash[params].with_indifferent_access]
45
+ end
46
+ end
47
+
48
+ def authorization_request
49
+ headers["Authorization"]
50
+ end
51
+
52
+ def token_params_from(auth)
53
+ rewrite_param_values(params_array_from(raw_params(auth)))
54
+ end
55
+
56
+ # Takes raw_params and turns it into an array of parameters
57
+ def params_array_from(raw_params)
58
+ raw_params.map { |param| param.split %r/=(.+)?/ }
59
+ end
60
+
61
+ # This removes the `"` characters wrapping the value.
62
+ def rewrite_param_values(array_params)
63
+ array_params.each { |param| param.last.gsub! %r/^"|"$/, '' }
64
+ end
65
+
66
+ # pairs by the standardized `:`, `;`, or `\t` delimiters defined in
67
+ # `AUTHN_PAIR_DELIMITERS`.
68
+ def raw_params(auth)
69
+ auth.sub(TOKEN_REGEX, '').split(/"\s*#{AUTHN_PAIR_DELIMITERS}\s*/)
70
+ end
71
+
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,59 @@
1
+ module SnowmanIO
2
+ module API
3
+ class Checks < Grape::API
4
+ before(&:authenticate!)
5
+
6
+ namespace :checks do
7
+ params do
8
+ requires :check, type: Hash do
9
+ requires :metric_id, type: String
10
+ requires :cmp, type: String
11
+ requires :value, type: Float
12
+ requires :template, type: String
13
+ end
14
+ end
15
+ post do
16
+ metric = Metric.find(permitted_params[:check][:metric_id])
17
+ { check: metric.checks.create!(
18
+ permitted_params[:check].to_h.except("metric_id").merge("user_id" => current_user._id)
19
+ ) }
20
+ end
21
+
22
+ route_param :id do
23
+ before do
24
+ @check = Check.find(params[:id])
25
+ end
26
+
27
+ get do
28
+ { check: @check }
29
+ end
30
+
31
+ params do
32
+ requires :check, type: Hash do
33
+ requires :metric_id, type: String
34
+ requires :cmp, type: String
35
+ requires :value, type: Float
36
+ requires :template, type: String
37
+ end
38
+ end
39
+ put do
40
+ { check: @check.tap { |m|
41
+ m.update_attributes!(permitted_params[:check].to_h.except("metric_id").merge(
42
+ "triggered" => false,
43
+ "last_status" => Check::STATUS_NEVER_RUNNED
44
+ ))
45
+ } }
46
+ end
47
+
48
+ put 'resolve' do
49
+ { check: @check.tap { |m| m.update_attributes!(triggered: false) } }
50
+ end
51
+
52
+ delete do
53
+ Extra::Meteor.model_destroy(Check, @check)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,45 @@
1
+ module SnowmanIO
2
+ module API
3
+ module Extra
4
+ # Simple and a little bit ugly web sockets replacement
5
+ module Meteor
6
+ def self.all(last)
7
+ at = Time.at(last)
8
+ now = Time.now
9
+
10
+ if last == 0
11
+ {
12
+ users: User.all,
13
+ apps: App.all,
14
+ metrics: Metric.all,
15
+ checks: Check.all,
16
+ users_deleted: [],
17
+ apps_deleted: [],
18
+ metrics_deleted: [],
19
+ checks_deleted: [],
20
+ last: now.to_i
21
+ }
22
+ else
23
+ scope = Deleted.where(:created_at.gte => at)
24
+ {
25
+ users: User.where(:updated_at.gte => at),
26
+ apps: App.where(:updated_at.gte => at),
27
+ metrics: Metric.where(:updated_at.gte => at),
28
+ checks: Check.where(:updated_at.gte => at),
29
+ users_deleted: scope.where(model_kind: User.to_s).map(&:model_id),
30
+ apps_deleted: scope.where(model_kind: App.to_s).map(&:model_id),
31
+ metrics_deleted: scope.where(model_kind: Metric.to_s).map(&:model_id),
32
+ checks_deleted: scope.where(model_kind: Check.to_s).map(&:model_id),
33
+ last: now.to_i
34
+ }
35
+ end
36
+ end
37
+
38
+ def self.model_destroy(model, record)
39
+ Deleted.create!(model_kind: model.to_s, model_id: record.id.to_s)
40
+ record.destroy
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,14 @@
1
+ module SnowmanIO
2
+ module API
3
+ class Fridge < Grape::API
4
+ before(&:authenticate!)
5
+
6
+ params do
7
+ requires :last, type: Integer
8
+ end
9
+ get "fridge" do
10
+ Extra::Meteor.all(permitted_params[:last])
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,30 @@
1
+ module SnowmanIO
2
+ module API
3
+ class Info < Grape::API
4
+ get "info0" do
5
+ {
6
+ unpacked: SnowmanIO.unpacked?
7
+ }
8
+ end
9
+
10
+ get "info" do
11
+ authenticate!
12
+ {
13
+ base_url: Setting.get(SnowmanIO::BASE_URL_KEY),
14
+ version: SnowmanIO::VERSION,
15
+ force_ssl: Setting.force_ssl?,
16
+ production: (ENV["DEV_MODE"].to_i != 1)
17
+ }
18
+ end
19
+
20
+ params do
21
+ requires :force_ssl, type: Boolean
22
+ end
23
+ post "force_ssl" do
24
+ authenticate!
25
+ Setting.set_force_ssl(permitted_params[:force_ssl])
26
+ {force_ssl: Setting.force_ssl?}
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,115 @@
1
+ module SnowmanIO
2
+ module API
3
+ class Metrics < Grape::API
4
+ before(&:authenticate!)
5
+
6
+ namespace :metrics do
7
+ params do
8
+ requires :metric, type: Hash do
9
+ requires :app_id, type: String
10
+ requires :name, type: String
11
+ requires :kind, type: String
12
+ optional :metric_name, type: String
13
+ end
14
+ end
15
+ post do
16
+ app = App.find(permitted_params[:metric][:app_id])
17
+ { metric: app.metrics.create!(permitted_params[:metric].to_h.except("app_id")) }
18
+ end
19
+
20
+ route_param :id do
21
+ before do
22
+ @metric = Metric.find(params[:id])
23
+ end
24
+
25
+ get do
26
+ { metric: @metric }
27
+ end
28
+
29
+ params do
30
+ requires :metric, type: Hash do
31
+ requires :app_id, type: String
32
+ requires :name, type: String
33
+ requires :kind, type: String
34
+ optional :metric_name, type: String
35
+ end
36
+ end
37
+ put do
38
+ { metric: @metric.tap { |m| m.update_attributes!(permitted_params[:metric].to_h.except("app_id")) } }
39
+ end
40
+
41
+ delete do
42
+ Extra::Meteor.model_destroy(Metric, @metric)
43
+ end
44
+
45
+ # Returns data to render chart for metric
46
+ params do
47
+ requires :duration, values: ["history", "day", "hour", "5min"]
48
+ end
49
+ get "render" do
50
+ if params[:duration] == "5min"
51
+ to = Utils.floor_5sec(Time.now)
52
+ from = to - 5.minutes + 5.seconds
53
+ delta = 5.seconds
54
+
55
+ data = @metric.data_points.where(
56
+ at: {"$gte" => from, "$lte" => to + delta}
57
+ ).to_a.group_by { |v| Utils.floor_5sec(v.at).to_i }.map { |k, v| [k, v.count] }.to_h
58
+
59
+ datapoints = []
60
+ at = from
61
+ while at <= to
62
+ datapoints.push(at: at, count: data[at.to_i].to_i)
63
+ at += delta
64
+ end
65
+
66
+ { datapoints: datapoints }
67
+
68
+ else
69
+ if params[:duration] == "history"
70
+ from = @metric.aggregations.where(precision: "daily").order_by("at" => "asc").first.try(:at)
71
+ to = @metric.aggregations.where(precision: "daily").order_by("at" => "asc").last.try(:at)
72
+ precision = "daily"
73
+ delta = 1.day
74
+ return { datapoints: [] } unless from && to
75
+ elsif params[:duration] == "day"
76
+ to = Time.now.beginning_of_hour
77
+ from = to - 1.day + 1.hour
78
+ precision = "hour"
79
+ delta = 1.hour
80
+ elsif params[:duration] == "hour"
81
+ to = Utils.floor_5min(Time.now)
82
+ from = to - 1.hour + 5.minutes
83
+ precision = "5min"
84
+ delta = 5.minutes
85
+ end
86
+
87
+ data = @metric.aggregations.where(
88
+ precision: precision,
89
+ at: {"$gte" => from, "$lte" => to + delta}
90
+ ).to_a.index_by { |v| v.at.to_i }
91
+
92
+ datapoints = []
93
+ at = from
94
+ while at <= to
95
+ point = data[at.to_i]
96
+ datapoints.push(
97
+ at: at,
98
+ min: data[at.to_i].try(:min).to_f,
99
+ avg: data[at.to_i].try(:avg).to_f,
100
+ up: data[at.to_i].try(:up).to_f,
101
+ max: data[at.to_i].try(:max).to_f,
102
+ sum: data[at.to_i].try(:sum).to_f,
103
+ count: data[at.to_i].try(:count).to_i
104
+ )
105
+ at += delta
106
+ end
107
+
108
+ { datapoints: datapoints }
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,231 @@
1
+ module SnowmanIO
2
+ module API
3
+ class Users < Grape::API
4
+ namespace :users do
5
+ desc "User signup"
6
+ params do
7
+ requires :user, type: Hash do
8
+ requires :name, type: String
9
+ requires :password, type: String
10
+ requires :email, type: String
11
+ end
12
+ end
13
+ post do
14
+ unless SnowmanIO.unpacked?
15
+ user = User.create!(permitted_params[:user].to_h)
16
+ { user: user }
17
+ else
18
+ status 400
19
+ { message: "SnowmanIO is unpacked already" }
20
+ end
21
+ end
22
+
23
+ params do
24
+ requires :token, type: String
25
+ end
26
+ post "check_invite" do
27
+ {
28
+ correct: !!(
29
+ permitted_params[:token].present? &&
30
+ User.where(status: "wait_invite", invite_token: permitted_params[:token]).first
31
+ )
32
+ }
33
+ end
34
+
35
+ params do
36
+ requires :token, type: String
37
+ requires :name, type: String
38
+ requires :password, type: String
39
+ end
40
+ post "accept_invite" do
41
+ if permitted_params[:token].present? && (user = User.where(status: "wait_invite", invite_token: permitted_params[:token]).first)
42
+ user.update_attributes!(
43
+ name: permitted_params[:name],
44
+ password: permitted_params[:password],
45
+ status: "active",
46
+ invite_token: ""
47
+ )
48
+ { user: user }
49
+ else
50
+ status 400
51
+ { message: "SnowmanIO bad accept_invite request" }
52
+ end
53
+ end
54
+
55
+ desc "User Signin"
56
+ params do
57
+ requires :user, type: Hash do
58
+ requires :email, type: String
59
+ optional :password, type: String
60
+ end
61
+ end
62
+ post "login" do
63
+ email = permitted_params[:user][:email]
64
+ password = permitted_params[:user][:password]
65
+ if (user = User.where(email: email).first) && password.present? && user.authenticate(password)
66
+ { token: user.authentication_token, email: user.email, user_id: user.id.to_s }
67
+ else
68
+ status 400
69
+ { message: "Wrong email or password" }
70
+ end
71
+ end
72
+
73
+ params do
74
+ requires :email, type: String
75
+ end
76
+ post "restore_password" do
77
+ if user = User.where(email: permitted_params[:email]).first
78
+ user.restore_password!
79
+ {}
80
+ else
81
+ status 400
82
+ { message: "Unknown email" }
83
+ end
84
+ end
85
+
86
+ params do
87
+ requires :token, type: String
88
+ requires :password, type: String
89
+ end
90
+ post "reset_password" do
91
+ if permitted_params[:token].present? && (user = User.where(status: "active", restore_pass_token: permitted_params[:token]).first)
92
+ user.update_attributes!(
93
+ password: permitted_params[:password],
94
+ restore_pass_token: ""
95
+ )
96
+ { user: user }
97
+ else
98
+ status 400
99
+ { message: "SnowmanIO bad reset_password request" }
100
+ end
101
+ end
102
+
103
+ desc "Follow user"
104
+ post ":id/follow" do
105
+ authenticate!
106
+ user = User.find(params[:id])
107
+ current_user.follow!(user)
108
+ user.touch
109
+ current_user.touch
110
+ { users: [current_user, user] }
111
+ end
112
+
113
+ desc "Unfollow user"
114
+ post ":id/unfollow" do
115
+ authenticate!
116
+ user = User.find(params[:id])
117
+ current_user.unfollow!(user)
118
+ user.touch
119
+ current_user.touch
120
+ { users: [current_user, user] }
121
+ end
122
+
123
+ post ":id/destroy" do
124
+ authenticate!
125
+ user = User.find(params[:id])
126
+ Extra::Meteor.model_destroy(User, user)
127
+ { }
128
+ end
129
+
130
+ params do
131
+ requires :email, type: String
132
+ end
133
+ post "invite" do
134
+ authenticate!
135
+ if User.where(email: permitted_params[:email]).empty?
136
+ user = User.create!(
137
+ email: permitted_params[:email],
138
+ password: SecureRandom.hex,
139
+ status: "wait_invite"
140
+ )
141
+ user.invite!(current_user)
142
+ { user: user }
143
+ else
144
+ status 400
145
+ { message: "wrong invite request" }
146
+ end
147
+ end
148
+
149
+ post ":id/cancel_invite" do
150
+ authenticate!
151
+ user = User.where(status: "wait_invite").find(params[:id])
152
+
153
+ if user.invite_token.present?
154
+ user.destroy
155
+ { }
156
+ else
157
+ status 400
158
+ { message: "wrong invite cancel request" }
159
+ end
160
+ end
161
+
162
+ post ":id/resend_invite" do
163
+ authenticate!
164
+ user = User.where(status: "wait_invite").find(params[:id])
165
+
166
+ if user.invite_token.present?
167
+ user.invite!(current_user)
168
+ { user: user }
169
+ else
170
+ status 400
171
+ { message: "wrong invite resend request" }
172
+ end
173
+ end
174
+
175
+ params do
176
+ requires :value, type: Boolean
177
+ end
178
+ post "profile/daily_report" do
179
+ authenticate!
180
+ current_user.update_attributes!(daily_report: permitted_params[:value])
181
+ { user: current_user }
182
+ end
183
+
184
+ params do
185
+ requires :name, type: String
186
+ end
187
+ post "profile/update_name" do
188
+ authenticate!
189
+ current_user.update_attributes!(name: permitted_params[:name])
190
+ { user: current_user }
191
+ end
192
+
193
+ params do
194
+ requires :email, type: String
195
+ end
196
+ post "profile/update_email" do
197
+ authenticate!
198
+ new_email = permitted_params[:email]
199
+ if User.ne(_id: current_user._id).where(email: new_email).empty?
200
+ current_user.update_attributes!(email: new_email)
201
+ { user: current_user }
202
+ else
203
+ status 400
204
+ { message: "Duplicate email" }
205
+ end
206
+ end
207
+
208
+ params do
209
+ requires :password, type: String
210
+ end
211
+ post "profile/update_password" do
212
+ authenticate!
213
+ current_user.update_attributes!(password: permitted_params[:password])
214
+ { }
215
+ end
216
+
217
+ params do
218
+ requires :token, type: String
219
+ end
220
+ post "check_pass_token" do
221
+ {
222
+ correct: !!(
223
+ permitted_params[:token].present? &&
224
+ User.where(status: "active", restore_pass_token: permitted_params[:token]).first
225
+ )
226
+ }
227
+ end
228
+ end
229
+ end
230
+ end
231
+ end