<commit_before>package controllers;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import models.DataSource;
import models.PublisherTask;
import play.Configuration;
import play.libs.F.Promise;
import play.libs.ws.WSClient;
import play.libs.ws.WSRequest;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.index;

public class Application extends Controller {
	@Inject Configuration config;
	@Inject WSClient ws;
	
	public Promise<Result> index() {
		final String logo = config.getString("dashboard.client.logo");
		
		return getDataSources().map(dataSources -> {
			List<PublisherTask> publisherTasks = getPublisherTasks();
			
			return ok(index.render(logo, dataSources, publisherTasks));
		});
	}
	
	private Promise<List<DataSource>> getDataSources() {
		final String url = config.getString("dashboard.provider.connection.url");
		WSRequest request = ws.url(url).setFollowRedirects(true);
		return request.get().map(response -> {
			Gson gson = new GsonBuilder().create();
			String json = new String(response.asByteArray());
			
			try {
				DataSource[] dataSources = gson.fromJson(json, DataSource[].class);
				
				if(dataSources != null) {
					List<DataSource> listDataSources = Arrays.asList(dataSources);
					
					return listDataSources;
				}
			} catch(JsonSyntaxException jse) {
				jse.printStackTrace();
			}
			
			return Collections.emptyList();
		});
	}
	
	private List<PublisherTask> getPublisherTasks() {
		final String databaseUser = config.getString("dashboard.publisher.db.user");
		final String databasePassword = config.getString("dashboard.publisher.db.password");
		
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		List<PublisherTask> publisherTasks = new ArrayList<PublisherTask>();
		
		try {
			Class.forName("org.postgresql.Driver");
			
			conn = 
					DriverManager
						.getConnection("jdbc:postgresql://pub.db/publisher", databaseUser, databasePassword);
			
			String sql = 
					"select js.job_id, js.state, js.create_time, j.type from publisher.job_state js "
						+ "join publisher.job j on j.id = js.job_id "
						+ "left join publisher.service_job sj on sj.job_id = js.job_id "
						+ "where js.state != ? "
						+ "and (j.type = ? or j.type = ? or j.type = ?) "
						+ "and (sj.type = ? or sj.type is null) "
						+ "order by js.create_time desc "
						+ "limit 10;";
			
			stmt = conn.prepareStatement(sql);<commit_msg>Return empty dataSources list if url to provider check fails<commit_after>package controllers;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import models.DataSource;
import models.PublisherTask;
import play.Configuration;
import play.libs.F.Promise;
import play.libs.ws.WSClient;
import play.libs.ws.WSRequest;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.index;

public class Application extends Controller {
	@Inject Configuration config;
	@Inject WSClient ws;
	
	public Promise<Result> index() {
		final String logo = config.getString("dashboard.client.logo");
		
		return getDataSources().map(dataSources -> {
			List<PublisherTask> publisherTasks = getPublisherTasks();
			
			return ok(index.render(logo, dataSources, publisherTasks));
		});
	}
	
	private Promise<List<DataSource>> getDataSources() {
		final String url = config.getString("dashboard.provider.connection.url");
		
		WSRequest request = ws.url(url).setFollowRedirects(true);
			Promise<List<DataSource>> listDataSources = request.get().map(response -> {
				Gson gson = new GsonBuilder().create();
				String json = new String(response.asByteArray());
				
				try {
					DataSource[] dataSources = gson.fromJson(json, DataSource[].class);
					
					if(dataSources != null) {
						return Arrays.asList(dataSources);
					}
				} catch(JsonSyntaxException jse) {
					jse.printStackTrace();
				}
				
				return Collections.emptyList();
			});
			
			return listDataSources.recover((Throwable throwable) -> {
				return Collections.emptyList();
			});
	}
	
	private List<PublisherTask> getPublisherTasks() {
		final String databaseUser = config.getString("dashboard.publisher.db.user");
		final String databasePassword = config.getString("dashboard.publisher.db.password");
		
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		List<PublisherTask> publisherTasks = new ArrayList<PublisherTask>();
		
		try {
			Class.forName("org.postgresql.Driver");
			
			conn = 
					DriverManager
						.getConnection("jdbc:postgresql://pub.db/publisher", databaseUser, databasePassword);
			
			String sql = 
					"select js.job_id, js.state, js.create_time, j.type from publisher.job_state js "
						+ "join publisher.job j on j.id = js.job_id "
						+ "left join publisher.service_job sj on sj.job_id = js.job_id "
						+ "where js.state != ? "
						+ "and (j.type = ? or j.type = ? or j.type = ?) "
						+ "and (sj.type = ? or sj.type is null) "
						+ "order by js.create_time desc "
						+ "limit 10;";
			
			stmt = conn.prepareStatement(sql);
{'fraction_non_alphanumeric': 0.12001943634596696, 'fraction_numerical': 0.0006478781988986071, 'mean_word_length': 3.991915925626516, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3866814', 'n_tokens_mistral': 2200, 'n_tokens_neox': 1847, 'n_words': 411}