<commit_before>/* status.dat handling */

package nagios

import (
	"bufio"
	"io"
	"regexp"
)

var blockStartRegex = regexp.MustCompile(`^(contactstatus|hoststatus|info|programstatus|servicecomment|servicestatus)\s\{`)
var blockEndRegex = regexp.MustCompile(`^\s}$`)
var kvRegex = regexp.MustCompile(`^\s*(\S+?)=(.*)$`)

type Status struct {
	Host    map[string]Host `json:"host,omitempty"`
	Service map[string]map[string]Service `json:"service,omitempty"`
}

func LoadStatus(r io.Reader) Status {
	scanner := bufio.NewScanner(r)
	var status Status
	status.Host = make(map[string]Host)
	status.Service = make(map[string]map[string]Service)
	block_type := ""
	block_content := make(map[string]string)
	for scanner.Scan() {
		t := scanner.Text()
		match := blockStartRegex.FindStringSubmatch(t)
		if len(match) > 1 {
			block_type = match[1]
			continue
		}
		match = kvRegex.FindStringSubmatch(t)
		if len(match) > 1 {
			block_content[match[1]] = match[2]
			continue
		}
		if blockEndRegex.MatchString(t) {
			if block_type == "servicestatus" {
				s, err := NewServiceFromMap(block_content)
				if err == nil {
					if status.Service[s.Hostname] == nil {
						status.Service[s.Hostname] = make(map[string]Service)
					}
					status.Service[s.Hostname][s.Description] = s

				}

			} else if block_type == "hoststatus" {
				h, err := NewHostFromMap(block_content)
				if err == nil {
					status.Host[h.Hostname] = h
				}

			}
			//end of block summary ,cleanup vars
			block_type = ""
			block_content = make(map[string]string)
			continue
		}
	}
	_ = block_type
	return status
}<commit_msg>add locking<commit_after>/* status.dat handling */

package nagios

import (
	"bufio"
	"io"
	"regexp"
	"sync"
)

var blockStartRegex = regexp.MustCompile(`^(contactstatus|hoststatus|info|programstatus|servicecomment|servicestatus)\s\{`)
var blockEndRegex = regexp.MustCompile(`^\s}$`)
var kvRegex = regexp.MustCompile(`^\s*(\S+?)=(.*)$`)

type Status struct {
	Host    map[string]Host               `json:"host,omitempty"`
	Service map[string]map[string]Service `json:"service,omitempty"`
	sync.RWMutex
}

func LoadStatus(r io.Reader) Status {
	scanner := bufio.NewScanner(r)
	var status Status
	status.Host = make(map[string]Host)
	status.Service = make(map[string]map[string]Service)
	block_type := ""
	block_content := make(map[string]string)
	for scanner.Scan() {
		t := scanner.Text()
		match := blockStartRegex.FindStringSubmatch(t)
		if len(match) > 1 {
			block_type = match[1]
			continue
		}
		match = kvRegex.FindStringSubmatch(t)
		if len(match) > 1 {
			block_content[match[1]] = match[2]
			continue
		}
		if blockEndRegex.MatchString(t) {
			if block_type == "servicestatus" {
				s, err := NewServiceFromMap(block_content)
				if err == nil {
					if status.Service[s.Hostname] == nil {
						status.Service[s.Hostname] = make(map[string]Service)
					}
					status.Service[s.Hostname][s.Description] = s

				}

			} else if block_type == "hoststatus" {
				h, err := NewHostFromMap(block_content)
				if err == nil {
					status.Host[h.Hostname] = h
				}

			}
			//end of block summary ,cleanup vars
			block_type = ""
			block_content = make(map[string]string)
			continue
		}
	}
	_ = block_type
	return status
}

func (s *Status)UpdateStatus(r io.Reader) {
	status := LoadStatus(r);
	s.Lock()
	s.Host = status.Host
	s.Service = status.Service
	s.Unlock()
}
{'fraction_non_alphanumeric': 0.14628368374296713, 'fraction_numerical': 0.002961208172934557, 'mean_word_length': 3.8885672937771347, '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': '3970831', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1196, 'n_words': 227}