Skip to content

Commit 895369f

Browse files
committed
Move to on-the-fly parsing
1 parent fa748a2 commit 895369f

File tree

4 files changed

+28
-45
lines changed

4 files changed

+28
-45
lines changed

app.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,10 @@ aws.initConfig(awsProps)
342342
ogTitle: result.specialMetadata ? result.specialMetadata.title.S : "Compiler Explorer"
343343
};
344344
if (!metadata.ogDescription) {
345-
if (result.metadata && result.metadata.single_code && result.metadata.single_code.M) {
346-
const lang = languages[result.metadata.single_code.M.language.S];
347-
metadata.ogDescription = result.metadata.single_code.M.source.S;
345+
const sources = utils.glGetEditorSources(config.content);
346+
if (sources.length === 1) {
347+
const lang = languages[sources[0].language];
348+
metadata.ogDescription = sources[0].source;
348349
if (lang) {
349350
metadata.ogTitle += ` - ${lang.name}`;
350351
}

lib/storage/storage-s3.js

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,6 @@ class StorageS3 extends StorageBase {
4747
this.dynamoDb = new AWS.DynamoDB();
4848
}
4949

50-
getSources(content) {
51-
let sources = [];
52-
_.each(content, element => {
53-
if (element.type === 'component') {
54-
if (element.componentName === 'codeEditor') {
55-
sources.push({source: element.componentState.source, language: element.componentState.lang});
56-
}
57-
} else {
58-
sources = this.getSources(element.content).concat(sources);
59-
}
60-
});
61-
return sources;
62-
}
63-
6450
storeItem(item, req) {
6551
logger.info(`Storing item ${item.prefix}`);
6652
const now = new Date();
@@ -70,26 +56,6 @@ class StorageS3 extends StorageBase {
7056
// Anonymize only client IP
7157
ip = `${anonymizeIp(ip.substring(0, commaIndex))}${ip.substring(commaIndex, ip.length)}`;
7258
}
73-
const sources = this.getSources(JSON.parse(item.config).content);
74-
const metadata = {
75-
M: {
76-
editor_count: {
77-
N: sources.length.toString()
78-
}
79-
}
80-
};
81-
if (sources.length === 1) {
82-
metadata.M.single_code = {
83-
M: {
84-
source: {
85-
S: sources[0].source
86-
},
87-
language: {
88-
S: sources[0].language
89-
}
90-
}
91-
};
92-
}
9359
now.setSeconds(0, 0);
9460
return Promise.all([
9561
this.dynamoDb.putItem({
@@ -109,7 +75,6 @@ class StorageS3 extends StorageBase {
10975
clicks: {N: '0'}
11076
}
11177
},
112-
metadata: metadata,
11378
creation_ip: {
11479
S: ip
11580
},
@@ -192,15 +157,13 @@ class StorageS3 extends StorageBase {
192157
.then(item => {
193158
const full_hash = item.Attributes.full_hash.S;
194159
const special_metadata = item.Attributes.named_metadata ? item.Attributes.named_metadata.M : null;
195-
const metadata = item.Attributes.metadata ? item.Attributes.metadata.M : null;
196160
return this.s3.get(full_hash, this.prefix)
197161
.then(result => {
198162
// If we're here, we are pretty confident there is a match. But never hurts to double check
199163
if (result.hit) {
200164
return {
201165
config: result.data.toString(),
202-
specialMetadata: special_metadata,
203-
metadata: metadata
166+
specialMetadata: special_metadata
204167
};
205168
} else {
206169
throw new Error(`ID ${id} not present in storage`);

lib/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,23 @@ exports.getHash = function getHash(object, HashVersion = 'Compiler Explorer Defa
123123
.update(asString)
124124
.digest('hex');
125125
};
126+
127+
/***
128+
* Gets every pair of source, lang available
129+
* @param content {Array}
130+
* @returns {Array}
131+
*/
132+
exports.glGetEditorSources = function glGetEditorSources(content) {
133+
let sources = [];
134+
_.each(content, element => {
135+
if (element.type === 'component') {
136+
if (element.componentName === 'codeEditor') {
137+
sources.push({source: element.componentState.source, language: element.componentState.lang});
138+
}
139+
} else {
140+
sources = glGetEditorSources(element.content).concat(sources);
141+
}
142+
});
143+
return sources;
144+
145+
};

test/storage/storage-s3-tests.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ describe('Stores to s3', () => {
162162
prefix: "ABCDEF",
163163
uniqueSubHash: "ABCDEFG",
164164
fullHash: "ABCDEFGHIJKLMNOP",
165-
config: '{"contents": []}'
165+
config: 'yo'
166166
};
167167

168168
const ran = {s3: false, dynamo: false};
169169
s3PutObjectHandlers.push((q) => {
170170
q.Bucket.should.equal('bucket');
171171
q.Key.should.equal('prefix/ABCDEFGHIJKLMNOP');
172-
q.Body.should.equal('{"contents": []}');
172+
q.Body.should.equal('yo');
173173
ran.s3 = true;
174174
return {};
175175
});
@@ -182,8 +182,7 @@ describe('Stores to s3', () => {
182182
stats: {M: {clicks: {N: '0'}}},
183183
creation_ip: {S: 'localhost'},
184184
// Cheat the date
185-
creation_date: {S: q.Item.creation_date.S},
186-
metadata: {M: {editor_count: {N: "0"}}}
185+
creation_date: {S: q.Item.creation_date.S}
187186
});
188187
ran.dynamo = true;
189188
return {};

0 commit comments

Comments
 (0)