Commit 5243cd9f authored by George Tziavas's avatar George Tziavas
Browse files

Added job and tmf model -> jdbc error on intelij

parent 9604fa7e
Loading
Loading
Loading
Loading
+39 −39
Original line number Diff line number Diff line
@@ -10,8 +10,8 @@
	</parent>


	<artifactId>org.etsi.osl.metco</artifactId>
	<name>org.etsi.osl.metco</name>
	<artifactId>org.etsi.osl.metrico</artifactId>
	<name>org.etsi.osl.metrico</name>
	<url>http://openslice.io</url>


+44 −0
Original line number Diff line number Diff line
package org.etsi.osl.metco;
package org.etsi.osl.metrico;


import org.etsi.osl.metrico.prometheus.PrometheusQueries;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
public class MetcoController {
public class MetricoController {

    private static final Logger logger = LoggerFactory.getLogger(MetricoController.class);

    private static final Logger logger = LoggerFactory.getLogger(MetcoController.class);
    private PrometheusQueries prometheusQueries;

    @GetMapping("/live")
    public ResponseEntity<String> livenessCheck() {
        return new ResponseEntity<>("Application is running", HttpStatus.OK);
    }

    @GetMapping("/queryPrometheus")
    public ResponseEntity<String> queryPrometheus(
            @RequestParam(defaultValue = "https") String protocol,
            @RequestParam(required = false) String prom_ip,
            @RequestParam(defaultValue = "9000") String prom_port,
            @RequestParam String query
    ) {
        if (prom_ip == null) {
            return new ResponseEntity<>("prom_ip parameter is missing", HttpStatus.BAD_REQUEST);
        }
        String prom_url = protocol + "://" + prom_ip + ":" + prom_port;
        String[] prometheusData = PrometheusQueries.sendQueryToPrometheus(prom_url, query).split("\n");
        return new ResponseEntity<>(Arrays.toString(prometheusData), HttpStatus.OK);
    }



}
+6 −16
Original line number Diff line number Diff line
package org.etsi.osl.metco;
package org.etsi.osl.metrico;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -6,19 +6,15 @@ import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;



@SpringBootApplication
public class MetcoSpringBoot implements CommandLineRunner {
public class MetricoSpringBoot implements CommandLineRunner {

  private static final Logger logger =
          LoggerFactory.getLogger(MetcoSpringBoot.class.getSimpleName());
          LoggerFactory.getLogger(MetricoSpringBoot.class.getSimpleName());


  @Override
@@ -29,8 +25,8 @@ public class MetcoSpringBoot implements CommandLineRunner {
  }

  public static void main(String[] args) {
    logger.info("=========== STARTING METCO ==============================");
    ApplicationContext applicationContext = new SpringApplication(MetcoSpringBoot.class).run(args);
    logger.info("============================== STARTING METRICO ==============================");
    ApplicationContext applicationContext = new SpringApplication(MetricoSpringBoot.class).run(args);
  }

  static class ExitException extends RuntimeException implements ExitCodeGenerator {
@@ -42,9 +38,3 @@ public class MetcoSpringBoot implements CommandLineRunner {
  }

}

@Configuration
@EnableScheduling
@ConditionalOnProperty(name = "scheduling.enabled", matchIfMissing = true)
class SchedulingConfiguration {
}
 No newline at end of file
+86 −0
Original line number Diff line number Diff line
package org.etsi.osl.metrico.model;

import lombok.Getter;
import lombok.Setter;
import org.etsi.osl.tmf.pm628.model.ExecutionStateType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.*;

@Getter
@Setter
public class Job {


    private Long id;


    private UUID uuid;


    private ExecutionStateType state;


    private boolean deleted = false;

    private final Runnable task;
    private ScheduledFuture<?> future;


    public Job(Runnable task) {
        this.task = task;
        this.state = ExecutionStateType.PENDING;
    }

    private static final Logger logger = LoggerFactory.getLogger(Job.class);
    @Getter
    private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    private static final Map<UUID, Job> jobs = new ConcurrentHashMap<>();

    public Job startJob(Runnable task, long initialDelay, long periodicDelay) {
        UUID jobId = UUID.randomUUID();
        Job job = new Job(task);
        job.setUuid(jobId);
        job.setState(ExecutionStateType.PENDING);
        try {
            ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(task, initialDelay, periodicDelay, TimeUnit.SECONDS);
            job.setFuture(future);
            jobs.put(jobId, job);
            job.setState(ExecutionStateType.INPROGRESS);
            logger.info("Job with ID {} started successfully.", jobId);
        } catch (RejectedExecutionException e) {
            job.setState(ExecutionStateType.FAILED);
            logger.error("Job with ID {} could not be scheduled.", jobId, e);
        }
        return job;
    }

    public void stopJob(UUID jobId) {
        Job job = jobs.get(jobId);
        if (job != null) {
            if (job.getState() == ExecutionStateType.CANCELLED ) {
                logger.info("Job with ID {} is already CANCELED.", jobId);
                return;
            } else if (job.getState() == ExecutionStateType.COMPLETED) {
                logger.info("Job with ID {} is already COMPLETED.", jobId);
                return;
            }
            if (job.getFuture() != null) {
                boolean wasCancelled = job.getFuture().cancel(true);
                if (wasCancelled) {
                    job.setState(ExecutionStateType.CANCELLED);
                    logger.info("Job with ID {} stopped successfully.", jobId);
                } else {
                    job.setState(ExecutionStateType.PENDING);
                    logger.warn("Job with ID {} could not be stopped because it has already completed, has been cancelled, or could not be cancelled for some other reason.", jobId);
                }
            }
        } else {
            logger.warn("Job with ID {} does not exist.", jobId);
        }
    }

}
+17 −0
Original line number Diff line number Diff line
package org.etsi.osl.metrico.model;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class StartPeriodicQueryRequest {
    private String protocol = "https";
    private String prom_ip;
    private String prom_port = "9090";
    private String query;
    private int initialDelay = 0;
    private int periodicDelay = 300;
    private int stopAfterSeconds = 0;

}
Loading