Skip to content

Instantly share code, notes, and snippets.

@xr1337
Created August 10, 2020 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xr1337/d990697cc4f802b950a89df8bf0ad869 to your computer and use it in GitHub Desktop.
Save xr1337/d990697cc4f802b950a89df8bf0ad869 to your computer and use it in GitHub Desktop.
medical_records
package main
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"time"
)
func main() {
results := retrieveAllRecords()
var guest []int
for _, record := range results {
age, err := years(record.UserDob)
if err != nil {
continue
}
// hard code for now the requirements
if age < 20 || age > 30 {
continue
}
// hard code for now the requirements
if record.Vitals.BloodPressureDiastole-record.Vitals.BloodPressureSystole > 1 {
guest = append(guest, record.ID)
}
}
sort.Ints(guest)
fmt.Println(guest)
}
// Download all records in parallel
func retrieveAllRecords() []MedicalRecord {
var results []MedicalRecord
data := downloadPage(0)
done := make(chan *MedicalRecordResponse)
for page := 1; page <= data.TotalPages; page++ {
go func(page int) {
done <- downloadPage(page)
}(page)
}
for j := 1; j <= data.TotalPages; j++ {
outcome := <-done
results = append(results, outcome.Data...)
}
return results
}
// return age from dob string
func years(dob string) (float64, error) {
timeDob, err := time.Parse("02-01-2006", dob)
if err != nil {
return 0, err
}
delta := time.Now().Sub(timeDob)
years := delta.Seconds() / 31207680
return years, nil
}
func downloadPage(page int) *MedicalRecordResponse {
r, err := http.Get(fmt.Sprintf("https://jsonmock.hackerrank.com/api/medical_records?&page=%d", page))
if err != nil {
fmt.Println(err)
}
defer r.Body.Close()
var j *MedicalRecordResponse
err = json.NewDecoder(r.Body).Decode(&j)
if err != nil {
fmt.Println(err)
}
return j
}
// Json data structure format
type MedicalRecordResponse struct {
TotalPages int `json:"total_pages"`
Data []MedicalRecord `json:"data"`
}
type MedicalRecord struct {
ID int `json:"id"`
Timestamp int64 `json:"timestamp"`
Vitals struct {
BloodPressureDiastole int `json:"bloodPressureDiastole"`
BloodPressureSystole int `json:"bloodPressureSystole"`
Pulse int `json:"pulse"`
BreathingRate int `json:"breathingRate"`
BodyTemperature float32 `json:"bodyTemperature"`
} `json:"vitals"`
UserID int `json:"userId"`
UserName string `json:"userName"`
UserDob string `json:"userDob"`
}
type Patient struct {
Id int `json:"id"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment