summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDan Goodliffe <dan.goodliffe@octal.co.uk>2026-05-05 12:24:11 +0100
committerDan Goodliffe <dan.goodliffe@octal.co.uk>2026-05-05 12:24:11 +0100
commit200c3636b600ac2f997316612f1b1321112496cd (patch)
tree21a86b460e1dca04d5a403e56133383ab78881b6 /src
parentf9b4422198281410218e371ffc3e1e4513a9ba7f (diff)
downloadwebstat-200c3636b600ac2f997316612f1b1321112496cd.tar.bz2
webstat-200c3636b600ac2f997316612f1b1321112496cd.tar.xz
webstat-200c3636b600ac2f997316612f1b1321112496cd.zip
Only call entity insert handler if detail is null
Improves handling of entity inserts where the entity already exists and already has detail; does not call the onInsert handler. This avoids repeatedly fetching UA detail every time the UA is first seen by a process.
Diffstat (limited to 'src')
-rw-r--r--src/ingestor.cpp6
-rw-r--r--src/schema.sql22
-rw-r--r--src/sql/entityInsert.sql2
3 files changed, 23 insertions, 7 deletions
diff --git a/src/ingestor.cpp b/src/ingestor.cpp
index 3e36d85..fe8d38c 100644
--- a/src/ingestor.cpp
+++ b/src/ingestor.cpp
@@ -562,8 +562,10 @@ namespace WebStat {
assert(!entity.id);
const auto & [typeName, onInsert] = ENTITY_TYPE_VALUES[std::to_underlying(entity.type)];
- entity.id = insert(dbconn, SQL::ENTITY_INSERT, SQL::ENTITY_INSERT_OPTS, entity.value, typeName);
- if (onInsert) {
+ bool entityNullDetail = true;
+ std::tie(entity.id, entityNullDetail)
+ = insert<EntityId, bool>(dbconn, SQL::ENTITY_INSERT, SQL::ENTITY_INSERT_OPTS, entity.value, typeName);
+ if (onInsert && entityNullDetail) {
std::invoke(onInsert, this, entity);
}
stats.entitiesInserted += 1;
diff --git a/src/schema.sql b/src/schema.sql
index 698ea3e..056f7ac 100644
--- a/src/schema.sql
+++ b/src/schema.sql
@@ -43,14 +43,20 @@ CREATE TABLE entities(
CREATE UNIQUE INDEX uni_entities_value ON entities(MD5(value));
CREATE OR REPLACE FUNCTION entity(newValue text, newType entity)
- RETURNS integer
+ RETURNS TABLE(
+ id integer,
+ nulldetail boolean
+ )
AS $$
DECLARE
now timestamp without time zone;
recid integer;
+ nulldetail boolean;
BEGIN
IF newValue IS NULL THEN
- RETURN NULL;
+ RETURN query
+ VALUES (NULL,
+ NULL);
END IF;
INSERT INTO entities(value, type)
SELECT
@@ -66,16 +72,22 @@ BEGIN
ON CONFLICT
DO NOTHING
RETURNING
- id INTO recid;
+ entities.id,
+ entities.detail IS NULL INTO recid,
+ nulldetail;
IF recid IS NULL THEN
+ RETURN query
SELECT
- id INTO recid
+ id,
+ detail IS NULL
FROM
entities
WHERE
md5(value) = md5(newValue);
END IF;
- RETURN recid;
+ RETURN query
+VALUES (recid,
+ nulldetail);
END;
$$
LANGUAGE plpgSQL
diff --git a/src/sql/entityInsert.sql b/src/sql/entityInsert.sql
index f518617..11e60af 100644
--- a/src/sql/entityInsert.sql
+++ b/src/sql/entityInsert.sql
@@ -1,2 +1,4 @@
SELECT
+ *
+FROM
entity(?, ?)