/
data.service.ts
236 lines (195 loc) · 7.74 KB
/
data.service.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
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
/*
Service for clients to access the Mongopop and MongoDB Atlas web APIs
*/
import { Injectable, OnInit } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable, Subscription } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { MongoResult } from './MongoResult';
import { ClientConfig } from './ClientConfig';
import { AddDocsRequest } from './AddDocsRequest';
import { SampleDocsRequest } from './SampleDocsRequest';
import { MongoReadResult } from './MongoReadResult';
import { UpdateDocsRequest } from './UpdateDocsRequest';
import { CountDocsRequest } from './CountDocsRequest';
@Injectable()
export class DataService {
private MongoDBURI: string; // The URI to use when accessing the MongoDB database
private baseURL: string = "http://localhost:3000/pop/"; // The URL for the Mongopop service
constructor (private http: Http) {
}
fetchServerIP() : Observable<string> {
// Ask the MongoPop API for its IP address
return this.http.get(this.baseURL + "ip")
.map(response => response.json().ip)
.catch((error:any) => Observable.throw(error.json().error || 'Server error'))
}
fetchClientConfig() : Observable<ClientConfig> {
return this.http.get(this.baseURL + "config")
.map(response => response.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error'))
}
setMongoDBURI(MongoDBURI: string) {
this.MongoDBURI = MongoDBURI;
}
calculateMongoDBURI(dbInputs: any): {"MongoDBURI": string, "MongoDBURIRedacted": string}
{
/*
Returns the URI for accessing the database; if it's for MongoDB Atlas then include the password and
use the chosen database name rather than 'admin'. Also returns the redacted URI (with the password
masked).
*/
let MongoDBURI: string;
let MongoDBURIRedacted: string;
if (dbInputs.MongoDBBaseURI == "mongodb://localhost:27017") {
MongoDBURI = dbInputs.MongoDBBaseURI
+ "/" + dbInputs.MongoDBDatabaseName
+ "?authSource=admin&socketTimeoutMS="
+ dbInputs.MongoDBSocketTimeout*1000
+ "&maxPoolSize="
+ dbInputs.MongoDBConnectionPoolSize;
MongoDBURIRedacted = dbInputs.MongoDBBaseURI;
} else {
// Can now assume that the URI is in the format provided by MongoDB Atlas
dbInputs.MongoDBUser = dbInputs.MongoDBBaseURI.split('mongodb://')[1].split(':')[0];
MongoDBURI = dbInputs.MongoDBBaseURI
.replace('<DATABASE>', dbInputs.MongoDBDatabaseName)
.replace('<PASSWORD>', dbInputs.MongoDBUserPassword)
+ "&socketTimeoutMS="
+ dbInputs.MongoDBSocketTimeout*1000
+ "&maxPoolSize="
+ dbInputs.MongoDBConnectionPoolSize;
MongoDBURIRedacted = dbInputs.MongoDBBaseURI
.replace('<DATABASE>', dbInputs.MongoDBDatabaseName)
.replace('<PASSWORD>', "**********")
+ "&socketTimeoutMS="
+ dbInputs.MongoDBSocketTimeout*1000
+ "&maxPoolSize="
+ dbInputs.MongoDBConnectionPoolSize;
}
this.setMongoDBURI(MongoDBURI);
return({"MongoDBURI": MongoDBURI,
"MongoDBURIRedacted": MongoDBURIRedacted});
}
tryParseJSON (jsonString: string): Object{
/*
Attempts to build an object from the supplied string. Raises an error if
the conversion fails (e.g. if it isn't valid JSON format).
*/
try {
let myObject = JSON.parse(jsonString);
if (myObject && typeof myObject === "object") {
return myObject;
}
}
catch (error) {
let errorString = "Not valid JSON: " + error.message;
console.log(errorString);
new Error(errorString);
}
return {};
};
sendUpdateDocs(doc: UpdateDocsRequest) : Observable<MongoResult> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let url: string = this.baseURL + "updateDocs";
return this.http.post(url, doc, options)
.timeout(360000000, new Error('Timeout exceeded'))
.map(response => response.json())
.catch((error:any) => {
return Observable.throw(error.toString() || " Server error")
});
};
updateDBDocs (collName: string, matchPattern: string, dataChange: string,
threads: number): Observable<MongoResult> {
/*
Apply an update to all documents in a collection
which match a given pattern. Uses the MongoPop API.
Returns an Observable which either resolves to the results of the operation
or throws an error.
*/
let matchObject: Object;
let changeObject: Object;
try {
matchObject = this.tryParseJSON(matchPattern);
}
catch (error) {
let errorString = "Match pattern: " + error.message;
console.log(errorString);
return Observable.throw(errorString);
}
try {
changeObject = this.tryParseJSON(dataChange);
}
catch (error) {
let errorString = "Data change: " + error.message;
console.log(errorString);
return Observable.throw(errorString);
}
let updateDocsRequest = new UpdateDocsRequest (this.MongoDBURI, collName, matchObject, changeObject, threads);
return this.sendUpdateDocs(updateDocsRequest)
.map(results => {return results})
.catch((error:any) => {
return Observable.throw(error.toString() || ' Server error')
})
}
sendCountDocs(CollName: string) : Observable<MongoResult> {
/*
Use the Mongopop API to count the number of documents in the specified
collection.
It returns an Observable that delivers objects of type MongoResult.
*/
// Need to indicate that the request parameters will be in the form
// of a JSON document
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({headers: headers});
// The CountDocsRequest class contains the same elements as the
// `pop/count` REST API POST method expects to receive
let countDocsRequest = new CountDocsRequest (this.MongoDBURI, CollName);
let url: string = this.baseURL + "countDocs";
return this.http.post(url, countDocsRequest, options)
.timeout(360000, new Error('Timeout exceeded'))
.map(response => response.json())
.catch((error:any) => {
return Observable.throw(error.toString() || ' Server error')
});
};
sendAddDoc(CollName:string, DocURL: string, DocCount: number,
Unique: boolean) : Observable<MongoResult> {
/*
Use the Mongopop API to add the requested number (in thousands) of documents
to the collection. The documents are fetched from a service such as Mockaroo
using the DocURL.
Returns an Observable which either resolves to the results of the operation
or throws an error.
*/
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
let addDocsRequest = new AddDocsRequest (this.MongoDBURI, CollName, DocURL, DocCount, Unique);
let url: string = this.baseURL + "addDocs";
return this.http.post(url, addDocsRequest, options)
.timeout(360000000, new Error('Timeout exceeded'))
.map(response => response.json())
.catch((error:any) => {
return Observable.throw(error.toString() || ' Server error')
});
};
sendSampleDoc(CollName:string, NumberDocs: number) : Observable<MongoReadResult> {
/*
Use the Mongopop API to request a sample of the documents from a collection.
Returns an Observable which either resolves to the results of the operation
or throws an error.
*/
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers});
let sampleDocsRequest = new SampleDocsRequest (this.MongoDBURI, CollName, NumberDocs);
let url: string = this.baseURL + "sampleDocs";
return this.http.post(url, sampleDocsRequest, options)
.timeout(360000, new Error('Timeout exceeded'))
.map(response => response.json())
.catch((error:any) => {
return Observable.throw(error.toString() || ' Server error')
});
};
}