2626import com .park .utmstack .service .network_scan .UtmNetworkScanService ;
2727import com .park .utmstack .util .enums .AlertSeverityEnum ;
2828import com .park .utmstack .util .enums .AlertStatus ;
29- import com .park .utmstack .util .exceptions .ApiException ;
3029import lombok .RequiredArgsConstructor ;
3130import org .apache .http .conn .util .InetAddressUtils ;
3231import org .opensearch .client .json .JsonData ;
3736import org .slf4j .LoggerFactory ;
3837import org .springframework .data .domain .Page ;
3938import org .springframework .data .domain .Pageable ;
40- import org .springframework .http .HttpStatus ;
4139import org .springframework .scheduling .annotation .Scheduled ;
4240import org .springframework .stereotype .Service ;
4341import org .springframework .transaction .annotation .Transactional ;
@@ -198,11 +196,14 @@ public void syncDataInputStatus() {
198196 latestStats .forEach ((key , stat ) -> {
199197 try {
200198 String dataType = stat .getDataType ();
201- String assetName = this .getDataSource (stat .getDataSource ());
199+ String statName = stat .getDataSource ();
200+ String sourceWithAlias = this .getSourceName (statName );
201+ String resolvedAlias = sourceWithAlias != null ? statName : null ;
202+ String source = sourceWithAlias == null ? statName : sourceWithAlias ;
202203
203204 long timestamp = Instant .parse (stat .getTimestamp ()).getEpochSecond ();
204205
205- String compositeKey = dataType + "-" + assetName ;
206+ String compositeKey = dataType + "-" + source ;
206207
207208 UtmDataInputStatus status = existing .get (compositeKey );
208209 boolean changed = false ;
@@ -211,19 +212,16 @@ public void syncDataInputStatus() {
211212 status = UtmDataInputStatus .builder ()
212213 .id (compositeKey )
213214 .dataType (dataType )
214- .source (assetName )
215+ .source (source )
216+ .alias (resolvedAlias )
215217 .timestamp (timestamp )
216218 .median (86400L )
217219 .build ();
218220 changed = true ;
219221
220- } else if (status .getTimestamp () != timestamp ) {
221-
222- if (!assetName .equals (stat .getDataSource ())) {
223- status .setSource (stat .getDataSource ());
224- }
225-
222+ } else if (status .getTimestamp () != timestamp || !Objects .equals (status .getAlias (), resolvedAlias )) {
226223 status .setTimestamp (timestamp );
224+ status .setAlias (resolvedAlias );
227225 changed = true ;
228226 }
229227
@@ -280,6 +278,7 @@ public void synchronizeSourcesToAssets() {
280278 }
281279
282280 Map <String , Boolean > sourcesWithStatus = extractSourcesWithUpDownStatus (sources );
281+ Map <String , String > sourcesWithAlias = extractSourcesWithAlias (sources );
283282
284283 List <String > keys = new ArrayList <>(sourcesWithStatus .keySet ());
285284 List <UtmNetworkScan > assets = networkScanRepository .findByAssetIpInOrAssetNameIn (keys , keys );
@@ -293,12 +292,23 @@ public void synchronizeSourcesToAssets() {
293292
294293 for (Map .Entry <String , Boolean > entry : sourcesWithStatus .entrySet ()) {
295294 String key = entry .getKey ();
295+ String alias = sourcesWithAlias .get (key );
296296 Boolean alive = entry .getValue ();
297297
298+
298299 UtmNetworkScan asset = assetsByKey .get (key );
299300
301+ if (asset == null && StringUtils .hasText (alias )) {
302+ asset = assetsByKey .get (alias );
303+ }
304+
300305 if (asset != null ) {
301306 if (asset .getUpdateLevel () == null || asset .getUpdateLevel ().equals (UpdateLevel .DATASOURCE ) || asset .getUpdateLevel ().equals (UpdateLevel .AGENT )) {
307+
308+ if (StringUtils .hasText (alias ) && !alias .equals (asset .getAssetAlias ())) {
309+ asset .assetAliases (alias );
310+ }
311+
302312 asset .assetAlive (alive )
303313 .updateLevel (UpdateLevel .DATASOURCE )
304314 .assetStatus (AssetStatus .CHECK )
@@ -332,6 +342,17 @@ public void synchronizeSourcesToAssets() {
332342 }
333343 }
334344
345+ private Map <String , String > extractSourcesWithAlias (List <UtmDataInputStatus > sources ) {
346+ Map <String , String > alias = new HashMap <>();
347+
348+ sources .forEach (src -> {
349+ if (StringUtils .hasText (src .getAlias ())) {
350+ alias .put (src .getSource (), src .getAlias ());
351+ }
352+ });
353+ return alias ;
354+ }
355+
335356 private Map <String , Boolean > extractSourcesWithUpDownStatus (List <UtmDataInputStatus > sources ) {
336357 Map <String , Boolean > upDown = new HashMap <>();
337358 sources .forEach (src -> {
@@ -509,26 +530,26 @@ private Map<String, StatisticDocument> getLatestStatisticsByDataSource() {
509530 return result ;
510531 }
511532
512- private String getDataSource (String assetName ) {
533+ private String getSourceName (String assetName ) {
513534 final String ctx = CLASSNAME + ".getDataSource" ;
514535
515536 Optional <UtmTenantConfig > tenantConfig = this .utmTenantConfigService .findByAssetName (assetName );
516537
517538 if (tenantConfig .isEmpty ()) {
518- return assetName ;
539+ return null ;
519540 }
520541
521542 List <String > sources = buildSourcesList (tenantConfig .get ());
522543
523544 if (CollectionUtils .isEmpty (sources )) {
524- return assetName ;
545+ return null ;
525546 }
526547
527548 Optional <UtmDataInputStatus > dataInputStatus = this .findDataInputBySource (sources );
528549
529550 return dataInputStatus
530551 .map (UtmDataInputStatus ::getSource )
531- .orElse (assetName );
552+ .orElse (null );
532553 }
533554
534555 /**
0 commit comments