aboutsummaryrefslogtreecommitdiff
path: root/pipeline/ui.sh
blob: 8fbcde0b0c75cf96281ae72574f54ab54a63f3d7 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/bin/bash
#
# Build the user interface.
#
# Usage:
#   ./ui.sh <function name>

set -o nounset
set -o pipefail
set -o errexit

readonly THIS_DIR=$(dirname $0)
readonly RAPPOR_SRC=$(cd $THIS_DIR/.. && pwd)

source $RAPPOR_SRC/pipeline/tools-lib.sh

# Change the default location of this file by setting DEP_DYGRAPHS_JS
readonly DYGRAPHS_JS=${DEP_DYGRAPHS_JS:-$RAPPOR_SRC/third_party/dygraph-combined.js}

_link() {
  ln --verbose -s -f "$@"
}

_copy() {
  cp --verbose -f "$@"
}

download-dygraphs() {
  local out=third_party
  wget --directory $out \
    http://dygraphs.com/1.1.1/dygraph-combined.js
}

import-table() {
  local src=~/git/scratch/ajax/
  cp --verbose $src/table-sort.{js,css} $src/url-hash.js ui
  pushd ui
  # TODO: Could minify it here
  cat table-sort.js url-hash.js > table-lib.js
  popd
}

# Use symlinks so we can edit and reload during development.
symlink-static() {
  local kind=$1
  local job_dir=$2

  local base=$RAPPOR_SRC/ui

  # HTML goes at the top level.
  if test "$kind" = dist; then
    _link \
      $base/overview.html $base/histograms.html $base/metric.html $base/day.html \
      $job_dir
  elif test "$kind" = assoc; then
    _link \
      $base/assoc-overview.html $base/assoc-metric.html $base/assoc-pair.html \
      $base/assoc-day.html \
      $job_dir
  else 
    log "Invalid kind $kind"
    exit 1
  fi

  mkdir --verbose -p $job_dir/static

  # Static subdir.
  _link \
    $base/ui.css $base/ui.js \
    $base/table-sort.css $base/table-lib.js \
    $DYGRAPHS_JS \
    $job_dir/static
}


# Write HTML fragment based on overview.csv.
overview-part-html() {
  local job_dir=${1:-_tmp/results-10}
  local out=$job_dir/cooked/overview.part.html
  # Sort by descending date!
  TOOLS-csv-to-html \
    --col-format 'metric <a href="metric.html#metric={metric}">{metric}</a>' \
    < $job_dir/cooked/overview.csv \
    > $out
  echo "Wrote $out"
}

metric-part-html() {
  local job_dir=${1:-_tmp/results-10}
  # Testing it out.  This should probably be a different dir.

  for entry in $job_dir/cooked/*; do
    # Only do it for dirs
    if ! test -d $entry; then
      continue
    fi
    # Now it's a metric dir
    echo $entry

    local metric_name=$(basename $entry)

    # Convert status.csv to status.part.html (a fragment)

    # NOTE: counts path could be useful.  You need the input tree though.  Hash
    # it?  Or point to the git link.

    # Link to raw CSV
    #--col-format 'date <a href="../../raw/{metric}/{date}/results.csv">{date}</a>' \

    # TODO: Link to ui/results_viewer.html#{metric}_{date}
    # And that needs some JavaScript to load the correct fragment.
    # I guess you could do the same with metric.html.  Currently it uses a
    # symlink.

    # Before job ID:
    # --col-format 'date <a href="{date}.html">{date}</a>' \
    # --col-format 'status <a href="../../raw/{metric}/{date}/log.txt">{status}</a>' \

    local fmt1='date <a href="day.html#jobId={job_id}&metric={metric}&date={date}">{date}</a>'
    local fmt2='status <a href="../{job_id}/raw/{metric}/{date}/log.txt">{status}</a>'

    TOOLS-csv-to-html \
      --def "metric $metric_name" \
      --col-format "$fmt1" \
      --col-format "$fmt2" \
      < $entry/status.csv \
      > $entry/status.part.html
  done
}

results-html-one() {
  local csv_in=$1
  echo "$csv_in -> HTML"

  # .../raw/Settings.HomePage2/2015-03-01/results.csv ->
  # .../cooked/Settings.HomePage2/2015-03-01.part.html
  # (This saves some directories)
  local html_out=$(echo $csv_in | sed -e 's|/raw/|/cooked/|; s|/results.csv|.part.html|')

  TOOLS-csv-to-html < $csv_in > $html_out
}

results-html() {
  local job_dir=${1:-_tmp/results-10}

  find $job_dir -name results.csv \
    | xargs -n 1 --verbose --no-run-if-empty -- $0 results-html-one
}

# Build parts of the HTML
build-html1() {
  local job_dir=${1:-_tmp/results-10}

  symlink-static dist $job_dir

  # writes overview.part.html, which is loaded by overview.html
  overview-part-html $job_dir

  # Writes status.part.html for each metric
  metric-part-html $job_dir
}

#
# Association Analysis
#

readonly ASSOC_TEST_JOB_DIR=~/rappor/chrome-assoc-smoke/smoke5-assoc

# Write HTML fragment based on CSV.
assoc-overview-part-html() {
  local job_dir=${1:-$ASSOC_TEST_JOB_DIR}
  local html_path=$job_dir/cooked/assoc-overview.part.html

  # Sort by descending date!

  TOOLS-csv-to-html \
    --col-format 'metric <a href="assoc-metric.html#metric={metric}">{metric}</a>' \
    < $job_dir/cooked/assoc-overview.csv \
    > $html_path
  echo "Wrote $html_path"
}

assoc-metric-part-html-one() {
  local csv_path=$1
  local html_path=$(echo $csv_path | sed 's/.csv$/.part.html/')

  local metric_dir=$(dirname $csv_path)
  local metric_name=$(basename $metric_dir)  # e.g. interstitial.harmful

  local fmt='days <a href="assoc-pair.html#metric={metric}&var1={var1}&var2={var2}">{days}</a>'

  TOOLS-csv-to-html \
    --def "metric $metric_name" \
    --col-format "$fmt" \
    < $csv_path \
    > $html_path

  echo "Wrote $html_path"
}

assoc-metric-part-html() {
  local job_dir=${1:-$ASSOC_TEST_JOB_DIR}
  # Testing it out.  This should probably be a different dir.

  find $job_dir/cooked -name metric-status.csv \
    | xargs -n 1 --verbose --no-run-if-empty -- $0 assoc-metric-part-html-one
}

# TODO:
# - Construct link in JavaScript instead?  It has more information.  The
# pair-metadata.txt file is a hack.

assoc-pair-part-html-one() {
  local csv_path=$1
  local html_path=$(echo $csv_path | sed 's/.csv$/.part.html/')

  local pair_dir_path=$(dirname $csv_path)
  local pair_dir_name=$(basename $pair_dir_path)  # e.g. domain_X_flags_IS_REPEAT_VISIT

  # This file is generated by metric_status.R for each pair of variables.
  local metadata="$pair_dir_path/pair-metadata.txt"
  # Read one variable per line.
  { read metric_name; read var1; read var2; } < $metadata

  local fmt1='date <a href="assoc-day.html#jobId={job_id}&metric={metric}&var1={var1}&var2={var2}&date={date}">{date}</a>'
  local fmt2="status <a href=\"../{job_id}/raw/{metric}/$pair_dir_name/{date}/assoc-log.txt\">{status}</a>"

  TOOLS-csv-to-html \
    --def "metric $metric_name" \
    --def "var1 $var1" \
    --def "var2 $var2" \
    --col-format "$fmt1" \
    --col-format "$fmt2" \
    < $csv_path \
    > $html_path
}

assoc-pair-part-html() {
  local job_dir=${1:-~/rappor/chrome-assoc-smoke/smoke3}
  # Testing it out.  This should probably be a different dir.

  find $job_dir/cooked -name pair-status.csv \
    | xargs -n 1 --verbose -- $0 assoc-pair-part-html-one

  return

  # OLD STUFF
  for entry in $job_dir/cooked/*; do
    # Only do it for dirs
    if ! test -d $entry; then
      continue
    fi
    # Now it's a metric dir
    echo $entry

    local metric_name=$(basename $entry)

    # Convert status.csv to status.part.html (a fragment)

    # NOTE: counts path could be useful.  You need the input tree though.  Hash
    # it?  Or point to the git link.

    # Link to raw CSV
    #--col-format 'date <a href="../../raw/{metric}/{date}/results.csv">{date}</a>' \

    # TODO: Link to ui/results_viewer.html#{metric}_{date}
    # And that needs some JavaScript to load the correct fragment.
    # I guess you could do the same with metric.html.  Currently it uses a
    # symlink.

    # Before job ID:
    # --col-format 'date <a href="{date}.html">{date}</a>' \
    # --col-format 'status <a href="../../raw/{metric}/{date}/log.txt">{status}</a>' \

    local fmt1='date <a href="day.html#jobId={job_id}&metric={metric}&date={date}">{date}</a>'
    local fmt2='status <a href="../{job_id}/raw/{metric}/{date}/log.txt">{status}</a>'

    TOOLS-csv-to-html \
      --def "metric $metric_name" \
      --col-format "$fmt1" \
      --col-format "$fmt2" \
      < $entry/status.csv \
      > $entry/status.part.html
  done
}

assoc-day-part-html-one() {
  local csv_in=$1
  echo "$csv_in -> HTML"

  # .../raw/interstitial.harmful/a_X_b/2015-03-01/assoc-results.csv ->
  # .../cooked/interstitial.harmful/a_X_b/2015-03-01.part.html
  # (This saves some directories)
  local html_out=$(echo $csv_in | sed -e 's|/raw/|/cooked/|; s|/assoc-results.csv|.part.html|')

  TOOLS-csv-to-html --as-percent proportion < $csv_in > $html_out
}

assoc-day-part-html() {
  local job_dir=${1:-_tmp/results-10}

  find $job_dir -name assoc-results.csv \
    | xargs -n 1 --verbose --no-run-if-empty -- $0 assoc-day-part-html-one
}

lint-html() {
  set -o xtrace
  set +o errexit  # don't fail fast
  tidy -errors -quiet ui/metric.html
  tidy -errors -quiet ui/overview.html
  tidy -errors -quiet ui/histograms.html
}

# Directory we should serve from
readonly WWW_DIR=_tmp

serve() {
  local port=${1:-7999}
  cd $WWW_DIR && python -m SimpleHTTPServer $port
}

"$@"