Last active
June 19, 2017 23:17
-
-
Save paxer/04cdf85e6f1fe950f2aee37b19ee8d9b to your computer and use it in GitHub Desktop.
Go inital version
This file contains 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
package main | |
import ( | |
"os" | |
"fmt" | |
"encoding/csv" | |
"io" | |
"time" | |
"strconv" | |
) | |
type SurveyQuestion struct { | |
Theme string | |
Type string | |
Text string | |
} | |
type RatingSurveyQuestion struct { | |
Question SurveyQuestion | |
Answers []int | |
AverageRating float32 | |
} | |
type SurveyResponse struct { | |
Email string | |
EmployeeId int | |
SubmittedAt time.Time | |
Responses []string | |
} | |
func main() { | |
RunReport() | |
} | |
func RunReport() { | |
surveyQuestionsFile, _ := os.Open("data/survey-2.csv") | |
defer surveyQuestionsFile.Close() | |
surveyResponsesFile, _ := os.Open("data/survey-2-big-responses.csv") | |
defer surveyResponsesFile.Close() | |
surveyQuestions := readSurveyQuestions(csv.NewReader(surveyQuestionsFile)) | |
surveyResponses := readSurveyResponses(csv.NewReader(surveyResponsesFile)) | |
totalCount := performTotalCounts(surveyResponses) | |
totalParticipantCount := performTotalParticipantCounts(surveyResponses) | |
totalParticipantPercentageCounts := performTotalParticipantPercentageCounts(totalCount, totalParticipantCount) | |
ratingSurveyQuestions := filterRatingQuestions(surveyQuestions, surveyResponses) | |
println(fmt.Sprintf("Total counts: %d", totalCount)) | |
println(fmt.Sprintf("Total participant counts: %d", totalParticipantCount)) | |
println(fmt.Sprintf("Total participant percentage counts: %.2f", totalParticipantPercentageCounts)) | |
println("Average Ratings:") | |
for _, q := range ratingSurveyQuestions { | |
println(fmt.Sprintf("\t%s: %.1f", q.Question.Text, q.AverageRating)) | |
} | |
} | |
func readSurveyQuestions(reader *csv.Reader) []SurveyQuestion { | |
lineCount := 0 | |
var surveyQuestions []SurveyQuestion | |
for { | |
record, err := reader.Read() | |
if err == io.EOF { | |
break | |
} | |
if lineCount > 0 { | |
q := SurveyQuestion{record[0], record[1], record[2]} | |
surveyQuestions = append(surveyQuestions, q) | |
} | |
lineCount += 1 | |
} | |
return surveyQuestions | |
} | |
func readSurveyResponses(reader *csv.Reader) []SurveyResponse { | |
lineCount := 0 | |
var surveyResponses []SurveyResponse | |
for { | |
record, err := reader.Read() | |
if err == io.EOF { | |
break | |
} | |
empoyeeId, err := strconv.Atoi(record[1]) | |
var submittedAt time.Time | |
if record[2] != "" { | |
parsedSubmittedAt, err := time.Parse(time.RFC3339, record[2]) | |
if err == nil { | |
submittedAt = parsedSubmittedAt | |
} | |
} | |
responses := record[3:] | |
q := SurveyResponse{record[0], empoyeeId, submittedAt, responses} | |
surveyResponses = append(surveyResponses, q) | |
lineCount += 1 | |
} | |
return surveyResponses | |
} | |
func performTotalParticipantCounts(surveyResponses []SurveyResponse) int { | |
count := 0 | |
for _, surveyResponse := range surveyResponses { | |
if !time.Time.IsZero(surveyResponse.SubmittedAt) { | |
count += 1 | |
} | |
} | |
return count | |
} | |
func performTotalCounts(surveyResponses []SurveyResponse) int { | |
return len(surveyResponses) | |
} | |
func performTotalParticipantPercentageCounts(totalCount int, totalParticipantCount int) float32 { | |
return 100 / float32(totalCount) * float32(totalParticipantCount) | |
} | |
func filterRatingQuestions(surveyQuestions []SurveyQuestion, surveyResponses []SurveyResponse) []RatingSurveyQuestion { | |
var surveyQuestionResponses []int | |
var ratingSurveyQuestions []RatingSurveyQuestion | |
// get only rating question indexes | |
for i, q := range surveyQuestions { | |
if q.Type == "ratingquestion" { | |
// grab all responses for rating questions only | |
for _, r := range surveyResponses { | |
responseRating, err := strconv.Atoi(r.Responses[i]) | |
if err == nil { | |
surveyQuestionResponses = append(surveyQuestionResponses, responseRating) | |
} | |
} | |
ratingSurveyQuestions = append(ratingSurveyQuestions, RatingSurveyQuestion{q, surveyQuestionResponses, average(surveyQuestionResponses)}) | |
surveyQuestionResponses = nil | |
} | |
} | |
return ratingSurveyQuestions | |
} | |
func average(array [] int) float32 { | |
sum := 0 | |
for _, v := range array { | |
sum += v | |
} | |
return float32(sum) / float32(len(array)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment