Brief
Bloggers recently because of work tasks ridden, I have no time to attend to this piece of the plots. Some time ago a little spare time, it took more effort to learn "Aha algorithm", did not learn a lot before too focused on content profitable. However, these algorithms have not read the title, such as behind the times, the need to redefine themselves to brush up on those previously written, and continues to work behind the algorithm knowledge.
A little bit of time today, to sum up some of the problems bloggers recent work encountered in the hope of opportunity to summarize the solution of similar problems, it can be considered an accumulation of it.
background
We regularly report the GPS data preset function in the driver's mobile phone APP, the APP feature set to collect every 15 seconds GPS positioning address, and then collected every 10 times to report to the server persistence. But because Android APP non-integrated, but independent and designed by us to provide this function, then collect the GPS data is likely to affect human operator or other networks due to the driver, produce or acquire positional deviation when a positioning failure. Now we have such a demand server requires an analysis of APP installed in the driver's mobile phone whether to collect on a regular basis to the GPS position.
In order to ensure more successful GPS location data can be uploaded to the server side, we APP after the completion of each server requests only when APP receives the correct response from the server, the data is uploaded to represent the normal and accurate, then empty out GPS location data once recorded on the APP end; but if the APP since the data for various reasons can not be reported on a collection of server-side, and try again after a round of no avail, the next will collect the next batch of data reported together, through this mechanism to ensure the stability of the data transfer.
analysis
If the driver phone APP is a timing of 15 seconds to collect GPS address, then the persistence time difference before and after the two records into the database by obtaining chronological should be is 15 seconds, and if greater than 15 seconds, then that timing acquisition problem. So now we have to do is filter out two records before and after acquiring the GPS location of the time difference is greater than 15 seconds, method of calculating the time difference has been in MySQL that TimeDiff (I TimeDiff pits for use in Java Bowen has summed up front, interested friends can look at Mark, take the time to look back.), and calculate how to calculate the difference before and after the two records, it does not seem to come into contact with, then this article will to solve this problem.
Portal: SQL function TIMEDIFF in Java programs using error problem analysis
https://www.cnblogs.com/captainad/p/10855608.html
solve
First of all bloggers in the service side there is a table to record the driver's GPS points up reporting information, table structure is as follows:
. 1 - driver GPS collected in Table 2 the CREATE TABLE captainad_driver_gps_position ( . 3 ID BIGINT the NOT NULL AUTO_INCREMENT the COMMENT ' master key ' , . 4 business_id BIGINT the DEFAULT NULL the COMMENT ' service ID ' , . 5 device_MAC VARCHAR ( 64 ) the DEFAULT NULL the COMMENT ' MAC address ' , 6 device_imei VARCHAR ( 64 )The DEFAULT NULL the COMMENT ' equipment the IMEI ' , . 7 lat_lng VARCHAR ( 64 ) the DEFAULT NULL the COMMENT ' latitude and longitude ' , . 8 capture_time TIMESTAMP the DEFAULT CURRENT_TIMESTAMP the COMMENT ' acquisition time ' , . 9 the create_time TIMESTAMP the DEFAULT CURRENT_TIMESTAMP the COMMENT ' Created ' , 10 UPDATE_TIME TIMESTAMP the DEFAULT CURRENT_TIMESTAMP the ON the UPDATE CURRENT_TIMESTAMP the COMMENT ' modified ' , . 11 a PRIMARY KEY (ID), 12 is KEY `idx_business_id` (` business_id`) BTREE the USING 13 is ) ENGINE = INNODB the DEFAULT the CHARSET = UTF8 the COMMENT = ' driver collect GPS ' ;
Data recorded in the table as follows:
Now calculate the difference before and after recording for two time by obtaining the GPS location ordered by time after capture_time performed. In order to calculate the difference between the two, then we definitely need to get one after two records , where we can skillfully use a variable to record the number of the current line , then circulating with the query each time the number of lines superimposed in order to achieve the purpose of rows, so that we can know which two records after the previous one.
Print line number of the SQL statement:
1 SELECT 2 (@rownum := @rownum + 1) AS rownum, 3 tab.business_id, 4 tab.device_mac, 5 tab.capture_time 6 FROM 7 captainad_driver_gps_position tab, 8 (SELECT @rownum := 0) r -- 声明变量 9 WHERE 10 1 = 1 11 AND DATE_FORMAT( 12 tab.capture_time, 13 '%Y-%m-%d' 14 ) = '2019-06-28' 15 ORDER BY 16 tab.capture_time
基于此,我们将目标SQL给写出来,这里我根据我们的实际业务将语句稍微做了整理,脚本大致如下:
1 SELECT 2 t.business_id, 3 t.device_mac, 4 t.capture_time, 5 t.tdiff 6 FROM 7 ( 8 SELECT 9 r1.business_id, 10 r1.device_mac, 11 r1.capture_time, 12 TIMEDIFF( 13 r2.capture_time, 14 r1.capture_time 15 ) AS 'tdiff' 16 FROM 17 ( 18 SELECT 19 (@rownum := @rownum + 1) AS rownum, 20 tab.business_id, 21 tab.device_mac, 22 tab.capture_time 23 FROM 24 captainad_driver_gps_position tab, 25 (SELECT @rownum := 0) r 26 WHERE 27 1 = 1 28 AND DATE_FORMAT( 29 tab.capture_time, 30 '%Y-%m-%d' 31 ) = '2019-06-28' 32 ORDER BY 33 tab.capture_time 34 ) r1 35 LEFT JOIN ( 36 SELECT 37 (@INDEX := @INDEX + 1) AS rownum, 38 tab.business_id, 39 tab.device_mac, 40 tab.capture_time 41 FROM 42 captainad_driver_gps_position tab, 43 (SELECT @INDEX := 0) r 44 WHERE 45 1 = 1 46 AND DATE_FORMAT( 47 tab.capture_time, 48 '%Y-%m-%d' 49 ) = '2019-06-28' 50 ORDER BY 51 tab.capture_time 52 ) r2 ON r1.business_id = r2.business_id 53 AND r1.device_mac = r2.device_mac 54 AND r1.rownum = r2.rownum - 1 55 ) t 56 WHERE 57 t.tdiff > '00:00:15'
在上面的代码中,我们通过 r1.rownum = r2.rownum - 1来判断两条记录是否是前后行,然后再使用TIMEDIFF函数来计算时间差,到此,我们的目标就实现了。