<commit_before>package jetpacker.api.service

import com.fasterxml.jackson.databind.ObjectMapper
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import jetpacker.api.configuration.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import org.springframework.web.client.AsyncRestTemplate

import java.util.concurrent.ExecutionException

/**
 * Created by donny on 30/10/16.
 */
@CompileStatic
@Slf4j
@Service
class CacheService {
    private final JetpackerProperties jetpackerProperties
    private final AsyncRestTemplate asyncRestTemplate

    @Autowired
    CacheService(JetpackerProperties jetpackerProperties, AsyncRestTemplate asyncRestTemplate) {
        this.jetpackerProperties = jetpackerProperties
        this.asyncRestTemplate = asyncRestTemplate
    }

    @Scheduled(fixedRate = 360000L)
    void updateCache() {
        try {
            getSdkManCandidates()
            // TODO: clone the jetpacker properties items and assigned to cache.
        } catch (InterruptedException | ExecutionException e) {}
    }

    List<Kit> getSdkManCandidates() {
        ResponseEntity<String> rawCandidates = asyncRestTemplate.getForEntity(Endpoint.SdkMan.url, String.class).get()

        List<Kit> kits = rawCandidates.body.split(",").collect { String candidate ->
            ResponseEntity<String> rawVersions = asyncRestTemplate.getForEntity("${Endpoint.SdkMan.url}/${candidate}", String.class).get()
            List<String> versions = Arrays.asList(rawVersions.body.split(",")).reverse()
            new Kit(name: candidate,
                    label: "SDKMAN!",
                    releases: new Releases(
                            defaultVersion: versions[0],
                            versions: versions,
                            property: new Property(
                                    name: "${candidate}_version",
                                    label: "Version"
                            )
                    )
            )
        }
    }
}<commit_msg>Add logic to initialize cache.<commit_after>package jetpacker.api.service

import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import jetpacker.api.cache.Bucket
import jetpacker.api.cache.Jetpacker
import jetpacker.api.configuration.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import org.springframework.web.client.AsyncRestTemplate

import java.util.concurrent.ExecutionException

/**
 * Created by donny on 30/10/16.
 */
@CompileStatic
@Slf4j
@Service
class CacheService {
    private final Jetpacker jetpacker = new Jetpacker()

    private final JetpackerProperties jetpackerProperties
    private final AsyncRestTemplate asyncRestTemplate

    @Autowired
    CacheService(JetpackerProperties jetpackerProperties, AsyncRestTemplate asyncRestTemplate) {
        this.jetpackerProperties = jetpackerProperties
        this.asyncRestTemplate = asyncRestTemplate

        initCache()
    }

    Jetpacker items() {
        jetpacker
    }

    private void initCache() {
        try {
            jetpacker.clear()

            Box ubuntu = jetpackerProperties.ubuntu.clone()
            Kit openjdk = jetpackerProperties.openjdk.clone()
            Kit node = jetpackerProperties.node.clone()
            Kit guard = jetpackerProperties.guard.clone()

            jetpacker.boxes << new Bucket<Box>(ubuntu.label, ubuntu)

            jetpacker.kits << new Bucket<Kit>(openjdk.label, openjdk)
            jetpacker.kits << new Bucket<Kit>(Endpoint.SdkMan.name, getSdkManCandidates())
            jetpacker.kits << new Bucket<Kit>(node.label, node)
            jetpacker.kits << new Bucket<Kit>(guard.label, guard)

            jetpackerProperties.databases.each { Container container ->
                jetpacker.databases << new Bucket<Container>(container.label, container)
            }

            jetpackerProperties.messageQueues.each { Container container ->
                jetpacker.messageQueues << new Bucket<Container>(container.label, container)
            }

            jetpackerProperties.searchEngines.each { Container container ->
                jetpacker.searchEngines << new Bucket<Container>(container.label, container)
            }
        } catch (InterruptedException | ExecutionException e) {}
    }

    @Scheduled(fixedRate = 30000L)
    private void updateCache() {
        // TODO: Update only the release versions
    }

    private List<Kit> getSdkManCandidates() {
        ResponseEntity<String> rawCandidates = asyncRestTemplate.getForEntity(Endpoint.SdkMan.url, String.class).get()

        rawCandidates.body.split(",").collect { String candidate ->
            ResponseEntity<String> rawVersions = asyncRestTemplate.getForEntity("${Endpoint.SdkMan.url}/${candidate}", String.class).get()
            List<String> versions = Arrays.asList(rawVersions.body.split(",")).reverse()
            new Kit(name: candidate,
                    label: candidate,
                    releases: new Releases(
                            defaultVersion: versions[0],
                            versions: versions,
                            property: new Property(
                                    name: "${candidate}_version",
                                    label: "Version"
                            )
                    )
            )
        }
    }
}
{'fraction_non_alphanumeric': 0.0881002824858757, 'fraction_numerical': 0.005120056497175141, 'mean_word_length': 2.4521633150517976, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4889823', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1422, 'n_words': 280}