]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
MS SQL: Simplify time handling using expansions
authorTerry Burton <tez@terryburton.co.uk>
Mon, 23 Mar 2020 16:58:43 +0000 (16:58 +0000)
committerAlan DeKok <aland@freeradius.org>
Mon, 23 Mar 2020 23:07:31 +0000 (19:07 -0400)
raddb/mods-config/sql/main/mssql/queries.conf

index a25b0bf95199b1eeff8e64b624e2cc4364d254a7..ee21f740e5a385071560e48fd3b9c57e11be09d1 100644 (file)
 #
 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 = "DATEADD(SS, ${event_timestamp_epoch}, '19700101')"
+
 #######################################################################
 #  Authorization Queries
 #######################################################################
@@ -107,14 +126,14 @@ accounting {
                        query = "\
                                UPDATE ${....acct_table1} \
                                SET \
-                                       AcctStopTime='%S', \
-                                       AcctSessionTime=unix_timestamp('%S') - \
-                                               unix_timestamp(AcctStartTime), \
+                                       AcctStopTime=${....event_timestamp}, \
+                                       AcctSessionTime=${....event_timestamp_epoch} - \
+                                               DATEDIFF(SS, '1970-01-01', AcctStartTime), \
                                        AcctTerminateCause='%{%{Acct-Terminate-Cause}:-NAS-Reboot}', \
                                        AcctStopDelay = %{%{Acct-Delay-Time}:-0} \
                                WHERE AcctStopTime = 0 \
                                AND NASIPAddress = '%{NAS-IP-Address}' \
-                               AND AcctStartTime <= '%S'"
+                               AND AcctStartTime <= ${....event_timestamp}"
                }
 
                accounting-off {
@@ -232,7 +251,7 @@ accounting {
                                        '%{NAS-IP-Address}', \
                                        '%{%{NAS-Port-ID}:-%{NAS-Port}}', \
                                        '%{NAS-Port-Type}', \
-                                       '%S', \
+                                       ${....event_timestamp}, \
                                        '0', \
                                        '%{Acct-Authentic}', \
                                        '%{Connect-Info}', \
@@ -286,7 +305,7 @@ accounting {
                        query = "\
                                UPDATE ${....acct_table1} \
                                SET \
-                                       AcctStartTime = '%S', \
+                                       AcctStartTime = ${....event_timestamp}, \
                                        AcctStartDelay = '%{%{Acct-Delay-Time}:-0}', \
                                        ConnectInfo_start = '%{Connect-Info}' \
                                WHERE AcctUniqueId = '%{Acct-Unique-Session-ID}' \
@@ -391,7 +410,7 @@ accounting {
                        query = "\
                                UPDATE ${....acct_table2} \
                                SET \
-                                       AcctStopTime = '%S', \
+                                       AcctStopTime = ${....event_timestamp}, \
                                        AcctSessionTime = '%{Acct-Session-Time}', \
                                        AcctInputOctets = convert(bigint, '%{%{Acct-Input-Gigawords}:-0}' * POWER(2.0, 32)) | '%{%{Acct-Input-Octets}:-0}', \
                                        AcctOutputOctets = convert(bigint, '%{%{Acct-Output-Gigawords}:-0}' * POWER(2.0, 32)) | '%{%{Acct-Output-Octets}:-0}', \
@@ -437,7 +456,7 @@ accounting {
                                        '%{NAS-IP-Address}', \
                                        '%{%{NAS-Port-ID}:-%{NAS-Port}}', \
                                        '%{NAS-Port-Type}', \
-                                       '%S', \
+                                       ${....event_timestamp}, \
                                        '%{Acct-Session-Time}', \
                                        '%{Acct-Authentic}', \
                                        '', \