summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/sort-predicates.js
blob: 1ead67c22e3d928ff362b478f9d9905d0d3d62d7 (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
"use strict";

const { getAbbreviatedMimeType,
        getUriNameWithQuery,
        getUriHostPort,
        loadCauseString } = require("./request-utils");

/**
 * Predicates used when sorting items.
 *
 * @param object first
 *        The first item used in the comparison.
 * @param object second
 *        The second item used in the comparison.
 * @return number
 *         <0 to sort first to a lower index than second
 *         =0 to leave first and second unchanged with respect to each other
 *         >0 to sort second to a lower index than first
 */

function waterfall(first, second) {
  return first.startedMillis - second.startedMillis;
}

function status(first, second) {
  return first.status == second.status
         ? first.startedMillis - second.startedMillis
         : first.status - second.status;
}

function method(first, second) {
  if (first.method == second.method) {
    return first.startedMillis - second.startedMillis;
  }
  return first.method > second.method ? 1 : -1;
}

function file(first, second) {
  let firstUrl = getUriNameWithQuery(first.url).toLowerCase();
  let secondUrl = getUriNameWithQuery(second.url).toLowerCase();
  if (firstUrl == secondUrl) {
    return first.startedMillis - second.startedMillis;
  }
  return firstUrl > secondUrl ? 1 : -1;
}

function domain(first, second) {
  let firstDomain = getUriHostPort(first.url).toLowerCase();
  let secondDomain = getUriHostPort(second.url).toLowerCase();
  if (firstDomain == secondDomain) {
    return first.startedMillis - second.startedMillis;
  }
  return firstDomain > secondDomain ? 1 : -1;
}

function cause(first, second) {
  let firstCause = loadCauseString(first.cause.type);
  let secondCause = loadCauseString(second.cause.type);
  if (firstCause == secondCause) {
    return first.startedMillis - second.startedMillis;
  }
  return firstCause > secondCause ? 1 : -1;
}

function type(first, second) {
  let firstType = getAbbreviatedMimeType(first.mimeType).toLowerCase();
  let secondType = getAbbreviatedMimeType(second.mimeType).toLowerCase();
  if (firstType == secondType) {
    return first.startedMillis - second.startedMillis;
  }
  return firstType > secondType ? 1 : -1;
}

function transferred(first, second) {
  return first.transferredSize - second.transferredSize;
}

function size(first, second) {
  return first.contentSize - second.contentSize;
}

exports.Sorters = {
  status,
  method,
  file,
  domain,
  cause,
  type,
  transferred,
  size,
  waterfall,
};