<commit_before>import { Injectable } from '@angular/core';
import { Model, Service } from './model';
import * as d from './diffusion.d.ts';
import { RequestContext } from './request-context';
const diffusion: d.Diffusion = require('diffusion');

@Injectable()
export class ModelService {
    private session: any;
    private context: RequestContext;
    model: Model = {
        services: []
    };

    private init(): Promise<any> {
        if (this.session) {
            return Promise.resolve(this.session);
        }

        console.log('Initialising');
        return new Promise((resolve, reject) => {
            diffusion.connect({
                host: 'localhost',
                port: 8080,
                secure: false
            }).then((session) => {
                console.log('Connected');
                this.session = session;
                this.context = new RequestContext(session, 'adapter/rest/model/store');
                resolve(this.session);
            }, (error) => {
                console.log(error);
                reject(error);
            });
        });
    }

    getModel(): Promise<Model> {
        return this.init()
            .then((session) => this.context.request({
                type: 'list-services'
            }))
            .then((response) => {
                this.model.services = response
                return this.model;
            });
    }

    getService(name: string): Promise<Service> {
        return this.init()
            .then(() => this.getModel())
            .then((model) => Promise.resolve(model.services.find((element) => element.name === name)));
    }

    createService(service: Service): Promise<void> {
        return this.init()
            .then((session) => this.context.request({
                type: 'create-service',
                service: service
            }))
            .then(() => {
                this.model.services.push(JSON.parse(JSON.stringify(service)));
            });
    }
}
<commit_msg>Ensure only one Diffusion connection is established.<commit_after>import { Injectable } from '@angular/core';
import { Model, Service } from './model';
import * as d from './diffusion.d.ts';
import { RequestContext } from './request-context';
const diffusion: d.Diffusion = require('diffusion');

@Injectable()
export class ModelService {
    private session: Promise<any>;
    private context: RequestContext;
    model: Model = {
        services: []
    };

    private init(): Promise<any> {
        if (!this.session) {
            this.session = new Promise((resolve, reject) => {
                diffusion.connect({
                    host: 'localhost',
                    port: 8080,
                    secure: false
                }).then((session) => {
                    console.log('Connected');
                    this.context = new RequestContext(session, 'adapter/rest/model/store');
                    resolve(session);
                }, (error) => {
                    console.log(error);
                    reject(error);
                });
            });
        }

        return this.session;
    }

    getModel(): Promise<Model> {
        return this.init()
            .then(() => this.context.request({
                type: 'list-services'
            }))
            .then((response) => {
                this.model.services = response
                return this.model;
            });
    }

    getService(name: string): Promise<Service> {
        return this.init()
            .then(() => this.getModel())
            .then((model) => Promise.resolve(model.services.find((element) => element.name === name)));
    }

    createService(service: Service): Promise<void> {
        return this.init()
            .then(() => this.context.request({
                type: 'create-service',
                service: service
            }))
            .then(() => {
                this.model.services.push(JSON.parse(JSON.stringify(service)));
            });
    }
}

{'fraction_non_alphanumeric': 0.1426792828685259, 'fraction_numerical': 0.00199203187250996, 'mean_word_length': 1.61864406779661, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 32, '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': '2403594', 'n_tokens_mistral': 1044, 'n_tokens_neox': 995, 'n_words': 209}