-
Notifications
You must be signed in to change notification settings - Fork 56
/
list.vue
273 lines (252 loc) · 8.99 KB
/
list.vue
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
<!--
Copyright 2017 ODK Central Developers
See the NOTICE file at the top-level directory of this distribution and at
https://github.com/opendatakit/central-frontend/blob/master/NOTICE.
This file is part of ODK Central. It is subject to the license terms in
the LICENSE file found in the top-level directory of this distribution and at
https://www.apache.org/licenses/LICENSE-2.0. No part of ODK Central,
including this file, may be copied, modified, propagated, or distributed
except according to the terms contained in the LICENSE file.
-->
<template>
<div id="backup-list">
<loading v-if="backups == null" :state="awaitingResponse"/>
<div v-else class="panel panel-simple">
<div class="panel-heading">
<h1 class="panel-title">Current Status</h1>
</div>
<div class="panel-body">
<div id="backup-list-status-icon-container">
<span :class="iconClasses"></span>
</div>
<div id="backup-list-button-container">
<button v-if="backups.status === 'notConfigured'"
id="backup-list-new-button" type="button" class="btn btn-primary"
@click="showModal('newBackup')">
<span class="icon-plus-circle"></span>Set up now
</button>
<button v-else id="backup-list-terminate-button" type="button"
class="btn btn-primary" @click="showModal('terminate')">
<span class="icon-times-circle"></span>Terminate
</button>
</div>
<div id="backup-list-status-message">
<template v-if="backups.status === 'notConfigured'">
<p>Backups are not configured.</p>
<p>
<strong>The data server has not been set up to automatically
back up its data anywhere.</strong>
</p>
<p>
Unless you have set up some other form of data backup that I
don’t know about, it is <strong>strongly recommended</strong>
that you do this now. If you are not sure, it is best to do it
just to be safe.
</p>
<p>
Automatic data backups happen through this system once a day.
All your data is encrypted with a password you provide so that
only you can unlock it.
</p>
</template>
<template v-else-if="backups.status === 'neverRun'">
<p>The configured backup has not yet run.</p>
<p>
If you have configured backups within the last couple of days,
this is normal. Otherwise, something has gone wrong.
</p>
<p>
In that case, the most likely fixes are to
<strong>terminate</strong> the connection and set it up again,
or to restart the service. If you are having trouble, please try
the
<a href="https://forum.opendatakit.org/" target="_blank">community forum</a>.
</p>
</template>
<template v-else-if="backups.status === 'somethingWentWrong'">
<p>Something is wrong!</p>
<p>
The latest backup that completed successfully was <strong>more
than three days ago.</strong>
</p>
<p>
The most likely fixes are to <strong>terminate</strong> the
connection and set it up again, or to restart the service. If
you are having trouble, please try the
<a href="https://forum.opendatakit.org/" target="_blank">community forum</a>.
</p>
</template>
<template v-else>
<p>Backup is working.</p>
<p>
The last backup completed successfully
<strong id="backup-list-most-recently-logged-at">
{{ mostRecentlyLoggedAt }}</strong>.
</p>
</template>
</div>
</div>
</div>
<backup-new v-bind="newBackup" @hide="hideModal('newBackup')"
@success="afterCreate"/>
<backup-terminate v-bind="terminate" @hide="hideModal('terminate')"
@success="afterTerminate"/>
</div>
</template>
<script>
import { DateTime } from 'luxon';
import BackupNew from './new.vue';
import BackupTerminate from './terminate.vue';
import modal from '../../mixins/modal';
import request from '../../mixins/request';
import { formatDate } from '../../util';
// The duration for which a backup attempt is considered "recent": if the server
// returns no log of a recent backup attempt, that means that there have been no
// backup attempts for that duration into the past. If the server returns no log
// of a recent backup attempt, and if backups were configured more than that
// duration into the past (that is, if the latest config is itself not recent),
// then the user is informed that something has gone wrong.
const RECENT_DURATION = { days: 3 };
class Backups {
constructor(data = {}) {
this._setAt = data.setAt;
this._recent = data.recent != null
? this.constructor.recentForConfig(data)
: null;
}
// recentForConfig() returns the recent backup attempts for the latest config
// Note that failed backup attempts do not have a `configSetAt` property, so
// it is not always possible to determine their corresponding config.
// recentForConfig() takes an inclusive approach, including all backup
// attempts that might correspond to the latest config.
static recentForConfig({ setAt, recent }) {
const result = [];
for (const attempt of recent) {
const { details } = attempt;
if (details.configSetAt !== setAt && details.configSetAt != null) {
// We have reached backup attempts from a previous config. Assuming that
// a backup attempt from a previous config cannot be logged after a new
// config is set up, this means that all backup attempts that follow are
// from a previous config.
break;
}
result.push(attempt);
}
return result;
}
static notConfigured() { return new Backups(); }
static fromResponse(response) {
return response.status !== 404
? new Backups(response.data)
: Backups.notConfigured();
}
get setAt() { return this._setAt; }
get recent() { return this._recent; }
get status() {
if (this._setAt == null) return 'notConfigured';
if (this._recent.length === 0) {
return DateTime.fromISO(this._setAt) < DateTime.local().minus(RECENT_DURATION)
? 'somethingWentWrong'
: 'neverRun';
}
return this._recent[0].details.success ? 'success' : 'somethingWentWrong';
}
}
const validateBackupsResponseStatus = (status) =>
(status >= 200 && status < 300) || status === 404;
export default {
name: 'BackupList',
components: { BackupNew, BackupTerminate },
mixins: [modal(['newBackup', 'terminate']), request()],
data() {
return {
requestId: null,
backups: null,
newBackup: {
state: false
},
terminate: {
state: false
}
};
},
computed: {
iconClasses() {
switch (this.backups.status) {
case 'notConfigured':
return ['icon-question-circle', 'text-muted'];
case 'neverRun':
case 'success':
return ['icon-check-circle', 'text-success'];
default: // 'somethingWentWrong'
return ['icon-times-circle', 'text-danger'];
}
},
mostRecentlyLoggedAt() {
return this.backups.recent.length !== 0
? formatDate(this.backups.recent[0].loggedAt)
: null;
}
},
created() {
this.fetchData();
},
methods: {
fetchData() {
this.backups = null;
this
.get('/config/backups', { validateStatus: validateBackupsResponseStatus })
.then(response => {
this.backups = Backups.fromResponse(response);
})
.catch(() => {});
},
afterCreate() {
this.$alert().success('Success! Automatic backups are now configured.');
this.fetchData();
},
afterTerminate() {
this.$alert().success('Your automatic backups were terminated. I recommend you set up a new one as soon as possible.');
this.backups = Backups.notConfigured();
},
// The following methods are used in tests. (It does not seem otherwise
// possible to export them from this single file component.)
recentDate() {
return DateTime.local().minus(RECENT_DURATION).toJSDate();
},
recentForConfig(data) {
return Backups.recentForConfig(data);
}
}
};
</script>
<style lang="sass">
@import '../../../assets/scss/variables';
$title-font-size: 28px;
#backup-list .panel-body {
padding-top: 10px;
}
#backup-list-status-icon-container {
position: relative;
span {
font-size: 32px;
position: absolute;
top: 4px;
}
}
#backup-list-button-container {
float: right;
// Setting font-size to $title-font-size so that the button is vertically
// aligned correctly.
font-size: $title-font-size;
}
#backup-list-status-message {
p {
margin-left: 41px;
max-width: 585px;
&:first-child {
font-size: $title-font-size;
}
}
}
</style>