Class: Asanban::BulkLoad
- Inherits:
-
Object
- Object
- Asanban::BulkLoad
- Defined in:
- lib/asanban/bulk_load.rb
Class Method Summary collapse
-
.record_time(task_id, task_completed, task_deleted, start_story, start_milestone, end_story, end_milestone, collection) ⇒ Object
Visible For Testing.
- .run(config = nil) ⇒ Object
Class Method Details
.record_time(task_id, task_completed, task_deleted, start_story, start_milestone, end_story, end_milestone, collection) ⇒ Object
Visible For Testing
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/asanban/bulk_load.rb', line 149 def self.record_time(task_id, task_completed, task_deleted, start_story, start_milestone, end_story, end_milestone, collection) end_story_id = end_story["id"] start_story_id = start_story["id"] = Time.parse(start_story["created_at"]) = Time.parse(end_story["created_at"]) elapsed_time_seconds = - elapsed_days = elapsed_time_seconds / (60.0 * 60.0 * 24.0) day = "#{.year}-#{.month}-#{.day}" month = "#{.year}-#{.month}" year = .year.to_s record = {"day" => day, "month" => month, "year" => year, "task_id" => task_id, "date" => Time.parse(day), "task_completed" => task_completed, "task_deleted" => task_deleted, "start_milestone" => start_milestone, "end_milestone" => end_milestone, "start_story_id" => start_story_id, "end_story_id" => end_story_id, "elapsed_days" => elapsed_days} collection.remove("end_story_id" => end_story_id) collection.insert(record) record end |
.run(config = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/asanban/bulk_load.rb', line 10 def self.run(config = nil) unless config if !File.exists?('asana.yml') puts "Must specify configuration in asana.yml" exit(1) end config = YAML::load(File.open('asana.yml')) end api_key = config['asana_api_key'] workspace_id = config['asana_workspace_id'] project_id = config['asana_project_id'] mongodb_uri = config['mongodb_uri'] mongodb_dbname = config['mongodb_dbname'] if (ARGV.count < 1 || ARGV.count > 2) puts "Syntax is: bulkload {stage} [mode]" puts "{stage} is 'LOCAL' or 'PROD'" puts "[mode] can be 'tasks' (to create task data) or 'times' to create milestone and lead time data. Script will do both if not specified." exit(1) else if (ARGV[0].downcase == "local") conn = Mongo::Connection.new elsif (ARGV[0].downcase == "prod") conn = Mongo::Connection.from_uri(mongodb_uri) else puts "Invalid stage: #{ARGV[0]}" exit(1) end if (mode = ARGV[1]) && !['tasks', 'times'].include?(mode) puts "Invalid mode: #{mode}" exit(1) end end db = conn.db(mongodb_dbname) tasks_collection = db["tasks"] if (mode == 'tasks' || !mode) puts "Creating times..." uri = URI.parse("https://app.asana.com/api/1.0/projects/#{project_id}/tasks?opt_fields=id,name,assignee,assignee_status,created_at,completed,completed_at,due_on,followers,modified_at,name,notes,projects,parent") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER header = { "Content-Type" => "application/json" } path = "#{uri.path}?#{uri.query}" req = Net::HTTP::Get.new(path, header) req.basic_auth(api_key, '') res = http.start { |http| http.request(req) } tasks = JSON.parse(res.body) if tasks['errors'] puts "Server returned an error retrieving tasks: #{tasks['errors'][0]['message']}" else #old_tasks = tasks_collection.find({"projects.id" => project_id, "completed" => false}).to_a tasks_collection.update({}, {"$set" => {"old" => true}}, {:multi => true}) tasks['data'].each_with_index do |task, i| uri = URI.parse("https://app.asana.com/api/1.0/tasks/#{task['id']}/stories") req = Net::HTTP::Get.new(uri.path, header) req.basic_auth(api_key, '') res = http.start { |http| http.request(req) } stories = JSON.parse(res.body) if task['errors'] puts "Server returned an error retrieving stories: #{task['errors'][0]['message']}" else task["stories"] = stories['data'] end task["old"] = false tasks_collection.update({"id" => task['id']}, task, {:upsert => true}) puts "Created task: #{task['id']}" if (((i + 1) % 100) == 0) puts "Sleeping for one minute to avoid Asana's rate limit of 100 requests per minute" sleep 60 end end puts "Done creating times." end end if (mode == 'times' || !mode) puts "Creating milestone and lead time data..." milestone_times_collection = db["milestone_times"] lead_times_collection = db["lead_times"] # TODO: filter - complete_time = null or completed_time - now <= 24hours tasks_collection.find().each do |task| stories = task["stories"] || [] task_id = task["_id"] #TODO: Write a test... task_completed = task["completed"] task_deleted = task["old"] stories.each do |story| if (story['text'] =~ /Moved from (.*)\(\d+\) to (.*)\(\d+\)/i) start_milestone = $1.strip end_milestone = $2.strip = Time.parse(story["created_at"]) day = "#{.year}-#{.month}-#{.day}" month = "#{.year}-#{.month}" year = .year.to_s end_story_id = story["id"] escaped_milestone = start_milestone.gsub('(', '\\(').gsub(')', '\\)') if (start_story = stories.find {|s| s['text'] =~ /Moved .*to #{escaped_milestone}/i}) #TODO: Refactor to use record_time start_story_id = start_story["id"] = Time.parse(start_story["created_at"]) elapsed_time_seconds = - elapsed_days = elapsed_time_seconds / (60.0 * 60.0 * 24.0) milestone = {"day" => day, "month" => month, "year" => year, "task_id" => task_id, "date" => Time.parse(day), "task_completed" => task_completed, "task_deleted" => task_deleted, "start_milestone" => start_milestone, "end_milestone" => end_milestone, "start_story_id" => start_story_id, "end_story_id" => end_story_id, "elapsed_days" => elapsed_days} milestone_times_collection.remove("end_story_id" => end_story_id) milestone_times_collection.insert(milestone) puts "Inserted milestone: #{milestone}" else puts "Could not find time task entered #{start_milestone}" end end end if ((end_story = stories.find_all {|s| s['text'] =~ /Moved .*to #{config['asana_ending_milestone']}/i}[-1]) && (start_story = stories.find_all {|s| s['text'] =~ /Moved .*to #{config['asana_beginning_milestone']}/i}[0])) lead_times_collection.remove("end_story_id" => end_story["id"]) lead_time = record_time(task_id, task_completed, task_deleted, start_story, config['asana_beginning_milestone'], end_story, config['asana_ending_milestone'], lead_times_collection) puts "Inserted lead time: #{lead_time}" end end puts "Finished creating milestone and lead time data." end puts "Done!" end |