<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.apache.ivyde.eclipse.resolve;

import org.apache.ivy.core.event.IvyEvent;
import org.apache.ivy.core.event.IvyListener;
import org.apache.ivy.core.event.download.EndArtifactDownloadEvent;
import org.apache.ivy.core.event.download.PrepareDownloadEvent;
import org.apache.ivy.core.event.download.StartArtifactDownloadEvent;
import org.apache.ivy.core.event.resolve.EndResolveDependencyEvent;
import org.apache.ivy.core.event.resolve.EndResolveEvent;
import org.apache.ivy.core.event.resolve.StartResolveDependencyEvent;
import org.apache.ivy.core.module.descriptor.Artifact;
import org.apache.ivy.plugins.repository.TransferEvent;
import org.apache.ivy.plugins.repository.TransferListener;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;

public class IvyResolveJobListener implements TransferListener, IvyListener {

    private static final int DOWNLOAD_MONITOR_LENGTH = 100;

    private static final int KILO_BITS_UNIT = 1024;

    private static final int WORK_PER_ARTIFACT = 100;

    private static final int RESOLVE_PERCENT = 2;

    private long totalLength = 1;

    private int workPerArtifact = WORK_PER_ARTIFACT;

    private long currentLength = 0;

    private final IProgressMonitor monitor;

    private IProgressMonitor dlmonitor;

    private final int downloadStep;

    private int resolveStep;

    public IvyResolveJobListener(IProgressMonitor monitor, int step) {
        this.monitor = monitor;
        this.resolveStep = step / RESOLVE_PERCENT;
        this.downloadStep = step;
    }

    public void transferProgress(TransferEvent evt) {
        switch (evt.getEventType()) {
            case TransferEvent.TRANSFER_INITIATED:
                monitor.subTask("downloading " + evt.getResource());
                break;
            case TransferEvent.TRANSFER_STARTED:
                currentLength = 0;
                if (evt.isTotalLengthSet()) {
                    totalLength = evt.getTotalLength();
                    dlmonitor
                            .beginTask("downloading " + evt.getResource(), DOWNLOAD_MONITOR_LENGTH);
                }
                break;
            case TransferEvent.TRANSFER_PROGRESS:
                if (totalLength > 1) {
                    currentLength += evt.getLength();
                    int progress = (int) (currentLength * DOWNLOAD_MONITOR_LENGTH / totalLength);
                    dlmonitor.worked(progress);
                    monitor.subTask("downloading " + evt.getResource() + ": "
                            + (currentLength / KILO_BITS_UNIT) + " / "
                            + (totalLength / KILO_BITS_UNIT) + "kB");
                }
                break;
            default:
        }
    }

    public void progress(IvyEvent event) {
        if (event instanceof TransferEvent) {
            if (dlmonitor != null) {
                transferProgress((TransferEvent) event);
            }
        } else if (event instanceof PrepareDownloadEvent) {
            PrepareDownloadEvent pde = (PrepareDownloadEvent) event;
            Artifact[] artifacts = pde.getArtifacts();
            if (artifacts.length > 0) {
                workPerArtifact = downloadStep / artifacts.length;
            }
        } else if (event instanceof StartArtifactDownloadEvent) {
            StartArtifactDownloadEvent evt = (StartArtifactDownloadEvent) event;
            monitor.subTask("downloading " + evt.getArtifact());
            if (dlmonitor != null) {
                dlmonitor.done();
            }
            dlmonitor = new SubProgressMonitor(monitor, workPerArtifact);
        } else if (event instanceof EndArtifactDownloadEvent) {
            if (dlmonitor != null) {
                dlmonitor.done();
            }
            monitor.subTask(" ");
            dlmonitor = null;
        } else if (event instanceof StartResolveDependencyEvent) {
            StartResolveDependencyEvent ev = (StartResolveDependencyEvent) event;
            monitor.subTask("resolving " + ev.getDependencyDescriptor().getDependencyRevisionId());
        } else if (event instanceof EndResolveDependencyEvent) {
            monitor.subTask(" ");
        } else if (event instanceof EndResolveEvent) {
            monitor.worked(resolveStep);
        }
    }

}
<commit_msg>Fix the progress of the download<commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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.apache.ivyde.eclipse.resolve;

