Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
usermetadb
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
id
usermetadb
Commits
f874b2d4
Commit
f874b2d4
authored
Mar 28, 2019
by
ale
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Quantize all timestamps stored in the db
Fixes issue
#1
.
parent
68195c87
Pipeline
#2639
failed with stages
in 5 minutes and 48 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
21 additions
and
12 deletions
+21
-12
server/lastlogin.go
server/lastlogin.go
+1
-1
server/lastlogin_test.go
server/lastlogin_test.go
+9
-4
server/server_test.go
server/server_test.go
+4
-2
server/userlog.go
server/userlog.go
+7
-5
No files found.
server/lastlogin.go
View file @
f874b2d4
...
...
@@ -71,7 +71,7 @@ func (l *lastloginDB) AddLastLogin(entry *usermetadb.LastLoginEntry) error {
args
:=
[]
interface
{}{
entry
.
Username
,
entry
.
Service
,
entry
.
Timestamp
,
roundTimestamp
(
entry
.
Timestamp
)
,
}
if
_
,
err
=
l
.
stmts
.
get
(
tx
,
stmt
)
.
Exec
(
args
...
);
err
!=
nil
{
...
...
server/lastlogin_test.go
View file @
f874b2d4
...
...
@@ -6,6 +6,7 @@ import (
"time"
"git.autistici.org/id/usermetadb"
"github.com/google/go-cmp/cmp"
)
func
generateLastLoginEntries
()
[]
*
usermetadb
.
LastLoginEntry
{
...
...
@@ -54,8 +55,10 @@ func TestLastlogin_LoginAdded(t *testing.T) {
t
.
Fatalf
(
"Expected exactly one entry, found %v"
,
out
)
}
if
*
in
[
0
]
!=
*
out
[
0
]
{
t
.
Fatalf
(
"Last login entries differ:
\n
Set %v
\n
Got %v"
,
*
in
[
0
],
*
out
[
0
])
// Modify the entries so that the quantized timestamp matches.
in
[
0
]
.
Timestamp
=
roundTimestamp
(
in
[
0
]
.
Timestamp
)
if
diffs
:=
cmp
.
Diff
(
in
[
0
],
out
[
0
]);
diffs
!=
""
{
t
.
Fatalf
(
"Last login entries differ:
\n
%s"
,
diffs
)
}
}
...
...
@@ -90,8 +93,10 @@ func TestLastLogin_MultipleServices(t *testing.T) {
}
for
i
:=
0
;
i
<
2
;
i
++
{
if
*
in
[
i
]
!=
*
out
[
i
]
{
t
.
Fatalf
(
"Last login entries #%d differ:
\n
Set %v
\n
Got %v"
,
i
,
*
in
[
i
],
*
out
[
i
])
// Modify the entries so that the quantized timestamp matches.
in
[
i
]
.
Timestamp
=
roundTimestamp
(
in
[
i
]
.
Timestamp
)
if
diffs
:=
cmp
.
Diff
(
in
[
0
],
out
[
0
]);
diffs
!=
""
{
t
.
Fatalf
(
"Last login entries #%d differ:
\n
%s"
,
i
,
diffs
)
}
}
}
...
...
server/server_test.go
View file @
f874b2d4
...
...
@@ -8,6 +8,7 @@ import (
"git.autistici.org/ai3/go-common/clientutil"
"git.autistici.org/id/usermetadb/client"
"github.com/google/go-cmp/cmp"
)
func
TestServer_AddLog
(
t
*
testing
.
T
)
{
...
...
@@ -69,12 +70,13 @@ func TestServer_AddLastLogin(t *testing.T) {
}
expResp
:=
*
entries
[
0
]
expResp
.
Timestamp
=
roundTimestamp
(
expResp
.
Timestamp
)
resp
,
err
:=
c
.
GetLastLogin
(
context
.
Background
(),
""
,
expResp
.
Username
,
expResp
.
Service
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
*
resp
[
0
]
!=
expResp
{
t
.
Fatalf
(
"Last login entries differ:
\n
Set %v
\n
Got %v"
,
expResp
,
*
resp
[
0
]
)
if
diffs
:=
cmp
.
Diff
(
resp
[
0
],
&
expResp
);
diffs
!=
""
{
t
.
Fatalf
(
"Last login entries differ:
\n
%s"
,
diffs
)
}
}
server/userlog.go
View file @
f874b2d4
...
...
@@ -164,7 +164,7 @@ func (u *userlogDB) Close() {
// Update or create an entry in the 'devices' table.
func
(
u
*
userlogDB
)
updateDeviceInfo
(
tx
*
sql
.
Tx
,
username
string
,
deviceInfo
*
auth
.
DeviceInfo
)
error
{
now
:=
time
.
Now
()
.
UTC
(
)
now
:=
roundTimestamp
(
time
.
Now
()
.
UTC
()
)
var
seen
bool
err
:=
u
.
stmts
.
get
(
tx
,
"check_device_info"
)
.
QueryRow
(
username
,
deviceInfo
.
ID
)
.
Scan
(
&
seen
)
...
...
@@ -333,17 +333,19 @@ func (u *userlogDB) GetUserDevices(username string) ([]*usermetadb.MetaDeviceInf
return
out
,
rows
.
Err
()
}
var
timestampRoundSeconds
int64
=
7200
const
timestampRoundSeconds
=
7200
func
roundTimestamp
(
t
time
.
Time
,
r
int64
)
time
.
Time
{
return
time
.
Unix
((
t
.
Unix
()
/
r
)
*
r
,
0
)
// Quantize timestamps to minimize time correlation possibilities.
func
roundTimestamp
(
t
time
.
Time
)
time
.
Time
{
// Keep the original timezone information.
return
time
.
Unix
((
t
.
Unix
()
/
timestampRoundSeconds
)
*
timestampRoundSeconds
,
0
)
.
In
(
t
.
Location
())
}
// Minimize a log entry, removing all the data that we don't want to
// store (even if it's already missing in the database schema).
func
minimizeLogEntry
(
e
*
usermetadb
.
LogEntry
)
{
// Round timestamps to the nearest even hour.
e
.
Timestamp
=
roundTimestamp
(
e
.
Timestamp
,
timestampRoundSeconds
)
e
.
Timestamp
=
roundTimestamp
(
e
.
Timestamp
)
// Drop fields in DeviceInfo that we don't want.
if
e
.
DeviceInfo
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment