aboutsummaryrefslogtreecommitdiff
path: root/src/update_graphs.nim
blob: 071f0ee53cdf93e3fcf763da70aa09cd2d0cb2fc (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
## RRD database graphing tool. Reads all databases from a directory and generates graphs from each
## of them.
##
## The generated graphs include
##
## - Temperature for today and yesterday
## - Temperature for current month and previous month
## - Relative humidity for today and yesterday
## - Relative humidity for current month and previous month
## - Pressure for today and yesterday
## - Pressure for current month and previous month
##
## The database directory is constructed from the path to this executable, by using the relative
## path ``../database``.
##
## The graphs are output to directory specified on the command line.

import os
import osproc
import strformat
import times

proc getLastDayOfCurrentMonth(): string =
    let now = times.now()
    let days = getDaysInMonth(now.month, now.year)
    result = &"{now.year}{ord(now.month):02}{days:02}"

proc getFirstDayOfCurrentMonth(): string =
    let now = times.now()
    result = &"{now.year}{ord(now.month):02}01"

proc getFirstDayOfPreviousMonth(): string =
    let now = times.now()
    let beginningOfMonth = initDateTime(1, now.month, now.year, 0, 0, 0)
    let endOfPrevMonth = beginningOfMonth - 1.days
    result = &"{endOfPrevMonth.year}{ord(endOfPrevMonth.month):02}01"

proc secondsInPrevMonth(): int =
    let beginningOfMonth = initDateTime(1, times.now().month, times.now().year, 0, 0, 0)
    let endOfPrevMonth = beginningOfMonth - 1.days
    let days = getDaysInMonth(endOfPrevMonth.month, endOfPrevMonth.year)
    result = days * 24 * 60 * 60

proc getDaysForMonthGraph(): int =
    let now = times.now()
    let beginningOfMonth = initDateTime(1, now.month, now.year, 0, 0, 0)
    let endOfPrevMonth = beginningOfMonth - 1.days
    result = max(getDaysInMonth(beginningOfMonth.month, beginningOfMonth.year),
        getDaysInMonth(endOfPrevMonth.month, endOfPrevMonth.year))

let
    databaseDir = getAppDir().parentDir() / "database"
    outputDir = paramStr(1)
    width = 800
    height = 600
    lastDayOfCurrentMonth = getLastDayOfCurrentMonth()
    firstDayOfCurrentMonth = getFirstDayOfCurrentMonth()
    firstDayOfPreviousMonth = getFirstDayOfPreviousMonth()

proc graph(filename, title, vlabel: string, args: varargs[string, `$`]) =
    var cmdline: seq[string] = @[
        "graph", joinPath(outputDir, filename),
        "--width", $width,
        "--height", $height,
        "--title", title,
        "--vertical-label", vlabel,
    ] & @args

    echo(&"Generating {joinPath(outputDir, filename)} ...")

    let p = startProcess("rrdtool", args=cmdline,
        options={poStdErrToStdOut, poUsePath, poParentStreams})

    if p.waitForExit() != 0:
        echo("failed")
        quit(1)


for database in walkFiles(joinPath(databaseDir, "*.rrd")):
    let (_, base, _) = splitFile(database)

    graph(&"{base}-temperature-today.png", "Temperature", "Celcius",
        "--end", "23:59 today",
        "--start", "00:00 today",
        "--alt-autoscale",
        "--left-axis-format", "%.1lf",
        &"DEF:temp1={database}:temperature:AVERAGE",
        &"DEF:temp2={database}:temperature:AVERAGE:end=00\\:00 today:start=end-24h",
        "LINE1:temp1#000000:today",
        "LINE1:temp2#FF0000:yesterday",
        "SHIFT:temp2:86400",
    )

    graph(&"{base}-temperature-month.png", "Temperature", "Celcius",
        "--start", &"00:00 {firstDayOfCurrentMonth}",
        "--end", &"start+{getDaysForMonthGraph()}days",
        "--step", "3600",
        "--alt-autoscale",
        "--x-grid", "DAY:1:DAY:1:DAY:1:86400:%d",
        &"DEF:curr={database}:temperature:AVERAGE",
        &"DEF:prev={database}:temperature:AVERAGE:end=00\\:00 {firstDayOfCurrentMonth}:start=00\\:00 {firstDayOfPreviousMonth}",
        "LINE1:curr#000000:current month",
        "LINE1:prev#FF0000:previous month",
        &"SHIFT:prev:{secondsInPrevMonth()}",
    )

    graph(&"{base}-humidity-today.png", "Relative humidity", "%",
        "--end", "23:59 today",
        "--start", "00:00 today",
        &"DEF:humi1={database}:humidity:AVERAGE",
        &"DEF:humi2={database}:humidity:AVERAGE:end=00\\:00 today:start=end-24h",
        "LINE1:humi1#000000:today",
        "LINE1:humi2#FF0000:yesterday",
        "SHIFT:humi2:86400",
    )

    graph(&"{base}-humidity-month.png", "Relative humidity", "%",
        "--start", &"00:00 {firstDayOfCurrentMonth}",
        "--end", &"start+{getDaysForMonthGraph()}days",
        "--step", "3600",
        "--alt-autoscale",
        "--x-grid", "DAY:1:DAY:1:DAY:1:86400:%d",
        &"DEF:curr={database}:humidity:AVERAGE",
        &"DEF:prev={database}:humidity:AVERAGE:end=00\\:00 {firstDayOfCurrentMonth}:start=00\\:00 {firstDayOfPreviousMonth}",
        "LINE1:curr#000000:current month",
        "LINE1:prev#FF0000:previous month",
        &"SHIFT:prev:{secondsInPrevMonth()}",
    )

    graph(&"{base}-pressure-today.png", "Pressure", "Pascal",
        "--end", "23:59 today",
        "--start", "00:00 today",
        "--alt-autoscale",
        "--left-axis-format", "%.1lf",
        &"DEF:pres1={database}:pressure:AVERAGE",
        &"DEF:pres2={database}:pressure:AVERAGE:end=00\\:00 today:start=end-24h",
        "LINE1:pres1#000000:today",
        "LINE1:pres2#FF0000:yesterday",
        "SHIFT:pres2:86400",
    )

    graph(&"{base}-pressure-month.png", "Pressure", "Pascal",
        "--start", &"00:00 {firstDayOfCurrentMonth}",
        "--end", &"start+{getDaysForMonthGraph()}days",
        "--step", "3600",
        "--alt-autoscale",
        "--left-axis-format", "%.1lf",
        "--x-grid", "DAY:1:DAY:1:DAY:1:86400:%d",
        &"DEF:curr={database}:pressure:AVERAGE",
        &"DEF:prev={database}:pressure:AVERAGE:end=00\\:00 {firstDayOfCurrentMonth}:start=00\\:00 {firstDayOfPreviousMonth}",
        "LINE1:curr#000000:current month",
        "LINE1:prev#FF0000:previous month",
        &"SHIFT:prev:{secondsInPrevMonth()}",
    )