Skip to content

Commit

Permalink
[improve] auto generate readable random monitor name (apache#2531)
Browse files Browse the repository at this point in the history
Signed-off-by: tomsun28 <[email protected]>
Co-authored-by: YuLuo <[email protected]>
  • Loading branch information
tomsun28 and yuluo-yx authored Aug 16, 2024
1 parent 53a5065 commit 9d62270
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

import { Component, Inject, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { ActivatedRoute, ParamMap, Router } from '@angular/router';
import { I18NService } from '@core';
import { ALAIN_I18N_TOKEN, TitleService } from '@delon/theme';
Expand All @@ -33,6 +32,7 @@ import { ParamDefine } from '../../../pojo/ParamDefine';
import { AppDefineService } from '../../../service/app-define.service';
import { CollectorService } from '../../../service/collector.service';
import { MonitorService } from '../../../service/monitor.service';
import { generateReadableRandomString } from '../../../shared/utils/common-util';

@Component({
selector: 'app-monitor-add',
Expand Down Expand Up @@ -153,11 +153,8 @@ export class MonitorNewComponent implements OnInit {
}

onHostChange(hostValue: string) {
if (this.monitor.app != 'prometheus') {
let autoName = `${this.monitor.app.toUpperCase()}_${hostValue}`;
if (this.monitor.name == undefined || this.monitor.name == '' || this.monitor.name.startsWith(this.monitor.app.toUpperCase())) {
this.monitor.name = autoName;
}
if (this.monitor.name == undefined || this.monitor.name == '') {
this.monitor.name = generateReadableRandomString();
}
}

Expand Down
119 changes: 119 additions & 0 deletions web-app/src/app/shared/utils/common-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,122 @@ export function findDeepestSelected(nodes: any): any {
}
return deepestSelectedNode;
}

export function generateReadableRandomString(): string {
const adjectives = [
'quick',
'bright',
'calm',
'brave',
'cool',
'eager',
'fancy',
'gentle',
'happy',
'jolly',
'kind',
'lively',
'merry',
'nice',
'proud',
'witty',
'zesty',
'nifty',
'quirky',
'unique',
'vivid',
'zany',
'zealous',
'yummy',
'agile',
'bold',
'daring',
'fearless',
'gleeful',
'humble',
'jumpy',
'keen',
'loyal',
'majestic',
'noble',
'playful',
'radiant',
'spirited',
'tenacious',
'vibrant',
'wise',
'youthful',
'zippy',
'serene',
'bubbly',
'dreamy',
'fierce',
'graceful'
];

const nouns = [
'fox',
'lion',
'eagle',
'shark',
'whale',
'falcon',
'panda',
'tiger',
'wolf',
'otter',
'lynx',
'moose',
'dolphin',
'bear',
'hawk',
'zebra',
'giraffe',
'koala',
'lemur',
'lemming',
'cheetah',
'dragon',
'owl',
'rhino',
'stingray',
'jaguar',
'panther',
'elk',
'ocelot',
'beaver',
'walrus',
'gazelle',
'coyote',
'vulture',
'iguana',
'porcupine',
'raccoon',
'sloth',
'armadillo',
'chameleon',
'kestrel',
'weasel',
'hedgehog'
];

const digits = '23456789';
const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz';

// Randomly select an adjective and a noun
let adjective = adjectives[Math.floor(Math.random() * adjectives.length)];
let noun = nouns[Math.floor(Math.random() * nouns.length)];

// Randomly generate a sequence of numbers and characters
const randomDigits = Array.from({ length: 2 }, () => digits.charAt(Math.floor(Math.random() * digits.length))).join('');

const randomChars = Array.from({ length: 2 }, () => chars.charAt(Math.floor(Math.random() * chars.length))).join('');
adjective = capitalizeFirstLetter(adjective);
noun = capitalizeFirstLetter(noun);
// Combine the parts to form the final string
return `${adjective}_${noun}_${randomDigits}${randomChars}`;
}

function capitalizeFirstLetter(word: string): string {
return word.charAt(0).toUpperCase() + word.slice(1);
}

0 comments on commit 9d62270

Please sign in to comment.