@@ -201,15 +201,17 @@ defmodule Astarte.AppEngine.API.Device.Queries do
201201 :reception_timestamp_submillis, :datetime_value) #{ ttl_string } ;
202202 """
203203
204+ { reception_ms , reception_submillis } = split_ms_and_submillis ( reception_timestamp )
205+
204206 insert_query =
205207 DatabaseQuery . new ( )
206208 |> DatabaseQuery . statement ( insert_statement )
207209 |> DatabaseQuery . put ( :device_id , device_id )
208210 |> DatabaseQuery . put ( :interface_id , interface_descriptor . interface_id )
209211 |> DatabaseQuery . put ( :endpoint_id , endpoint_id )
210212 |> DatabaseQuery . put ( :path , path )
211- |> DatabaseQuery . put ( :reception_timestamp , div ( reception_timestamp , 1000 ) )
212- |> DatabaseQuery . put ( :reception_timestamp_submillis , rem ( reception_timestamp , 1000 ) )
213+ |> DatabaseQuery . put ( :reception_timestamp , reception_ms )
214+ |> DatabaseQuery . put ( :reception_timestamp_submillis , reception_submillis )
213215 |> DatabaseQuery . put ( :datetime_value , value_timestamp )
214216
215217 DatabaseQuery . call! ( db_client , insert_query )
@@ -278,6 +280,8 @@ defmodule Astarte.AppEngine.API.Device.Queries do
278280 ) do
279281 ttl_string = get_ttl_string ( opts )
280282
283+ { timestamp_ms , timestamp_submillis } = split_ms_and_submillis ( timestamp )
284+
281285 # TODO: :reception_timestamp_submillis is just a place holder right now
282286 insert_query =
283287 DatabaseQuery . new ( )
@@ -292,8 +296,8 @@ defmodule Astarte.AppEngine.API.Device.Queries do
292296 |> DatabaseQuery . put ( :interface_id , interface_descriptor . interface_id )
293297 |> DatabaseQuery . put ( :endpoint_id , endpoint_id )
294298 |> DatabaseQuery . put ( :path , path )
295- |> DatabaseQuery . put ( :reception_timestamp , div ( timestamp , 1000 ) )
296- |> DatabaseQuery . put ( :reception_timestamp_submillis , div ( timestamp , 100 ) )
299+ |> DatabaseQuery . put ( :reception_timestamp , timestamp_ms )
300+ |> DatabaseQuery . put ( :reception_timestamp_submillis , timestamp_submillis )
297301 |> DatabaseQuery . put ( :value , to_db_friendly_type ( value ) )
298302
299303 DatabaseQuery . call! ( db_client , insert_query )
@@ -316,6 +320,7 @@ defmodule Astarte.AppEngine.API.Device.Queries do
316320 opts
317321 ) do
318322 ttl_string = get_ttl_string ( opts )
323+ { timestamp_ms , timestamp_submillis } = split_ms_and_submillis ( timestamp )
319324
320325 insert_query =
321326 DatabaseQuery . new ( )
@@ -330,9 +335,9 @@ defmodule Astarte.AppEngine.API.Device.Queries do
330335 |> DatabaseQuery . put ( :interface_id , interface_descriptor . interface_id )
331336 |> DatabaseQuery . put ( :endpoint_id , endpoint . endpoint_id )
332337 |> DatabaseQuery . put ( :path , path )
333- |> DatabaseQuery . put ( :value_timestamp , div ( timestamp , 1000 ) )
334- |> DatabaseQuery . put ( :reception_timestamp , div ( timestamp , 1000 ) )
335- |> DatabaseQuery . put ( :reception_timestamp_submillis , rem ( timestamp , 1000 ) )
338+ |> DatabaseQuery . put ( :value_timestamp , timestamp_ms )
339+ |> DatabaseQuery . put ( :reception_timestamp , timestamp_ms )
340+ |> DatabaseQuery . put ( :reception_timestamp_submillis , timestamp_submillis )
336341 |> DatabaseQuery . put ( :value , to_db_friendly_type ( value ) )
337342
338343 # TODO: |> DatabaseQuery.consistency(insert_consistency(interface_descriptor, endpoint))
@@ -409,6 +414,8 @@ defmodule Astarte.AppEngine.API.Device.Queries do
409414 { query_columns , placeholders }
410415 end
411416
417+ { timestamp_ms , timestamp_submillis } = split_ms_and_submillis ( timestamp )
418+
412419 # TODO: :reception_timestamp_submillis is just a place holder right now
413420 insert_query =
414421 DatabaseQuery . new ( )
@@ -418,9 +425,9 @@ defmodule Astarte.AppEngine.API.Device.Queries do
418425 """ )
419426 |> DatabaseQuery . put ( :device_id , device_id )
420427 |> DatabaseQuery . put ( :path , path )
421- |> DatabaseQuery . put ( :value_timestamp , div ( timestamp , 1000 ) )
422- |> DatabaseQuery . put ( :reception_timestamp , div ( timestamp , 1000 ) )
423- |> DatabaseQuery . put ( :reception_timestamp_submillis , rem ( timestamp , 1000 ) )
428+ |> DatabaseQuery . put ( :value_timestamp , timestamp_ms )
429+ |> DatabaseQuery . put ( :reception_timestamp , timestamp_ms )
430+ |> DatabaseQuery . put ( :reception_timestamp_submillis , timestamp_submillis )
424431 |> DatabaseQuery . merge ( query_values )
425432
426433 # TODO: |> DatabaseQuery.consistency(insert_consistency(interface_descriptor, endpoint))
@@ -1036,4 +1043,11 @@ defmodule Astarte.AppEngine.API.Device.Queries do
10361043 { :error , :database_error }
10371044 end
10381045 end
1046+
1047+ defp split_ms_and_submillis ( timestamp_micro ) do
1048+ timestamp_ms = div ( timestamp_micro , 1000 )
1049+ timestamp_submillis = rem ( timestamp_micro , 1000 )
1050+
1051+ { timestamp_ms , timestamp_submillis }
1052+ end
10391053end
0 commit comments