<commit_before>/**
 * Copyright (C) 2015 Zalando SE (http://tech.zalando.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zalando.stups.fullstop.swagger.api;

import com.wordnik.swagger.annotations.*;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.web.bind.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.zalando.stups.fullstop.s3.S3Service;
import org.zalando.stups.fullstop.swagger.model.LogObj;
import org.zalando.stups.fullstop.swagger.model.Violation;
import org.zalando.stups.fullstop.teams.InfrastructureAccount;
import org.zalando.stups.fullstop.teams.TeamOperations;
import org.zalando.stups.fullstop.teams.UserTeam;
import org.zalando.stups.fullstop.violation.entity.ViolationEntity;
import org.zalando.stups.fullstop.violation.service.ViolationService;

import java.io.IOException;
import java.util.List;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.springframework.data.domain.Sort.Direction.ASC;
import static org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@RestController
@RequestMapping(value = "/api", produces = { APPLICATION_JSON_VALUE })
@Api(value = "/api", description = "the api API")
public class FullstopApi {

    private final Logger log = LoggerFactory.getLogger(FullstopApi.class);

    @Autowired
    private S3Service s3Writer;

    @Autowired
    private ViolationService violationService;

    @Autowired
    private TeamOperations teamOperations;

    private static Violation mapToDto(ViolationEntity entity) {

        if (entity == null) {
            return null;
        }

        Violation violation = new Violation();

        violation.setId(entity.getId());
        violation.setVersion(entity.getVersion());

        violation.setCreated(entity.getCreated());
        violation.setCreatedBy(entity.getCreatedBy());
        violation.setLastModified(entity.getLastModified());
        violation.setLastModifiedBy(entity.getLastModifiedBy());

        violation.setAccountId(entity.getAccountId());
        violation.setEventId(entity.getEventId());
        violation.setMessage(entity.getMessage());
        violation.setRegion(entity.getRegion());
        violation.setComment(entity.getComment());
        violation.setViolationObject(entity.getViolationObject());
        return violation;
    }

    @ApiOperation(
            value = "violations", notes = "Get one violation", response = Violation.class
    )
    @ApiResponses(value = { @ApiResponse(code = 200, message = "Violation") })
    @PreAuthorize("#oauth2.hasScope('uid')")
    @RequestMapping(value = "/violations/{id}", method = RequestMethod.GET)
    public Violation getViolation(
            @ApiParam(value = "Violation id")
            @PathVariable(value = "id")
            final Long id,
            @AuthenticationPrincipal(errorOnInvalidType = true) final String uid) throws NotFoundException {
        Violation violation = mapToDto(violationService.findOne(id));
        if (violation == null) {
            throw new NotFoundException("Violation with id: " + id + " not found!");
        }
        return violation;
    }

    @ApiOperation(
            value = "violations", notes = "Get all violations", response = Violation.class, responseContainer = "List"
    )
    @ApiResponses(value = { @ApiResponse(code = 200, message = "List of all violations") })
    @PreAuthorize("#oauth2.hasScope('uid')")
    @RequestMapping(value = "/violations", method = RequestMethod.GET)
    public Page<Violation> violations(
            @ApiParam(value = "Include only violations in these accounts")
            @RequestParam(value = "accounts", required = false)
            final List<String> accounts,
            @ApiParam(value = "Include only violations that happened after this point in time")
            @RequestParam(value = "since", required = false)
            @DateTimeFormat(iso = DATE_TIME)
            final DateTime since,
            @ApiParam(value = "Include only violations after the one with this id")
            @RequestParam(value = "last-violation", required = false)<commit_msg>#176 remove unused uid<commit_after>/**
 * Copyright (C) 2015 Zalando SE (http://tech.zalando.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zalando.stups.fullstop.swagger.api;

import com.wordnik.swagger.annotations.*;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.web.bind.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.zalando.stups.fullstop.s3.S3Service;
import org.zalando.stups.fullstop.swagger.model.LogObj;
import org.zalando.stups.fullstop.swagger.model.Violation;
import org.zalando.stups.fullstop.teams.InfrastructureAccount;
import org.zalando.stups.fullstop.teams.TeamOperations;
import org.zalando.stups.fullstop.teams.UserTeam;
import org.zalando.stups.fullstop.violation.entity.ViolationEntity;
import org.zalando.stups.fullstop.violation.service.ViolationService;

import java.io.IOException;
import java.util.List;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.springframework.data.domain.Sort.Direction.ASC;
import static org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@RestController
@RequestMapping(value = "/api", produces = { APPLICATION_JSON_VALUE })
@Api(value = "/api", description = "the api API")
public class FullstopApi {

    private final Logger log = LoggerFactory.getLogger(FullstopApi.class);

    @Autowired
    private S3Service s3Writer;

    @Autowired
    private ViolationService violationService;

    @Autowired
    private TeamOperations teamOperations;

    private static Violation mapToDto(ViolationEntity entity) {

        if (entity == null) {
            return null;
        }

        Violation violation = new Violation();

        violation.setId(entity.getId());
        violation.setVersion(entity.getVersion());

        violation.setCreated(entity.getCreated());
        violation.setCreatedBy(entity.getCreatedBy());
        violation.setLastModified(entity.getLastModified());
        violation.setLastModifiedBy(entity.getLastModifiedBy());

        violation.setAccountId(entity.getAccountId());
        violation.setEventId(entity.getEventId());
        violation.setMessage(entity.getMessage());
        violation.setRegion(entity.getRegion());
        violation.setComment(entity.getComment());
        violation.setViolationObject(entity.getViolationObject());
        return violation;
    }

    @ApiOperation(
            value = "violations", notes = "Get one violation", response = Violation.class
    )
    @ApiResponses(value = { @ApiResponse(code = 200, message = "Violation") })
    @PreAuthorize("#oauth2.hasScope('uid')")
    @RequestMapping(value = "/violations/{id}", method = RequestMethod.GET)
    public Violation getViolation(
            @ApiParam(value = "Violation id")
            @PathVariable(value = "id")
            final Long id) throws NotFoundException {
        Violation violation = mapToDto(violationService.findOne(id));
        if (violation == null) {
            throw new NotFoundException("Violation with id: " + id + " not found!");
        }
        return violation;
    }

    @ApiOperation(
            value = "violations", notes = "Get all violations", response = Violation.class, responseContainer = "List"
    )
    @ApiResponses(value = { @ApiResponse(code = 200, message = "List of all violations") })
    @PreAuthorize("#oauth2.hasScope('uid')")
    @RequestMapping(value = "/violations", method = RequestMethod.GET)
    public Page<Violation> violations(
            @ApiParam(value = "Include only violations in these accounts")
            @RequestParam(value = "accounts", required = false)
            final List<String> accounts,
            @ApiParam(value = "Include only violations that happened after this point in time")
            @RequestParam(value = "since", required = false)
            @DateTimeFormat(iso = DATE_TIME)
            final DateTime since,
            @ApiParam(value = "Include only violations after the one with this id")
            @RequestParam(value = "last-violation", required = false)
{'fraction_non_alphanumeric': 0.1054292343387471, 'fraction_numerical': 0.004361948955916474, 'mean_word_length': 3.902638762511374, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3638317', 'n_tokens_mistral': 2965, 'n_tokens_neox': 2966, 'n_words': 685}