From e2edc2bb77eaeb5a83147af6b6414a7ee7e3ee45 Mon Sep 17 00:00:00 2001 From: flashultra Date: Sat, 27 Oct 2018 21:06:53 +0300 Subject: [PATCH 01/14] Add downloads per day --- src/haxelib/server/Repo.hx | 11 +++++++++++ src/haxelib/server/SiteDb.hx | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/src/haxelib/server/Repo.hx b/src/haxelib/server/Repo.hx index b9d3abcc3..170c69c3b 100644 --- a/src/haxelib/server/Repo.hx +++ b/src/haxelib/server/Repo.hx @@ -359,6 +359,17 @@ class Repo implements SiteApi { v.update(); p.downloads++; p.update(); + + var d = Downloads.manager.select($pid == p.id); + if ( d == null ) { + d.pid = p.id; + d.num = 1; + d.date = Date.now(); + d.insert(); + } else { + d.num++; + d.update(); + } } static function main() { diff --git a/src/haxelib/server/SiteDb.hx b/src/haxelib/server/SiteDb.hx index 482a0e71a..a74374bc7 100644 --- a/src/haxelib/server/SiteDb.hx +++ b/src/haxelib/server/SiteDb.hx @@ -109,6 +109,14 @@ class Version extends Object { } +@:id(pid) +@:index(date) +class Downloads extends Object { + public var pid : Int; + public var date : SDate; + public var num : Int; +} + @:id(user,project) class Developer extends Object { From 7c04a43be05084b92139f84a9be3594c5bc872f8 Mon Sep 17 00:00:00 2001 From: flashultra Date: Sat, 27 Oct 2018 21:47:04 +0300 Subject: [PATCH 02/14] Fixes --- src/haxelib/server/Repo.hx | 2 +- src/haxelib/server/SiteDb.hx | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/haxelib/server/Repo.hx b/src/haxelib/server/Repo.hx index 170c69c3b..f44729e2b 100644 --- a/src/haxelib/server/Repo.hx +++ b/src/haxelib/server/Repo.hx @@ -360,7 +360,7 @@ class Repo implements SiteApi { p.downloads++; p.update(); - var d = Downloads.manager.select($pid == p.id); + var d = Downloads.manager.select({ $pid : p.id, date : Date.now() }); if ( d == null ) { d.pid = p.id; d.num = 1; diff --git a/src/haxelib/server/SiteDb.hx b/src/haxelib/server/SiteDb.hx index a74374bc7..b66425143 100644 --- a/src/haxelib/server/SiteDb.hx +++ b/src/haxelib/server/SiteDb.hx @@ -109,8 +109,7 @@ class Version extends Object { } -@:id(pid) -@:index(date) +@:index(pid,date,unique) class Downloads extends Object { public var pid : Int; public var date : SDate; From e08089519dea51057f2937a34243e4c13f410e0d Mon Sep 17 00:00:00 2001 From: flashultra Date: Mon, 29 Oct 2018 07:41:40 +0200 Subject: [PATCH 03/14] Fix racing condtions for haxelib downloads --- src/haxelib/server/Repo.hx | 11 +---------- src/haxelib/server/SiteDb.hx | 3 ++- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/haxelib/server/Repo.hx b/src/haxelib/server/Repo.hx index f44729e2b..f5b47a656 100644 --- a/src/haxelib/server/Repo.hx +++ b/src/haxelib/server/Repo.hx @@ -360,16 +360,7 @@ class Repo implements SiteApi { p.downloads++; p.update(); - var d = Downloads.manager.select({ $pid : p.id, date : Date.now() }); - if ( d == null ) { - d.pid = p.id; - d.num = 1; - d.date = Date.now(); - d.insert(); - } else { - d.num++; - d.update(); - } + Downloads.manager.cnx.request("INSERT INTO Downloads ( `pid`, `date`, `num`) VALUES (${p.id}, 1, CURDATE() ) ON DUPLICATE KEY UPDATE num = num +1 "); } static function main() { diff --git a/src/haxelib/server/SiteDb.hx b/src/haxelib/server/SiteDb.hx index b66425143..c4c3114c1 100644 --- a/src/haxelib/server/SiteDb.hx +++ b/src/haxelib/server/SiteDb.hx @@ -152,7 +152,8 @@ class SiteDb { Project.manager, Tag.manager, Version.manager, - Developer.manager + Developer.manager, + Downloads.manager ]; for (m in managers) if (!TableCreate.exists(m)) From c5bf48a8fe003097149ba3d243a4bc80dcd697f1 Mon Sep 17 00:00:00 2001 From: flashultra Date: Mon, 29 Oct 2018 07:49:55 +0200 Subject: [PATCH 04/14] Fix order --- src/haxelib/server/Repo.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/haxelib/server/Repo.hx b/src/haxelib/server/Repo.hx index f5b47a656..1a179ddd0 100644 --- a/src/haxelib/server/Repo.hx +++ b/src/haxelib/server/Repo.hx @@ -360,7 +360,7 @@ class Repo implements SiteApi { p.downloads++; p.update(); - Downloads.manager.cnx.request("INSERT INTO Downloads ( `pid`, `date`, `num`) VALUES (${p.id}, 1, CURDATE() ) ON DUPLICATE KEY UPDATE num = num +1 "); + Downloads.manager.cnx.request("INSERT INTO Downloads ( `pid`, `date`, `num`) VALUES (${p.id}, CURDATE(), 1 ) ON DUPLICATE KEY UPDATE num = num +1 "); } static function main() { From 69cb1d1d601cab7ed6fdb24e864091f26820e16f Mon Sep 17 00:00:00 2001 From: flashultra Date: Tue, 30 Oct 2018 07:58:38 +0200 Subject: [PATCH 05/14] Add integration test --- src/haxelib/server/Repo.hx | 2 +- test/IntegrationTests.hx | 1 + test/tests/integration/TestDownloads.hx | 57 +++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/tests/integration/TestDownloads.hx diff --git a/src/haxelib/server/Repo.hx b/src/haxelib/server/Repo.hx index 1a179ddd0..75fce962c 100644 --- a/src/haxelib/server/Repo.hx +++ b/src/haxelib/server/Repo.hx @@ -360,7 +360,7 @@ class Repo implements SiteApi { p.downloads++; p.update(); - Downloads.manager.cnx.request("INSERT INTO Downloads ( `pid`, `date`, `num`) VALUES (${p.id}, CURDATE(), 1 ) ON DUPLICATE KEY UPDATE num = num +1 "); + Downloads.manager.cnx.request("INSERT INTO Downloads ( `pid`, `date`, `num`) VALUES (${p.id}, CURDATE(), 1 ) ON DUPLICATE KEY UPDATE num = num +1;"); } static function main() { diff --git a/test/IntegrationTests.hx b/test/IntegrationTests.hx index dab056234..8e9c345f0 100644 --- a/test/IntegrationTests.hx +++ b/test/IntegrationTests.hx @@ -202,6 +202,7 @@ class IntegrationTests extends TestBase { runner.add(new tests.integration.TestUser()); runner.add(new tests.integration.TestOwner()); runner.add(new tests.integration.TestDev()); + runner.add(new tests.integration.TestDownloads()); var success = runner.run(); if (!success) { diff --git a/test/tests/integration/TestDownloads.hx b/test/tests/integration/TestDownloads.hx new file mode 100644 index 000000000..fbe2608ff --- /dev/null +++ b/test/tests/integration/TestDownloads.hx @@ -0,0 +1,57 @@ +package tests.integration; + +import haxe.io.Path; +import IntegrationTests.*; +using IntegrationTests; + +class TestDownloads extends IntegrationTests { + function test():Void { + { + var r = haxelib(["register", bar.user, bar.email, bar.fullname, bar.pw, bar.pw]).result(); + assertSuccess(r); + } + + { + var r = haxelib(["submit", Path.join([IntegrationTests.projectRoot, "test/libraries/libBar.zip"]), bar.pw]).result(); + assertSuccess(r); + } + + { + var r = haxelib(["search", "Bar"]).result(); + assertSuccess(r); + assertTrue(r.out.indexOf("Bar") >= 0); + } + + { + var r = haxelib(["install", "Bar"]).result(); + assertSuccess(r); + } + + { + var r = haxelib(["list", "Bar"]).result(); + assertTrue(r.out.indexOf("Bar") >= 0); + assertSuccess(r); + } + + { + var db = dbConfig.database; + dbCnx.request('USE ${db};'); + var projectRequest = dbCnx.request("SELECT id FROM Project WHERE name = 'Bar';"); + var pid = projectRequest.getIntResult(0); + var rqOne = dbCnx.request('SELECT num FROM Downloads WHERE pid = ${pid} AND `date` = CURDATE();'); + var num = rqOne.getIntResult(0); + assertTrue(num == 1); + assertSuccess(r); + var rmv = haxelib(["remove", "Bar"]).result(); + var inst = haxelib(["install", "Bar"]).result(); + var rqTwo = dbCnx.request('SELECT num FROM Downloads WHERE pid = ${pid} AND `date` = CURDATE();'); + num = rqTwo.getIntResult(0); + assertTrue(num == 2); + } + + { + var r = haxelib(["remove", "Bar"]).result(); + assertSuccess(r); + } + } +} \ No newline at end of file From 5b62fb5b82d3b62ae33d34085093e86acccd6e06 Mon Sep 17 00:00:00 2001 From: flashultra Date: Tue, 30 Oct 2018 11:21:21 +0200 Subject: [PATCH 06/14] Fix ctx access --- src/haxelib/server/Repo.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/haxelib/server/Repo.hx b/src/haxelib/server/Repo.hx index 75fce962c..2633faafa 100644 --- a/src/haxelib/server/Repo.hx +++ b/src/haxelib/server/Repo.hx @@ -360,7 +360,7 @@ class Repo implements SiteApi { p.downloads++; p.update(); - Downloads.manager.cnx.request("INSERT INTO Downloads ( `pid`, `date`, `num`) VALUES (${p.id}, CURDATE(), 1 ) ON DUPLICATE KEY UPDATE num = num +1;"); + Manager.cnx.request("INSERT INTO Downloads ( `pid`, `date`, `num`) VALUES (${p.id}, CURDATE(), 1 ) ON DUPLICATE KEY UPDATE num = num +1;"); } static function main() { From 67704c1fad96e2bb8bff7d226a2085021f703cd0 Mon Sep 17 00:00:00 2001 From: flashultra Date: Tue, 30 Oct 2018 11:38:47 +0200 Subject: [PATCH 07/14] Fix sys.db.Manager --- src/haxelib/server/Repo.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/haxelib/server/Repo.hx b/src/haxelib/server/Repo.hx index 2633faafa..44412b11e 100644 --- a/src/haxelib/server/Repo.hx +++ b/src/haxelib/server/Repo.hx @@ -360,7 +360,7 @@ class Repo implements SiteApi { p.downloads++; p.update(); - Manager.cnx.request("INSERT INTO Downloads ( `pid`, `date`, `num`) VALUES (${p.id}, CURDATE(), 1 ) ON DUPLICATE KEY UPDATE num = num +1;"); + sys.db.Manager.cnx.request("INSERT INTO Downloads ( `pid`, `date`, `num`) VALUES (${p.id}, CURDATE(), 1 ) ON DUPLICATE KEY UPDATE num = num +1;"); } static function main() { From 35e1bc8f76ba10769d94a8f81383e2c04e12d414 Mon Sep 17 00:00:00 2001 From: flashultra Date: Tue, 30 Oct 2018 14:39:58 +0200 Subject: [PATCH 08/14] Add primary key --- src/haxelib/server/SiteDb.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/haxelib/server/SiteDb.hx b/src/haxelib/server/SiteDb.hx index c4c3114c1..2df6681db 100644 --- a/src/haxelib/server/SiteDb.hx +++ b/src/haxelib/server/SiteDb.hx @@ -109,7 +109,7 @@ class Version extends Object { } -@:index(pid,date,unique) +@:id(pid,date) class Downloads extends Object { public var pid : Int; public var date : SDate; From 3782c2a006d2cb96d7f3549dee2f4757f90d6928 Mon Sep 17 00:00:00 2001 From: flashultra Date: Tue, 30 Oct 2018 15:12:03 +0200 Subject: [PATCH 09/14] Fix integration test --- test/tests/integration/TestDownloads.hx | 1 - 1 file changed, 1 deletion(-) diff --git a/test/tests/integration/TestDownloads.hx b/test/tests/integration/TestDownloads.hx index fbe2608ff..76f6a2a3b 100644 --- a/test/tests/integration/TestDownloads.hx +++ b/test/tests/integration/TestDownloads.hx @@ -41,7 +41,6 @@ class TestDownloads extends IntegrationTests { var rqOne = dbCnx.request('SELECT num FROM Downloads WHERE pid = ${pid} AND `date` = CURDATE();'); var num = rqOne.getIntResult(0); assertTrue(num == 1); - assertSuccess(r); var rmv = haxelib(["remove", "Bar"]).result(); var inst = haxelib(["install", "Bar"]).result(); var rqTwo = dbCnx.request('SELECT num FROM Downloads WHERE pid = ${pid} AND `date` = CURDATE();'); From 3dd136774b304ae8fcc779f28944b63588292ee7 Mon Sep 17 00:00:00 2001 From: flashultra Date: Wed, 31 Oct 2018 21:03:51 +0200 Subject: [PATCH 10/14] Add 'downloads per week' in infos. --- src/haxelib/server/Repo.hx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/haxelib/server/Repo.hx b/src/haxelib/server/Repo.hx index 44412b11e..9db7fa026 100644 --- a/src/haxelib/server/Repo.hx +++ b/src/haxelib/server/Repo.hx @@ -78,6 +78,7 @@ class Repo implements SiteApi { license : p.license, downloads : totalDownloads, tags : Tag.manager.search($project == p.id).map(function(t) return t.tag), + downloadsPerWeek: [for (d in sys.db.Manager.cnx.request('SELECT (WEEKOFYEAR(`date` ) + IF( WEEKDAY( `date` ) > WEEKDAY( DATE_SUB(CURDATE(), INTERVAL 1 DAY) ) , 1, 0 )) weeknumber, SUM( num ) cnum , CONCAT(Date_Format(MIN(`date`),\'%Y-%m-%d\'),\' to \',Date_Format(MAX(`date`),\'%Y-%m-%d\')) weeklydownloads FROM `Downloads` WHERE pid =${p.id} and `date` < CURDATE() GROUP BY weeknumber ')) { week:d.weeklydownloads, count: d.cnum }], }; } From 3a4987f8505e07b11b6283b54f0c553ee55f762e Mon Sep 17 00:00:00 2001 From: flashultra Date: Wed, 31 Oct 2018 22:13:13 +0200 Subject: [PATCH 11/14] Add downloads per week in ProjectInfos --- src/haxelib/Data.hx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/haxelib/Data.hx b/src/haxelib/Data.hx index 2f4e2273d..7e29b4972 100644 --- a/src/haxelib/Data.hx +++ b/src/haxelib/Data.hx @@ -55,6 +55,7 @@ typedef ProjectInfos = { var downloads : Int; var versions : Array; var tags : List; + var downloadsPerWeek : Array<{ week:String, count:Int }>; } abstract DependencyVersion(String) to String from SemVer { From 7c25afd5f7cc739c99e9e7a94dede24cc1a9c5ed Mon Sep 17 00:00:00 2001 From: flashultra Date: Sat, 3 Nov 2018 21:19:15 +0200 Subject: [PATCH 12/14] Change check for test downloads --- test/tests/integration/TestDownloads.hx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/test/tests/integration/TestDownloads.hx b/test/tests/integration/TestDownloads.hx index 76f6a2a3b..376e32da5 100644 --- a/test/tests/integration/TestDownloads.hx +++ b/test/tests/integration/TestDownloads.hx @@ -39,13 +39,18 @@ class TestDownloads extends IntegrationTests { var projectRequest = dbCnx.request("SELECT id FROM Project WHERE name = 'Bar';"); var pid = projectRequest.getIntResult(0); var rqOne = dbCnx.request('SELECT num FROM Downloads WHERE pid = ${pid} AND `date` = CURDATE();'); - var num = rqOne.getIntResult(0); - assertTrue(num == 1); + assertTrue(rqOne.length == 1); + for( row in rqOne ) + { + assertTrue(row.num == 1); + } var rmv = haxelib(["remove", "Bar"]).result(); var inst = haxelib(["install", "Bar"]).result(); var rqTwo = dbCnx.request('SELECT num FROM Downloads WHERE pid = ${pid} AND `date` = CURDATE();'); - num = rqTwo.getIntResult(0); - assertTrue(num == 2); + for( row in rqTwo ) + { + assertTrue(row.num == 2); + } } { From 2fac6008a0ae5071a957aff984ac7b34f715225a Mon Sep 17 00:00:00 2001 From: flashultra Date: Sun, 4 Nov 2018 10:58:34 +0200 Subject: [PATCH 13/14] Change pid --- test/tests/integration/TestDownloads.hx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/tests/integration/TestDownloads.hx b/test/tests/integration/TestDownloads.hx index 376e32da5..97d632916 100644 --- a/test/tests/integration/TestDownloads.hx +++ b/test/tests/integration/TestDownloads.hx @@ -37,7 +37,12 @@ class TestDownloads extends IntegrationTests { var db = dbConfig.database; dbCnx.request('USE ${db};'); var projectRequest = dbCnx.request("SELECT id FROM Project WHERE name = 'Bar';"); - var pid = projectRequest.getIntResult(0); + var pid = 0; + trace("Found "+projectRequest.length+" Bar projects"); + for( row in projectRequest ) + { + pid = row.id; + } var rqOne = dbCnx.request('SELECT num FROM Downloads WHERE pid = ${pid} AND `date` = CURDATE();'); assertTrue(rqOne.length == 1); for( row in rqOne ) From ccb6cba936327377b97918e0eeee196015438f1c Mon Sep 17 00:00:00 2001 From: flashultra Date: Wed, 9 Jan 2019 20:36:20 +0200 Subject: [PATCH 14/14] Add database checks in integration test file --- test/tests/integration/TestDownloads.hx | 57 +++++++++++++++++++++---- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/test/tests/integration/TestDownloads.hx b/test/tests/integration/TestDownloads.hx index 97d632916..a291becb9 100644 --- a/test/tests/integration/TestDownloads.hx +++ b/test/tests/integration/TestDownloads.hx @@ -4,8 +4,43 @@ import haxe.io.Path; import IntegrationTests.*; using IntegrationTests; +import sys.db.*; +import sys.db.Types; +import haxelib.server.Paths.*; + + +@:id(pid,date) +class Downloads extends Object { + public var pid : Int; + public var date : SDate; + public var num : Int; +} + class TestDownloads extends IntegrationTests { function test():Void { + { + var db : Connection = + if (Sys.getEnv("HAXELIB_DB_HOST") != null) + Mysql.connect({ + "host": Sys.getEnv("HAXELIB_DB_HOST"), + "port": Std.parseInt(Sys.getEnv("HAXELIB_DB_PORT")), + "database": Sys.getEnv("HAXELIB_DB_NAME"), + "user": Sys.getEnv("HAXELIB_DB_USER"), + "pass": Sys.getEnv("HAXELIB_DB_PASS"), + "socket": null + }); + else if (sys.FileSystem.exists(DB_CONFIG)) + Mysql.connect(haxe.Json.parse(sys.io.File.getContent(DB_CONFIG))); + else + Sqlite.open(DB_FILE); + + Manager.cnx = db; + Manager.initialize(); + + if (!TableCreate.exists(Downloads.manager)) + TableCreate.create(Downloads.manager); + } + { var r = haxelib(["register", bar.user, bar.email, bar.fullname, bar.pw, bar.pw]).result(); assertSuccess(r); @@ -32,29 +67,33 @@ class TestDownloads extends IntegrationTests { assertTrue(r.out.indexOf("Bar") >= 0); assertSuccess(r); } + + { + var db = dbConfig.database; + dbCnx.request('USE ${db};'); + var projectRequest = dbCnx.request("SELECT id FROM Project WHERE name = 'Bar';"); + var pid = 0; + for( row in projectRequest ) + { + pid = row.id; + } + dbCnx.request('INSERT INTO Downloads ( `pid`, `date`, `num`) VALUES (${pid}, CURDATE(), 1 ) ON DUPLICATE KEY UPDATE num = num +1;'); + } { var db = dbConfig.database; dbCnx.request('USE ${db};'); var projectRequest = dbCnx.request("SELECT id FROM Project WHERE name = 'Bar';"); var pid = 0; - trace("Found "+projectRequest.length+" Bar projects"); for( row in projectRequest ) { pid = row.id; } var rqOne = dbCnx.request('SELECT num FROM Downloads WHERE pid = ${pid} AND `date` = CURDATE();'); - assertTrue(rqOne.length == 1); + assertTrue(rqOne.length==1); for( row in rqOne ) { assertTrue(row.num == 1); - } - var rmv = haxelib(["remove", "Bar"]).result(); - var inst = haxelib(["install", "Bar"]).result(); - var rqTwo = dbCnx.request('SELECT num FROM Downloads WHERE pid = ${pid} AND `date` = CURDATE();'); - for( row in rqTwo ) - { - assertTrue(row.num == 2); } }