-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
136 lines (115 loc) · 4.85 KB
/
app.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
import { app, sparql, sparqlEscapeDate } from 'mu';
// helpers since a given date
app.get('/helpers', function(req, res, next) {
const since = req.query.since || new Date().toISOString();
const query = `
PREFIX schema: <http://schema.org/>
PREFIX bravoer: <http://mu.semte.ch/vocabularies/ext/bravoer/>
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT DISTINCT ?uuid
WHERE {
?e a schema:Event ;
schema:additionalType <http://mu.semte.ch/vocabularies/ext/bravoer/event-types/Concert> ;
schema:startDate ?startDate .
FILTER(?startDate >= ${sparqlEscapeDate(since)})
?e bravoer:helper ?s .
?s a bravoer:Sympathizer ;
mu:uuid ?uuid .
}`;
sparql.query(query).then( function(response) {
const uuids = response.results.bindings.map( function(binding) {
return binding.uuid.value;
});
res.json({ data: { uuids: uuids } });
}).catch( function(err) {
next(new Error(err));
});
} );
// attendee/absentee count for repetition between 2 given dates
app.get('/statistics/attendances/events', function(req, res, next) {
const from = req.query.from;
const to = req.query.to || new Date().toISOString();
const query = `
PREFIX schema: <http://schema.org/>
PREFIX bravoer: <http://mu.semte.ch/vocabularies/ext/bravoer/>
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?e ?uuid ?startDate (COUNT(?attendee) as ?attendeeCount) (COUNT(?absentee) as ?absenteeCount)
WHERE {
?e a schema:Event ;
mu:uuid ?uuid ;
schema:additionalType <http://mu.semte.ch/vocabularies/ext/bravoer/event-types/Rehearsal> ;
schema:startDate ?startDate .
FILTER(?startDate >= ${sparqlEscapeDate(from)})
FILTER(?startDate <= ${sparqlEscapeDate(to)})
{ ?e bravoer:attendee ?attendee . } UNION { ?e bravoer:absentee ?absentee . }
}
GROUP BY ?e ?uuid ?startDate
ORDER BY DESC(?startDate)
`;
sparql.query(query).then( function(response) {
const statistics = response.results.bindings.map( function(binding) {
return {
event: binding.e.value,
eventId: binding.uuid.value,
startDate: binding.startDate.value,
attendeeCount: binding.attendeeCount.value,
absenteeCount: binding.absenteeCount.value
};
});
res.json({ data: statistics });
}).catch( function(err) {
next(new Error(err));
});
} );
// attendee/absentee count for musician between 2 given dates
app.get('/statistics/attendances/musicians', function(req, res, next) {
const from = req.query.from;
const to = req.query.to || new Date().toISOString();
const query = `
PREFIX schema: <http://schema.org/>
PREFIX bravoer: <http://mu.semte.ch/vocabularies/ext/bravoer/>
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
SELECT ?m ?uuid ?firstName ?lastName (COUNT(?attendEvent) as ?attendancesCount) (COUNT(?absentEvent) as ?absencesCount)
WHERE {
?m a bravoer:Musician ;
mu:uuid ?uuid ;
vcard:hasGivenName ?firstName ;
vcard:hasFamilyName ?lastName .
{
?attendEvent bravoer:attendee ?m ;
a schema:Event ;
schema:additionalType <http://mu.semte.ch/vocabularies/ext/bravoer/event-types/Rehearsal> ;
schema:startDate ?startDate .
}
UNION
{
?absentEvent bravoer:absentee ?m ;
a schema:Event ;
schema:additionalType <http://mu.semte.ch/vocabularies/ext/bravoer/event-types/Rehearsal> ;
schema:startDate ?startDate .
}
FILTER(?startDate >= ${sparqlEscapeDate(from)})
FILTER(?startDate <= ${sparqlEscapeDate(to)})
}
GROUP BY ?m ?uuid ?firstName ?lastName
ORDER BY DESC(?attendancesCount)
`;
sparql.query(query).then( function(response) {
const statistics = response.results.bindings.map( function(binding) {
return {
musician: binding.m.value,
musicianId: binding.uuid.value,
name: `${binding.firstName.value} ${binding.lastName.value}`,
attendancesCount: binding.attendancesCount.value,
absencesCount: binding.absencesCount.value
};
});
res.json({ data: statistics });
}).catch( function(err) {
next(new Error(err));
});
} );