Skip to content

Commit

Permalink
Merge pull request #3 from bugmark/master
Browse files Browse the repository at this point in the history
merging
  • Loading branch information
Sneha Sinha authored Jul 25, 2018
2 parents e6687e4 + 6716151 commit 0a42ed5
Show file tree
Hide file tree
Showing 38 changed files with 505 additions and 133 deletions.
4 changes: 3 additions & 1 deletion exercise/Base/lib/dotenv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ def env_file_in_parent(path)
parent = File.expand_path("../.env", path)
grandparent = File.expand_path("../../.env", path)
targets = [base, parent, grandparent]
Dir.glob(targets).first
result = Dir.glob(targets).first
raise "ERROR: .env file not found" unless result
result
end
2 changes: 1 addition & 1 deletion exercise/Base/lib/trial_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def participant_opts
end

def base_opts
base = File.expand_path("./.trial_data/Settings_base.yml", TRIAL_DIR)
base = File.expand_path("../Settings_base.yml")
yaml_settings(base)
end

Expand Down
17 changes: 10 additions & 7 deletions exercise/intern/CronJobs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ BASEDIR = File.expand_path(__dir__)

job_type :runscript, "cd #{BASEDIR} && :task"

every 15.minutes do
runscript 'script/issue_sync_all'
every '30 * * * *' do
runscript 'script/issue_sync'
end

every '10,20,40,50 * * * *' do
runscript 'script/resolve_contracts'
every '40 * * * *' do
runscript 'script/offer_cross'
end

# every 1.day at '12:35am' do
# runscript 'script/reset_nightly'
# end
every '50 * * * *' do
runscript 'script/contract_resolve'
end

every :monday, at: '00:05am' do
runscript 'script/weekly_reset'
end
4 changes: 3 additions & 1 deletion exercise/intern/Settings_base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ repo_list:
- bugmark/test_bench
- bugmark/website
- bugmark/outreachy
- bugmark/iora
- bugmark/iora
- bugmark/paypro
- Bugmark-Simulator/bugmark_simulation
3 changes: 2 additions & 1 deletion exercise/intern/script/contract_resolve
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ require 'iora'
# ----- expired offers -----
puts 'MARK ALL EXPIRED OFFERS'
Offer.open.expired_by_time.each do |offer|
offer.update_attribute(:status, 'expired')
OfferCmd::Expire.new(offer)
# offer.update_attribute(:status, 'expired')
print "."
end
puts ""
Expand Down
10 changes: 9 additions & 1 deletion exercise/intern/script/issue_sync
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ Exchange.load_rails
puts 'LOADING IORA'
require 'iora'

# ----- bugmark tracker -----

puts 'CREATE A BUGMARK TRACKER FOR EVERY APP REPO'
all_repos.each do |repo|
next if repo.nil?
puts "Create tracker for #{repo}"
FB.create(:tracker, name: repo)
end

# ----- expired offers -----

puts 'MARK ALL EXPIRED OFFERS'
Expand All @@ -39,7 +48,6 @@ puts ""
# ----- sync -----

puts 'SYNC ALL ISSUES TO BUGMARK'
require 'pry'
all_repos.each do |repo|
next if repo.nil?
puts "Sync Issues for #{repo}"
Expand Down
58 changes: 58 additions & 0 deletions exercise/intern/script/offer_cross
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env ruby

# ----- setup -----

Dir.chdir File.expand_path(__dir__)
require_relative '../../Base/lib/dotenv'
TRIAL_DIR = dotenv_trial_dir(__dir__)

# ----- libraries -----

require_relative '../../Base/lib/exchange'
require_relative '../../Base/lib/trial_settings'
require_relative '../../Base/lib/helpers'
require 'awesome_print'
require 'securerandom'

# ----- info -----

puts "EXCHANGE_DIR=#{Exchange.src_dir}"
puts 'EXERCISE SETTINGS'
ap(TrialSettings.settings, sort_keys: true)

# ----- load -----

puts 'LOADING RAILS'
Exchange.load_rails

# ----- reset -----

puts 'CROSSING OFFERS'

Offer.is_buy_fixed.open.each do |offer|
ContractCmd::Cross.new(offer, :expand).project
print "."
end

Offer.is_buy_unfixed.open.each do |offer|
ContractCmd::Cross.new(offer, :expand).project
print "."
end

Offer.is_sell.open.each do |offer|
ContractCmd::Cross.new(offer, :transfer).project
print "."
end

Offer.is_sell_fixed.open.each do |offer|
ContractCmd::Cross.new(offer, :reduce).project
print "."
end

Offer.is_sell_unfixed.open.each do |offer|
ContractCmd::Cross.new(offer, :reduce).project
print "."
end

puts "."
puts "DONE."
10 changes: 0 additions & 10 deletions exercise/intern/script/system_clean
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,4 @@ TS.participants.each do |email|
end
puts ''

# ----- bugmark tracker -----

puts 'CREATE A BUGMARK TRACKER FOR EVERY APP REPO'
FB.create(:tracker, name: 'TraderExercise')
all_repos.each do |repo|
next if repo.nil?
puts "Create tracker for #{repo}"
FB.create(:tracker, name: repo)
end

puts 'DONE'
47 changes: 47 additions & 0 deletions exercise/intern/script/weekly_reset
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env ruby

# ----- setup -----

Dir.chdir File.expand_path(__dir__)
require_relative '../../Base/lib/dotenv'
TRIAL_DIR = dotenv_trial_dir(__dir__)

