Class: WhoIsSlacking::DataTools

Inherits:
Object
  • Object
show all
Defined in:
lib/whois_slacking.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#apikeyObject

Returns the value of attribute apikey.



108
109
110
# File 'lib/whois_slacking.rb', line 108

def apikey
  @apikey
end

#data_formatObject

Returns the value of attribute data_format.



107
108
109
# File 'lib/whois_slacking.rb', line 107

def data_format
  @data_format
end

Class Method Details

.save_task(project, task_id, task, user, current_state, accepted_at, options = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/whois_slacking.rb', line 123

def self.save_task(project, task_id, task, user, current_state, accepted_at, options={})

  # dont use accepted_at
  accepted_at = accepted_at 
  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, 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 #   -- if task/username combo exists and is not completed 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

      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 #{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}"
      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
      store[mkey] = task_entity 

    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" #     -- if task is not-completed in pivotal
        #       -- 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 = "Today #{user} started #{task}"

      elsif current_state == "finished" #     -- 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} finished #{task}"
      end

    end
  end
  WhoIsSlacking::SlackWrapper.post_to_slack(message) if message
end

.whois_key(project, task, user) ⇒ Object



189
190
191
# File 'lib/whois_slacking.rb', line 189

def self.whois_key(project, task, user)
  "#{project}-#{task}-#{user}"
end

.whois_store(store_type = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/whois_slacking.rb', line 110

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