<commit_before>package info.nightscout.androidaps.plugins.Insulin;

import com.squareup.otto.Bus;

import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.Treatments.Treatment;
import info.nightscout.androidaps.interfaces.InsulinInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;

/**
 * Created by adrian on 13.08.2017.
 */

public abstract class InsulinOrefBasePlugin extends PluginBase implements InsulinInterface {

    public static double MIN_DIA = 5;

    long lastWarned = 0;

    public InsulinOrefBasePlugin() {
        super(new PluginDescription()
                .mainType(PluginType.INSULIN)
                .fragmentClass(InsulinFragment.class.getName())
                .pluginName(R.string.fastactinginsulin)
                .shortName(R.string.insulin_shortname)
                .visibleByDefault(false)
        );
    }

    public Bus getBus() {
        return MainApp.bus();
    }

    @Override
    public double getDia() {
        double dia = getUserDefinedDia();
        if (dia >= MIN_DIA) {
            return dia;
        } else {
            sendShortDiaNotification(dia);
            return MIN_DIA;
        }
    }

    void sendShortDiaNotification(double dia) {
        if ((System.currentTimeMillis() - lastWarned) > 60 * 1000) {
            lastWarned = System.currentTimeMillis();
            Notification notification = new Notification(Notification.SHORT_DIA, String.format(this.getNotificationPattern(), dia, MIN_DIA), Notification.URGENT);
            this.getBus().post(new EventNewNotification(notification));
        }
    }

    public String getNotificationPattern() {
        return MainApp.gs(R.string.dia_too_short);
    }

    public double getUserDefinedDia() {
        return ProfileFunctions.getInstance().getProfile() != null ? ProfileFunctions.getInstance().getProfile().getDia() : MIN_DIA;
    }

    public Iob iobCalcForTreatment(Treatment treatment, long time) {
        return this.iobCalcForTreatment(treatment, time, 0d);
    }

    @Override
    public Iob iobCalcForTreatment(Treatment treatment, long time, double dia) {
        Iob result = new Iob();

        int peak = getPeak();

        if (treatment.insulin != 0d) {

            long bolusTime = treatment.date;
            double t = (time - bolusTime) / 1000d / 60d;
<commit_msg>Query for profile only once<commit_after>package info.nightscout.androidaps.plugins.Insulin;

import com.squareup.otto.Bus;

import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.Treatments.Treatment;
import info.nightscout.androidaps.interfaces.InsulinInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;

/**
 * Created by adrian on 13.08.2017.
 */

public abstract class InsulinOrefBasePlugin extends PluginBase implements InsulinInterface {

    public static double MIN_DIA = 5;

    long lastWarned = 0;

    public InsulinOrefBasePlugin() {
        super(new PluginDescription()
                .mainType(PluginType.INSULIN)
                .fragmentClass(InsulinFragment.class.getName())
                .pluginName(R.string.fastactinginsulin)
                .shortName(R.string.insulin_shortname)
                .visibleByDefault(false)
        );
    }

    public Bus getBus() {
        return MainApp.bus();
    }

    @Override
    public double getDia() {
        double dia = getUserDefinedDia();
        if (dia >= MIN_DIA) {
            return dia;
        } else {
            sendShortDiaNotification(dia);
            return MIN_DIA;
        }
    }

    void sendShortDiaNotification(double dia) {
        if ((System.currentTimeMillis() - lastWarned) > 60 * 1000) {
            lastWarned = System.currentTimeMillis();
            Notification notification = new Notification(Notification.SHORT_DIA, String.format(this.getNotificationPattern(), dia, MIN_DIA), Notification.URGENT);
            this.getBus().post(new EventNewNotification(notification));
        }
    }

    public String getNotificationPattern() {
        return MainApp.gs(R.string.dia_too_short);
    }

    public double getUserDefinedDia() {
        Profile profile = ProfileFunctions.getInstance().getProfile();
        return profile != null ? profile.getDia() : MIN_DIA;
    }

    public Iob iobCalcForTreatment(Treatment treatment, long time) {
        return this.iobCalcForTreatment(treatment, time, 0d);
    }

    @Override
    public Iob iobCalcForTreatment(Treatment treatment, long time, double dia) {
        Iob result = new Iob();

        int peak = getPeak();

        if (treatment.insulin != 0d) {

            long bolusTime = treatment.date;
            double t = (time - bolusTime) / 1000d / 60d;

{'fraction_non_alphanumeric': 0.09575413039616273, 'fraction_numerical': 0.008527269497246403, 'mean_word_length': 3.064981949458484, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4559659', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1581, 'n_words': 305}