diff --git a/example.json b/example.json new file mode 100644 index 0000000000000000000000000000000000000000..4c887006a34d8220c203731c7f03ded3d6d9e54b --- /dev/null +++ b/example.json @@ -0,0 +1,32 @@ +[ + { + "userid": "user1", + "url": "http://www.someamazingwebsite.com/1", + "type": "POST", + "timestamp": 1360662163000 + }, + { + "userid": "user2", + "url": "http://www.someamazingwebsite.com/2", + "type": "GET", + "timestamp": 1360662163000 + }, + { + "userid": "user2", + "url": "http://www.someamazingwebsite.com/1", + "type": "GET", + "timestamp": 1360662163000 + }, + { + "userid": "user2", + "url": "http://www.someamazingwebsite.com/1", + "type": "GET", + "timestamp": 1360462163000 + }, + { + "userid": "user2", + "url": "http://www.someamazingwebsite.com/3", + "type": "POST", + "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..de079339eb73be6df7f869159b5228254759b755 --- /dev/null +++ b/main.go @@ -0,0 +1,66 @@ +/* +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 ( + "log" + "os" + "sync" +) + +type Load struct { + Userid string `json:"userid"` + Url string `json:"url"` + TypeRequest string `json:"type"` + Timestamp int64 `json:"timestamp"` +} + +type URLs struct { + URL map[string]*Dates + mux sync.RWMutex +} +type Dates struct { + Exists map[string]*Visits +} + +type Visits struct { + mux sync.RWMutex + Counter int + UserIds map[string]bool +} + +func main() { + //Check if the usage of the script has been correct. + if len(os.Args) != 2 { + log.Println("USAGE: ./json-utility [path/to/file]") + return + } + openFile(os.Args[1]) +} + +func openFile(argument string) { + jsonFile, err := os.Open(argument) + if err != nil { + log.Println(err) + return + } + log.Println("Successfully Opened", argument) + //Close when done. + defer jsonFile.Close() + result := handler(jsonFile) + + for k, v := range result.URL { + + log.Println("For URL: ", k) + + for m, x := range v.Exists { + log.Println("↳", m, "there are:", x.Counter, "unique visits.") + } + + } +}