LiuTianyou commented on code in PR #1658: URL: https://github.com/apache/hertzbeat/pull/1658#discussion_r1740097627
########## grafana/src/main/java/org/apache/hertzbeat/grafana/service/DatasourceService.java: ########## @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.grafana.service; + + +import static org.apache.hertzbeat.grafana.common.CommonConstants.CREATE_DATASOURCE_API; +import static org.apache.hertzbeat.grafana.common.CommonConstants.DATASOURCE_ACCESS; +import static org.apache.hertzbeat.grafana.common.CommonConstants.DATASOURCE_NAME; +import static org.apache.hertzbeat.grafana.common.CommonConstants.DATASOURCE_TYPE; +import static org.apache.hertzbeat.grafana.common.CommonConstants.DELETE_DATASOURCE_API; +import jakarta.annotation.PostConstruct; +import java.util.Base64; +import lombok.extern.slf4j.Slf4j; +import org.apache.hertzbeat.grafana.config.GrafanaConfiguration; +import org.apache.hertzbeat.warehouse.store.history.vm.VictoriaMetricsProperties; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +/** + * Service for managing Grafana datasources. + */ +@Service +@Slf4j +public class DatasourceService { + + private String grafanaUrl; + private String username; + private String password; + private String prefix; + private String victoriaMetricsUrl; + + private final GrafanaConfiguration grafanaConfiguration; + private final VictoriaMetricsProperties warehouseProperties; + private final RestTemplate restTemplate; + + @Autowired + public DatasourceService( + GrafanaConfiguration grafanaConfiguration, + VictoriaMetricsProperties warehouseProperties, + RestTemplate restTemplate + ) { + this.grafanaConfiguration = grafanaConfiguration; + this.warehouseProperties = warehouseProperties; + this.restTemplate = restTemplate; + } + + @PostConstruct + public void init() { + this.grafanaUrl = grafanaConfiguration.getUrl(); + this.username = grafanaConfiguration.getUsername(); + this.password = grafanaConfiguration.getPassword(); + this.prefix = grafanaConfiguration.getPrefix(); + this.victoriaMetricsUrl = warehouseProperties.url(); + } + + /** + * Creates a new datasource in Grafana. + * + * @return ResponseEntity containing the response from Grafana + */ + public ResponseEntity<String> createDatasource() { + String url = String.format(prefix + CREATE_DATASOURCE_API, username, password, grafanaUrl); + + HttpHeaders headers = createHeaders(); + + String body = String.format( + "{\"name\":\"%s\",\"type\":\"%s\",\"access\":\"%s\",\"url\":\"%s\",\"basicAuth\":%s}", + DATASOURCE_NAME, DATASOURCE_TYPE, DATASOURCE_ACCESS, victoriaMetricsUrl, false + ); + + HttpEntity<String> entity = new HttpEntity<>(body, headers); + + try { + ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class); + if (response.getStatusCode().is2xxSuccessful()) { + log.info("Create datasource success"); + } + return response; + } catch (Exception ex) { + log.error("Create datasource error", ex); + throw new RuntimeException("Create datasource error", ex); + } + } + + /** + * Deletes a datasource in Grafana. + * + * @return ResponseEntity containing the response from Grafana + */ + public ResponseEntity<String> deleteDatasource() { + String url = String.format(prefix + DELETE_DATASOURCE_API, username, password, grafanaUrl, DATASOURCE_NAME); + + HttpHeaders headers = createHeaders(); + + HttpEntity<Void> entity = new HttpEntity<>(headers); + + try { + ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.DELETE, entity, String.class); + if (response.getStatusCode().is2xxSuccessful()) { + log.info("Delete datasource success"); + } + return response; + } catch (Exception ex) { + log.error("Delete datasource error", ex); + throw new RuntimeException("Delete datasource error", ex); + } + } + Review Comment: hi, if the datasource is manually deleted by the user in Grafana, the status code through Grafana's HTTP API will be 404, an exception will be thrown, and the manager will not be able to be started normally next time -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
