<commit_before>package name.abuchen.portfolio.online.portfolioreport;

import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import name.abuchen.portfolio.PortfolioLog;
import name.abuchen.portfolio.model.Account;
import name.abuchen.portfolio.model.AccountTransaction;
import name.abuchen.portfolio.model.AccountTransferEntry;
import name.abuchen.portfolio.model.BuySellEntry;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.Portfolio;
import name.abuchen.portfolio.model.PortfolioTransaction;
import name.abuchen.portfolio.model.PortfolioTransferEntry;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.model.Transaction;

@SuppressWarnings("nls")
public class PortfolioReportSync
{
    private static final String PORTFOLIO_ID_KEY = "net.portfolio-report.portfolioId";
    private static final String SYNCHRONIZED_AT_KEY = "net.portfolio-report.synchronizedAt";

    private final Client client;
    private final PRApiClient api;

    public PortfolioReportSync(String apiKey, Client client)
    {
        this.client = client;

        this.api = new PRApiClient(apiKey);
    }

    public void sync() throws IOException
    {
        Instant synchronizedAt;
        try
        {
            synchronizedAt = Instant.parse(client.getProperty(SYNCHRONIZED_AT_KEY));
            PortfolioLog.warning("Latest sync: " + synchronizedAt.toString());
        }
        catch (java.lang.NullPointerException e)
        {
            synchronizedAt = null;
        }
        Instant synchronizationStarted = Instant.now();

        long portfolioId = getOrCreatePortfolio();

        PortfolioLog.warning(
                        "Syncing with portfolioId " + portfolioId + ", started: " + synchronizationStarted.toString());

        syncSecurities(portfolioId);
        syncAccounts(portfolioId);
        syncTransactions(portfolioId);

        client.setProperty(SYNCHRONIZED_AT_KEY, synchronizationStarted.toString());
    }

    private long getOrCreatePortfolio() throws IOException
    {
        PRPortfolio remote = null;

        String portfolioId = client.getProperty(PORTFOLIO_ID_KEY);
        if (portfolioId != null)
        {
            try
            {
                long id = Long.parseLong(portfolioId);

                List<PRPortfolio> portfolios = api.listPortfolios();
                remote = portfolios.stream().filter(p -> p.getId() == id).findAny().orElse(null);
            }
            catch (NumberFormatException e)
            {<commit_msg>Log end of synchronization in PortfolioReportSync<commit_after>package name.abuchen.portfolio.online.portfolioreport;

import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import name.abuchen.portfolio.PortfolioLog;
import name.abuchen.portfolio.model.Account;
import name.abuchen.portfolio.model.AccountTransaction;
import name.abuchen.portfolio.model.AccountTransferEntry;
import name.abuchen.portfolio.model.BuySellEntry;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.Portfolio;
import name.abuchen.portfolio.model.PortfolioTransaction;
import name.abuchen.portfolio.model.PortfolioTransferEntry;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.model.Transaction;

@SuppressWarnings("nls")
public class PortfolioReportSync
{
    private static final String PORTFOLIO_ID_KEY = "net.portfolio-report.portfolioId";
    private static final String SYNCHRONIZED_AT_KEY = "net.portfolio-report.synchronizedAt";

    private final Client client;
    private final PRApiClient api;

    public PortfolioReportSync(String apiKey, Client client)
    {
        this.client = client;

        this.api = new PRApiClient(apiKey);
    }

    public void sync() throws IOException
    {
        Instant synchronizedAt;
        try
        {
            synchronizedAt = Instant.parse(client.getProperty(SYNCHRONIZED_AT_KEY));
            PortfolioLog.warning("Latest sync: " + synchronizedAt.toString());
        }
        catch (java.lang.NullPointerException e)
        {
            synchronizedAt = null;
        }
        Instant synchronizationStarted = Instant.now();

        long portfolioId = getOrCreatePortfolio();

        PortfolioLog.warning(
                        "Syncing with portfolioId " + portfolioId + ", started: " + synchronizationStarted.toString());

        syncSecurities(portfolioId);
        syncAccounts(portfolioId);
        syncTransactions(portfolioId);

        client.setProperty(SYNCHRONIZED_AT_KEY, synchronizationStarted.toString());

        PortfolioLog.warning("Sync finished " + Instant.now().toString());
    }

    private long getOrCreatePortfolio() throws IOException
    {
        PRPortfolio remote = null;

        String portfolioId = client.getProperty(PORTFOLIO_ID_KEY);
        if (portfolioId != null)
        {
            try
            {
                long id = Long.parseLong(portfolioId);

                List<PRPortfolio> portfolios = api.listPortfolios();
                remote = portfolios.stream().filter(p -> p.getId() == id).findAny().orElse(null);
            }
            catch (NumberFormatException e)
            {
{'fraction_non_alphanumeric': 0.09372733865119652, 'fraction_numerical': 0.0, 'mean_word_length': 3.120238984316654, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3233860', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1562, 'n_words': 284}