# ----- libraries -----

require_relative '../../Base/lib/exchange'
require_relative '../../Base/lib/trial_settings'
require_relative '../../Base/lib/helpers'
require 'awesome_print'
require 'securerandom'

# ----- info -----

puts "EXCHANGE_DIR=#{Exchange.src_dir}"
puts 'EXERCISE SETTINGS'
ap(TrialSettings.settings, sort_keys: true)

# ----- load -----

puts 'LOADING RAILS'
Exchange.load_rails

# ----- reset -----

puts 'UPDATE USER BALANCES'

users = User.all.select do |user|
user.balance < 200.0
end

users.each do |user|
delta = 200.0 - user.balance
args = {
uuid: user.uuid ,
amount: delta ,
note: "Low-balance adjustment"
}
puts "Updating balance for #{user.email}"
UserCmd::Deposit.new(args).project
end


54 changes: 52 additions & 2 deletions exercise/intern/webapp/app.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative "./lib/web_util"
require_relative "./lib/balance"
require_relative "./app_helpers"

require 'sinatra'
Expand All @@ -13,6 +14,30 @@

helpers AppHelpers

# ----- filters -----
before do
@start_clock = Time.now
end

after do
if USE_INFLUX == true
path = request.env["REQUEST_PATH"]
meth = request.env["REQUEST_METHOD"]
user = current_user&.email || "NA"
time = Time.now - @start_clock
args = {
tags: {
user: user,
method: meth,
path: path
},
values: {req_time: time},
timestamp: BugmTime.now.to_i
}
InfluxViews.write_point "Request", args
end
end

# ----- core app -----

get "/" do
Expand All @@ -26,6 +51,11 @@
slim :events
end

get "/events/:event_uuid" do
@event = Event.find_by_event_uuid(params['event_uuid'])
slim :event
end

get "/events_user/:user_uuid" do
user = User.find_by_uuid(params['user_uuid'])
@title = user.email
Expand All @@ -38,7 +68,10 @@
# show one issue
get "/issues/:uuid" do
protected!
@issue = Issue.find_by_uuid(params['uuid'])
@issue = Issue.find_by_uuid(params['uuid'])
@contracts = @issue.contracts
@events = Event.where("payload->>'exid' = ?", @issue.exid) || []
@offers = @issue.offers.open.without_branch_position
slim :issue
end

Expand All @@ -56,6 +89,15 @@
slim :issues
end

get "/sync_now" do
protected!
script = File.expand_path("../script/issue_sync", __dir__)
job = fork {exec script}
Process.detach(job)
flash[:success] = "Issue sync has started - estimated finish in 60 seconds..."
redirect "/issues"
end

# render a dynamic SVG for the issues
get '/badge_ex/*' do |issue_exid|
content_type 'image/svg+xml'
Expand Down Expand Up @@ -83,6 +125,13 @@
slim :offers
end

get "/all_offers" do
protected!
@label = "All Offers"
@offers = Offer.all
slim :offers
end

# cancel an offer
get "/offer_cancel/:offer_uuid" do
offer = Offer.find_by_uuid(params['offer_uuid'])
Expand Down Expand Up @@ -193,7 +242,8 @@

get "/account" do
protected!
@events = Event.for_user(current_user)
@events = Event.for_user(current_user).order(:id)
@rows = Balance.new(@events).rows
slim :account
end

Expand Down
5 changes: 2 additions & 3 deletions exercise/intern/webapp/app_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def clean_payload(event)

def user_links(event)
event.user_uuids.map do |x|
"<a href='/events_user/#{x}'>#{x[0..5]}</a>"
y = x || ""
"<a href='/events_user/#{y}'>#{y[0..5]}</a>"
end.join(", ")
end

Expand Down Expand Up @@ -195,8 +196,6 @@ def offer_funder_link(user, offer, action = "offer_accept")
when 'open'
if offer.user.uuid == user.uuid
"My Offer"
# elsif offer.issue.offers.where('expiration > ?', BugmTime.now).where(type: "Offer::Buy::Unfixed").pluck(:user_uuid).include?(user.uuid)
# "You funded this issue"
else
"<a class='btn btn-primary btn-sm bxs' href='/#{action}/#{offer.uuid}'>ACCEPT OFFER</a>"
end
Expand Down
48 changes: 48 additions & 0 deletions exercise/intern/webapp/lib/balance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "ostruct"

class Balance
attr_reader :events

def initialize(events)
@events = balance_events(events)
end

def rows
events.reduce([]) do |acc, event|
adjustment = adjustment(event)
new_balance = (acc.last&.balance || 0) + adjustment(event)
acc + [new_row(event, adjustment, new_balance)]
end
end

private

def new_row(event, adjustment, balance)
row = OpenStruct.new
row.balance = balance
row.adjustment = adjustment
row.id = event.id
row.uuid = event.event_uuid
row.date = event.projected_at.strftime("%m-%d %H:%M:%S")
row.cmd_type = event.cmd_type
row.event_type = event.event_type.gsub("Event::", "")
row.payload = event.payload
row.note = event.note
row
end

def adjustment(event)
increment_types = %w(UserDeposited)
type = event.event_type.split("::").last
amount = event.payload["amount"] || 0
increment_types.include?(type) ? amount : -1 * amount
end

def balance_events(events)
list = %w(UserCreated UserDeposited UserWithdrawn)
events.to_a.select do |event|
type = event.event_type.split("::").last
list.include?(type)
end
end
end
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 0a42ed5

Please sign in to comment.