<commit_before>package org.mbari.vars.services.impl.vampiresquid.v1;

import org.mbari.vars.core.util.HexUtils;
import org.mbari.vars.services.gson.ByteArrayConverter;
import org.mbari.vars.services.model.LastUpdate;
import org.mbari.vars.services.model.Media;
import org.mbari.vars.services.AuthService;
import org.mbari.vars.services.MediaService;
import org.mbari.vars.services.RetrofitWebService;

import javax.inject.Inject;
import javax.inject.Named;
import java.net.URI;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author <NAME>
 * @since 2017-05-27T12:32:00
 */
public class VamService implements MediaService, RetrofitWebService {

    private final VamWebService vamWebService;
    private final Map<String, String> defaultHeaders;

    @Inject
    public VamService(VamWebServiceFactory serviceFactory, @Named("MEDIA_AUTH") AuthService authService) {
        vamWebService = serviceFactory.create(VamWebService.class, authService);
        defaultHeaders = new HashMap<>();
        defaultHeaders.put("Accept", "application/json");
        defaultHeaders.put("Accept-Charset", "utf-8");
    }

    public CompletableFuture<Media> create(Media media) {
        var fieldMap = new HashMap<String, String>();
        // Note that UUIDs are not currently consumed by vampire-squid
        addField(fieldMap, "video_sequence_uuid", () -> media.getVideoSequenceUuid().toString());
        addField(fieldMap, "video_reference_uuid", () -> media.getVideoReferenceUuid().toString());
        addField(fieldMap, "video_uuid", () -> media.getVideoUuid().toString());
//        addField(fieldMap, "video_sequence_name", media::getVideoSequenceName);
//        addField(fieldMap, "camera_id", media::getCameraId);
//        addField(fieldMap, "uri", () -> media.getUri().toString());
//        addField(fieldMap, "start_timestamp", () -> media.getStartTimestamp().toString());
        addField(fieldMap, "duration_millis", () -> media.getDuration().toMillis() + "");
        addField(fieldMap, "container", media::getContainer);
        addField(fieldMap, "width", () -> media.getWidth().toString());
        addField(fieldMap, "height", () -> media.getHeight().toString());
        addField(fieldMap, "frameRate", () -> media.getFrameRate().toString());
        addField(fieldMap, "size_bytes", () -> media.getSizeBytes().toString());
        addField(fieldMap, "sha512", () -> HexUtils.printHexBinary(media.getSha512()));
        addField(fieldMap, "video_codec", media::getVideoCodec);
        addField(fieldMap, "audio_codec", media::getAudioCodec);
        addField(fieldMap, "video_description", media::getDescription);
//        addField(fieldMap, "video_name", media::getVideoName);
        return sendRequest(vamWebService.createMedia(media.getVideoSequenceName(), media.getCameraId(),
                media.getVideoName(), media.getUri(), media.getStartTimestamp(), fieldMap,
                defaultHeaders));
    }

    @Override
    public CompletableFuture<Media> create(String videoSequenceName,
            String cameraId,
            String videoName,
            URI uri,
            Instant startTimestamp) {
        return sendRequest(vamWebService.createMedia(videoSequenceName,
                cameraId,
                videoName,
                uri,
                startTimestamp,
                new HashMap<>(),
                defaultHeaders));
    }

    public CompletableFuture<Media> update(UUID videoUuid,
            Instant startTimestamp,
            Duration duration) {

        Map<String, String> fieldMap = new HashMap<>();
        addField(fieldMap, "duration_millis", duration.toMillis());
        addField(fieldMap, "start", startTimestamp);
        return sendRequest(vamWebService.update(videoUuid,
                fieldMap, defaultHeaders));
    }

