-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.js
156 lines (146 loc) · 4.89 KB
/
container.js
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
import { query, update, sparqlEscapeUri, sparqlEscapeString, uuid } from 'mu';
const PREFIXES = `
PREFIX docker: <https://w3.org/ns/bde/docker#>
`;
class Container {
constructor({id, name, uri, status, stateURI, labels}){
this.id = id;
this.name = name;
this.uri = uri;
this.status = status;
this.stateURI = stateURI;
this.labels = labels;
}
async setStatus(status) {
this.status = status;
await this.save();
}
async remove() {
this.setStatus('removed');
}
static async findAll() {
const result = await query(`
${PREFIXES}
SELECT ?uri ?id ?name ?stateURI ?status
FROM ${sparqlEscapeUri(process.env.MU_APPLICATION_GRAPH)}
WHERE {
?uri a docker:Container;
docker:id ?id;
docker:name ?name;
docker:state ?stateURI.
?stateURI docker:status ?status.
}
`);
const bindingKeys = result.head.vars;
const objects = result.results.bindings.map( (r) => {
let obj = {};
bindingKeys.forEach((key) => {
if (r[key])
obj[key] = r[key].value;
});
return new this(obj);
});
// for (let obj of objects) {
// obj.labels = await Container.getLabels(obj.uri);
// }
return objects;
}
static async getLabels(uri) {
const result = await query(`
${PREFIXES}
SELECT ?label ?labelKey ?labelValue
FROM ${sparqlEscapeUri(process.env.MU_APPLICATION_GRAPH)}
WHERE {
${sparqlEscapeUri(uri)} docker:label ?label.
?label docker:key ?labelKey.
?label docker:value ?labelValue.
}
`);
const labels = {};
for (let result of result.results.bindings) {
labels[result.labelKey.value] = { uri: result.label.value, value: result.labelValue.value};
}
return labels;
}
update(newInformation) {
this.id = newInformation.id;
this.name = newInformation.name;
this.status = newInformation.status;
this.labels = newInformation.labels;
}
async save() {
if (this.uri) {
// assume it already exists in the database if we have a uri
await update(`
${PREFIXES}
WITH ${sparqlEscapeUri(process.env.MU_APPLICATION_GRAPH)}
DELETE {
${sparqlEscapeUri(this.uri)} a docker:Container;
docker:id ?id;
docker:name ?name;
docker:state ?stateURI;
docker:label ?label.
?stateURI docker:status ?status.
?label a docker:ContainerLabel;
docker:key ?labelKey;
docker:value ?labelValue.
}
INSERT {
${sparqlEscapeUri(this.uri)} a docker:Container;
docker:id ${sparqlEscapeString(this.id)};
docker:name ${sparqlEscapeString(this.name)};
docker:state ?stateURI.
?stateURI docker:status ${sparqlEscapeString(this.status)}.
${this.labelTriples().join("\n")}
}
WHERE {
${sparqlEscapeUri(this.uri)} a docker:Container;
docker:id ?id;
docker:name ?name;
docker:state ?stateURI.
?stateURI docker:status ?status.
OPTIONAL {
${sparqlEscapeUri(this.uri)} docker:label ?label.
?label a docker:ContainerLabel;
docker:key ?labelKey;
docker:value ?labelValue.
}
}`);
}
else {
// it's not persisted yet
const stateURI = `http://data.lblod.info/id/docker-state/${uuid()}`;
const containerURI = `http://data.lblod.info/id/docker-container/${this.id}`;
await update(`
${PREFIXES}
INSERT DATA {
GRAPH ${sparqlEscapeUri(process.env.MU_APPLICATION_GRAPH)} {
${sparqlEscapeUri(containerURI)} a docker:Container;
docker:id ${sparqlEscapeString(this.id)};
docker:name ${sparqlEscapeString(this.name)};
docker:state ${sparqlEscapeUri(stateURI)}.
${sparqlEscapeUri(stateURI)} docker:status ${sparqlEscapeString(this.status)}.
${this.labelTriples(containerURI).join("\n")}
}
}
`);
}
}
labelTriples(objUri=this.uri) {
const triples = new Array();
if (!this.labels && this.uri)
this.labels = Container.getLabels(this.uri);
for (let key of Object.keys(this.labels)) {
let value = this.labels[key];
let uri = value.uri ? value.uri : `http://data.lblod.info/id/container-label/${uuid()}`;
triples.push(`
${sparqlEscapeUri(objUri)} docker:label ${sparqlEscapeUri(uri)}.
${sparqlEscapeUri(uri)} a docker:ContainerLabel;
docker:key ${sparqlEscapeString(key)};
docker:value ${sparqlEscapeString(value.value)}.
`);
}
return triples;
}
}
export default Container;