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

First proof of concept. Working with example.json file. Everything

stored in different strucutres. Initial testing started.
parent 12b546a6
Branches Chude_Chiamaka
No related tags found
1 merge request!1Initial POC
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
{ {
"userid": "user1", "userid": "user1",
"url": "http://www.someamazingwebsite.com/1", "url": "http://www.someamazingwebsite.com/1",
"type": "GET", "type": "POST",
"timestamp": 1360662163000 "timestamp": 1360662163000
}, },
{ {
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
{ {
"userid": "user2", "userid": "user2",
"url": "http://www.someamazingwebsite.com/3", "url": "http://www.someamazingwebsite.com/3",
"type": "GET", "type": "POST",
"timestamp": 1360462163000 "timestamp": 1360462163000
} }
] ]
\ No newline at end of file
...@@ -8,12 +8,9 @@ timestamp: The timestamp for when the action occurred ...@@ -8,12 +8,9 @@ timestamp: The timestamp for when the action occurred
package main package main
import ( import (
"encoding/json"
"log" "log"
"net/http" "os"
"time" "sync"
"github.com/gorilla/mux"
) )
type Load struct { type Load struct {
...@@ -25,94 +22,44 @@ type Load struct { ...@@ -25,94 +22,44 @@ type Load struct {
type URLs struct { type URLs struct {
URL map[string]*Dates URL map[string]*Dates
mux sync.RWMutex
} }
type Dates struct { type Dates struct {
Exists map[string]*Visits Exists map[string]*Visits
} }
type Visits struct { type Visits struct {
mux sync.RWMutex
Counter int Counter int
UserIds map[string]bool UserIds map[string]bool
} }
func main() { func main() {
//Check if the usage of the script has been correct.
router := mux.NewRouter().StrictSlash(true) if len(os.Args) != 2 {
router.HandleFunc("/", Index) log.Println("USAGE: ./json-utility [path/to/file]")
return
log.Fatal(http.ListenAndServe(":8080", router)) }
openFile(os.Args[1])
} }
func Index(w http.ResponseWriter, r *http.Request) { func openFile(argument string) {
decoder := json.NewDecoder(r.Body) jsonFile, err := os.Open(argument)
var loads []Load
visited := URLs{URL: make(map[string]*Dates)}
err := decoder.Decode(&loads)
if err != nil { if err != nil {
panic(err) log.Println(err)
return
} }
log.Println("Successfully Opened", argument)
//Close when done.
defer jsonFile.Close()
result := handler(jsonFile)
for _, load := range loads { for k, v := range result.URL {
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 date, ok := visited.URL[load.Url]; ok {
log.Println("URL exists.")
if visit, ok := date.Exists[t]; ok {
log.Println("Date exists.")
if visit.UserIds[load.Userid] {
continue //Continue as the user has already visited that website for this day once.
} else {
visit.Counter += 1
visit.UserIds[load.Userid] = true
log.Println(visit.Counter)
}
} else {
//Add the Date and a Visit.
entry := Visits{Counter: 0, UserIds: make(map[string]bool)}
entry.Counter += 1
entry.UserIds[load.Userid] = true
visited.URL[load.Url].Exists[t] = &entry
log.Println("Date and Visit is added.")
}
} else {
log.Println("URL doesn't exist.")
//Initialising URL, date and visits for that date.
entry := Visits{Counter: 0, UserIds: make(map[string]bool)}
dates := Dates{Exists: make(map[string]*Visits)}
entry.Counter += 1
entry.UserIds[load.Userid] = true
dates.Exists[t] = &entry
visited.URL[load.Url] = &dates
log.Println("We added it though")
}
log.Println(visited)
//fmt.Fprintf(w, "%q", json.NewEncoder(w).Encode(load))
}
// loop over elements of slice log.Println("For URL: ", k)
for k, v := range visited.URL {
// m is a map[string]interface.
// loop over keys and values in the map.
log.Println(k, " has these records:")
for m, x := range v.Exists { for m, x := range v.Exists {
log.Print(" For date: ", m, " there are: ", x.Counter, " unique visits.") log.Println("↳", m, "there are:", x.Counter, "unique visits.")
} }
} }
......
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