Skip to content

Commit 55abfe0

Browse files
author
Adam Zaninovich
committed
Merge branch 'master' of github.com:NateBarnes/hashiru
2 parents cdfb69c + 6f4de56 commit 55abfe0

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

Gemfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ gem 'pry'
1515
gem 'rb-libsvm', :require => 'libsvm'
1616

1717
# Use sqlite3 as the database for Active Record
18-
gem 'sqlite3'
18+
group :development do
19+
gem 'sqlite3'
20+
end
21+
22+
group :production do
23+
gem 'pg'
24+
end
1925

2026
# Use SCSS for stylesheets
2127
gem 'sass-rails', '~> 4.0.0'

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ GEM
105105
daemon_controller (>= 1.1.0)
106106
rack
107107
rake (>= 0.8.1)
108+
pg (0.17.0)
108109
polyglot (0.3.3)
109110
pry (0.9.12.2)
110111
coderay (~> 1.0.5)
@@ -191,6 +192,7 @@ DEPENDENCIES
191192
jbuilder (~> 1.2)
192193
jquery-rails
193194
passenger
195+
pg
194196
pry
195197
rails (~> 4.0.0)
196198
rb-libsvm

app/models/user.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class User < ActiveRecord::Base
22
include ActiveModel::ForbiddenAttributesProtection
33
validates_presence_of :name, :gender, :mile_time, :longest_distance, :goal_type
44
has_many :workouts, :dependent => :destroy
5+
has_many :workout_exercises, :through => :workouts
56
has_one :event
67

78
after_save :recluster
@@ -59,4 +60,16 @@ def search_goal
5960
end
6061
end
6162

63+
def exercise_history exercise
64+
hsh = {}
65+
workout_exercises.where(:exercise_id => exercise.id).load.each do |workout_exercise|
66+
JSON.parse(workout_exercise.exercise.units).each do |unit|
67+
hsh.merge_or_add unit => [workout_exercise.measurements.where(:unit => unit).first.value]
68+
end
69+
hsh.merge_or_add "timestamps" => [workout_exercise.workout.day.to_i]
70+
end
71+
72+
hsh
73+
end
6274
end
75+

app/models/workout.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,27 @@ def self.generate user
1515
workout = user.workouts.create day: Date.today, exercises: exercises
1616
workout.workout_exercises.each do |workout_exercise|
1717
JSON.parse(workout_exercise.exercise.units).each do |unit|
18-
measurement = workout.measurements.build unit: unit, value: 5, workout_exercise_id: workout_exercise.id
18+
measurement = workout.measurements.build unit: unit, value: predict_value(user, workout_exercise, unit), workout_exercise_id: workout_exercise.id
1919
measurement.save
2020
end
2121
end
2222
workout
2323
end
2424

25+
def self.predict_value user, workout_exercise, unit
26+
val = 5
27+
begin
28+
units_hash = user.exercise_history workout_exercise.exercise
29+
timestamps = units_hash.delete "timestamps"
30+
lr = LinearRegression.new(timestamps.fetch("timestamps", []), units_hash.fetch(unit, default_array))
31+
32+
val = lr.predict(workout_exercise.workout.day.to_i)
33+
rescue
34+
puts "Rescuing Regression Prediction"
35+
end
36+
37+
val
38+
end
39+
2540
end
2641

config/actv.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
development:
22
host: http://api.amp.active.com
33
api_key: jsxev8samqc3jej5nyzp5b5x
4+
production:
5+
host: http://api.amp.active.com
6+
api_key: jsxev8samqc3jej5nyzp5b5x
7+

config/sidekiq.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
development:
22
host: redis://localhost:6379
3+
production:
4+
host: redis://localhost:6379
5+

0 commit comments

Comments
 (0)