Class: Hubtime::Activity

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

Defined Under Namespace

Classes: Day, Forever, Month, Period, Year

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli, username, num_months) ⇒ Activity

Returns a new instance of Activity.



281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/hubtime/activity.rb', line 281

def initialize(cli, username, num_months)
  num_months = num_months.to_i
  num_months = 12 if num_months <= 0
  username ||= HubConfig.user
  raise("Need github user name. Use hubtime auth") unless username

  Time.zone = "Pacific Time (US & Canada)"  # TODO: command to allow this being set
  @cli = cli
  @username = username
  @start_time = (num_months-1).months.ago.beginning_of_month
  @end_time = Time.zone.now.end_of_month

  @time = Forever.load(username, start_time, end_time)
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



280
281
282
# File 'lib/hubtime/activity.rb', line 280

def end_time
  @end_time
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



280
281
282
# File 'lib/hubtime/activity.rb', line 280

def start_time
  @start_time
end

#usernameObject (readonly)

Returns the value of attribute username.



280
281
282
# File 'lib/hubtime/activity.rb', line 280

def username
  @username
end

Instance Method Details

#compile!Object



296
297
298
299
300
301
302
303
304
305
# File 'lib/hubtime/activity.rb', line 296

def compile!
  unless @time.compiled?
    GithubService.owner.commits(username, start_time, end_time) do |commit|
      @time.add(commit)
    end
    puts "... compiling data for #{username}"
    @time.compile!(nil)
    @time.store!
  end
end

#default_week(keys) ⇒ Object



396
397
398
399
400
401
402
# File 'lib/hubtime/activity.rb', line 396

def default_week(keys)
  default_week = {}
  keys.each do |key|
    default_week[key] = {"data" => 0}
  end
  default_week
end

#graph(type = :impact, stacked = false) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/hubtime/activity.rb', line 404

def graph(type = :impact, stacked=false)
  compile!

  type = type.to_s
  other = "count"
  other = "impact" if type == "count"

  others = []
  labels = []

  data   = {}
  if stacked
    keys = @time.repositories
  else
    keys = ["total"]
  end

  keys.each do |key|
    data[key] = []
  end

  week = default_week(keys)
  week_other = 0
  week_label = nil
  day = 0
  @time.each(:day) do |label, period|
    day += 1
    week_label ||= label
    keys.each do |key|
      week[key]["data"]  += period.send(type, key)
    end
    week_other += period.send(other)

    if (day % 7) == 0
      keys.each do |key|
        data[key] << week[key]["data"]
      end
      others << week_other

      if (day % 28) == 0
        labels << week_label
      else
        labels << ""
      end

      week = default_week(keys)
      week_other = 0
      week_label = nil
    end
  end

  charts = File.join(File.dirname(__FILE__), "charts")
  template = File.read(File.join(charts, "graph.erb"))
  template = Erubis::Eruby.new(template)
  html = template.result(:data => data, :other => others, :labels => labels, :data_type => type, :other_type => other, :username => username)

  root = File.join(File.expand_path("."), "data", "charts")
  path = "#{username}-graph-#{type}-#{start_time.to_i}-#{end_time.to_i}.html"
  file_name = File.join(root, path)
  directory = File.dirname(file_name)
  FileUtils.mkdir_p(directory) unless File.exist?(directory)
  File.open(file_name, 'w') {|f| f.write(html) }
  file_name
end

#impactObject



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/hubtime/activity.rb', line 343

def impact
  compile!

  additions = []
  deletions = []
  impacts   = [] 
  labels    = []

  week_additions = 0
  week_deletions = 0
  week_label = nil

  total_impact = 0

  day = 0
  @time.each(:day) do |label, period|
    day += 1
    week_label ||= label
    week_additions += period.additions
    week_deletions += period.deletions
    total_impact   += period.impact

    if (day % 7) == 0
      additions << week_additions
      deletions << week_deletions
      impacts   << total_impact

      if (day % 28) == 0
        labels << week_label
      else
        labels << ""
      end

      week_additions = 0
      week_deletions = 0
      week_label = nil
    end
  end

  charts = File.join(File.dirname(__FILE__), "charts")
  template = File.read(File.join(charts, "impact.erb"))
  template = Erubis::Eruby.new(template)
  html = template.result(:additions => additions, :deletions => deletions, :impacts => impacts, :labels => labels, :username => username)

  root = File.join(File.expand_path("."), "data", "charts")
  path = "#{username}-impact-#{start_time.to_i}-#{end_time.to_i}.html"
  file_name = File.join(root, path)
  directory = File.dirname(file_name)
  FileUtils.mkdir_p(directory) unless File.exist?(directory)
  File.open(file_name, 'w') {|f| f.write(html) }
  file_name
end

#pie(type = :impact) ⇒ Object



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/hubtime/activity.rb', line 469

def pie(type = :impact)
  compile!

  type = type.to_s
  data   = []

  @time.repositories.each do |repo_name|
    value = @time.send(type, repo_name)
    data << [repo_name, value] if value > 0
  end

  charts = File.join(File.dirname(__FILE__), "charts")
  template = File.read(File.join(charts, "pie.erb"))
  template = Erubis::Eruby.new(template)
  html = template.result(:data => data, :data_type => type, :username => username)

  root = File.join(File.expand_path("."), "data", "charts")
  path = "#{username}-pie-#{type}-#{start_time.to_i}-#{end_time.to_i}.html"
  file_name = File.join(root, path)
  directory = File.dirname(file_name)
  FileUtils.mkdir_p(directory) unless File.exist?(directory)
  File.open(file_name, 'w') {|f| f.write(html) }
  file_name
end

#spark(unit = :month, type = :impact) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/hubtime/activity.rb', line 317

def spark(unit = :month, type = :impact)
  compile!
  data = []
  @time.each(unit) do |label, period|
    data << period.send(type)
  end

  ticks=%w[             ]

  return "" if data.size == 0
  return ticks.last if data.size == 1

  distance = data.max.to_f / ticks.size

  str = ''
  data.each do |val|
    if val == 0
      str << " "
    else
      tick = (val / distance).round - 1
      str << ticks[tick]
    end
  end
  str
end

#table(unit = :month) ⇒ Object



307
308
309
310
311
312
313
314
315
# File 'lib/hubtime/activity.rb', line 307

def table(unit = :month)
  compile!
  table = Terminal::Table.new(:headings => [unit.to_s.titleize, 'Commits', 'Impact', 'Additions', 'Deletions'])

  @time.each(unit) do |label, period|
    table.add_row [label, period.count, period.impact, period.additions, period.deletions]
  end
  table
end