Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
J
json-utility
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ivanov, Ivan (UG - Computer Science)
json-utility
Commits
f4e5c410
Commit
f4e5c410
authored
4 years ago
by
Ivan Ivanov
Browse files
Options
Downloads
Patches
Plain Diff
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
!1
Initial POC
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
example.json
+2
-2
2 additions, 2 deletions
example.json
main.go
+21
-74
21 additions, 74 deletions
main.go
with
23 additions
and
76 deletions
example.json
+
2
−
2
View file @
f4e5c410
...
@@ -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"
:
"
GE
T"
,
"type"
:
"
POS
T"
,
"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"
:
"
GE
T"
,
"type"
:
"
POS
T"
,
"timestamp"
:
1360462163000
"timestamp"
:
1360462163000
}
}
]
]
\ No newline at end of file
This diff is collapsed.
Click to expand it.
main.go
+
21
−
74
View file @
f4e5c410
...
@@ -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
.
Print
ln
(
"↳
"
,
m
,
"there are:"
,
x
.
Counter
,
"unique visits."
)
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment