Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.72 KB | None | 0 0
  1. package net.astercrono;
  2.  
  3. import java.awt.Color;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Objects;
  10. import java.util.Timer;
  11. import java.util.TimerTask;
  12. import java.util.Date;
  13.  
  14. import javax.swing.JFrame;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JTable;
  17.  
  18. public class App {
  19.     private static final String SQL = " " + "select " + "   case "
  20.             + "       when minutes > 60 then round((minutes / 60)::numeric, 1) || ' hours' "
  21.             + "       else round(minutes::numeric, 1) || ' minutes'  " + "       end as dur, "
  22.             + "       pid, usr, app, addr::varchar, start, wait_event_type, wait_event, state, query "
  23.             + "       from ( "
  24.             + "           select extract(epoch from now() - query_start) / 60 as minutes, pid, usename as usr, application_name as app, client_addr as addr, wait_event_type, wait_event, to_char(query_start, 'MM/DD/YY HH24:MI:SS') as start, state, query "
  25.             + "           from pg_stat_activity ps " + "           where state != 'idle' " + "       ) stats "
  26.             + "       where minutes > 0 " + "       and lower(query) !~ 'autovacuum' " + " order by minutes desc ";
  27.  
  28.     private static final String[] COLUMN_NAMES = new String[] { "dur", "pid", "usr", "app", "addr", "start",
  29.             "wait-event-type", "wait-event", "state", "query" };
  30.  
  31.     private static JFrame window = new JFrame();
  32.     private static JTable currentTable;
  33.     private static JScrollPane scrollPane;
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.         window.setSize(1000, 800);
  38.  
  39.         Timer timer = new Timer();
  40.         timer.scheduleAtFixedRate(new TimerTask() {
  41.             @Override
  42.             public void run() {
  43.                 populateTable();
  44.             }
  45.         }, 0, 1000 * 30);
  46.     }
  47.  
  48.     private static void populateTable() {
  49.         System.out.println("populating table");
  50.  
  51.         try {
  52.             if (currentTable != null) {
  53.                 window.remove(scrollPane);
  54.             }
  55.  
  56.             String[][] data = query();
  57.             currentTable = new JTable(data, COLUMN_NAMES);
  58.             currentTable.setShowHorizontalLines(false);
  59.             currentTable.setShowVerticalLines(false);
  60.  
  61.             currentTable.getColumn("dur").setPreferredWidth(100);
  62.             currentTable.getColumn("pid").setPreferredWidth(100);
  63.             currentTable.getColumn("usr").setPreferredWidth(100);
  64.             currentTable.getColumn("app").setPreferredWidth(100);
  65.             currentTable.getColumn("addr").setPreferredWidth(100);
  66.             currentTable.getColumn("start").setPreferredWidth(100);
  67.             currentTable.getColumn("wait-event-type").setPreferredWidth(100);
  68.             currentTable.getColumn("wait-event").setPreferredWidth(100);
  69.             currentTable.getColumn("state").setPreferredWidth(100);
  70.             currentTable.getColumn("query").setPreferredWidth(500);
  71.  
  72.             currentTable.getTableHeader().setBackground(new Color(70, 70, 70));
  73.             currentTable.getTableHeader().setForeground(Color.WHITE);
  74.  
  75.             scrollPane = new JScrollPane(currentTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  76.                     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  77.             currentTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
  78.  
  79.             window.add(scrollPane);
  80.             window.setVisible(true);
  81.             window.repaint();
  82.         } catch (Exception ex) {
  83.             ex.printStackTrace();
  84.         }
  85.  
  86.         System.out.println("table populated");
  87.     }
  88.  
  89.     private static String[][] query() throws Exception {
  90.         System.out.println("querying");
  91.  
  92.         Class.forName("org.postgresql.Driver");
  93.         Connection connection = null;
  94.         connection = DriverManager.getConnection("jdbc:postgresql://pgprimary.saucontds.com:5432/tds", "postgres",
  95.                 "postgres");
  96.  
  97.         ResultSet rs = connection.createStatement().executeQuery(SQL);
  98.         System.out.println("query returned");
  99.         List<String[]> allRows = new ArrayList<String[]>();
  100.  
  101.         while (rs.next()) {
  102.             String dur = Objects.toString(rs.getString("dur"), "--");
  103.             String pid = Objects.toString(rs.getInt("pid"), "--");
  104.             String usr = Objects.toString(rs.getString("usr"), "--");
  105.             String app = Objects.toString(rs.getString("app"), "--");
  106.             String addr = Objects.toString(rs.getString("addr"), "--");
  107.             String start = Objects.toString(rs.getString("start"), "--");
  108.             String waitEventType = Objects.toString(rs.getString("wait_event_type"), "--");
  109.             String waitEvent = Objects.toString(rs.getString("wait_event"), "--");
  110.             String state = Objects.toString(rs.getString("state"), "--");
  111.  
  112.             String query = Objects.toString(rs.getString("query"), "--");
  113.             if (query.length() > 110) {
  114.                 query = query.substring(0, 110);
  115.             }
  116.  
  117.             String[] row = new String[] { dur, pid, usr, app, addr, start, waitEventType, waitEvent, state, query };
  118.  
  119.             allRows.add(row);
  120.         }
  121.  
  122.         connection.close();
  123.  
  124.         System.out.println(allRows.size() + " rows");
  125.         return allRows.toArray(new String[allRows.size()][10]);
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement