[tor-commits] [gettor/master] Update create_db script and stats db operations

hiro at torproject.org hiro at torproject.org
Thu May 23 18:35:02 UTC 2019


commit 9723be4280fc06811054d85274f5ac6c8e19ee15
Author: hiro <hiro at torproject.org>
Date:   Thu May 23 20:34:58 2019 +0200

    Update create_db script and stats db operations
---
 gettor/utils/db.py    | 8 ++++----
 scripts/create_db     | 9 +++++----
 tests/test_locales.py | 2 +-
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/gettor/utils/db.py b/gettor/utils/db.py
index c48d36a..525287b 100644
--- a/gettor/utils/db.py
+++ b/gettor/utils/db.py
@@ -88,12 +88,12 @@ class SQLite3(object):
 		Update statistics to the database
 		"""
 		now_str = datetime.now().strftime("%Y%m%d")
-		query = "REPLACE INTO stats(num_requests, platform, language, "\
-		"command, service, date) VALUES(COALESCE((SELECT num_requests FROM stats "\
-		"WHERE platform=? AND language=? AND date=?)+1, 0), ?, ?, ?, ?, ?) "\
+		query = "INSERT INTO stats(num_requests, platform, language, command, "\
+		        "service, date) VALUES (1, ?, ?, ?, ?, ?) ON CONFLICT(platform, "\
+				"language, command, service, date) DO UPDATE SET num_requests=num_requests+1"
 
 		return self.dbpool.runQuery(
-			query, (platform, language, now_str, platform, language, command, service, now_str)
+			query, (platform, language, command, service, now_str)
 		).addCallback(self.query_callback).addErrback(self.query_errback)
 
 	def get_links(self, platform, language, status):
diff --git a/scripts/create_db b/scripts/create_db
index 7f8cc11..32121df 100755
--- a/scripts/create_db
+++ b/scripts/create_db
@@ -91,7 +91,8 @@ def main():
             )
             c.execute(
                 "CREATE TABLE stats(num_requests NUMBER, platform TEXT,"
-                " language TEXT, command TEXT, service TEXT, date TEXT)"
+                " language TEXT, command TEXT, service TEXT, date,"
+                " PRIMARY KEY (platform, language, command, service, date))"
             )
         print("Database {} created.".format(abs_filename))
     elif args.clear:
@@ -125,9 +126,9 @@ def main():
                         "PRIMARY KEY(platform, arch, version, provider, status))"
                     )
                     c.execute(
-                        "CREATE TABLE stats(date TEXT PRIMARY KEY, "
-                        "num_requests INTEGER, platform TEXT, language TEXT, command TEXT, "
-                        "service TEXT)"
+                        "CREATE TABLE stats(date TEXT, num_requests INTEGER, "
+                        "platform TEXT, language TEXT, command TEXT, service TEXT,"
+                        "PRIMARY KEY (platform, language, command, service, date))"
                     )
                     print("New database {} created.".format(abs_filename))
 
diff --git a/tests/test_locales.py b/tests/test_locales.py
index b6eb777..bea7e9f 100644
--- a/tests/test_locales.py
+++ b/tests/test_locales.py
@@ -6,7 +6,7 @@ from twisted.internet import task
 
 from . import conftests
 
-class EmailServiceTests(unittest.TestCase):
+class LocalesTests(unittest.TestCase):
 
     # Fail any tests which take longer than 15 seconds.
     timeout = 15



More information about the tor-commits mailing list