diff --git a/example.json b/example.json new file mode 100644 index 0000000000000000000000000000000000000000..8d7ccf38535c60585a8d8f8eca202b55cab2bcb7 --- /dev/null +++ b/example.json @@ -0,0 +1,32 @@ +[ + { + "userid": "user1", + "url": "http://www.someamazingwebsite.com/1", + "type": "GET", + "timestamp": 1360662163000 + }, + { + "userid": "user2", + "url": "http://www.someamazingwebsite.com/2", + "type": "GET", + "timestamp": 1360662163000 + }, + { + "userid": "user2", + "url": "http://www.someamazingwebsite.com/3", + "type": "GET", + "timestamp": 1360662163000 + }, + { + "userid": "user2", + "url": "http://www.someamazingwebsite.com/1", + "type": "GET", + "timestamp": 1360462163000 + }, + { + "userid": "user2", + "url": "http://www.someamazingwebsite.com/2", + "type": "GET", + "timestamp": 1360462163000 + } +] \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..ab33724913633c1d3e43c43f0d3c836148e705a9 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module json-utility + +go 1.16 + +require github.com/gorilla/mux v1.8.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000000000000000000000000000000000000..535028803d222b0e4e9174f56529c0ed9fece4e0 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= diff --git a/main.go b/main.go new file mode 100644 index 0000000000000000000000000000000000000000..eba4f1321bb5e3908de9fa594f08477869ba4cff --- /dev/null +++ b/main.go @@ -0,0 +1,116 @@ +/* +Example payload: +userid: A unique ID representing the user +url : The URL the visitor has accessed +type: The HTTP method used to access the URL +timestamp: The timestamp for when the action occurred +*/ +package main + +import ( + "encoding/json" + "log" + "net/http" + "time" + + "github.com/gorilla/mux" +) + +type Load struct { + Userid string `json:"userid"` + Url string `json:"url"` + TypeRequest string `json:"type"` + Timestamp int64 `json:"timestamp"` +} + +type Dates struct { + Exists map[string]*URLs +} +type URLs struct { + Exists map[string]*URL +} + +type URL struct { + Counter int + UserIds map[string]bool +} + +func main() { + + router := mux.NewRouter().StrictSlash(true) + router.HandleFunc("/", Index) + + log.Fatal(http.ListenAndServe(":8080", router)) +} + +func Index(w http.ResponseWriter, r *http.Request) { + decoder := json.NewDecoder(r.Body) + + var loads []Load + dates := Dates{Exists: make(map[string]*URLs)} + + err := decoder.Decode(&loads) + if err != nil { + panic(err) + } + + for _, load := range loads { + if load.TypeRequest != "GET" { + log.Println("Request is invalid") + continue + } + + var t string + + if time.Now().UnixNano() > load.Timestamp { + t = time.Unix(load.Timestamp/1000, 0).Format("2006-01-02") + } else { + t = time.Unix(load.Timestamp/1000, 0).Format("2006-01-02") + } + log.Println(t) + if site, ok := dates.Exists[t]; ok { + log.Println("Date exists.") + + if url, ok := site.Exists[load.Url]; ok { + log.Println("URL exists.") + if url.UserIds[load.Userid] { + continue //Continue as the user has already visited that website for this day once. + } else { + + url.Counter += 1 + url.UserIds[load.Userid] = true + log.Println(url.Counter) + } + } else { + //Add the URL and a visit. + entry := URL{Counter: 0, UserIds: make(map[string]bool)} + + entry.Counter += 1 + entry.UserIds[load.Userid] = true + dates.Exists[t].Exists[load.Url] = &entry + log.Println("URL is added.") + } + } else { + log.Println("Date doesnt yet exist.") + //Initialising Date and Urls for that date. + entry := URL{Counter: 0, UserIds: make(map[string]bool)} + urls := URLs{Exists: make(map[string]*URL)} + + entry.Counter += 1 + entry.UserIds[load.Userid] = true + urls.Exists[load.Url] = &entry + dates.Exists[t] = &urls + log.Println("We added it though") + } + log.Println(dates) + //fmt.Fprintf(w, "%q", json.NewEncoder(w).Encode(load)) + } + + // loop over elements of slice + for k, v := range dates.Exists { + // m is a map[string]interface. + // loop over keys and values in the map. + log.Println(k, "value is", v) + + } +}