import org.apache.ivy.core.event.IvyEvent;
import org.apache.ivy.core.event.IvyListener;
import org.apache.ivy.core.event.download.EndArtifactDownloadEvent;
import org.apache.ivy.core.event.download.PrepareDownloadEvent;
import org.apache.ivy.core.event.download.StartArtifactDownloadEvent;
import org.apache.ivy.core.event.resolve.EndResolveDependencyEvent;
import org.apache.ivy.core.event.resolve.EndResolveEvent;
import org.apache.ivy.core.event.resolve.StartResolveDependencyEvent;
import org.apache.ivy.core.module.descriptor.Artifact;
import org.apache.ivy.core.module.id.ModuleRevisionId;
import org.apache.ivy.plugins.repository.TransferEvent;
import org.apache.ivy.plugins.repository.TransferListener;
import org.eclipse.core.runtime.IProgressMonitor;

public class IvyResolveJobListener implements TransferListener, IvyListener {

    private static final int KILO_BITS_UNIT = 1024;

    private static final int RESOLVE_PERCENT = 2;

    private long totalLength = 1;

    private int workPerArtifact = 0;

    private long currentLength = 0;

    private int currentProgress = 0;

    private final IProgressMonitor monitor;

    private final int downloadStep;

    private int resolveStep;

    public IvyResolveJobListener(final IProgressMonitor monitor, int step) {
        this.monitor = monitor;
        this.resolveStep = step / RESOLVE_PERCENT;
        this.downloadStep = step;
    }

    public void transferProgress(TransferEvent evt) {
        switch (evt.getEventType()) {
            case TransferEvent.TRANSFER_INITIATED:
                monitor.subTask("downloading " + evt.getResource());
                break;
            case TransferEvent.TRANSFER_STARTED:
                currentLength = 0;
                currentProgress = 0;
                if (evt.isTotalLengthSet()) {
                    totalLength = evt.getTotalLength();
                    monitor.subTask("downloading " + evt.getResource() + ": 0 / "
                            + (totalLength / KILO_BITS_UNIT) + "kB");
                }
                break;
            case TransferEvent.TRANSFER_PROGRESS:
                if (totalLength > 1) {
                    currentLength += evt.getLength();
                    int progress = (int) ((currentLength * workPerArtifact) / totalLength);
                    // log an accumulated diff
                    monitor.worked(progress - currentProgress);
                    currentProgress = progress;
                    monitor.subTask("downloading " + evt.getResource() + ": "
                            + (currentLength / KILO_BITS_UNIT) + " / "
                            + (totalLength / KILO_BITS_UNIT) + "kB");
                }
                break;
            case TransferEvent.TRANSFER_COMPLETED:
                monitor.worked(workPerArtifact - currentProgress);
                break;
            default:
        }
    }

    public void progress(IvyEvent event) {
        if (event instanceof TransferEvent) {
            transferProgress((TransferEvent) event);
        } else if (event instanceof PrepareDownloadEvent) {
            PrepareDownloadEvent pde = (PrepareDownloadEvent) event;
            Artifact[] artifacts = pde.getArtifacts();
            if (artifacts.length > 0) {
                workPerArtifact = downloadStep / artifacts.length;
            }
        } else if (event instanceof StartArtifactDownloadEvent) {
            StartArtifactDownloadEvent evt = (StartArtifactDownloadEvent) event;
            monitor.subTask("downloading " + evt.getArtifact());
        } else if (event instanceof EndArtifactDownloadEvent) {
            monitor.worked(workPerArtifact - currentProgress);
            currentProgress = 0;
            monitor.subTask(" ");
        } else if (event instanceof StartResolveDependencyEvent) {
            StartResolveDependencyEvent ev = (StartResolveDependencyEvent) event;
            ModuleRevisionId mrid = ev.getDependencyDescriptor().getDependencyRevisionId();
            monitor.subTask("looking for " + mrid);
        } else if (event instanceof EndResolveDependencyEvent) {
            monitor.subTask(" ");
        } else if (event instanceof EndResolveEvent) {
            monitor.worked(resolveStep);
        }
    }

}

{'fraction_non_alphanumeric': 0.08179701144964098, 'fraction_numerical': 0.0037842033766737823, 'mean_word_length': 2.4610476830087307, 'pattern_counts': {'":': 3, '<': 3, '<?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': 41, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5568116', 'n_tokens_mistral': 2699, 'n_tokens_neox': 2624, 'n_words': 684}