Browse Source

cluster age

master
arnaucode 7 years ago
parent
commit
1d0fcbef47
3 changed files with 29 additions and 4 deletions
  1. +5
    -3
      mongodbModels.go
  2. +4
    -1
      serverRoutes.go
  3. +20
    -0
      userOperations.go

+ 5
- 3
mongodbModels.go

@ -1,9 +1,11 @@
package main
type UserModel struct {
ID string `json:"id"`
Age int `json:",string"`
Actions []string
ID string `json:"id"`
Age int `json:",string"`
//age clusters: a<20 (0), 20<a<40 (1), 40<a<60 (2), 60<a (3)
AgeCluster int
Actions []string
}
type ItemModel struct {
ID string

+ 4
- 1
serverRoutes.go

@ -51,11 +51,14 @@ func NewUser(w http.ResponseWriter, r *http.Request) {
err := decoder.Decode(&newUser)
check(err)
defer r.Body.Close()
newUser = user.clusterAge(newUser)
saveUser(userCollection, newUser)
fmt.Println(newUser)
fmt.Fprintln(w, "new user added: ", newUser.ID)
jNewUser, err := json.Marshal(newUser)
check(err)
fmt.Fprintln(w, string(jNewUser))
}
func Recommendations(w http.ResponseWriter, r *http.Request) {
ipFilter(w, r)

+ 20
- 0
userOperations.go

@ -0,0 +1,20 @@
package main
var user UserModel
func (userModel UserModel) clusterAge(u UserModel) UserModel {
if u.Age < 15 {
u.AgeCluster = 0
} else if 15 < u.Age && u.Age <= 25 {
u.AgeCluster = 1
} else if 25 < u.Age && u.Age <= 40 {
u.AgeCluster = 2
} else if 40 < u.Age && u.Age <= 50 {
u.AgeCluster = 3
} else if 50 < u.Age && u.Age <= 60 {
u.AgeCluster = 4
} else if 60 < u.Age {
u.AgeCluster = 5
}
return u
}

Loading…
Cancel
Save