diff --git a/src/haxelib/Data.hx b/src/haxelib/Data.hx index b5d696bb8..b8d5007d0 100644 --- a/src/haxelib/Data.hx +++ b/src/haxelib/Data.hx @@ -56,6 +56,7 @@ typedef ProjectInfos = { var downloads : Int; var versions : Array; var tags : List; + var downloadsPerWeek : Array<{ week:String, count:Int }>; } @:enum abstract CheckLevel(Int) { diff --git a/src/haxelib/server/Repo.hx b/src/haxelib/server/Repo.hx index b9d3abcc3..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 }], }; } @@ -359,6 +360,8 @@ class Repo implements SiteApi { v.update(); p.downloads++; p.update(); + + 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() { diff --git a/src/haxelib/server/SiteDb.hx b/src/haxelib/server/SiteDb.hx index 482a0e71a..2df6681db 100644 --- a/src/haxelib/server/SiteDb.hx +++ b/src/haxelib/server/SiteDb.hx @@ -109,6 +109,13 @@ class Version extends Object { } +@:id(pid,date) +class Downloads extends Object { + public var pid : Int; + public var date : SDate; + public var num : Int; +} + @:id(user,project) class Developer extends Object { @@ -145,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)) diff --git a/test/IntegrationTests.hx b/test/IntegrationTests.hx index a40cd5e7b..27589bd57 100644 --- a/test/IntegrationTests.hx +++ b/test/IntegrationTests.hx @@ -201,6 +201,7 @@ class IntegrationTests extends TestBase { runner.add(new tests.integration.TestDev()); runner.add(new tests.integration.TestRun()); runner.add(new tests.integration.TestPath()); + 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..a291becb9 --- /dev/null +++ b/test/tests/integration/TestDownloads.hx @@ -0,0 +1,105 @@ +package tests.integration; + +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); + } + + { + 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 = 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; + 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 ) + { + assertTrue(row.num == 1); + } + } + + { + var r = haxelib(["remove", "Bar"]).result(); + assertSuccess(r); + } + } +} \ No newline at end of file