    @Override
    public CompletableFuture<Media> update(Media media) {
        Map<String, String> fieldMap = new HashMap<>();
        addField(fieldMap, "video_sequence_uuid", () -> media.getVideoSequenceUuid().toString());
        addField(fieldMap, "video_reference_uuid", () -> media.getVideoReferenceUuid().toString());
        addField(fieldMap, "video_uuid", () -> media.getVideoUuid().toString());
        addField(fieldMap, "video_sequence_name", media::getVideoSequenceName);
        addField(fieldMap, "camera_id", media::getCameraId);
        addField(fieldMap, "uri", () -> media.getUri().toString());
        addField(fieldMap, "start_timestamp", () -> media.getStartTimestamp().toString());
        addField(fieldMap, "duration_millis", () -> media.getDuration().toMillis() + "");
        addField(fieldMap, "container", media::getContainer);
        addField(fieldMap, "width", () -> media.getWidth().toString());
        addField(fieldMap, "height", () -> media.getHeight().toString());
        addField(fieldMap, "frameRate", () -> media.getFrameRate().toString());
        addField(fieldMap, "size_bytes", () -> media.getSizeBytes().toString());
        addField(fieldMap, "sha512", () -> HexUtils.printHexBinary(media.getSha512()));
        addField(fieldMap, "video_codec", media::getVideoCodec);
        addField(fieldMap, "audio_codec", media::getAudioCodec);
        addField(fieldMap, "video_description", media::getDescription);
        addField(fieldMap, "video_name", media::getVideoName);
        return sendRequest(vamWebService.updateMedia(media.getVideoUuid(), fieldMap, defaultHeaders));
    }

    @Override
    public CompletableFuture<Boolean> delete(UUID videoReferenceUuid) {
        return sendRequest(vamWebService.deleteVideoReference(videoReferenceUuid))
                .handle((v, ex) -> ex == null);
    }

    @Override
    public CompletableFuture<Media> findByUuid(UUID uuid) {
        return sendRequest(vamWebService.findByUuid(uuid));
    }

