From: Terry Burton Date: Mon, 23 Mar 2020 16:59:50 +0000 (+0000) Subject: SQLite: Simplify time handling using expansions X-Git-Tag: release_3_0_21~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b51358da78f17fc79aa5667fd8ff050a7074d7eb;p=thirdparty%2Ffreeradius-server.git SQLite: Simplify time handling using expansions Don't use NOW() since the queries can be replayed from a file --- diff --git a/raddb/mods-config/sql/main/sqlite/queries.conf b/raddb/mods-config/sql/main/sqlite/queries.conf index 69cb58b2719..ef413138781 100644 --- a/raddb/mods-config/sql/main/sqlite/queries.conf +++ b/raddb/mods-config/sql/main/sqlite/queries.conf @@ -26,6 +26,30 @@ # sql_user_name = "%{User-Name}" +####################################################################### +# Query config: Event-Timestamp +####################################################################### +# event_timestamp_epoch is the basis for the time inserted into +# accounting records. Typically this will be the Event-Timestamp of the +# accounting request, which is usually provided by a NAS. +# +# Uncomment the next line, if you want the timestamp to be based on the +# request reception time recorded by this server, for example if you +# distrust the provided Event-Timestamp. +#event_timestamp_epoch = "%l" + +event_timestamp_epoch = "%{%{integer:Event-Timestamp}:-%l}" + +# event_timestamp is the SQL snippet for converting an epoch timestamp +# to an SQL date. + +event_timestamp = "${event_timestamp_epoch}" + +# NOTE: Recent SQLite versions allow proper arithmetic with dates +# stored as strings including comparison using an index, so we keep +# these variables differentiated in preparation for switching away from +# integer storage. + ####################################################################### # Default profile ####################################################################### @@ -222,14 +246,14 @@ accounting { query = "\ UPDATE ${....acct_table1} \ SET \ - acctstoptime = %{%{integer:Event-Timestamp}:-date('now')}, \ + acctstoptime = ${....event_timestamp}, \ acctsessiontime = \ - (%{%{integer:Event-Timestamp}:-strftime('%%s', 'now')} \ - - strftime('%%s', acctstarttime)), \ + (${....event_timestamp_epoch} \ + - acctstarttime), \ acctterminatecause = '%{Acct-Terminate-Cause}' \ WHERE acctstoptime IS NULL \ AND nasipaddress = '%{NAS-IP-Address}' \ - AND acctstarttime <= %{integer:Event-Timestamp}" + AND acctstarttime <= ${....event_timestamp}" } accounting-off { @@ -255,7 +279,7 @@ accounting { '%{%{NAS-IPv6-Address}:-%{NAS-IP-Address}}', \ NULLIF('%{%{NAS-Port-ID}:-%{NAS-Port}}', ''), \ '%{NAS-Port-Type}', \ - %{%{integer:Event-Timestamp}:-date('now')}, \ + ${....event_timestamp}, \ NULL, \ NULL, \ 0, \ @@ -277,8 +301,8 @@ accounting { query = "\ UPDATE ${....acct_table1} SET \ - AcctStartTime = %{%{integer:Event-Timestamp}:-date('now')}, \ - AcctUpdateTime = %{%{integer:Event-Timestamp}:-date('now')}, \ + AcctStartTime = ${....event_timestamp}, \ + AcctUpdateTime = ${....event_timestamp}, \ ConnectInfo_start = '%{Connect-Info}', \ AcctSessionId = '%{Acct-Session-Id}' \ WHERE UserName = '%{SQL-User-Name}' \ @@ -303,8 +327,8 @@ accounting { '%{NAS-IP-Address}', \ '%{%{NAS-Port-ID}:-%{NAS-Port}}', \ '%{NAS-Port-Type}', \ - %{%{integer:Event-Timestamp}:-date('now')}, \ - %{%{integer:Event-Timestamp}:-date('now')}, \ + ${....event_timestamp}, \ + ${....event_timestamp}, \ NULL, \ '0', \ '%{Acct-Authentic}', \ @@ -346,8 +370,8 @@ accounting { framedipv6prefix = '%{Framed-IPv6-Prefix}', \ framedinterfaceid = '%{Framed-Interface-Id}', \ delegatedipv6prefix = '%{Delegated-IPv6-Prefix}', \ - AcctStartTime = %{%{integer:Event-Timestamp}:-date('now')}, \ - AcctUpdateTime = %{%{integer:Event-Timestamp}:-date('now')} \ + AcctStartTime = ${....event_timestamp}, \ + AcctUpdateTime = ${....event_timestamp} \ WHERE UserName = '%{SQL-User-Name}' \ AND NASIPAddress = '%{%{NAS-IPv6-Address}:-%{NAS-IP-Address}}' \ AND NASPortId = '%{%{NAS-Port-ID}:-%{NAS-Port}}' \ @@ -360,8 +384,8 @@ accounting { # query = "\ UPDATE ${....acct_table1} SET \ - acctstarttime = %{%{integer:Event-Timestamp}:-date('now')}, \ - acctupdatetime = %{%{integer:Event-Timestamp}:-date('now')}, \ + acctstarttime = ${....event_timestamp}, \ + acctupdatetime = ${....event_timestamp}, \ connectinfo_start = '%{Connect-Info}' \ WHERE AcctUniqueId = '%{Acct-Unique-Session-Id}'" } @@ -375,7 +399,7 @@ accounting { query = "\ UPDATE ${....acct_table1} \ SET \ - acctupdatetime = %{%{integer:Event-Timestamp}:-date('now')}, \ + acctupdatetime = ${....event_timestamp}, \ acctinterval = 0, \ framedipaddress = '%{Framed-IP-Address}', \ framedipv6address = '%{Framed-IPv6-Address}', \ @@ -404,8 +428,8 @@ accounting { '%{NAS-IP-Address}', \ '%{%{NAS-Port-ID}:-%{NAS-Port}}', \ '%{NAS-Port-Type}', \ - (%{%{integer:Event-Timestamp}:-strftime('%%s', 'now')} - %{%{Acct-Session-Time}:-0}), \ - %{%{integer:Event-Timestamp}:-date('now')}, \ + (${....event_timestamp_epoch} - %{%{Acct-Session-Time}:-0}), \ + ${....event_timestamp}, \ NULL, \ %{%{Acct-Session-Time}:-NULL}, \ '%{Acct-Authentic}', \ @@ -449,7 +473,7 @@ accounting { framedipv6prefix = '%{Framed-IPv6-Prefix}', \ framedinterfaceid = '%{Framed-Interface-Id}', \ delegatedipv6prefix = '%{Delegated-IPv6-Prefix}', \ - AcctUpdateTime = %{%{integer:Event-Timestamp}:-date('now')}, \ + AcctUpdateTime = ${....event_timestamp}, \ AcctSessionTime = %{%{Acct-Session-Time}:-NULL}, \ AcctInputOctets = '%{%{Acct-Input-Gigawords}:-0}' \ << 32 | '%{%{Acct-Input-Octets}:-0}', \ @@ -469,7 +493,7 @@ accounting { # query = "\ UPDATE ${....acct_table2} SET \ - acctstoptime = %{%{integer:Event-Timestamp}:-date('now')}, \ + acctstoptime = ${....event_timestamp}, \ acctsessiontime = %{%{Acct-Session-Time}:-NULL}, \ acctinputoctets = %{%{Acct-Input-Gigawords}:-0} \ << 32 | %{%{Acct-Input-Octets}:-0}, \ @@ -494,9 +518,9 @@ accounting { '%{NAS-IP-Address}', \ '%{%{NAS-Port-ID}:-%{NAS-Port}}', \ '%{NAS-Port-Type}', \ - (%{%{integer:Event-Timestamp}:-strftime('%%s', 'now')} - %{%{Acct-Session-Time}:-0}), \ - %{%{integer:Event-Timestamp}:-date('now')}, \ - %{%{integer:Event-Timestamp}:-date('now')}, \ + (${....event_timestamp_epoch} - %{%{Acct-Session-Time}:-0}), \ + ${....event_timestamp}, \ + ${....event_timestamp}, \ %{%{Acct-Session-Time}:-NULL}, \ '%{Acct-Authentic}', \ '', \ @@ -539,8 +563,8 @@ accounting { framedipv6prefix = '%{Framed-IPv6-Prefix}', \ framedinterfaceid = '%{Framed-Interface-Id}', \ delegatedipv6prefix = '%{Delegated-IPv6-Prefix}', \ - AcctStopTime = %{%{integer:Event-Timestamp}:-date('now')}, \ - AcctUpdateTime = %{%{integer:Event-Timestamp}:-date('now')}, \ + AcctStopTime = ${....event_timestamp}, \ + AcctUpdateTime = ${....event_timestamp}, \ AcctSessionTime = %{%{Acct-Session-Time}:-NULL}, \ AcctInputOctets = '%{%{Acct-Input-Gigawords}:-0}' \ << 32 | '%{%{Acct-Input-Octets}:-0}', \