<commit_before>// Copyright 2016 <NAME> and The JoeFriday authors.
// Licensed 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 usage calculates disk usage.  Usage is calculated by taking the
// difference in two /proc/diskstats snapshots and reflect the difference
// between the two snapshots.
package usage

import (
	"fmt"
	"io"
	"sync"
	"time"

	"github.com/SermoDigital/helpers"
	joe "github.com/mohae/joefriday"
	"github.com/mohae/joefriday/disk/stats"
	"github.com/mohae/joefriday/disk/structs"
)

// Profiler is used to process the disk usage..
type Profiler struct {
	*stats.Profiler
	prior *structs.Stats
}

// Returns an initialized Profiler; ready to use.  The prior stats is set to
// the current stats snapshot.
func New() (prof *Profiler, err error) {
	p, err := stats.New()
	if err != nil {
		return nil, err
	}
	s, err := p.Get()
	if err != nil {
		return nil, err
	}
	return &Profiler{Profiler: p, prior: s}, nil
}

// Get returns the current disk usage.  Usage is calculated as the difference
// between the prior stats snapshot and the current one.
func (prof *Profiler) Get() (u *structs.Usage, err error) {
	time.Sleep(time.Second)
	st, err := prof.Profiler.Get()
	if err != nil {
		return nil, err
	}
	u = prof.CalculateUsage(st)
	prof.prior = st
	return u, nil
}

var std *Profiler
var stdMu sync.Mutex

// Get returns the current disk usage using the package's global Profiler..
func Get() (u *structs.Usage, err error) {
	stdMu.Lock()
	defer stdMu.Unlock()
	if std == nil {
		std, err = New()
		if err != nil {
			return nil, err
		}
	}
	return std.Get()
}

// Ticker calculates disk usage on a ticker.  The generated data is sent to
// the out channel.  Any errors encountered are sent to the errs channel.<commit_msg>remove obsolete sleep from get usage; update global get comment<commit_after>// Copyright 2016 <NAME> and The JoeFriday authors.
// Licensed 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 usage calculates disk usage.  Usage is calculated by taking the
// difference in two /proc/diskstats snapshots and reflect the difference
// between the two snapshots.
package usage

import (
	"fmt"
	"io"
	"sync"
	"time"

	"github.com/SermoDigital/helpers"
	joe "github.com/mohae/joefriday"
	"github.com/mohae/joefriday/disk/stats"
	"github.com/mohae/joefriday/disk/structs"
)

// Profiler is used to process the disk usage..
type Profiler struct {
	*stats.Profiler
	prior *structs.Stats
}

// Returns an initialized Profiler; ready to use.  The prior stats is set to
// the current stats snapshot.
func New() (prof *Profiler, err error) {
	p, err := stats.New()
	if err != nil {
		return nil, err
	}
	s, err := p.Get()
	if err != nil {
		return nil, err
	}
	return &Profiler{Profiler: p, prior: s}, nil
}

// Get returns the current disk usage.  Usage is calculated as the difference
// between the prior stats snapshot and the current one.
func (prof *Profiler) Get() (u *structs.Usage, err error) {
	st, err := prof.Profiler.Get()
	if err != nil {
		return nil, err
	}
	u = prof.CalculateUsage(st)
	prof.prior = st
	return u, nil
}

var std *Profiler
var stdMu sync.Mutex

// Get returns the current disk usage using the package's global Profiler.
// The profiler is lazily instantiated.  This means that probability of
// the first utilization snapshot returning inaccurate information is high
// due to the lack of time elapsing between the initial and current
// snapshot for utilization calculation.
func Get() (u *structs.Usage, err error) {
	stdMu.Lock()
	defer stdMu.Unlock()
	if std == nil {
		std, err = New()
		if err != nil {
			return nil, err
		}
	}
	return std.Get()
}

// Ticker calculates disk usage on a ticker.  The generated data is sent to
// the out channel.  Any errors encountered are sent to the errs channel.
{'fraction_non_alphanumeric': 0.1009212730318258, 'fraction_numerical': 0.0033500837520938024, 'mean_word_length': 3.443720930232558, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2714247', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1468, 'n_words': 630}