require 'commander' require 'csv' require_relative './environments' require_relative './fetch_emails' require 'renuo/cli/app/toggl/workspace' require 'renuo/cli/app/toggl/detail' require 'renuo/cli/app/toggl/user' require 'terminal-table' require 'colorize' require 'active_support/core_ext/numeric/time' class TogglRedmineComparator class << self def call(days_behind = 7) report = {} since_date = days_behind.days.before(Date.yesterday).strftime('%F') until_date = Date.yesterday.strftime('%F') extract_redmine(report, since_date, until_date) extract_toggl(report, since_date, until_date) report = report.sort.reverse.to_h print_table(report) report end private def print_table(report) rows = [] report.each do |date, value| rows << colorize_table_row(date, value) rows << :separator end rows.pop table = Terminal::Table.new headings: %w[Day Redmine Toggl].map(&:cyan), rows: rows, style: { padding_left: 2, padding_right: 2, border_x: '-'.blue, border_y: '|'.blue, border_i: '+'.blue } puts table end def colorize_table_row(date, value) printed_day = date.strftime('%F %a') printed_redmine = to_time(value[:redmine]) printed_toggl = to_time(value[:toggl]) colorize_method = colorization_for_value(value) [printed_day, printed_redmine, printed_toggl].map { |v| v.colorize(colorize_method) } end def colorization_for_value(value) if more_toggl?(value) :red elsif more_redmine?(value) :default else :green end end def extract_redmine(report, since_date, _until_date) encoded_body = perform_redmine_call(since_date) csv = convert_redmine_csv(encoded_body) csv.each do |date, entry| report[Date.parse(date)] ||= default_value report[Date.parse(date)][:redmine] = to_seconds(entry) if entry.present? end end def convert_redmine_csv(encoded_body) separated_csv_entries = CSV.parse(encoded_body, col_sep: ',') keys = separated_csv_entries.shift[1..-2] entries = separated_csv_entries.shift[1..-2] keys.zip(entries) end def perform_redmine_call(since_date) query = generate_redmine_query(since_date) url = URI("https://redmine.renuo.ch/time_entries/report.csv?#{query}") req = Net::HTTP::Get.new(url) req['X-Redmine-API-Key'] = RenuoCliConfig.redmine_api_key response = Net::HTTP.start(url.hostname, url.port, use_ssl: true) { |http| http.request(req) } response.body.force_encoding('ISO-8859-1').encode('UTF-8') end def generate_redmine_query(since_date) URI.encode_www_form( [['utf8', '✓'], ['criteria[]', 'user'], ['f[]', 'spent_on'], ['f[]', 'user_id'], ['op[spent_on]', '>='], ['op[user_id]', '='], ['v[spent_on][]', since_date], ['v[user_id][]', 'me'], ['f[]', ''], ['c[]', 'project'], ['c[]', 'spent_on'], ['c[]', 'user'], ['c[]', 'activity'], ['c[]', 'issue'], ['c[]', 'comments'], ['c[]', 'hours'], %w[columns day], ['criteria[]', '']] ) end def extract_toggl(report, since_date, until_date) user_id = Toggl::User.me.id workspace_ids = Toggl::Workspace.all.map(&:id) workspace_ids.each do |workspace_id| time_entries = Toggl::Detail.where(since: since_date, until: until_date, user_agent: 'renuo-cli', workspace_id: workspace_id, user_ids: user_id) parse_toggl_entries(report, time_entries) end end def parse_toggl_entries(report, time_entries) time_entries .reject { |te| te.end.nil? } .group_by { |time_entry| Date.parse(time_entry.end) } .each do |date, grouped_time_entries| report[date] ||= default_value report[date][:toggl] += grouped_time_entries.sum(&:dur) end end def default_value { redmine: 0.0, toggl: 0.0 } end def to_time(value) sec = value / 1000.0 min, _sec = sec.divmod(60.0) hour, min = min.divmod(60.0) format('%02d:%02d', hour, min) end def to_seconds(value) hours, minutes = value.to_d.divmod(1.0) (hours * 60 * 60 * 1000) + (minutes * 60 * 60 * 1000) end def non_working_day?(value) [value[:redmine], value[:toggl]].all?(&:zero?) end BUFFER = 20_000 def more_toggl?(value) (value[:toggl] - value[:redmine]) > BUFFER end def more_redmine?(value) (value[:redmine] - value[:toggl]) > BUFFER end end end