Created
June 23, 2012 12:59
-
-
Save mneedham/2978253 to your computer and use it in GitHub Desktop.
thoughtworks colleague to colleague algorithm
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
| require 'rubygems' | |
| require 'neography' | |
| start_time = Time.now | |
| puts "Start Time: #{start_time}" | |
| @neo = Neography::Rest.new(:port => 7476) | |
| all_the_people_query = "start n = node(*) where n.type? = 'person' and has(n.name) return n.name, id(n)" | |
| all_the_people = @neo.execute_query(all_the_people_query)["data"] | |
| all_the_colleagues = [] | |
| person_count = 1 | |
| all_the_people.each do |name, id| | |
| puts "Person ##{person_count} (#{Time.now - start_time})" | |
| person_count = person_count + 1 | |
| puts "Processing #{name} (#{Time.now - start_time})" | |
| query = "START n=node(#{id}) MATCH n-[r:worked_on]->p<-[r2:worked_on]-o RETURN o.name, r.start_date, r2.start_date, r.end_date, r2.end_date" | |
| result = @neo.execute_query query | |
| puts "neo query done (#{Time.now - start_time})" | |
| colleagues = result["data"].select { |row| row[1] <= row[4] && row[3] >= row[2] }.group_by { |row| row[0] } | |
| colleagues_with_days = colleagues.map do |row| | |
| worked_together = row[1] | |
| days_worked_together = worked_together.inject(0) do |total, row| | |
| me_start = row[1] | |
| me_end = row[3] | |
| them_start = row[2] | |
| them_end = row[4] | |
| my_range = (Time.at(me_start).send(:to_date)..Time.at(me_end).send(:to_date)).to_a | |
| their_range = (Time.at(them_start).send(:to_date)..Time.at(them_end).send(:to_date)).to_a | |
| overlapping = my_range & their_range | |
| total + overlapping.select { |d| d <= DateTime.now }.size | |
| end | |
| {:name => row[0], :days => days_worked_together } | |
| end.select { |row| row[:days] > 0 } | |
| all_the_colleagues << { :id => id, :name => name, :colleagues => colleagues_with_days } | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about
select a project
Find the "Engagement nodes" or "worked-on" relationship with date properties: either way you want something like "Start date, End date, Consultant" : So, "23/1 23/7, Jen"
Generate permutations, so if you have tuples A B C (where A, B etc = [23/1 23/7 "Jen"]) you get AB AC BC
find the overlap days for each pair
Filter off zero overlap
transform into [Jen , Mark, 23/1 , 23 /5] - so the overlap period and the pair of consultants
Insert
Profit!!