-
Notifications
You must be signed in to change notification settings - Fork 7
/
plot.sc
138 lines (102 loc) · 3.23 KB
/
plot.sc
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
import $ivy.`com.github.tototoshi::scala-csv:1.3.5`
import $ivy.`com.twitter::algebird-core:0.13.0`
import $ivy.`org.plotly-scala::plotly-render:0.5.2`
import java.io.File
import java.nio.file.{Files, Paths}
import java.time._
import com.twitter.algebird.Operators._
import plotly._
import plotly.element._
import plotly.layout._
import plotly.Plotly._
import upickle.default._
import ujson.{read => _, _}
import com.github.tototoshi.csv._
case class UniqueIpData(total: Int)
case class UniqueIpResp(data: UniqueIpData)
implicit val uniqueIpDataRW: ReadWriter[UniqueIpData] = macroRW
implicit val uniqueIpRespRW: ReadWriter[UniqueIpResp] = macroRW
def blobPattern(s: String) =
("\\Q" + s.replace("*", "\\E.*\\Q") + "\\E").r
def csvScatter(dir: File, name: String, filterNames: Option[String] = None, filterOutMonths: Set[YearMonth] = Set()) = {
val patternOpt = filterNames.map(blobPattern(_).pattern)
val data = for {
year <- 2015 to Year.now(ZoneOffset.UTC).getValue
month <- 1 to 12
f = new File(dir, f"$year/$month%02d.csv")
if f.exists()
ym = YearMonth.of(year, month)
elem <- CSVReader.open(f)
.iterator
.map(l => (ym, l(0), l(1).toInt))
.toVector
} yield elem
val byMonth = data
.filter {
patternOpt match {
case None => _ => true
case Some(p) => t => p.matcher(t._2).matches()
}
}
.map { case (ym, _, n) => ym -> n }
.sumByKey
.toVector
.sortBy(_._1)
def byMonth0 = byMonth.filter { case (ym, _) => !filterOutMonths(ym) }
def x = byMonth0.map(_._1).map { m =>
plotly.element.LocalDateTime(m.getYear, m.getMonthValue, 1, 0, 0, 0)
}
def y = byMonth0.map(_._2)
Bar(x, y, name = name)
}
def uniqueIpScatter(dir: File, name: String) = {
val data = for {
year <- 2015 to Year.now(ZoneOffset.UTC).getValue
month <- 1 to 12
f = new File(dir, f"$year/$month%02d.json")
if f.exists()
ym = YearMonth.of(year, month)
} yield {
val s = new String(Files.readAllBytes(f.toPath), "UTF-8")
val resp = read[UniqueIpResp](ujson.read(s))
ym -> resp.data.total
}
def x = data.map(_._1).map { m =>
plotly.element.LocalDateTime(m.getYear, m.getMonthValue, 1, 0, 0, 0)
}
def y = data.map(_._2)
Bar(x, y, name = name)
}
val dataBase = Paths.get("data")
val filterNames = sys.env.get("NAME_FILTER")
val downloadsTraces = Seq(
csvScatter(dataBase.resolve("stats").toFile, "# downloads", filterNames)
)
val uniqueIpsTraces =
if (filterNames.isEmpty)
Seq(
uniqueIpScatter(dataBase.resolve("unique-ips").toFile, "Unique IPs")
)
else
Nil
val dlDivId = "downloads"
val ipDivId = "uniqueips"
val layout = Layout()
val html =
s"""<!DOCTYPE html>
|<html>
|<head>
|<title>${layout.title.getOrElse("plotly chart")}</title>
|<script src="https://cdn.plot.ly/plotly-${Plotly.plotlyVersion}.min.js"></script>
|</head>
|<body>
|<div id="$dlDivId"></div>
|<div id="$ipDivId"></div>
|<script>
|${Plotly.jsSnippet(dlDivId, downloadsTraces, layout)}
|${Plotly.jsSnippet(ipDivId, uniqueIpsTraces, layout)}
|</script>
|</body>
|</html>
|""".stripMargin
Files.write(Paths.get("stats.html"), html.getBytes("UTF-8"))