    @Override
    public CompletableFuture<Media> findBySha512(byte[] sha512) {<commit_msg>VAA-32: Bug fix, using wrong UUID for media API call<commit_after>package org.mbari.vars.services.impl.vampiresquid.v1;

import org.mbari.vars.core.util.HexUtils;
import org.mbari.vars.services.gson.ByteArrayConverter;
import org.mbari.vars.services.model.LastUpdate;
import org.mbari.vars.services.model.Media;
import org.mbari.vars.services.AuthService;
import org.mbari.vars.services.MediaService;
import org.mbari.vars.services.RetrofitWebService;

import javax.inject.Inject;
import javax.inject.Named;
import java.net.URI;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author <NAME>
 * @since 2017-05-27T12:32:00
 */
public class VamService implements MediaService, RetrofitWebService {

    private final VamWebService vamWebService;
    private final Map<String, String> defaultHeaders;

    @Inject
    public VamService(VamWebServiceFactory serviceFactory, @Named("MEDIA_AUTH") AuthService authService) {
        vamWebService = serviceFactory.create(VamWebService.class, authService);
        defaultHeaders = new HashMap<>();
        defaultHeaders.put("Accept", "application/json");
        defaultHeaders.put("Accept-Charset", "utf-8");
    }

    public CompletableFuture<Media> create(Media media) {
        var fieldMap = new HashMap<String, String>();
        // Note that UUIDs are not currently consumed by vampire-squid
        addField(fieldMap, "video_sequence_uuid", () -> media.getVideoSequenceUuid().toString());
        addField(fieldMap, "video_reference_uuid", () -> media.getVideoReferenceUuid().toString());
        addField(fieldMap, "video_uuid", () -> media.getVideoUuid().toString());
//        addField(fieldMap, "video_sequence_name", media::getVideoSequenceName);
//        addField(fieldMap, "camera_id", media::getCameraId);
//        addField(fieldMap, "uri", () -> media.getUri().toString());
//        addField(fieldMap, "start_timestamp", () -> media.getStartTimestamp().toString());
        addField(fieldMap, "duration_millis", () -> media.getDuration().toMillis() + "");
        addField(fieldMap, "container", media::getContainer);
        addField(fieldMap, "width", () -> media.getWidth().toString());
        addField(fieldMap, "height", () -> media.getHeight().toString());
        addField(fieldMap, "frameRate", () -> media.getFrameRate().toString());
        addField(fieldMap, "size_bytes", () -> media.getSizeBytes().toString());
        addField(fieldMap, "sha512", () -> HexUtils.printHexBinary(media.getSha512()));
        addField(fieldMap, "video_codec", media::getVideoCodec);
        addField(fieldMap, "audio_codec", media::getAudioCodec);
        addField(fieldMap, "video_description", media::getDescription);
//        addField(fieldMap, "video_name", media::getVideoName);
        return sendRequest(vamWebService.createMedia(media.getVideoSequenceName(), media.getCameraId(),
                media.getVideoName(), media.getUri(), media.getStartTimestamp(), fieldMap,
                defaultHeaders));
    }

    @Override
    public CompletableFuture<Media> create(String videoSequenceName,
            String cameraId,
            String videoName,
            URI uri,
            Instant startTimestamp) {
        return sendRequest(vamWebService.createMedia(videoSequenceName,
                cameraId,
                videoName,
                uri,
                startTimestamp,
                new HashMap<>(),
                defaultHeaders));
    }

    public CompletableFuture<Media> update(UUID videoUuid,
            Instant startTimestamp,
            Duration duration) {

        Map<String, String> fieldMap = new HashMap<>();
        addField(fieldMap, "duration_millis", duration.toMillis());
        addField(fieldMap, "start", startTimestamp);
        return sendRequest(vamWebService.update(videoUuid,
                fieldMap, defaultHeaders));
    }

    @Override
    public CompletableFuture<Media> update(Media media) {
        Map<String, String> fieldMap = new HashMap<>();
        addField(fieldMap, "video_sequence_uuid", () -> media.getVideoSequenceUuid().toString());
        addField(fieldMap, "video_reference_uuid", () -> media.getVideoReferenceUuid().toString());
        addField(fieldMap, "video_uuid", () -> media.getVideoUuid().toString());
        addField(fieldMap, "video_sequence_name", media::getVideoSequenceName);
        addField(fieldMap, "camera_id", media::getCameraId);
        addField(fieldMap, "uri", () -> media.getUri().toString());
        addField(fieldMap, "start_timestamp", () -> media.getStartTimestamp().toString());
        addField(fieldMap, "duration_millis", () -> media.getDuration().toMillis() + "");
        addField(fieldMap, "container", media::getContainer);
        addField(fieldMap, "width", () -> media.getWidth().toString());
        addField(fieldMap, "height", () -> media.getHeight().toString());
        addField(fieldMap, "frameRate", () -> media.getFrameRate().toString());
        addField(fieldMap, "size_bytes", () -> media.getSizeBytes().toString());
        addField(fieldMap, "sha512", () -> HexUtils.printHexBinary(media.getSha512()));
        addField(fieldMap, "video_codec", media::getVideoCodec);
        addField(fieldMap, "audio_codec", media::getAudioCodec);
        addField(fieldMap, "video_description", media::getDescription);
        addField(fieldMap, "video_name", media::getVideoName);
        return sendRequest(vamWebService.updateMedia(media.getVideoReferenceUuid(), fieldMap, defaultHeaders));
    }

    @Override
    public CompletableFuture<Boolean> delete(UUID videoReferenceUuid) {
        return sendRequest(vamWebService.deleteVideoReference(videoReferenceUuid))
                .handle((v, ex) -> ex == null);
    }

    @Override
    public CompletableFuture<Media> findByUuid(UUID uuid) {
        return sendRequest(vamWebService.findByUuid(uuid));
    }

    @Override
    public CompletableFuture<Media> findBySha512(byte[] sha512) {
{'fraction_non_alphanumeric': 0.13985148514851486, 'fraction_numerical': 0.005775577557755775, 'mean_word_length': 3.710843373493976, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 81, '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': '3756476', 'n_tokens_mistral': 3464, 'n_tokens_neox': 3400, 'n_words': 581}