Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add haxelib downloads per day #432

Open
wants to merge 16 commits into
base: development
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions src/haxelib/Data.hx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ typedef ProjectInfos = {
var downloads : Int;
var versions : Array<VersionInfos>;
var tags : List<String>;
var downloadsPerWeek : Array<{ week:String, count:Int }>;
}

@:enum abstract CheckLevel(Int) {
Expand Down
3 changes: 3 additions & 0 deletions src/haxelib/server/Repo.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }],
};
}

Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 9 additions & 1 deletion src/haxelib/server/SiteDb.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions test/IntegrationTests.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
105 changes: 105 additions & 0 deletions test/tests/integration/TestDownloads.hx
Original file line number Diff line number Diff line change
@@ -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);
}
}
}