-
Notifications
You must be signed in to change notification settings - Fork 2
/
Geocoding.jsx
171 lines (161 loc) · 6.3 KB
/
Geocoding.jsx
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
import { useState, useEffect, useRef } from 'react';
import { Link, Prompt } from 'react-router-dom';
import humanizeDuration from 'humanize-duration';
import { DocumentTextIcon } from '@heroicons/react/outline';
import { useGeocodeContext } from '../components/GeocodeContext.js';
const percentFormatter = new Intl.NumberFormat('en-US', { style: 'percent' });
export default function Geocoding() {
const [stats, setStats] = useState({
rowsProcessed: 0,
totalRows: 0,
activeMatchRate: 0,
averageScore: 0,
status: 'idle',
});
const startTime = useRef(new Date());
const { geocodeContext } = useGeocodeContext();
const draggable = useRef(null);
const onDragStart = (event) => {
event.preventDefault();
window.ugrc.startDrag('ugrc_geocode_results.csv');
};
const cancel = () => {
window.ugrc.cancelGeocode();
};
useEffect(() => {
window.ugrc.subscribeToGeocodingUpdates((_, data) => {
setStats(data);
});
window.ugrc.getConfigItem('wkid').then((wkid) => {
window.ugrc.geocode({
filePath: geocodeContext.data.file.path,
fields: { street: geocodeContext.data.street, zone: geocodeContext.data.zone },
apiKey: geocodeContext.apiKey,
wkid,
sampleData: geocodeContext.data.sampleData,
});
});
return () => {
cancel();
window.ugrc.unsubscribeFromGeocodingUpdates();
};
}, [geocodeContext]);
const progress = stats.rowsProcessed / stats.totalRows || 0;
const elapsedTime = new Date().getTime() - startTime.current.getTime();
const timePerRow = elapsedTime / stats.rowsProcessed;
const estimatedTimeRemaining = timePerRow * (stats.totalRows - stats.rowsProcessed);
const getElementsByStatus = (status) => {
switch (status) {
case 'running': {
return (
<>
<button
type="button"
onClick={cancel}
disabled={stats.rowsProcessed > 0 && stats.totalRows === stats.rowsProcessed}
>
Cancel
</button>
<Prompt
message={JSON.stringify({
detail: 'Navigating to a different page will cancel the current geocoding process.',
message: 'Are you sure you would like to navigate?',
})}
/>
</>
);
}
case 'complete': {
return (
<div
className="p-12 mx-auto text-center bg-gray-100 border rounded-lg shadow cursor-grab"
ref={draggable}
draggable={true}
onDragStart={onDragStart}
>
<p>Drag and drop this file to save the results.</p>
<DocumentTextIcon className="w-32 mx-auto" />
<span className="mx-auto font-mono text-sm">ugrc_geocode_results.csv</span>
</div>
);
}
case 'cancelled': {
return (
<section className="px-3 bg-red-100 border border-red-200 rounded shadow">
<h3 className="text-center text-red-800 ">This job was cancelled</h3>
<p>
<Link to="/plan">Go back to the plan page</Link> to restart the process.
</p>
</section>
);
}
case 'fail-fast': {
return (
<section className="px-3 bg-red-100 border border-red-200 rounded shadow">
<h3 className="text-center text-red-800">This job has fast failed</h3>
<p>
A fast failure occurs when the <span className="font-bold">first {stats.failures} records</span> do not
succeed to geocode. This is often an indication that the data being processed is not correct. You should
try to fix the data and re-run the job.
</p>
<p>
<Link to="/plan">Go back to the plan page</Link> to restart the process.
</p>
</section>
);
}
default:
return null;
}
};
return (
<article>
<Link type="back-button" to="/plan">
← Back
</Link>
<h2>Geocoding progress</h2>
<progress className="w-full h-16" value={progress}>
{progress}%
</progress>
<section>
<dl>
<div className="px-4 py-5 bg-white sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt className="font-medium text-gray-500">Total Rows</dt>
<dd className="mt-1 text-gray-900 sm:mt-0 sm:col-span-2">{stats.totalRows}</dd>
</div>
<div className="px-4 py-5 bg-gray-50 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt className="font-medium text-gray-500">Rows processed</dt>
<dd className="mt-1 text-gray-900 sm:mt-0 sm:col-span-2">{stats.rowsProcessed}</dd>
</div>
<div className="px-4 py-5 bg-gray-white sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt className="font-medium text-gray-500">Active match rate</dt>
<dd className="mt-1 text-gray-900 sm:mt-0 sm:col-span-2">
{percentFormatter.format(stats.activeMatchRate)}
</dd>
</div>
<div className="px-4 py-5 bg-gray-50 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt className="font-medium text-gray-500">Average match score</dt>
<dd className="mt-1 text-gray-900 sm:mt-0 sm:col-span-2">{stats.averageScore || ''}</dd>
</div>
<div className="px-4 py-5 bg-white sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt className="font-medium text-gray-500">Failures</dt>
<dd className="mt-1 text-gray-900 sm:mt-0 sm:col-span-2">{stats.failures}</dd>
</div>
<div className="px-4 py-5 bg-gray-50 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt className="font-medium text-gray-500">Time elapsed</dt>
<dd className="mt-1 text-gray-900 sm:mt-0 sm:col-span-2">
{humanizeDuration(elapsedTime, { round: true })}
</dd>
</div>
<div className="px-4 py-5 bg-white sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
<dt className="font-medium text-gray-500">Estimated time remaining</dt>
<dd className="mt-1 text-gray-900 sm:mt-0 sm:col-span-2">
{humanizeDuration(estimatedTimeRemaining, { round: true })}
</dd>
</div>
</dl>
{getElementsByStatus(stats.status)}
</section>
</article>
);
}