-
Notifications
You must be signed in to change notification settings - Fork 6
Feat/add rollup #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feat/add rollup #400
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
524ef9b
feat: add rollup job
GraceRuan b5a8c3b
feat: update workflow on rollup job
GraceRuan 18c7bf0
feat: update readme
GraceRuan 6f5da79
fix: format ts files
GraceRuan 97fb060
fix: format ts file
GraceRuan 46874d9
feat: add rollup
GraceRuan 7d394e0
fix: properties for value_count
GraceRuan 6471b7a
fix: add delay in transitions
GraceRuan 3b7abc6
fix: add alias for rollup index
GraceRuan 1444000
fix: inline definition no need delay
GraceRuan 028b0ed
feat: delay 14 days before rollup
GraceRuan b333b49
fix: scale reset to 4d
GraceRuan 7dd5059
fix: adjust scaled stage
GraceRuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
workflow-cli/configuration-opensearch/index_template/accessrollup.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"index_patterns": ["nrm-accessrollup-*"], | ||
"template": { | ||
"aliases": { | ||
"nrm-accessrollup": {} | ||
}, | ||
"settings": { | ||
"number_of_shards": 1, | ||
"number_of_replicas": 1 | ||
}, | ||
"mappings": { | ||
"dynamic": "strict", | ||
"properties": { | ||
"url.path": { | ||
"type": "keyword" | ||
}, | ||
"url.domain": { | ||
"type": "keyword" | ||
}, | ||
"@timestamp": { | ||
"type": "date" | ||
}, | ||
"event.outcome": { | ||
"properties": { | ||
"value_count": { | ||
"type": "long" | ||
} | ||
} | ||
}, | ||
"event.duration": { | ||
"properties": { | ||
"avg": { | ||
"type": "double" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"version": 1, | ||
"priority": 300, | ||
"_meta": { | ||
"description": "NRM Access Url Rollup indices" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'reflect-metadata'; | ||
import { Command } from '@oclif/core'; | ||
import AwsService from '../services/aws.service'; | ||
import { | ||
accessId, | ||
accessKey, | ||
accountNumber, | ||
arn, | ||
domainName, | ||
help, | ||
hostname, | ||
region, | ||
dryRun, | ||
} from '../flags'; | ||
import OpenSearchRollupService from '../services/opensearch-rollup.service'; | ||
import OpenSearchDomainService from '../services/opensearch-domain.service'; | ||
|
||
export default class OpenSearchRollup extends Command { | ||
static description = 'Create/Update OpenSearch Rollup Index'; | ||
|
||
static examples = ['<%= config.bin %> <%= command.id %>']; | ||
|
||
static flags = { | ||
...hostname, | ||
...domainName, | ||
...region, | ||
...accessId, | ||
...accessKey, | ||
...accountNumber, | ||
...arn, | ||
...help, | ||
...dryRun, | ||
}; | ||
|
||
public async run(): Promise<void> { | ||
const { flags } = await this.parse(OpenSearchRollup); | ||
await AwsService.assumeIdentity(flags); | ||
|
||
const service = new OpenSearchRollupService(); | ||
const domainService = new OpenSearchDomainService(); | ||
|
||
await domainService.getDomain(flags); | ||
await service.createOrUpdateRollupJob(flags); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import AwsService from './aws.service'; | ||
import { WorkflowSettings } from './opensearch-domain.service'; | ||
|
||
export default class OpenSearchRollupService extends AwsService { | ||
public async createOrUpdateRollupJob( | ||
settings: WorkflowSettings, | ||
): Promise<any> { | ||
const templateDir = path.resolve( | ||
__dirname, | ||
'../../configuration-opensearch/rollup_index_jobs', | ||
); | ||
|
||
for (const filePath of fs.readdirSync(templateDir)) { | ||
if (!filePath.endsWith('.json')) { | ||
continue; | ||
} | ||
const rollupJobName = path.basename(filePath, '.json'); | ||
let rollupJobPath = `/_plugins/_rollup/jobs/${rollupJobName}`; | ||
const exists = await this.checkRollupJobExists(settings, rollupJobName); | ||
const rollupConfig = JSON.parse( | ||
fs.readFileSync(path.join(templateDir, filePath), 'utf8'), | ||
); | ||
|
||
if (exists) { | ||
rollupJobPath += '?if_seq_no=1&if_primary_term=1'; | ||
console.log(`Updating existing rollup job: ${rollupJobName}`); | ||
return this.executeRollupRequest( | ||
'PUT', | ||
settings, | ||
rollupJobPath, | ||
rollupConfig, | ||
).then((res) => { | ||
console.log( | ||
`PUT ${rollupJobPath}\n[${res.statusCode}] Rollup job updated`, | ||
); | ||
}); | ||
} else { | ||
console.log(`Creating new rollup job: ${rollupJobName}`); | ||
return this.executeRollupRequest( | ||
'PUT', | ||
settings, | ||
rollupJobPath, | ||
rollupConfig, | ||
).then((res) => { | ||
console.log( | ||
`PUT ${rollupJobPath}\n[${res.statusCode}] Rollup job created`, | ||
); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
private async executeRollupRequest( | ||
method: string, | ||
settings: WorkflowSettings, | ||
path: string, | ||
body?: any, | ||
): Promise<any> { | ||
const [pathWithoutQuery, queryString] = path.split('?'); | ||
const queryParams = queryString | ||
? Object.fromEntries(new URLSearchParams(queryString).entries()) | ||
: undefined; | ||
const requestOptions = { | ||
method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
host: settings.hostname, | ||
}, | ||
hostname: settings.hostname, | ||
path: pathWithoutQuery, | ||
query: queryParams, | ||
body: body ? JSON.stringify(body) : undefined, | ||
}; | ||
|
||
return this.executeSignedHttpRequest(requestOptions) | ||
.then((res) => this.waitAndReturnResponseBody(res)) | ||
.catch((err) => { | ||
console.error( | ||
`Error during ${method} request to ${path}: ${err.message}`, | ||
); | ||
throw err; | ||
}); | ||
} | ||
|
||
// check if the rollup job exists | ||
private async checkRollupJobExists( | ||
settings: WorkflowSettings, | ||
jobName: string, | ||
): Promise<boolean> { | ||
const rollupJobPath = `/_plugins/_rollup/jobs/${jobName}`; | ||
return this.executeRollupRequest('GET', settings, rollupJobPath) | ||
.then((res) => { | ||
if (res.statusCode === 200) { | ||
console.log(`[${res.statusCode}] Rollup job exists`); | ||
return true; | ||
} else if (res.statusCode === 404) { | ||
console.log(`[${res.statusCode}] Rollup job does not exist`); | ||
return false; | ||
} else { | ||
throw new Error(`Unexpected status code: ${res.statusCode}`); | ||
} | ||
}) | ||
.catch((err) => { | ||
if (err.response?.statusCode === 404) { | ||
console.log(`[404] Rollup job does not exist`); | ||
return false; | ||
} | ||
console.error(`Error checking rollup job: ${err.message}`); | ||
throw err; | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.