Philipp Hörist pushed to branch master at gajim / gajim

Commits:

1 changed file:

Changes:

  • gajim/common/logger.py
    ... ... @@ -183,6 +183,15 @@ def caps_decoder(dict_):
    183 183
                         features=features,
    
    184 184
                         dataforms=dataforms)
    
    185 185
     
    
    186
    +def timeit(func):
    
    187
    +    def func_wrapper(self, *args, **kwargs):
    
    188
    +        start = time.monotonic_ns()
    
    189
    +        result = func(self, *args, **kwargs)
    
    190
    +        exec_time = (time.monotonic_ns() - start) / 1000000
    
    191
    +        level = 30 if exec_time > 50 else 10
    
    192
    +        log.log(level, 'Execution time for %s: %s ms', func.__name__, exec_time)
    
    193
    +        return result
    
    194
    +    return func_wrapper
    
    186 195
     
    
    187 196
     def _convert_disco_info(disco_info):
    
    188 197
         return parse_disco_info(Iq(node=disco_info))
    
    ... ... @@ -284,6 +293,7 @@ class Logger:
    284 293
                 log.exception('Error')
    
    285 294
                 sys.exit()
    
    286 295
     
    
    296
    +    @timeit
    
    287 297
         def _migrate_logs(self, con):
    
    288 298
             if self._get_user_version(con) == 0:
    
    289 299
                 # All migrations from 0.16.9 until 1.0.0
    
    ... ... @@ -336,6 +346,7 @@ class Logger:
    336 346
                 ]
    
    337 347
                 self._execute_multiple(con, statements)
    
    338 348
     
    
    349
    +    @timeit
    
    339 350
         def _migrate_cache(self, con):
    
    340 351
             if self._get_user_version(con) == 0:
    
    341 352
                 # All migrations from 0.16.9 until 1.0.0
    
    ... ... @@ -434,6 +445,7 @@ class Logger:
    434 445
                 self._con.close()
    
    435 446
                 sys.exit()
    
    436 447
     
    
    448
    +    @timeit
    
    437 449
         def _set_synchronous(self, sync):
    
    438 450
             try:
    
    439 451
                 if sync:
    
    ... ... @@ -459,6 +471,7 @@ class Logger:
    459 471
         def _like(search_str):
    
    460 472
             return '%{}%'.format(search_str)
    
    461 473
     
    
    474
    +    @timeit
    
    462 475
         def commit(self):
    
    463 476
             try:
    
    464 477
                 self._con.commit()
    
    ... ... @@ -472,6 +485,7 @@ class Logger:
    472 485
                 return
    
    473 486
             self._commit_timout_id = GLib.timeout_add(500, self.commit)
    
    474 487
     
    
    488
    +    @timeit
    
    475 489
         def simple_commit(self, sql_to_commit):
    
    476 490
             """
    
    477 491
             Helper to commit
    
    ... ... @@ -479,6 +493,7 @@ class Logger:
    479 493
             self._con.execute(sql_to_commit)
    
    480 494
             self._timeout_commit()
    
    481 495
     
    
    496
    +    @timeit
    
    482 497
         def _get_jid_ids_from_db(self):
    
    483 498
             """
    
    484 499
             Load all jid/jid_id tuples into a dict for faster access
    
    ... ... @@ -536,6 +551,7 @@ class Logger:
    536 551
             jid = app.get_jid_from_account(account)
    
    537 552
             return self.get_jid_id(jid, type_=JIDConstant.NORMAL_TYPE)
    
    538 553
     
    
    554
    +    @timeit
    
    539 555
         def get_jid_id(self, jid, kind=None, type_=None):
    
    540 556
             """
    
    541 557
             Get the jid id from a jid.
    
    ... ... @@ -692,6 +708,7 @@ class Logger:
    692 708
             if sub == SubscriptionConstant.BOTH:
    
    693 709
                 return 'both'
    
    694 710
     
    
    711
    +    @timeit
    
    695 712
         def insert_unread_events(self, message_id, jid_id):
    
    696 713
             """
    
    697 714
             Add unread message with id: message_id
    
    ... ... @@ -701,6 +718,7 @@ class Logger:
    701 718
             self._con.execute(sql, (message_id, jid_id))
    
    702 719
             self._timeout_commit()
    
    703 720
     
    
    721
    +    @timeit
    
    704 722
         def set_read_messages(self, message_ids):
    
    705 723
             """
    
    706 724
             Mark all messages with ids in message_ids as read
    
    ... ... @@ -709,6 +727,7 @@ class Logger:
    709 727
             sql = 'DELETE FROM unread_messages WHERE message_id IN (%s)' % ids
    
    710 728
             self.simple_commit(sql)
    
    711 729
     
    
    730
    +    @timeit
    
    712 731
         def set_shown_unread_msgs(self, msg_log_id):
    
    713 732
             """
    
    714 733
             Mark unread message as shown un GUI
    
    ... ... @@ -717,6 +736,7 @@ class Logger:
    717 736
                     msg_log_id
    
    718 737
             self.simple_commit(sql)
    
    719 738
     
    
    739
    +    @timeit
    
    720 740
         def reset_shown_unread_messages(self):
    
    721 741
             """
    
    722 742
             Set shown field to False in unread_messages table
    
    ... ... @@ -724,6 +744,7 @@ class Logger:
    724 744
             sql = 'UPDATE unread_messages SET shown = 0'
    
    725 745
             self.simple_commit(sql)
    
    726 746
     
    
    747
    +    @timeit
    
    727 748
         def get_unread_msgs(self):
    
    728 749
             """
    
    729 750
             Get all unread messages
    
    ... ... @@ -755,6 +776,7 @@ class Logger:
    755 776
                 all_messages.append((result, shown))
    
    756 777
             return all_messages
    
    757 778
     
    
    779
    +    @timeit
    
    758 780
         def get_last_conversation_lines(self, account, jid, pending):
    
    759 781
             """
    
    760 782
             Get recent messages
    
    ... ... @@ -818,6 +840,7 @@ class Logger:
    818 840
             start_of_day = int(time.mktime(local_time))
    
    819 841
             return start_of_day
    
    820 842
     
    
    843
    +    @timeit
    
    821 844
         def get_conversation_for_date(self, account, jid, date):
    
    822 845
             """
    
    823 846
             Load the complete conversation with a given jid on a specific date
    
    ... ... @@ -849,6 +872,7 @@ class Logger:
    849 872
                                           (date.timestamp(),
    
    850 873
                                           (date + delta).timestamp())).fetchall()
    
    851 874
     
    
    875
    +    @timeit
    
    852 876
         def search_log(self, account, jid, query, date=None):
    
    853 877
             """
    
    854 878
             Search the conversation log for messages containing the `query` string.
    
    ... ... @@ -890,6 +914,7 @@ class Logger:
    890 914
     
    
    891 915
             return self._con.execute(sql, tuple(jids) + (query,)).fetchall()
    
    892 916
     
    
    917
    +    @timeit
    
    893 918
         def get_days_with_logs(self, account, jid, year, month):
    
    894 919
             """
    
    895 920
             Request the days in a month where we received messages
    
    ... ... @@ -930,6 +955,7 @@ class Logger:
    930 955
                                           (date.timestamp(),
    
    931 956
                                           (date + delta).timestamp())).fetchall()
    
    932 957
     
    
    958
    +    @timeit
    
    933 959
         def get_last_date_that_has_logs(self, account, jid):
    
    934 960
             """
    
    935 961
             Get the timestamp of the last message we received for the jid.
    
    ... ... @@ -956,6 +982,7 @@ class Logger:
    956 982
             # attributes set to None because of the MAX() function
    
    957 983
             return self._con.execute(sql, tuple(jids)).fetchone().time
    
    958 984
     
    
    985
    +    @timeit
    
    959 986
         def get_first_date_that_has_logs(self, account, jid):
    
    960 987
             """
    
    961 988
             Get the timestamp of the first message we received for the jid.
    
    ... ... @@ -982,6 +1009,7 @@ class Logger:
    982 1009
             # attributes set to None because of the MIN() function
    
    983 1010
             return self._con.execute(sql, tuple(jids)).fetchone().time
    
    984 1011
     
    
    1012
    +    @timeit
    
    985 1013
         def get_date_has_logs(self, account, jid, date):
    
    986 1014
             """
    
    987 1015
             Get single timestamp of a message we received for the jid
    
    ... ... @@ -1013,6 +1041,7 @@ class Logger:
    1013 1041
             return self._con.execute(
    
    1014 1042
                 sql, tuple(jids) + (start, end)).fetchone()
    
    1015 1043
     
    
    1044
    +    @timeit
    
    1016 1045
         def save_transport_type(self, jid, type_):
    
    1017 1046
             """
    
    1018 1047
             Save the type of the transport in DB
    
    ... ... @@ -1034,6 +1063,7 @@ class Logger:
    1034 1063
             self._con.execute(sql, (jid, type_id))
    
    1035 1064
             self._timeout_commit()
    
    1036 1065
     
    
    1066
    +    @timeit
    
    1037 1067
         def get_transports_type(self):
    
    1038 1068
             """
    
    1039 1069
             Return all the type of the transports in DB
    
    ... ... @@ -1047,6 +1077,7 @@ class Logger:
    1047 1077
                         result.type)
    
    1048 1078
             return answer
    
    1049 1079
     
    
    1080
    +    @timeit
    
    1050 1081
         def load_caps_data(self):
    
    1051 1082
             '''
    
    1052 1083
             Load caps cache data
    
    ... ... @@ -1064,6 +1095,7 @@ class Logger:
    1064 1095
                 cache[(row.hash_method, row.hash)] = data
    
    1065 1096
             return cache
    
    1066 1097
     
    
    1098
    +    @timeit
    
    1067 1099
         def add_caps_entry(self, hash_method, hash_, caps_data):
    
    1068 1100
             serialized = json.dumps(caps_data, cls=CapsEncoder)
    
    1069 1101
             self._con.execute('''
    
    ... ... @@ -1072,12 +1104,14 @@ class Logger:
    1072 1104
                     ''', (hash_method, hash_, serialized, int(time.time())))
    
    1073 1105
             self._timeout_commit()
    
    1074 1106
     
    
    1107
    +    @timeit
    
    1075 1108
         def update_caps_time(self, method, hash_):
    
    1076 1109
             sql = '''UPDATE caps_cache SET last_seen = ?
    
    1077 1110
                      WHERE hash_method = ? and hash = ?'''
    
    1078 1111
             self._con.execute(sql, (int(time.time()), method, hash_))
    
    1079 1112
             self._timeout_commit()
    
    1080 1113
     
    
    1114
    +    @timeit
    
    1081 1115
         def clean_caps_table(self):
    
    1082 1116
             """
    
    1083 1117
             Remove caps which was not seen for 3 months
    
    ... ... @@ -1087,6 +1121,7 @@ class Logger:
    1087 1121
                               (timestamp,))
    
    1088 1122
             self._timeout_commit()
    
    1089 1123
     
    
    1124
    +    @timeit
    
    1090 1125
         def replace_roster(self, account_name, roster_version, roster):
    
    1091 1126
             """
    
    1092 1127
             Replace current roster in DB by a new one
    
    ... ... @@ -1120,6 +1155,7 @@ class Logger:
    1120 1155
             app.config.set_per('accounts', account_name, 'roster_version',
    
    1121 1156
                 roster_version)
    
    1122 1157
     
    
    1158
    +    @timeit
    
    1123 1159
         def del_contact(self, account_jid, jid):
    
    1124 1160
             """
    
    1125 1161
             Remove jid from account_jid roster
    
    ... ... @@ -1137,6 +1173,7 @@ class Logger:
    1137 1173
                     (account_jid_id, jid_id))
    
    1138 1174
             self._timeout_commit()
    
    1139 1175
     
    
    1176
    +    @timeit
    
    1140 1177
         def add_or_update_contact(self, account_jid, jid, name, sub, ask, groups,
    
    1141 1178
                                   commit=True):
    
    1142 1179
             """
    
    ... ... @@ -1176,6 +1213,7 @@ class Logger:
    1176 1213
             if commit:
    
    1177 1214
                 self._timeout_commit()
    
    1178 1215
     
    
    1216
    +    @timeit
    
    1179 1217
         def get_roster(self, account_jid):
    
    1180 1218
             """
    
    1181 1219
             Return the accound_jid roster in NonBlockingRoster format
    
    ... ... @@ -1220,6 +1258,7 @@ class Logger:
    1220 1258
     
    
    1221 1259
             return data
    
    1222 1260
     
    
    1261
    +    @timeit
    
    1223 1262
         def remove_roster(self, account_jid):
    
    1224 1263
             """
    
    1225 1264
             Remove the roster of an account
    
    ... ... @@ -1241,6 +1280,7 @@ class Logger:
    1241 1280
             self._con.executescript(sql)
    
    1242 1281
             self._timeout_commit()
    
    1243 1282
     
    
    1283
    +    @timeit
    
    1244 1284
         def deduplicate_muc_message(self, account, jid, resource,
    
    1245 1285
                                     timestamp, message_id):
    
    1246 1286
             """
    
    ... ... @@ -1288,6 +1328,7 @@ class Logger:
    1288 1328
                 return True
    
    1289 1329
             return False
    
    1290 1330
     
    
    1331
    +    @timeit
    
    1291 1332
         def search_for_duplicate(self, account, jid, timestamp, msg):
    
    1292 1333
             """
    
    1293 1334
             Check if a message is already in the `logs` table
    
    ... ... @@ -1323,6 +1364,7 @@ class Logger:
    1323 1364
                 return True
    
    1324 1365
             return False
    
    1325 1366
     
    
    1367
    +    @timeit
    
    1326 1368
         def find_stanza_id(self, account, archive_jid, stanza_id, origin_id=None,
    
    1327 1369
                            groupchat=False):
    
    1328 1370
             """
    
    ... ... @@ -1395,6 +1437,7 @@ class Logger:
    1395 1437
             """
    
    1396 1438
             return self.get_jid_id(jid, kind, type_)
    
    1397 1439
     
    
    1440
    +    @timeit
    
    1398 1441
         def insert_into_logs(self, account, jid, time_, kind,
    
    1399 1442
                              unread=True, **kwargs):
    
    1400 1443
             """
    
    ... ... @@ -1443,6 +1486,7 @@ class Logger:
    1443 1486
     
    
    1444 1487
             return lastrowid
    
    1445 1488
     
    
    1489
    +    @timeit
    
    1446 1490
         def set_message_error(self, account_jid, jid, message_id, error):
    
    1447 1491
             """
    
    1448 1492
             Update the corresponding message with the error
    
    ... ... @@ -1471,6 +1515,7 @@ class Logger:
    1471 1515
             self._con.execute(sql, (error, account_id, jid_id, message_id))
    
    1472 1516
             self._timeout_commit()
    
    1473 1517
     
    
    1518
    +    @timeit
    
    1474 1519
         def set_marker(self, account_jid, jid, message_id, state):
    
    1475 1520
             """
    
    1476 1521
             Update the marker state of the corresponding message
    
    ... ... @@ -1503,6 +1548,7 @@ class Logger:
    1503 1548
             self._con.execute(sql, (state, account_id, jid_id, message_id))
    
    1504 1549
             self._timeout_commit()
    
    1505 1550
     
    
    1551
    +    @timeit
    
    1506 1552
         def set_avatar_sha(self, account_jid, jid, sha=None):
    
    1507 1553
             """
    
    1508 1554
             Set the avatar sha of a jid on an account
    
    ... ... @@ -1525,6 +1571,7 @@ class Logger:
    1525 1571
             self._con.execute(sql, (sha, account_jid_id, jid_id))
    
    1526 1572
             self._timeout_commit()
    
    1527 1573
     
    
    1574
    +    @timeit
    
    1528 1575
         def set_muc_avatar_sha(self, jid, sha=None):
    
    1529 1576
             """
    
    1530 1577
             Set the avatar sha of a MUC
    
    ... ... @@ -1546,6 +1593,7 @@ class Logger:
    1546 1593
     
    
    1547 1594
             self._timeout_commit()
    
    1548 1595
     
    
    1596
    +    @timeit
    
    1549 1597
         def get_muc_avatar_sha(self, jid):
    
    1550 1598
             """
    
    1551 1599
             Get the avatar sha of a MUC
    
    ... ... @@ -1559,6 +1607,7 @@ class Logger:
    1559 1607
             if row is not None:
    
    1560 1608
                 return row.avatar_sha
    
    1561 1609
     
    
    1610
    +    @timeit
    
    1562 1611
         def get_archive_infos(self, jid):
    
    1563 1612
             """
    
    1564 1613
             Get the archive infos
    
    ... ... @@ -1570,6 +1619,7 @@ class Logger:
    1570 1619
             sql = '''SELECT * FROM last_archive_message WHERE jid_id = ?'''
    
    1571 1620
             return self._con.execute(sql, (jid_id,)).fetchone()
    
    1572 1621
     
    
    1622
    +    @timeit
    
    1573 1623
         def set_archive_infos(self, jid, **kwargs):
    
    1574 1624
             """
    
    1575 1625
             Set archive infos
    
    ... ... @@ -1614,6 +1664,7 @@ class Logger:
    1614 1664
             log.info('Set message archive info: %s %s', jid, kwargs)
    
    1615 1665
             self._timeout_commit()
    
    1616 1666
     
    
    1667
    +    @timeit
    
    1617 1668
         def get_last_disco_info(self, jid, max_age=0):
    
    1618 1669
             """
    
    1619 1670
             Get last disco info from jid
    
    ... ... @@ -1648,6 +1699,7 @@ class Logger:
    1648 1699
             self._disco_info_cache[jid] = disco_info
    
    1649 1700
             return disco_info
    
    1650 1701
     
    
    1702
    +    @timeit
    
    1651 1703
         def set_last_disco_info(self, jid, disco_info):
    
    1652 1704
             """
    
    1653 1705
             Get last disco info from jid