Skip to content
Snippets Groups Projects
Commit 8155ad19 authored by Ivan Ivanov's avatar Ivan Ivanov
Browse files

initial try

parent 1d1d6070
No related branches found
No related tags found
1 merge request!1Initial POC
[
{
"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
go.mod 0 → 100644
module json-utility
go 1.16
require github.com/gorilla/mux v1.8.0
main.go 0 → 100644
/*
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)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment