-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.js
136 lines (110 loc) · 3.51 KB
/
query.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var assert = require('assert');
var db = require('./db');
exports.biggestWins = function (team, callback) {
assert.ok(team, 'team is required');
assert.ok(typeof(callback) === 'function', 'callback should be a function');
var biggestHomeWin;
var biggestAwayWin;
var biggestWin;
var conn = db.connect(function (error) {
if (error) { return callback(error); }
var gamesCollection = conn.db.collection('games');
gamesCollection.find({
homeTeam: team,
}).sort({
'score.fullTime.diff': -1,
'score.fullTime.away': 1,
'date': -1,
}).limit(1).toArray(function (error, games) {
if (error) { return callback(error); }
var game = games[0];
biggestHomeWin = game;
gamesCollection.find({
awayTeam: team,
}).sort({
'score.fullTime.diff': 1,
'score.fullTime.home': 1,
'date': -1,
}).limit(1).toArray(function (error, games) {
if (error) { return callback(error); }
var game = games[0];
biggestAwayWin = game;
if (biggestAwayWin.score.fullTime.diff >= biggestHomeWin.score.fullTime.diff) {
biggestWin = biggestAwayWin;
} else {
biggestWin = biggestHomeWin;
}
callback(null, biggestHomeWin, biggestAwayWin, biggestWin);
});
});
});
};
exports.numberOfSeasons = function (callback) {
assert.ok(typeof(callback) === 'function', 'callback should be a function');
var conn = db.connect(function (error) {
if (error) { return callback(error); }
var tablesCollection = conn.db.collection('tables');
tablesCollection.aggregate([
{ $group: { _id: '$name', count: { $sum: 0.5 }}}
]).sort({ count: -1 }).toArray(function (error, result) {
callback(error, result);
});
});
};
exports.biggestComeback = function (callback) {
assert.ok(typeof(callback) === 'function', 'callback should be a function');
var conn = db.connect(function (error) {
if (error) { return callback(error); }
var tablesCollection = conn.db.collection('tables');
var map = function () {
emit(this.season + '_' + this.name, {
season: this.season,
team: this.name,
position: this.position,
gamesPlayed: this.games.played,
midPosition: 0,
endPosition: 0,
improvement: 0,
});
};
var reduce = function (key, values) {
var result = {
season: values[0].season,
team: values[0].team,
position: 0,
gamesPlayed: 0,
midPosition: 0,
endPosition: 0,
improvement: 0,
};
// We assume there is going to be only two values per season-team
if (values[0].gamesPlayed > values[1].gamesPlayed) {
result.midPosition = values[1].position;
result.endPosition = values[0].position;
} else {
result.midPosition = values[0].position;
result.endPosition = values[1].position;
}
result.improvement = result.midPosition - result.endPosition;
return result;
};
tablesCollection.mapReduce(map, reduce, {
out: { inline: 1 }
}, function (error, result) {
if (error) { return callback(error); }
result = result.map(function (res) {
return res.value;
});
result.sort(function (t1, t2) {
if (t2.improvement > t1.improvement) {
return 1;
} else if (t2.improvement < t1.improvement) {
return -1
} else {
return 0;
}
});
callback(null, result[0]);
});
});
};