Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 536ea4a

Browse files
Remove init scripts
1 parent c405782 commit 536ea4a

File tree

3 files changed

+2
-164
lines changed

3 files changed

+2
-164
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Configuration for the tests is at `config/test.js`, only add such new configurat
8989
docker-compose up -d
9090
```
9191

92-
3. initialize Elasticsearch, create configured Elasticsearch index: `npm run init-es force`
92+
3. initialize Elasticsearch. Execute the `insert-data` script in the [API repository](https://github.com/topcoder-platform/u-bahn-api) to set it up and then clear only the data
9393

9494
## Local deployment
9595

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"start": "node src/app.js",
88
"lint": "standard",
99
"lint:fix": "standard --fix",
10-
"init-es": "node test/common/init-es.js",
1110
"view-data": "node test/common/view-data.js",
1211
"test": "mocha test/unit/test.js --require test/unit/prepare.js --timeout 20000 --exit",
1312
"test:cov": "nyc --reporter=html --reporter=text mocha test/unit/test.js --require test/unit/prepare.js --timeout 20000 --exit",

test/common/init-es.js

Lines changed: 1 addition & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -11,159 +11,12 @@
1111
* node src/init-es
1212
* node src/init-es force
1313
*/
14-
15-
const _ = require('lodash')
1614
const logger = require('../../src/common/logger')
1715
const helper = require('../../src/common/helper')
18-
const { topResources, userResources } = require('../../src/common/constants')
16+
const { topResources } = require('../../src/common/constants')
1917

2018
let client
2119

22-
let needsNestedTypes = ['user']
23-
const enrichedFields = ['attributegroup', 'skillprovider']
24-
25-
/**
26-
* Initialize elastic search index
27-
* @param {Boolean} isForce boolean flag indicate it is forced operation
28-
*/
29-
const init = async (isForce) => {
30-
if (!client) {
31-
client = await helper.getESClient()
32-
}
33-
if (isForce) {
34-
await clearES()
35-
}
36-
const processors = {}
37-
for (const key in topResources) {
38-
const exists = await client.indices.exists({ index: topResources[key].index })
39-
const top = topResources[key]
40-
if (exists.body) {
41-
logger.info(`The index ${top.index} exists.`)
42-
} else {
43-
logger.info(`The index ${top.index} will be created.`)
44-
await client.indices.create({
45-
index: top.index,
46-
body: {
47-
mappings: {
48-
properties: _(_.get(top, 'enrich.enrichFields', [])).map(p => [p, { type: _.includes(enrichedFields, p) ? 'nested' : 'keyword' }]).fromPairs()
49-
}
50-
}
51-
})
52-
if (needsNestedTypes.includes(key)) {
53-
for (const childKey in userResources) {
54-
if (userResources[childKey].isNested) {
55-
await client.indices.putMapping({
56-
index: topResources[key].index,
57-
type: topResources[key].type,
58-
include_type_name: true,
59-
body: {
60-
properties: {
61-
[userResources[childKey].propertyName]: {
62-
type: 'nested'
63-
}
64-
}
65-
}
66-
})
67-
}
68-
}
69-
}
70-
}
71-
if (top['enrich']) {
72-
logger.info(`The enrich policy ${top.enrich.policyName} will be created.`)
73-
await client.enrich.putPolicy({
74-
name: top.enrich.policyName,
75-
body: {
76-
match: {
77-
indices: top.index,
78-
match_field: top.enrich.matchField,
79-
enrich_fields: top.enrich.enrichFields
80-
}
81-
}
82-
})
83-
await client.enrich.executePolicy({ name: top.enrich.policyName })
84-
}
85-
if (top.pipeline) {
86-
if (top.pipeline.processors) {
87-
processors[top.pipeline.id] = []
88-
_.each(top.pipeline.processors, processor => {
89-
processors[top.pipeline.id].push({
90-
foreach: {
91-
field: processor.referenceField,
92-
ignore_missing: true,
93-
processor: {
94-
enrich: {
95-
policy_name: processor.enrichPolicyName,
96-
ignore_missing: true,
97-
field: processor.field,
98-
target_field: processor.targetField,
99-
max_matches: processor.maxMatches
100-
}
101-
}
102-
}
103-
})
104-
})
105-
} else {
106-
processors[top.pipeline.id] = [{
107-
enrich: {
108-
policy_name: top.enrich.policyName,
109-
ignore_missing: true,
110-
field: top.pipeline.field,
111-
target_field: top.pipeline.targetField,
112-
max_matches: top.pipeline.maxMatches
113-
}
114-
}]
115-
}
116-
}
117-
}
118-
119-
for (const key in processors) {
120-
logger.info(`The pipeline ${key} will be created.`)
121-
await client.ingest.putPipeline({
122-
id: key,
123-
body: {
124-
processors: processors[key]
125-
}
126-
})
127-
}
128-
}
129-
130-
/**
131-
* Delete elastic search index
132-
*/
133-
const clearES = async () => {
134-
for (const key in topResources) {
135-
if (topResources[key].pipeline) {
136-
try {
137-
logger.info(`Delete pipeline ${topResources[key].pipeline.id} if any.`)
138-
await client.ingest.deletePipeline({ id: topResources[key].pipeline.id })
139-
} catch (err) {
140-
// ignore
141-
}
142-
}
143-
}
144-
for (const key in topResources) {
145-
if (topResources[key].enrich) {
146-
try {
147-
const policyName = topResources[key].enrich.policyName
148-
logger.info(`Delete enrich policy ${policyName} if any.`)
149-
await client.enrich.deletePolicy({
150-
name: policyName
151-
})
152-
} catch (err) {
153-
// ignore
154-
}
155-
}
156-
}
157-
for (const key in topResources) {
158-
logger.info(`Delete index ${topResources[key].index} if any.`)
159-
try {
160-
await client.indices.delete({ index: topResources[key].index })
161-
} catch (err) {
162-
// ignore
163-
}
164-
}
165-
}
166-
16720
/**
16821
* Check if elastic search is empty
16922
*/
@@ -202,21 +55,7 @@ const clearData = async () => {
20255
}
20356
}
20457

205-
if (!module.parent) {
206-
const isForce = process.argv.length === 3 && process.argv[2] === 'force'
207-
208-
init(isForce).then(() => {
209-
logger.info('done')
210-
process.exit()
211-
}).catch((e) => {
212-
logger.logFullError(e)
213-
process.exit()
214-
})
215-
}
216-
21758
module.exports = {
218-
init,
219-
clearES,
22059
checkEmpty,
22160
clearData
22261
}

0 commit comments

Comments
 (0)