@@ -23,6 +23,7 @@ responsiveness is not perfect, please reload the page after resizing
23
23
<label for="until">until month:</label><br>
24
24
<input type="month" id="until" name="until"><br>
25
25
</span>
26
+
26
27
<span >
27
28
<label for="by-period">by:</label>
28
29
<select name="by-period" id="by-period">
@@ -33,7 +34,10 @@ responsiveness is not perfect, please reload the page after resizing
33
34
<option value="day">Day</option>
34
35
</select>
35
36
</span >
36
- <input type =" submit " value =" Submit " >
37
+ <div id =" submits " >
38
+ <input type="submit" value="Submit">
39
+ <span id="csv">csv</span>
40
+ </div >
37
41
</form >
38
42
39
43
</div >
@@ -101,11 +105,28 @@ responsiveness is not perfect, please reload the page after resizing
101
105
margin-bottom : 0 ;
102
106
margin-right : 1em ;
103
107
}
104
- input [type = " submit" ] {
108
+ #submits {
109
+ display : flex ;
110
+ flex-flow : column nowrap ;
111
+ align-items : stretch ;
105
112
margin-left : 1em ;
106
- border-radius : 8px ;
107
- padding-top : 5px ;
108
- background-color : #A0D683 ;
113
+
114
+ input [type ="submit "] {
115
+ padding-top : 5px ;
116
+ padding-bottom : 0px ;
117
+ border-width : 0px ;
118
+ border-radius : 8px ;
119
+ background-color : #A0D683 ;
120
+ }
121
+ #csv {
122
+ width : 100% ;
123
+ text-align : center ;
124
+ margin-top : 4px ;
125
+ background-color : #E6C767 ;
126
+ border-width : 0px ;
127
+ border-radius : 8px ;
128
+ cursor : alias ;
129
+ }
109
130
}
110
131
}
111
132
@@ -133,14 +154,10 @@ responsiveness is not perfect, please reload the page after resizing
133
154
134
155
<script >
135
156
const displayStatsFromDialog = () => {
136
- const byBin = document .getElementById (" by-period" ).value ;
157
+ const byPeriod = document .getElementById (" by-period" ).value ;
137
158
const fromMonth = document .getElementById (" from" ).value ;
138
159
const untilMonth = document .getElementById (" until" ).value ;
139
- console .log (" from" , fromMonth, " until" , untilMonth, " by" , byBin);
140
- // if (fromMonth === "") { alert("Please provide a from date"); return; }
141
- // if (untilMonth === "") { alert("Please provide a until date"); return; }
142
- console .log (" displayStatsFromDialog" , byBin, fromMonth, untilMonth)
143
- displayStats (vegaEmbed, byBin, fromMonth, untilMonth);
160
+ displayStats (vegaEmbed, byPeriod, fromMonth, untilMonth);
144
161
}
145
162
146
163
const displayStats = (vegaEmbed , byPeriod , fromMonth , untilMonth ) => {
@@ -151,7 +168,7 @@ responsiveness is not perfect, please reload the page after resizing
151
168
.then (spec => {
152
169
spec .data .url += ` /${ fromMonth} `
153
170
if (untilMonth) { spec .data .url += ` /${ untilMonth} ` }
154
- console .log (spec)
171
+ // console.log(spec)
155
172
156
173
const embedOpt = { mode: " vega-lite" };
157
174
@@ -171,10 +188,36 @@ responsiveness is not perfect, please reload the page after resizing
171
188
.catch ((error ) => showError (el, error));
172
189
})
173
190
}
191
+
192
+ const download = (download_as , text ) => {
193
+ const element = document .createElement (' a' )
194
+ element .setAttribute (
195
+ ' href' ,
196
+ ' data:text/plain;charset=utf-8,' + encodeURIComponent (text))
197
+ element .setAttribute (' download' , download_as)
198
+ element .style .display = ' none'
199
+ document .body .appendChild (element)
200
+ element .click ()
201
+ document .body .removeChild (element)
202
+ }
203
+
204
+ const dowloadCsv = () => {
205
+ const fromMonth = document .getElementById (" from" ).value
206
+ const untilMonth = document .getElementById (" until" ).value || " now"
207
+ const url = ` /api/stats/slice/csv/${ fromMonth} /${ untilMonth} `
208
+ fetch (url)
209
+ .then (response => response .text ())
210
+ .then (
211
+ text => download (
212
+ ` r2lab-stats-${ fromMonth} -${ untilMonth} .csv` , text))
213
+ }
214
+
174
215
window .addEventListener (" DOMContentLoaded" , () => {
175
216
displayStatsFromDialog ()
217
+ document .getElementById (" by-period" )
218
+ .addEventListener (" change" , displayStatsFromDialog)
219
+ document .getElementById (" csv" )
220
+ .addEventListener (" click" , dowloadCsv)
176
221
})
177
- // temporary, while we can't choose dates yet
178
- document .getElementById (" by-period" ).addEventListener (" change" , displayStatsFromDialog)
179
222
// document.querySelector('input[type="submit"]').style.display="none"
180
223
</script >
0 commit comments