mirror of
https://github.com/netbirdio/netbird.git
synced 2025-06-19 17:31:39 +02:00
Merge pull request #1309 from netbirdio/fix/duplicated-entries-on-events-api
Fix duplicated Activity events shown
This commit is contained in:
commit
52b5a31058
@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
//eventSinkDB is the default name of the events database
|
// eventSinkDB is the default name of the events database
|
||||||
eventSinkDB = "events.db"
|
eventSinkDB = "events.db"
|
||||||
createTableQuery = "CREATE TABLE IF NOT EXISTS events " +
|
createTableQuery = "CREATE TABLE IF NOT EXISTS events " +
|
||||||
"(id INTEGER PRIMARY KEY AUTOINCREMENT, " +
|
"(id INTEGER PRIMARY KEY AUTOINCREMENT, " +
|
||||||
@ -28,22 +28,46 @@ const (
|
|||||||
creatTableDeletedUsersQuery = `CREATE TABLE IF NOT EXISTS deleted_users (id TEXT NOT NULL, email TEXT NOT NULL, name TEXT);`
|
creatTableDeletedUsersQuery = `CREATE TABLE IF NOT EXISTS deleted_users (id TEXT NOT NULL, email TEXT NOT NULL, name TEXT);`
|
||||||
|
|
||||||
selectDescQuery = `SELECT events.id, activity, timestamp, initiator_id, i.name as "initiator_name", i.email as "initiator_email", target_id, t.name as "target_name", t.email as "target_email", account_id, meta
|
selectDescQuery = `SELECT events.id, activity, timestamp, initiator_id, i.name as "initiator_name", i.email as "initiator_email", target_id, t.name as "target_name", t.email as "target_email", account_id, meta
|
||||||
FROM events
|
FROM events
|
||||||
LEFT JOIN deleted_users i ON events.initiator_id = i.id
|
LEFT JOIN (
|
||||||
LEFT JOIN deleted_users t ON events.target_id = t.id
|
SELECT id, MAX(name) as name, MAX(email) as email
|
||||||
|
FROM deleted_users
|
||||||
|
GROUP BY id
|
||||||
|
) i ON events.initiator_id = i.id
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT id, MAX(name) as name, MAX(email) as email
|
||||||
|
FROM deleted_users
|
||||||
|
GROUP BY id
|
||||||
|
) t ON events.target_id = t.id
|
||||||
WHERE account_id = ?
|
WHERE account_id = ?
|
||||||
ORDER BY timestamp DESC LIMIT ? OFFSET ?;`
|
ORDER BY timestamp DESC LIMIT ? OFFSET ?;`
|
||||||
|
|
||||||
selectAscQuery = `SELECT events.id, activity, timestamp, initiator_id, i.name as "initiator_name", i.email as "initiator_email", target_id, t.name as "target_name", t.email as "target_email", account_id, meta
|
selectAscQuery = `SELECT events.id, activity, timestamp, initiator_id, i.name as "initiator_name", i.email as "initiator_email", target_id, t.name as "target_name", t.email as "target_email", account_id, meta
|
||||||
FROM events
|
FROM events
|
||||||
LEFT JOIN deleted_users i ON events.initiator_id = i.id
|
LEFT JOIN (
|
||||||
LEFT JOIN deleted_users t ON events.target_id = t.id
|
SELECT id, MAX(name) as name, MAX(email) as email
|
||||||
|
FROM deleted_users
|
||||||
|
GROUP BY id
|
||||||
|
) i ON events.initiator_id = i.id
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT id, MAX(name) as name, MAX(email) as email
|
||||||
|
FROM deleted_users
|
||||||
|
GROUP BY id
|
||||||
|
) t ON events.target_id = t.id
|
||||||
WHERE account_id = ?
|
WHERE account_id = ?
|
||||||
ORDER BY timestamp ASC LIMIT ? OFFSET ?;`
|
ORDER BY timestamp ASC LIMIT ? OFFSET ?;`
|
||||||
|
|
||||||
insertQuery = "INSERT INTO events(activity, timestamp, initiator_id, target_id, account_id, meta) " +
|
insertQuery = "INSERT INTO events(activity, timestamp, initiator_id, target_id, account_id, meta) " +
|
||||||
"VALUES(?, ?, ?, ?, ?, ?)"
|
"VALUES(?, ?, ?, ?, ?, ?)"
|
||||||
|
|
||||||
|
/*
|
||||||
|
TODO:
|
||||||
|
The insert should avoid duplicated IDs in the table. So the query should be changes to something like:
|
||||||
|
`INSERT INTO deleted_users(id, email, name) VALUES(?, ?, ?) ON CONFLICT (id) DO UPDATE SET email = EXCLUDED.email, name = EXCLUDED.name;`
|
||||||
|
For this to work we have to set the id column as primary key. But this is not possible because the id column is not unique
|
||||||
|
and some selfhosted deployments might have duplicates already so we need to clean the table first.
|
||||||
|
*/
|
||||||
|
|
||||||
insertDeleteUserQuery = `INSERT INTO deleted_users(id, email, name) VALUES(?, ?, ?)`
|
insertDeleteUserQuery = `INSERT INTO deleted_users(id, email, name) VALUES(?, ?, ?)`
|
||||||
|
|
||||||
fallbackName = "unknown"
|
fallbackName = "unknown"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user