forked from mit-history/analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfrp-schema.js
164 lines (144 loc) · 4.03 KB
/
cfrp-schema.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* CFRP categories and formatting
*
* Copyright (c) 2015 MIT Hyperstudio
* Christopher York, 06/2015
*
*/
const d3 = require('d3')
import {fr_spec, fr, en_spec, en} from './js/util/i18n'
const subscript = (range, items) => {
var result = range.map( (i) => items.map( (item) => item + "_" + i ) )
return result.reduce( (a, b) => a.concat(b) )
}
const playbills = d3.range(1,4)
const schema = {
time:
[ "decade",
"season",
"month",
// TODO. day yields 40,000 results -- query latency unacceptable
// "day",
"weekday" ],
performance:
subscript(playbills,
[ "author",
"title",
"genre",
"acts",
"prose_vers" ]) /*,
performance_addl:
subscript(playbills,
[ "prologue",
"musique_danse_machine",
"free_entry",
"reprise",
"firstrun",
"newactor",
"debut",
"ex_attendance",
"ex_representation",
"ex_place" ]),
theater:
[ "theater_period",
"seating_area" ] */
}
const public_aggregates = [
"sum_receipts",
"performances_days",
"mean_receipts_day",
"mean_price" ]
function group() {
return schema
}
function dimension() {
return d3.values(schema).reduce( (a,b) => a.concat(b) )
}
function aggregate() {
return public_aggregates
}
function parse(field) {
// only necessary for non-text fields received via AJAX/CSV
switch(true) {
case /^decade(_.*)?/.test(field):
return parseInt
case /^month(_.*)?/.test(field):
return parseInt
case /^weekday(_.*)?/.test(field):
return parseInt
case /^acts(_.*)?/.test(field):
return parseInt
case /^prologue(_.*)?/.test(field):
return parseBool
case /^musique_danse_machine(_.*)?/.test(field):
return parseBool
case /^free_entry(_.*)?/.test(field):
return parseBool
case /^reprise(_.*)?/.test(field):
return parseBool
case /^firstrun(_.*)?/.test(field):
return parseBool
// aggregates
case /sum_receipts/.test(field):
return parseFloat
case /performances_days/.test(field):
return parseInt
case /mean_receipts_day/.test(field):
return parseFloat
case /mean_price/.test(field):
return parseFloat
}
return (x) => x
function parseBool(s) {
switch(s) {
case 't': return true
case 'f': return false
default: return null
}
}
}
function format(lang, field) {
// TODO logic could be clearer... some much easier in ruby
// lets us use the same logic for both languages
var spec = (lang === 'en') ? en_spec : fr_spec
var fmt = (lang === 'en') ? en : fr
switch(true) {
// dimensions
case /^month(_.*)?/.test(field):
// NB. months and weekdays are kept in 1-indexed format (like postgresql; unlike javascript)
return (i) => (i === null) ? "" : spec.months[+i-1]
case /^day(_.*)?/.test(field):
return fmt.timeFormat("%a %d %b %Y")
case /^weekday(_.*)?/.test(field):
// NB. months and weekdays are kept in 1-indexed format (like postgresql; unlike javascript)
return (i) => (i === null) ? "" : spec.days[+i-1]
case /^prologue(_.*)?/.test(field):
return formatBool
case /^musique_danse_machine(_.*)?/.test(field):
return formatBool
case /^free_entry(_.*)?/.test(field):
return formatBool
case /^reprise(_.*)?/.test(field):
return formatBool
case /^firstrun(_.*)?/.test(field):
return formatBool
// aggregates
case /sum_receipts/.test(field):
return fmt.numberFormat(",.2f")
case /performances_days/.test(field):
return fmt.numberFormat(",.0f")
case /mean_receipts_day/.test(field):
return fmt.numberFormat(",.2f")
case /mean_price/.test(field):
return fmt.numberFormat(",.2f")
}
// return (x) => x ? ("*** " + x) : ""
return (x) => x ? ("" + x) : ""
function formatBool(b) {
if (b === true) { return lang === 'en' ? 'yes' : 'oui' }
if (b === false) { return lang === 'en' ? 'no' : 'non' }
if (b === "undefined") { return ""; }
return b;
}
}
export { group, dimension, aggregate, parse, format };