package com.jasamedika.medifirst2000.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.entities.PgStatActivity;

/**
 * @author salmanoe
 * @since Jan 12, 2022
 */
@Repository("pgStatActivityDao")
public interface PgStatActivityDao extends PagingAndSortingRepository<PgStatActivity, Integer> {

	@Query(value = "select pid, " + "justify_interval(now() - pg_stat_activity.query_start) as duration, " + "query, "
			+ "state " + "from pg_stat_activity " + "where state != 'idle' "
			+ "and (now() - pg_stat_activity.query_start) > interval '1 minutes' "
			+ "order by pg_stat_activity.query_start", nativeQuery = true)
	List<PgStatActivity> findByStateAndInterval();

	@Query(value = "select pg_cancel_backend(:pid)", nativeQuery = true)
	void callPgCancelBackend(@Param("pid") Integer pid);

	@Query(value = "select pg_terminate_backend(:pid)", nativeQuery = true)
	void callPgTerminateBackend(@Param("pid") Integer pid);

}
