-
Notifications
You must be signed in to change notification settings - Fork 0
/
local-api.ts
139 lines (123 loc) · 3.85 KB
/
local-api.ts
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
/*
* Copyright (c) 2016 - now, David Sehnal, licensed under Apache 2.0, See LICENSE file for more info.
*/
import * as Api from './api'
import * as Data from './query/data-model'
import * as Coordinate from './algebra/coordinate'
import * as fs from 'fs'
import * as path from 'path'
export interface JobEntry {
source: {
filename: string,
name: string,
id: string
},
query: {
kind: 'box' | 'cell',
space?: 'fractional' | 'cartesian',
bottomLeft?: number[],
topRight?: number[],
}
params: {
/** Determines the detail level as specified in server-config */
detail?: number,
/**
* Determines the sampling level:
* 1: Original data
* 2: Downsampled by factor 1/2
* ...
* N: downsampled 1/2^(N-1)
*/
forcedSamplingLevel?: number,
asBinary: boolean,
},
outputFolder: string
}
export async function run(jobs: JobEntry[]) {
let progress = 0;
let started = getTime();
for (const job of jobs) {
try {
await query(job);
} catch (e) {
console.error(e);
}
progress++;
const elapsed = (getTime() - started) / 1000;
console.log(`[Progress] ${progress}/${jobs.length} in ${elapsed.toFixed(2)}s`);
}
}
function getTime() {
let t = process.hrtime();
return t[0] * 1000 + t[1] / 1000000;
}
async function query(job: JobEntry) {
let box: Data.QueryParamsBox;
if (job.query.kind.toLocaleLowerCase() === 'cell') {
box = { kind: 'Cell' };
} else if (job.query.space === 'fractional') {
box = {
kind: 'Fractional',
a: Coordinate.fractional(job.query.bottomLeft![0], job.query.bottomLeft![1], job.query.bottomLeft![2]),
b: Coordinate.fractional(job.query.topRight![0], job.query.topRight![1], job.query.topRight![2]),
}
} else {
box = {
kind: 'Cartesian',
a: Coordinate.cartesian(job.query.bottomLeft![0], job.query.bottomLeft![1], job.query.bottomLeft![2]),
b: Coordinate.cartesian(job.query.topRight![0], job.query.topRight![1], job.query.topRight![2]),
}
}
const params: Data.QueryParams = {
sourceFilename: job.source.filename,
sourceId: job.source.id,
asBinary: job.params.asBinary,
box,
detail: !job.params.detail ? 0 : job.params.detail,
forcedSamplingLevel: job.params.forcedSamplingLevel
};
if (!fs.existsSync(job.outputFolder)) {
makeDir(job.outputFolder);
}
const filename = path.join(job.outputFolder, Api.getOutputFilename(job.source.name, job.source.id, params))
const res = () => wrapFile(filename);
await Api.queryBox(params, res)
}
function makeDir(path: string, root?: string): boolean {
let dirs = path.split(/\/|\\/g),
dir = dirs.shift();
root = (root || '') + dir + '/';
try { fs.mkdirSync(root); }
catch (e) {
if (!fs.statSync(root).isDirectory()) throw new Error(e);
}
return !dirs.length || makeDir(dirs.join('/'), root);
}
function wrapFile(fn: string) {
const w = {
open(this: any) {
if (this.opened) return;
this.file = fs.openSync(fn, 'w');
this.opened = true;
},
writeBinary(this: any, data: Uint8Array) {
this.open();
fs.writeSync(this.file, new Buffer(data));
return true;
},
writeString(this: any, data: string) {
this.open();
fs.writeSync(this.file, data);
return true;
},
end(this: any) {
if (!this.opened || this.ended) return;
fs.close(this.file, function () { });
this.ended = true;
},
file: 0,
ended: false,
opened: false
};
return w;
}