@@ -1859,7 +1859,7 @@ func migrateDeviceMetricsFn(devEUI []byte) {
18591859 key := fmt .Sprintf ("%slora:as:metrics:{device:%s}:*" , asPrefix , devEUIStr )
18601860 keys , err := asRedis .Keys (context .Background (), key ).Result ()
18611861 if err != nil {
1862- panic ( err )
1862+ log . Fatalf ( "Get device-metrics keys error: %s" , err )
18631863 }
18641864
18651865 asPipe := asRedis .Pipeline ()
@@ -1912,16 +1912,34 @@ func migrateDeviceMetricsFn(devEUI []byte) {
19121912}
19131913
19141914func migrateGatewayMetricsFn (gatewayID []byte ) {
1915- key := fmt .Sprintf ("%slora:as:metrics:{gw:%s}:*" , asPrefix , hex .EncodeToString (gatewayID ))
1915+ gatewayIDStr := hex .EncodeToString (gatewayID )
1916+ key := fmt .Sprintf ("%slora:as:metrics:{gw:%s}:*" , asPrefix , gatewayIDStr )
19161917 keys , err := asRedis .Keys (context .Background (), key ).Result ()
19171918 if err != nil {
1918- panic ( err )
1919+ log . Fatalf ( "Get gateway-metrics keys error: %s" , err )
19191920 }
19201921
1922+ asPipe := asRedis .Pipeline ()
1923+ cmds := map [string ]* redis.StringStringMapCmd {}
1924+
19211925 for _ , key := range keys {
1922- vals , err := asRedis .HGetAll (context .Background (), key ).Result ()
1923- if err != nil {
1924- panic (err )
1926+ cmds [key ] = asPipe .HGetAll (context .Background (), key )
1927+ }
1928+
1929+ _ , err = asPipe .Exec (context .Background ())
1930+ if err != nil {
1931+ log .Fatalf ("Get gateway-metrics error, gateway_id: %s, error: %s" , gatewayIDStr , err )
1932+ }
1933+
1934+ csPipe := csRedis .Pipeline ()
1935+ for key , resp := range cmds {
1936+ if resp .Err () != nil {
1937+ log .Fatalf ("Get gateway-metrics error, gateway_id: %s, error: %" , gatewayIDStr , err )
1938+ }
1939+
1940+ vals , err := resp .Result ()
1941+ if resp .Err () != nil {
1942+ log .Fatalf ("Get gateway-metrics error, gateway_id: %s, error: %" , gatewayIDStr , err )
19251943 }
19261944
19271945 keyParts := strings .Split (key , ":" )
@@ -1941,12 +1959,12 @@ func migrateGatewayMetricsFn(gatewayID []byte) {
19411959 "MONTH" : time .Hour * 24 * 31 * 365 * 2 ,
19421960 }
19431961
1944- if err := csRedis .HSet (context .Background (), newKey , vals ). Err (); err != nil {
1945- log . Printf ( "Migrate gateway metrics error: %s" , err )
1946- }
1947- if err := csRedis . PExpire (context .Background (), newKey , ttl [ aggregation ]). Err (); err != nil {
1948- panic ( err )
1949- }
1962+ csPipe .HSet (context .Background (), newKey , vals )
1963+ csPipe . PExpire ( context . Background (), newKey , ttl [ aggregation ] )
1964+ }
1965+ _ , err = csPipe . Exec (context .Background ())
1966+ if err != nil {
1967+ log . Fatalf ( "Store gateway-metrics error, gateway_id: %s, error: %s" , gatewayIDStr , err )
19501968 }
19511969}
19521970
0 commit comments