From f4e5c410d4cb1934a239b103313c250a3ae3290a Mon Sep 17 00:00:00 2001 From: Ivan Ivanov <ivanivanov@Ivans-MacBook-Air.local> Date: Mon, 29 Mar 2021 13:11:11 +0100 Subject: [PATCH] First proof of concept. Working with example.json file. Everything stored in different strucutres. Initial testing started. --- example.json | 4 +-- main.go | 95 ++++++++++++---------------------------------------- 2 files changed, 23 insertions(+), 76 deletions(-) diff --git a/example.json b/example.json index 9e7b846..4c88700 100644 --- a/example.json +++ b/example.json @@ -2,7 +2,7 @@ { "userid": "user1", "url": "http://www.someamazingwebsite.com/1", - "type": "GET", + "type": "POST", "timestamp": 1360662163000 }, { @@ -26,7 +26,7 @@ { "userid": "user2", "url": "http://www.someamazingwebsite.com/3", - "type": "GET", + "type": "POST", "timestamp": 1360462163000 } ] \ No newline at end of file diff --git a/main.go b/main.go index 358d9d8..de07933 100644 --- a/main.go +++ b/main.go @@ -8,12 +8,9 @@ timestamp: The timestamp for when the action occurred package main import ( - "encoding/json" "log" - "net/http" - "time" - - "github.com/gorilla/mux" + "os" + "sync" ) type Load struct { @@ -25,94 +22,44 @@ type Load struct { 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() { - - router := mux.NewRouter().StrictSlash(true) - router.HandleFunc("/", Index) - - log.Fatal(http.ListenAndServe(":8080", router)) + //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 Index(w http.ResponseWriter, r *http.Request) { - decoder := json.NewDecoder(r.Body) - - var loads []Load - visited := URLs{URL: make(map[string]*Dates)} - - err := decoder.Decode(&loads) +func openFile(argument string) { + jsonFile, err := os.Open(argument) 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 { - 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)) - } + for k, v := range result.URL { - // loop over elements of slice - for k, v := range visited.URL { - // m is a map[string]interface. - // loop over keys and values in the map. + log.Println("For URL: ", k) - log.Println(k, " has these records:") 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.") } } -- GitLab