Skip to content

Instantly share code, notes, and snippets.

@yoxisem544
Created June 11, 2016 05:21
Show Gist options
  • Select an option

  • Save yoxisem544/2a43996aa3b3426f5cc25dd0f6518914 to your computer and use it in GitHub Desktop.

Select an option

Save yoxisem544/2a43996aa3b3426f5cc25dd0f6518914 to your computer and use it in GitHub Desktop.
require 'json'
require "pp"
require 'csv'
require "time"
file = File.read('./Questionare.json')
data_hash = JSON.parse(file)
if data_hash
pp("json has data")
end
temp_csv_data = {}
data_hash["results"].each do |data|
# 根據ID建立新的KEY
if temp_csv_data[data["userUUID"]].nil?
temp_csv_data[data["userUUID"]] = []
end
if data["morningStamp"] and data["noonStamp"] and data["nightStamp"]
# Fully finished questionare.
a = [
# morning
data["expectedQuantitativeJobDemand1"],
data["expectedQuantitativeJobDemand2"],
data["expectedQuantitativeJobDemand3"],
data["expectedQuantitativeJobDemand4"],
data["expectedQuantitativeJobDemand5"],
data["expectedEmotionalJobDemand1"],
data["expectedEmotionalJobDemand2"],
data["expectedEmotionalJobDemand3"],
data["expectedEmotionalJobDemand4"],
data["expectedEmotionalJobDemand5"],
# second part
data["authoritarianLeadershipNeeded1"],
data["authoritarianLeadershipNeeded2"],
data["authoritarianLeadershipNeeded3"],
data["authoritarianLeadershipNeeded4"],
data["authoritarianLeadershipNeeded5"],
data["benevolentLeadershipNeeded1"],
data["benevolentLeadershipNeeded2"],
data["benevolentLeadershipNeeded3"],
data["benevolentLeadershipNeeded4"],
data["benevolentLeadershipNeeded5"],
# noon
data["authoritarianLeadershipReceived1"],
data["authoritarianLeadershipReceived2"],
data["authoritarianLeadershipReceived3"],
data["authoritarianLeadershipReceived4"],
data["authoritarianLeadershipReceived5"],
data["benevolentLeadershipReceived1"],
data["benevolentLeadershipReceived2"],
data["benevolentLeadershipReceived3"],
data["benevolentLeadershipReceived4"],
data["benevolentLeadershipReceived5"],
# night
data["positiveAffect1"],
data["positiveAffect2"],
data["positiveAffect3"],
data["positiveAffect4"],
data["positiveAffect5"],
data["positiveAffect6"],
data["positiveAffect7"],
data["positiveAffect8"],
data["positiveAffect9"],
data["positiveAffect10"],
data["negativeAffect1"],
data["negativeAffect2"],
data["negativeAffect3"],
data["negativeAffect4"],
data["negativeAffect5"],
data["negativeAffect6"],
data["negativeAffect7"],
data["negativeAffect8"],
data["negativeAffect9"],
data["negativeAffect10"],
data["jobSatisfaction1"],
data["jobSatisfaction2"],
data["jobSatisfaction3"],
data["jobSatisfaction4"],
data["jobSatisfaction5"],
data["supervisorDirectedOCB1"],
data["supervisorDirectedOCB2"],
data["supervisorDirectedOCB3"],
data["supervisorDirectedOCB4"],
data["updatedAt"]
]
temp_csv_data[data["userUUID"]] << a
end
end
sorted_temp_csv_data = {}
temp_csv_data.each do |key, data|
if data.length >= 10
pp "=====", key, data.length
aa = data.map.sort { |a, b|
ad = DateTime.strptime(a.last, '%Y-%m-%dT%H:%M:%S')
bd = DateTime.strptime(b.last, '%Y-%m-%dT%H:%M:%S')
bd <=> ad
# b.last <=> a.last
}
shit = []
aa.each do |aaaa|
# pp aaaa.last
shit << aaaa.last
end
# pp shit
# shit.map(&Time.method(:parse)).sort.last
shit.map.sort { |a, b|
ad = DateTime.strptime(a, '%Y-%m-%dT%H:%M:%S')
bd = DateTime.strptime(b, '%Y-%m-%dT%H:%M:%S')
bd <=> ad
}
# pp shit
pp "======="
data = aa
sorted_temp_csv_data[key] = aa
end
end
CSV.open("questionare.csv", "w") do |csv|
sorted_temp_csv_data.each do |key, data|
if data.length >= 10
a = [key]
pp key
data.each do |d|
a = a + d
pp d.last
end
csv << a
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment