<commit_before>package info.nightscout.androidaps.db;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DecimalFormatter;

@DatabaseTable(tableName = DatabaseHelper.DATABASE_TEMPBASALS)
public class TempBasal {
    private static Logger log = LoggerFactory.getLogger(TempBasal.class);

    public long getTimeIndex() {
        return timeStart.getTime();
    }

    public void setTimeIndex(long timeIndex) {
        this.timeIndex = timeIndex;
    }

    @DatabaseField(id = true, useGetSet = true)
    public long timeIndex;

    @DatabaseField
    public Date timeStart;

    @DatabaseField
    public Date timeEnd;

    @DatabaseField
    public int percent;     // In % of current basal.  100% == current basal

    @DatabaseField
    public Double absolute;    // Absolute value in U

    @DatabaseField
    public int duration;    // in minutes

    @DatabaseField
    public boolean isExtended = false; // true if set as extended bolus

    @DatabaseField
    public boolean isAbsolute = false; // true if if set as absolute value in U


    public IobTotal iobCalc(Date time) {
        IobTotal result = new IobTotal();
        NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();

        if (profile == null)
            return result;

        int realDuration = getRealDuration();

        if (realDuration > 0) {
            Double netBasalRate = 0d;
            Double basalRate = profile.getBasal(profile.secondsFromMidnight(time));
            Double tempBolusSize = 0.05;

            if (isExtended) {
                netBasalRate = this.absolute;
            } else {
                if (this.isAbsolute) {
                    netBasalRate = this.absolute - basalRate;
                } else {
                    netBasalRate = (this.percent - 100) / 100d * basalRate;
                }
            }

            result.netRatio = netBasalRate;
            Double netBasalAmount = Math.round(netBasalRate * realDuration * 10 / 6) / 100d;
            result.netInsulin = netBasalAmount;
            if (netBasalAmount < 0.1) {
                tempBolusSize = 0.01;
            }
            if (netBasalRate < 0) {
                tempBolusSize = -tempBolusSize;
            }
            Long tempBolusCount = Math.round(netBasalAmount / tempBolusSize);
            if (tempBolusCount > 0) {
                Long tempBolusSpacing = realDuration / tempBolusCount;
                for (Long j = 0l; j < tempBolusCount; j++) {
                    Treatment tempBolusPart = new Treatment();
                    tempBolusPart.insulin = tempBolusSize;
                    Long date = this.timeStart.getTime() + j * tempBolusSpacing * 60 * 1000;
                    tempBolusPart.created_at = new Date(date);

                    Iob aIOB = tempBolusPart.iobCalc(time, profile.getDia());
                    result.basaliob += aIOB.iobContrib;
                    Double dia_ago = time.getTime() - profile.getDia() * 60 * 60 * 1000;
                    if (date > dia_ago && date <= time.getTime()) {
                        result.netbasalinsulin += tempBolusPart.insulin;
                        if (tempBolusPart.insulin > 0) {
                            result.hightempinsulin += tempBolusPart.insulin;
                        }
                    }
                }
            }
        }
        return result;
    }

    // Determine end of basal
    public Date getTimeEnd() {<commit_msg>Calculate activityContrib for TempBasal<commit_after>package info.nightscout.androidaps.db;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.plugins.OpenAPSMA.IobTotal;
import info.nightscout.client.data.NSProfile;
import info.nightscout.utils.DecimalFormatter;

@DatabaseTable(tableName = DatabaseHelper.DATABASE_TEMPBASALS)
public class TempBasal {
    private static Logger log = LoggerFactory.getLogger(TempBasal.class);

    public long getTimeIndex() {
        return timeStart.getTime();
    }

    public void setTimeIndex(long timeIndex) {
        this.timeIndex = timeIndex;
    }

    @DatabaseField(id = true, useGetSet = true)
    public long timeIndex;

    @DatabaseField
    public Date timeStart;

    @DatabaseField
    public Date timeEnd;

    @DatabaseField
    public int percent;     // In % of current basal.  100% == current basal

    @DatabaseField
    public Double absolute;    // Absolute value in U

    @DatabaseField
    public int duration;    // in minutes

    @DatabaseField
    public boolean isExtended = false; // true if set as extended bolus

    @DatabaseField
    public boolean isAbsolute = false; // true if if set as absolute value in U


    public IobTotal iobCalc(Date time) {
        IobTotal result = new IobTotal();
        NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();

        if (profile == null)
            return result;

        int realDuration = getRealDuration();

        if (realDuration > 0) {
            Double netBasalRate = 0d;
            Double basalRate = profile.getBasal(profile.secondsFromMidnight(time));
            Double tempBolusSize = 0.05;

            if (isExtended) {
                netBasalRate = this.absolute;
            } else {
                if (this.isAbsolute) {
                    netBasalRate = this.absolute - basalRate;
                } else {
                    netBasalRate = (this.percent - 100) / 100d * basalRate;
                }
            }

            result.netRatio = netBasalRate;
            Double netBasalAmount = Math.round(netBasalRate * realDuration * 10 / 6) / 100d;
            result.netInsulin = netBasalAmount;
            if (netBasalAmount < 0.1) {
                tempBolusSize = 0.01;
            }
            if (netBasalRate < 0) {
                tempBolusSize = -tempBolusSize;
            }
            Long tempBolusCount = Math.round(netBasalAmount / tempBolusSize);
            if (tempBolusCount > 0) {
                Long tempBolusSpacing = realDuration / tempBolusCount;
                for (Long j = 0l; j < tempBolusCount; j++) {
                    Treatment tempBolusPart = new Treatment();
                    tempBolusPart.insulin = tempBolusSize;
                    Long date = this.timeStart.getTime() + j * tempBolusSpacing * 60 * 1000;
                    tempBolusPart.created_at = new Date(date);

                    Iob aIOB = tempBolusPart.iobCalc(time, profile.getDia());
                    result.basaliob += aIOB.iobContrib;
                    result.activity += aIOB.activityContrib;
                    Double dia_ago = time.getTime() - profile.getDia() * 60 * 60 * 1000;
                    if (date > dia_ago && date <= time.getTime()) {
                        result.netbasalinsulin += tempBolusPart.insulin;
                        if (tempBolusPart.insulin > 0) {
                            result.hightempinsulin += tempBolusPart.insulin;
                        }
                    }
                }
            }
        }
        return result;
    }

    // Determine end of basal
    public Date getTimeEnd() {
{'fraction_non_alphanumeric': 0.08608082103912765, 'fraction_numerical': 0.013085311096856959, 'mean_word_length': 1.9209441738478832, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3870938', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2161, 'n_words': 501}