From 8155ad198ef0c5b597b80696f2a5f5e0aec98109 Mon Sep 17 00:00:00 2001
From: Ivan Ivanov <ivanivanov@Ivans-MacBook-Air.local>
Date: Sat, 27 Mar 2021 17:18:11 +0000
Subject: [PATCH] initial try

---
 example.json |  32 ++++++++++++++
 go.mod       |   5 +++
 go.sum       |   2 +
 main.go      | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 155 insertions(+)
 create mode 100644 example.json
 create mode 100644 go.mod
 create mode 100644 go.sum
 create mode 100644 main.go

diff --git a/example.json b/example.json
new file mode 100644
index 0000000..8d7ccf3
--- /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 0000000..ab33724
--- /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 0000000..5350288
--- /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 0000000..eba4f13
--- /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)
+
+	}
+}
-- 
GitLab