<commit_before>package fi.csc.chipster.sessiondb.resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import fi.csc.chipster.auth.model.Role;
import fi.csc.chipster.rest.hibernate.HibernateUtil;
import fi.csc.chipster.rest.hibernate.Transaction;
import fi.csc.chipster.sessiondb.model.Dataset;
import fi.csc.chipster.sessiondb.model.File;
import fi.csc.chipster.sessiondb.model.Rule;
import fi.csc.chipster.sessiondb.model.Session;

@Path("users")
public class UserResource {
	
	@SuppressWarnings("unused")
	private static Logger logger = LogManager.getLogger();
	
	private HibernateUtil hibernate;
	
	public UserResource(HibernateUtil hibernate) {
		this.hibernate = hibernate;
	}
    
	@GET
	@RolesAllowed({Role.ADMIN})
    @Produces(MediaType.APPLICATION_JSON)
	@Transaction
    public Response getAll(@Context SecurityContext sc) {

		@SuppressWarnings("unchecked")
		List<String> users = hibernate.session()
				.createQuery("select distinct(username) from Rule")
				.list();		
		
		return Response.ok(users).build();
    }
	
    @GET
    @Path("{username}/quota")
    @RolesAllowed({Role.ADMIN})
    @Produces(MediaType.APPLICATION_JSON)
    @Transaction
    public Response getQuota(@PathParam("username") String username, @Context SecurityContext sc) {
    	
    	@SuppressWarnings("unchecked")
		List<Rule> rules = hibernate.session()
    			.createQuery("from Rule where username=:username")
    			.setParameter("username", username)
    			.list();    	    		
    	
    	List<Dataset> datasets = rules.stream()
	    	.map(rule -> rule.getSession())	    	
	    	.flatMap(session -> session.getDatasets().values().stream())
	    	.collect(Collectors.toList());
    	
    	Map<UUID, File> uniqueFiles = datasets.stream()
	    	.map(dataset -> dataset.getFile())
	    	.filter(file -> file != null)
	    	.collect(Collectors.toMap(file -> file.getFileId(), file -> file));
	    
	    Long size = uniqueFiles.values().stream()	
	    	.collect(Collectors.summingLong(file -> file.getSize()));       
    	<commit_msg>Remove everyone from the user list<commit_after>package fi.csc.chipster.sessiondb.resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import fi.csc.chipster.auth.model.Role;
import fi.csc.chipster.rest.hibernate.HibernateUtil;
import fi.csc.chipster.rest.hibernate.Transaction;
import fi.csc.chipster.sessiondb.model.Dataset;
import fi.csc.chipster.sessiondb.model.File;
import fi.csc.chipster.sessiondb.model.Rule;
import fi.csc.chipster.sessiondb.model.Session;

@Path("users")
public class UserResource {
	
	@SuppressWarnings("unused")
	private static Logger logger = LogManager.getLogger();
	
	private HibernateUtil hibernate;
	
	public UserResource(HibernateUtil hibernate) {
		this.hibernate = hibernate;
	}
    
	@GET
	@RolesAllowed({Role.ADMIN})
    @Produces(MediaType.APPLICATION_JSON)
	@Transaction
    public Response getAll(@Context SecurityContext sc) {

		@SuppressWarnings("unchecked")
		List<String> users = hibernate.session()
				.createQuery("select distinct(username) from Rule")
				.list();
		
		// everyone isn't a real user
		users.remove(RuleTable.EVERYONE);
		
		return Response.ok(users).build();
    }
	
    @GET
    @Path("{username}/quota")
    @RolesAllowed({Role.ADMIN})
    @Produces(MediaType.APPLICATION_JSON)
    @Transaction
    public Response getQuota(@PathParam("username") String username, @Context SecurityContext sc) {
    	
    	@SuppressWarnings("unchecked")
		List<Rule> rules = hibernate.session()
    			.createQuery("from Rule where username=:username")
    			.setParameter("username", username)
    			.list();    	    		
    	
    	List<Dataset> datasets = rules.stream()
	    	.map(rule -> rule.getSession())	    	
	    	.flatMap(session -> session.getDatasets().values().stream())
	    	.collect(Collectors.toList());
    	
    	Map<UUID, File> uniqueFiles = datasets.stream()
	    	.map(dataset -> dataset.getFile())
	    	.filter(file -> file != null)
	    	.collect(Collectors.toMap(file -> file.getFileId(), file -> file));
	    
	    Long size = uniqueFiles.values().stream()	
	    	.collect(Collectors.summingLong(file -> file.getSize()));       
    	
{'fraction_non_alphanumeric': 0.1330739299610895, 'fraction_numerical': 0.0007782101167315176, 'mean_word_length': 3.868371212121212, 'pattern_counts': {'":': 0, '<': 11, '<?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': '3633530', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1672, 'n_words': 287}