class WhoIsSlacking::DataTools

Attributes

apikey[RW]
data_format[RW]

Public Class Methods

save_task(project, task_id, task, user, current_state, accepted_at, url, options={}) click to toggle source
# File lib/whois_slacking.rb, line 127
def self.save_task(project, task_id, task, user, current_state, accepted_at, url, options={})

  # dont use accepted_at
  accepted_at = accepted_at 
  url = url 
  created_dt = DateTime.now.to_s
  start_dt = DateTime.now.to_s
  updated_dt = created_dt
  current_state = current_state 
  active = true
  message = nil 
  task_entity = {project: project, task_id: task_id, task: task, user: user, current_state: current_state, url: url, active: active, start_dt: start_dt, created_dt: created_dt, accepted_at: accepted_at, updated_dt: updated_dt}
  # initialize datastore type
  store = whois_store
  mutex = Moneta::Mutex.new(store, 'moneta')
  mutex.synchronize do
    mkey = self.whois_key(project, task_id, user)
    entity_exists = store[mkey]

    if entity_exists && 
        entity_exists[:current_state] != 'finished' &&
        entity_exists[:current_state] != 'delivered' && 
        entity_exists[:current_state] != 'accepted' #   -- if task/username combo exists and is not delivered/finished in db
      #     -- calculate how long (realtime) the task has been worked on in days (.5, 1.5 etc)
      #     -- update time on task
      #     -- save who started task
      #     -- publish message task/user/how long to slack
      #       -- "Johnny has spent 2 days working on 'As a user I should be able to log in'"
      # keep created at date

      start_dt = entity_exists[:start_dt].to_datetime
      puts "start_dt in db was #{start_dt}"

      days_worked = TimeDifference.between(DateTime.now,  start_dt).in_days 
      if days_worked >= 1.0 
        message = "*#{user}* has spent *#{days_worked.to_i} days* working on <#{url}|#{task}>"
      else # show hours instead
        # hours_worked = TimeDifference.between(DateTime.now,  start_dt).in_hours
        # message = "*#{user} has spent #{hours_worked.to_i} hours working on #{task}*"
        message = "*#{user}* has spent *less than a day* working on <#{url}|#{task}>"
      end
      # keep the created dt and start_dt
      created_dt = entity_exists[:created_dt]
      start_dt = entity_exists[:start_dt].to_datetime
      task_entity[:created_dt] = created_dt
      task_entity[:start_dt] = start_dt.to_s
      puts "start_dt in db will be #{start_dt}"
      store[mkey] = task_entity 

    elsif entity_exists && entity_exists[:current_state] == 'delivered' && current_state == 'delivered'
      start_dt = entity_exists[:start_dt].to_datetime
      puts "start_dt in db for delivered state was #{start_dt}"

      days_worked = TimeDifference.between(DateTime.now,  start_dt).in_days 
      if days_worked >= 1.0 
        message = "Task <#{url}|#{task}> has been in a *delivered state for #{days_worked.to_i} days*"
      else
        message = "Task <#{url}|#{task}> has been in a *delivered state for less than a day*"
      end
    elsif entity_exists && entity_exists[:current_state] == 'finished' 
      # don't do anything if entity already exists as delivered
    else #   -- if task/username combo does not exist in db
      #     -- save project/task/user
      #     -- save when task was started
      #     -- save who started task
      store[mkey] = task_entity 

      if current_state != "finished" && current_state != "delivered" #     -- if task is not-completed in pivotal and is not in db
        #       -- save status as status not-completed
        #       -- publish message task/user started today
        #         -- "Today Johnny started 'As a user I should be able to log in'"
        message = "Now tracking *#{user}* as doing <#{url}|#{task}>"

      elsif current_state == "finished" #     -- if task is completed in pivotal and is not in db
        #       -- save status as status completed
        #       -- publish message task/user started today
        #         -- "Today Johnny completed 'As a user I should be able to log in'"
        message = "Today *#{user} finished* <#{url}|#{task}> and it is *waiting to be delivered*"
      elsif current_state == "delivered" #     -- if task is completed in pivotal
        #       -- save status as status completed
        #       -- publish message task/user started today
        #         -- "Today Johnny completed 'As a user I should be able to log in'"
        message = "Today *#{user} delivered* <#{url}|#{task}> and it is *waiting to be accepted*"
      end

    end
  end
  WhoIsSlacking::SlackWrapper.post_to_slack(message) if message
end
whois_key(project, task, user) click to toggle source
# File lib/whois_slacking.rb, line 217
def self.whois_key(project, task, user)
  "#{project}-#{task}-#{user}"
end
whois_store(store_type=nil) click to toggle source
# File lib/whois_slacking.rb, line 114
def self.whois_store(store_type=nil)
  t = store_type.nil? ? ENV["WHOIS_DATA_STORE"] : store_type.to_s

  case t
  when "redis"
    url = ENV["REDISTOGO_URL"] || "redis://127.0.0.1:6379/"
    uri = URI.parse url 
    store = Moneta.new(:Redis, host: uri.host, port: uri.port, password: uri.password)
  else # also for "file"
    store = Moneta.new(:File, :dir => 'moneta')
  end 
end