Last active
August 24, 2016 07:50
-
-
Save tomkukral/5f3752f607759b915432debc497daa4f to your computer and use it in GitHub Desktop.
Watson frames to toggl time entries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'togglv8' | |
| require 'date' | |
| project = ARGV[0] | |
| token = ARGV[1] | |
| workspace_id = ARGV[2] | |
| date = ARGV[3] || Time.new.strftime("%Y-%m-%d") | |
| # watson commands | |
| watson_cmd="watson log --project #{ARGV[0]} --from #{date} --to #{date} | sed '1d'" | |
| watson_log=`#{watson_cmd}` | |
| puts "Watson cmd: #{watson_cmd}" | |
| # toggl | |
| api = TogglV8::API.new(token) | |
| api.debug(true) | |
| # read time entries | |
| toggl_time_entries = api.get_time_entries(start_date: "#{date} 00:00", end_date: "#{date} 23:59") | |
| puts toggl_time_entries | |
| time_entries = api.get_time_entries(start_date: "#{date} 00:00", end_date: "#{date} 23:59").map do |i| | |
| i['description'][0..6] | |
| end | |
| puts "Detected time entries:\n" + time_entries.join("\n") | |
| # read projects | |
| projects = {} | |
| api.projects(workspace_id).each{|i| projects[i['name']] = i['id']} | |
| # tag -> project mappings | |
| tags_projects = { | |
| web: 'Web FIT', | |
| one: 'infra.one', | |
| ceph: 'infra.ceph', | |
| gitlab: 'ICT.Gitlab', | |
| docker: 'infra.docker', | |
| net: 'infra.network', | |
| marast: 'Marast' | |
| } | |
| # send watson frames | |
| watson_log.split("\n").each do |line| | |
| line = line[1..-1].squeeze(' ') | |
| # read frame | |
| tags = if line[/\[.*?\]/].nil? | |
| [] | |
| else | |
| line[/\[.*?\]/].gsub(/[\[\] ]/, '').split(',') | |
| end | |
| frame = { | |
| id: line[0..6], | |
| task: line[0..6], | |
| start: Time.parse(date + ' ' + line[8..12]), | |
| stop: Time.parse(date + ' ' + line[17..21]), | |
| tags: tags | |
| } | |
| # try to predict project | |
| frame[:project] = nil | |
| Array(tags_projects).each do |tag, project| | |
| if frame[:tags].include? tag.to_s | |
| puts "tag #{tag} for project #{project} detected" | |
| frame[:project] = projects[project] | |
| break | |
| end | |
| end | |
| # add name from t_tag | |
| frame[:tags].each do |tag| | |
| if tag.match(/^t:/) | |
| # remove tag | |
| frame[:tags] -= [tag] | |
| # add to task name | |
| frame[:task] += ' ' + tag.gsub(/^t:/, '').gsub('_', ' ') | |
| end | |
| end | |
| puts frame | |
| # send frame to toggl | |
| api.create_time_entry({ | |
| 'description' => frame[:task], | |
| 'wid' => workspace_id, | |
| 'pid' => frame[:project], | |
| 'duration' => frame[:stop] - frame[:start], | |
| 'start' => frame[:start].strftime("%FT%T%:z"), | |
| 'created_with' => "send.rb", | |
| 'tags' => frame[:tags] | |
| }) unless time_entries.include? frame[:id] | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment