aboutsummaryrefslogtreecommitdiff
path: root/src/nix/progress-bar.cc
blob: 2ecbea8eeea833ffbde26c8470f32e1bb31cfb0f (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
#include "progress-bar.hh"
#include "util.hh"
#include "sync.hh"
#include "store-api.hh"

#include <map>
#include <atomic>

#include <sys/ioctl.h>

namespace nix {

class ProgressBar : public Logger
{
private:

    struct ActInfo
    {
        std::string s, s2;
    };

    struct DownloadInfo
    {
        std::string uri;
        uint64_t current = 0;
        uint64_t expected = 0;
        DownloadInfo(const std::string & uri) : uri(uri) { }
    };

    struct State
    {
        std::map<Activity::t, Path> builds;
        std::set<Activity::t> runningBuilds;
        uint64_t succeededBuilds = 0;
        uint64_t failedBuilds = 0;
        std::map<Activity::t, Path> substitutions;
        std::set<Activity::t> runningSubstitutions;
        uint64_t succeededSubstitutions = 0;
        uint64_t downloadedBytes = 0; // finished downloads
        std::map<Activity::t, DownloadInfo> downloads;
        std::list<ActInfo> activities;
        std::map<Activity::t, std::list<ActInfo>::iterator> its;
    };

    Sync<State> state_;

    int width = 0;

public:

    ProgressBar()
    {
        struct winsize ws;
        if (ioctl(1, TIOCGWINSZ, &ws) == 0)
            width = ws.ws_col;
    }

    ~ProgressBar()
    {
        auto state(state_.lock());
        writeToStderr("\r\e[K");
    }

    void log(Verbosity lvl, const FormatOrString & fs) override
    {
        auto state(state_.lock());
        log(*state, lvl, fs.s);
    }

    void log(State & state, Verbosity lvl, const std::string & s)
    {
        writeToStderr("\r\e[K" + s + "\n");
        update(state);
    }

    void createActivity(State & state, Activity::t activity, const std::string & s)
    {
        state.activities.emplace_back(ActInfo{s});
        state.its.emplace(activity, std::prev(state.activities.end()));
    }

    void deleteActivity(State & state, Activity::t activity)
    {
        auto i = state.its.find(activity);
        if (i != state.its.end()) {
            state.activities.erase(i->second);
            state.its.erase(i);
        }
    }

    void updateActivity(State & state, Activity::t activity, const std::string & s2)
    {
        auto i = state.its.find(activity);
        assert(i != state.its.end());
        ActInfo info = *i->second;
        state.activities.erase(i->second);
        info.s2 = s2;
        state.activities.emplace_back(info);
        i->second = std::prev(state.activities.end());
    }

    void update()
    {
        auto state(state_.lock());
        update(*state);
    }

    void update(State & state)
    {
        std::string line = "\r";

        std::string status = getStatus(state);
        if (!status.empty()) {
            line += '[';
            line += status;
            line += "]";
        }

        if (!state.activities.empty()) {
            if (!status.empty()) line += " ";
            auto i = state.activities.rbegin();
            line += i->s;
            if (!i->s2.empty()) {
                line += ": ";
                line += i->s2;
            }
        }

        line += "\e[K";
        writeToStderr(std::string(line, 0, width - 1));
    }

    std::string getStatus(State & state)
    {
        std::string res;

        if (state.failedBuilds) {
            if (!res.empty()) res += ", ";
            res += fmt(ANSI_RED "%d failed" ANSI_NORMAL, state.failedBuilds);
        }

        if (!state.builds.empty() || state.succeededBuilds)
        {
            if (!res.empty()) res += ", ";
            if (!state.runningBuilds.empty())
                res += fmt(ANSI_BLUE "%d" "/" ANSI_NORMAL, state.runningBuilds.size());
            res += fmt(ANSI_GREEN "%d/%d built" ANSI_NORMAL,
                state.succeededBuilds, state.succeededBuilds + state.builds.size());
        }

        if (!state.substitutions.empty() || state.succeededSubstitutions) {
            if (!res.empty()) res += ", ";
            if (!state.runningSubstitutions.empty())
                res += fmt(ANSI_BLUE "%d" "/" ANSI_NORMAL, state.runningSubstitutions.size());
            res += fmt(ANSI_GREEN "%d/%d fetched" ANSI_NORMAL,
                state.succeededSubstitutions,
                state.succeededSubstitutions + state.substitutions.size());
        }

        if (!state.downloads.empty() || state.downloadedBytes) {
            if (!res.empty()) res += ", ";
            uint64_t expected = state.downloadedBytes, current = state.downloadedBytes;
            for (auto & i : state.downloads) {
                expected += i.second.expected;
                current += i.second.current;
            }
            res += fmt("%1$.0f/%2$.0f KiB", current / 1024.0, expected / 1024.0);
        }

        return res;
    }

    void event(const Event & ev) override
    {
        if (ev.type == evBuildCreated) {
            auto state(state_.lock());
            state->builds[ev.getI(0)] = ev.getS(1);
            update(*state);
        }

        if (ev.type == evBuildStarted) {
            auto state(state_.lock());
            Activity::t act = ev.getI(0);
            state->runningBuilds.insert(act);
            auto name = storePathToName(state->builds[act]);
            if (hasSuffix(name, ".drv"))
                name.resize(name.size() - 4);
            createActivity(*state, act, fmt("building " ANSI_BOLD "%s" ANSI_NORMAL, name));
            update(*state);
        }

        if (ev.type == evBuildFinished) {
            auto state(state_.lock());
            Activity::t act = ev.getI(0);
            if (ev.getI(1)) {
                if (state->runningBuilds.count(act))
                    state->succeededBuilds++;
            } else
                state->failedBuilds++;
            state->runningBuilds.erase(act);
            state->builds.erase(act);
            deleteActivity(*state, act);
            update(*state);
        }

        if (ev.type == evBuildOutput) {
            auto state(state_.lock());
            Activity::t act = ev.getI(0);
            assert(state->runningBuilds.count(act));
            updateActivity(*state, act, ev.getS(1));
            update(*state);
        }

        if (ev.type == evSubstitutionCreated) {
            auto state(state_.lock());
            state->substitutions[ev.getI(0)] = ev.getS(1);
            update(*state);
        }

        if (ev.type == evSubstitutionStarted) {
            auto state(state_.lock());
            Activity::t act = ev.getI(0);
            state->runningSubstitutions.insert(act);
            auto name = storePathToName(state->substitutions[act]);
            createActivity(*state, act, fmt("fetching " ANSI_BOLD "%s" ANSI_NORMAL, name));
            update(*state);
        }

        if (ev.type == evSubstitutionFinished) {
            auto state(state_.lock());
            Activity::t act = ev.getI(0);
            if (ev.getI(1)) {
                if (state->runningSubstitutions.count(act))
                    state->succeededSubstitutions++;
            }
            state->runningSubstitutions.erase(act);
            state->substitutions.erase(act);
            deleteActivity(*state, act);
            update(*state);
        }

        if (ev.type == evDownloadCreated) {
            auto state(state_.lock());
            Activity::t act = ev.getI(0);
            std::string uri = ev.getS(1);
            state->downloads.emplace(act, DownloadInfo{uri});
            if (state->runningSubstitutions.empty()) // FIXME: hack
                createActivity(*state, act, fmt("downloading " ANSI_BOLD "%s" ANSI_NORMAL "", uri));
            update(*state);
        }

        if (ev.type == evDownloadProgress) {
            auto state(state_.lock());
            Activity::t act = ev.getI(0);
            auto i = state->downloads.find(act);
            assert(i != state->downloads.end());
            i->second.expected = ev.getI(1);
            i->second.current = ev.getI(2);
            update(*state);
        }

        if (ev.type == evDownloadSucceeded) {
            auto state(state_.lock());
            Activity::t act = ev.getI(0);
            auto i = state->downloads.find(act);
            assert(i != state->downloads.end());
            state->downloadedBytes += ev.getI(1);
            state->downloads.erase(i);
            deleteActivity(*state, act);
            update(*state);
        }

        if (ev.type == evDownloadDestroyed) {
            auto state(state_.lock());
            Activity::t act = ev.getI(0);
            auto i = state->downloads.find(act);
            if (i != state->downloads.end()) {
                state->downloads.erase(i);
                deleteActivity(*state, act);
                update(*state);
            }
        }
    }
};

StartProgressBar::StartProgressBar()
{
    if (isatty(STDERR_FILENO)) {
        prev = logger;
        logger = new ProgressBar();
    }
}

StartProgressBar::~StartProgressBar()
{
    if (prev) {
        auto bar = logger;
        logger = prev;
        delete bar;
    }
}

}