text:
###################"

# personal properties for -D Option

#############

if(WIDE_STRING)
set(WIDE_STRING "WIDE_STRING")
ENDIF(WIDE_STRING)

if(LONG_PROBABILITY)
set(PROBABILITY "LONGDOUBLE_PROB")
ELSE(LONG_PROBABILITY)
set(PROBABILITY "DOUBLE_PROB")
ENDIF(LONG_PROBABILITY)

IF(WIN32)
#add_definitions(-DWIN32 -D_DEBUG -D_WINDOWS -D_USRDLL )
#add_definitions(-D__MINGW32__)

IF(TOULBAR2)
	set_property(
			TARGET toulbar2${EXE}
			PROPERTY COMPILE_DEFINITIONS NARYCHAR WCSPFORMATONLY INT_COST DOUBLE_PROB WIN32 _DEBUG _WINDOWS WINDOWS
		    )
ENDIF(TOULBAR2)

IF(MENDELSOFT)
	set_property(
			TARGET mendelsoft${EXE}
			PROPERTY COMPILE_DEFINITIONS NARYCHAR WCSPFORMATONLY INT_COST DOUBLE_PROB MENDELSOFT WIN32 _DEBUG _WINDOWS WINDOWS
		    )
ENDIF(MENDELSOFT)

IF(LIBTB2)
	set_property(
			TARGET tb2
			PROPERTY COMPILE_DEFINITIONS NARYCHAR WCSPFORMATONLY INT_COST DOUBLE_PROB WIN32 _DEBUG _WINDOWS WINDOWS
		    )
ENDIF(LIBTB2)

ELSE(WIN32)

IF(TOULBAR2)
	    MESSAGE(STATUS "####DEFINE OPTION ==> COST= ${COST}  XMLFLAG= ${XMLFLAG} BOOST = ${boostflag}##########################")
	set_property(
			TARGET toulbar2
			PROPERTY COMPILE_DEFINITIONS NARYCHAR WCSPFORMATONLY ${COST} ${XMLFLAG} LINUX ${boostflag} ${WIDE_STRING} ${PROBABILITY}
		    )
ENDIF(TOULBAR2)

IF(MENDELSOFT)
	set_property(
			TARGET mendelsoft
			PROPERTY COMPILE_DEFINITIONS NARYCHAR WCSPFORMATONLY ${COST} MENDELSOFT LINUX ${WIDE_STRING} ${PROBABILITY}
		    )
ENDIF(MENDELSOFT)

IF(LIBTB2)
	set_property(
			TARGET tb2
			PROPERTY COMPILE_DEFINITIONS NARYCHAR WCSPFORMATONLY ${COST} LINUX ${WIDE_STRING} ${PROBABILITY}
		    )
ENDIF(LIBTB2)

IF(LIBTB2INT)
	set_property(
			TARGET tb2int
			PROPERTY COMPILE_DEFINITIONS NARYCHAR WCSPFORMATONLY INT_COST ILOGLUE IL_STD LINUX ${WIDE_STRING} ${PROBABILITY}
		    )
ENDIF(LIBTB2INT)

if(ILOG)
	set_property(
			TARGET iloglue
			PROPERTY COMPILE_DEFINITIONS NARYCHAR WCSPFORMATONLY INT_COST ILOGLUE IL_STD LINUX ${WIDE_STRING} ${PROBABILITY}
		    )
ENDIF(ILOG)

ENDIF(WIN32)

repository: clinicalml/fw-inference

repo_name: clinicalml/fw-inference


text:
// A common pattern is for many goroutines to request the same resource at the same time.
// If that resource is expensive or long running to get, it's beneficial to deduplicate
// requests for identical resources. This library uses a fixed size worker pool that's
// managed by a broker which will only use the worker pool if the new request is unique.
package deduper

import (
	"sync"
)

// Deduper runs a fixed size worker pool that can run long-running
// requests. The Deduper will de-duplicate requests that arrive while
// a current request of the same ID is still in-flight.
// There is no attempt to recover if your WorkerFunc panics, try to avoid that
// or recover internally
type Deduper struct {
	requestCh    chan *requestWrapper
	workerCh     chan Request
	resultCh     chan *resultWrapper
	shutdownChan chan struct{}
	shutdownOnce *sync.Once
	wg           *sync.WaitGroup
	workerFunc   WorkerFunc
	cache        Cache
}

// NewDeduper creates a new Deduper with specified size worker pool and WorkerFunc
func NewDeduper(workerCount int, worker WorkerFunc) *Deduper {
	return NewDeduperWithCache(workerCount, worker, nil)
}

// NewDeduperWithCache creates a new Deduper with specified size worker pool and WorkerFunc and a Cache
func NewDeduperWithCache(workerCount int, worker WorkerFunc, cache Cache) *Deduper {
	dd := &Deduper{
		requestCh:    make(chan *requestWrapper, 50),
		workerCh:     make(chan Request, 2*workerCount),
		resultCh:     make(chan *resultWrapper, 2*workerCount),
		shutdownChan: make(chan struct{}),
		shutdownOnce: &sync.Once{},
		wg:           &sync.WaitGroup{},
		workerFunc:   worker,
		cache:        cache,
	}
	go dd.broker()

	dd.wg.Add(workerCount)
	for i := 0; i < workerCount; i++ {
		go dd.worker()
	}
	return dd
}

// Cache is an optional type that allows persistent caching of the
// results. If present, the value will be returned from the cache.
// The cache will be used from a single goroutine so it doesn't need
// to be goroutine safe
type Cache interface {
	Get(key interface{}) (interface{}, bool)
	Set(key interface{}, value interface{})
}

// Request is a deduplicatable request which includes a Payload that will be passed to the WorkerFunc
type Request interface {
	// Key should produce a unique request identifier
	// All concurrent requests for the same Key will get the same value from Get
	Key() interface{}
	// Payload is optional and not directly used by Deduper. It can be anything extra that needs
	// to be passed to the WorkerFunc in order to retrieve the resource (like a URL or http.Request).
	Payload() interface{}
}

// WorkerFunc is a function that performs the long running Request and return type T. The Payload of the Request is type U.
type WorkerFunc func(req Request) (interface{}, error)

type requestWrapper struct {
	request  Request
	returnCh chan *resultWrapper
}

type resultWrapper struct {
	request Request
	value   interface{}
	err     error
}

// Get will block until the value is available or there is an error.
func (dd *Deduper) Get(m Request) (interface{}, error) {
	returnCh := make(chan *resultWrapper, 1)
	dd.requestCh <- &requestWrapper{m, returnCh}
	rw := <-returnCh
	return rw.value, rw.err
}

// Shutdown kills the workers and the broker. Any in-flight requests will be completed.
// Calling Get after Shutdown will panic (with a write to a closed channel)
func (dd *Deduper) Shutdown() {
	dd.shutdownOnce.Do(func() { close(dd.shutdownChan) })
}

func (dd *Deduper) broker() {
	savedRequest := make(map[interface{}][]*requestWrapper)
	shutdown := false
	for !shutdown {
		select {
		case request := <-dd.requestCh:
			dd.queueRequestChan(request, savedRequest)
		case rw := <-dd.resultCh:
			dd.processResult(rw, savedRequest)
		case <-dd.shutdownChan:
			shutdown = true
		}
	}

	dd.cleanup(savedRequest)
}

func (dd *Deduper) cleanup(savedRequest map[interface{}][]*requestWrapper) {
	// Stop accepting new requests and drain any remaining in the channel
	close(dd.requestCh)
	for request := range dd.requestCh {
		dd.queueRequestChan(request, savedRequest)
	}
	// Close the workerCh and process the results
	close(dd.workerCh)
	wgDoneCh := make(chan struct{})
	go dd.workersDone(wgDoneCh)
	wgDone := false
	for !wgDone {
		select {
		case rw := <-dd.resultCh:
			dd.processResult(rw, savedRequest)
		case <-wgDoneCh:
			wgDone = true
		}
	}
	// not done yet... even though the workers are finished, there still could be unprecessed results left in resultCh
	close(dd.resultCh)
	for rw := range dd.resultCh {
		dd.processResult(rw, savedRequest)
	}
}

func (dd *Deduper) workersDone(wgDoneCh chan struct{}) {
	dd.wg.Wait()
	close(wgDoneCh)
}

func (dd *Deduper) queueRequestChan(request *requestWrapper, savedRequest map[interface{}][]*requestWrapper) {
	// return fast if found in the cache
	if dd.cache != nil {
		val, cached := dd.cache.Get(request.request.Key())
		if cached {
			request.returnCh <- &resultWrapper{request.request, val, nil}
			return
		}
	}

	// if the request is already in-flight, just save for later
	requests, found := savedRequest[request.request.Key()]
	savedRequest[request.request.Key()] = append(requests, request)
	if !found {
		// if the request is not in-flight then send to the worker to get it
		// this loops ensures we don't block on the workerCh send if the
		// channel is full. If we drain the resultCh, then the workerCh
		// will eventually free up
		sent := false
		for !sent {
			select {
			case dd.workerCh <- request.request:
				sent = true
			case rw := <-dd.resultCh:
				dd.processResult(rw, savedRequest)
			}
		}
	}
}

func (dd *Deduper) processResult(rw *resultWrapper, savedRequest map[interface{}][]*requestWrapper) {
	if rw.err == nil && dd.cache != nil {
		dd.cache.Set(rw.request.Key(), rw.value)
	}
	requests := savedRequest[rw.request.Key()]
	for _, r := range requests {
		r.returnCh <- rw
	}

	delete(savedRequest, rw.request.Key())
}

func (dd *Deduper) worker() {
	defer dd.wg.Done()
	for m := range dd.workerCh {
		// do the work
		value, err := dd.workerFunc(m)
		dd.resultCh <- &resultWrapper{m, value, err}
	}
}

license: MIT

repository: tonalfitness/deduper

license_type: permissive

repo_name: tonalfitness/deduper

branch_name: refs/heads/main

extension: go


text:
@Article{bossinicastillo2021genomic,
  title = {Genomic Risk Score impact on susceptibility to systemic sclerosis},
  author = {Lara Bossini-Castillo and Gonzalo Villanueva-Martin and Martin Kerick and Marialbert Acosta-Herrera and Elena Lopez-Isac and Carmen P Simeon and Norberto Ortego-Centeno and Shervin Assassi and Nicolas Hunzelmann and Armando Gabrielli and J K {de Vries-Bouwstra} and Yannick Allanore and Carmen Fonseca and Christopher P Denton and Timothy Rdj Radstake and Marta Eugenia Alarcon-Riquelme and Lorenzo Beretta and Maureen D Mayes and Javier Martin},
  year = {2021},
  month = {01},
  journal = {Annals of the rheumatic diseases},
  volume = {80},
  number = {1},
  pages = {118-127},
  eprint = {33004331},
  doi = {10.1136/annrheumdis-2020-218558},
  archiveprefix = {pubmed},
}

@Article{simon2021cytokine,
  title = {A cytokine network profile delineates a common Th1/Be1 pro-inflammatory group of patients in four systemic autoimmune diseases},
  author = {Quentin Simon and Alexis Grasseau and Marina Boudigou and Laetitia {Le Pottier} and Eleonore Bettachioli and Divi Cornec and Benedicte Rouviere and Christophe Jamin and Lucas {Le Lann} and Maria Orietta Borghi and Rocio Aguilar-Quesada and Yves Renaudineau and Marta E Alarcon-Riquelme and Jacques-Olivier Pers and Sophie Hillion},
  year = {2021},
  month = {Feb},
  journal = {Arthritis & rheumatology},
  eprint = {33605069},
  doi = {10.1002/art.41697},
  archiveprefix = {pubmed},
}

@Article{lelann2020standardization,
  title = {Standardization procedure for flow cytometry data harmonization in prospective multicenter studies},
  author = {Lucas {Le Lann} and Pierre-Emmanuel Jouve and Marta Alarcon-Riquelme and Christophe Jamin and Jacques-Olivier Pers},
  year = {2020},
  month = {07},
  journal = {Scientific reports},
  volume = {10},
  number = {1},
  pages = {11567},
  eprint = {32665668},
  doi = {10.1038/s41598-020-68468-3},
  archiveprefix = {pubmed},
}

@Article{beretta2020genome,
  title = {Genome-wide whole blood transcriptome profiling in a large European cohort of systemic sclerosis patients},
  author = {Lorenzo Beretta and Guillermo Barturen and Barbara Vigone and Chiara Bellocchi and Nicolas Hunzelmann and Ellen {De Langhe} and Ricard Cervera and Maria Gerosa and Laszlo Kovacs and Rafaela Ortega Castro and Isabel Almeida and Divi Cornec and Carlo Chizzolini and Jacques-Olivier Pers and Zuzanna Makowska and Ralf Lesche and Martin Kerick and Marta Eugenia Alarcon-Riquelme and Javier Martin},
  year = {2020},
  month = {09},
  journal = {Annals of the rheumatic diseases},
  volume = {79},
  number = {9},
  pages = {1218-1226},
  eprint = {32561607},
  doi = {10.1136/annrheumdis-2020-217116},
  archiveprefix = {pubmed},
}

@Article{ruizlimon2019molecular,
  title = {Molecular Characterization of Monocyte Subsets Reveals Specific and Distinctive Molecular Signatures Associated With Cardiovascular Disease in Rheumatoid Arthritis},
  author = {Patricia Ruiz-Limon and Rafaela Ortega-Castro and Nuria Barbarroja and Carlos Perez-Sanchez and Christophe Jamin and Alejandra Maria Patino-Trives and Maria Luque-Tevar and Alejandro Ibanez-Costa and Laura Perez-Sanchez and Ivan Arias {de la Rosa} and MaCarmen Abalos-Aguilera and Yolanda Jimenez-Gomez and Jerusalem Calvo-Gutierrez and Pilar Font and Alejandro Escudero-Contreras and Marta E Alarcon-Riquelme and Eduardo Collantes-Estevez and Chary Lopez-Pedrera},
  year = {2019},
  month = {05},
  journal = {Frontiers in immunology},
  volume = {10},
  pages = {1111},
  eprint = {31169830},
  doi = {10.3389/fimmu.2019.01111},
  archiveprefix = {pubmed},
}

branch_name: refs/heads/master

extension: bib


text:
@InProceedings{Kazakos_2019_ICCV,
author = {Kazakos, Evangelos and Nagrani, Arsha and Zisserman, Andrew and Damen, Dima},
title = {EPIC-Fusion: Audio-Visual Temporal Binding for Egocentric Action Recognition},
booktitle = {The IEEE International Conference on Computer Vision (ICCV)},
month = {October},
year = {2019}
}


text:
package ucp

import (
	"bytes"
	"fmt"
	"io"
	"os"

	log "github.com/sirupsen/logrus"
)

// GetSupportDump - will download a UCP support dump
func (c *Client) GetSupportDump() error {

	// TODO add a spinner here
	log.Infoln("Downloading a UCP support dump (may take some time)")
	// Create the file
	out, err := os.Create("support-dump.zip")
	if err != nil {
		return err
	}
	defer out.Close()

	url := fmt.Sprintf("%s/api/support", c.UCPURL)

	response, err := c.postRequest(url, nil)
	if err != nil {
		return err
	}

	_, err = io.Copy(out, bytes.NewReader(response))
	if err != nil {
		return err
	}

	return nil
}

repository: Ettasnow/diver

repo_name: Ettasnow/diver


text:
DESCRIPTION = "Wifi demo descriptions for Matrix v2"
HOMEPAGE = "https://gforge.ti.com/gf/project/matrixguiv2apps/"
LICENSE = "CC-BY-SA"

inherit allarch

SRC_URI = "https://gforge.ti.com/gf/download/frsrelease/678/5101/wifidemos_1.1.tar.gz"

S = ${WORKDIR}/wifidemos

require matrix-gui-apps.inc

# Make sure wifi submenu has been installed
RDEPENDS +=  matrix-gui-submenus-wifi

FILES_${PN} += "${MATRIX_BASE_DIR}/*"

SRC_URI[md5sum] = "60d85261bcad85da7ecd024613343068"
SRC_URI[sha256sum] = "36fe8bbc461e157b00ebf8f9deab1e5c83eef2f618bc98f2956d974e76534c56"

repository: kergoth/meta-ti

repo_name: kergoth/meta-ti

extension: bb


text:
DESCRIPTION = "Minisip is a SIP User Agent"
SECTION = "opie/network"
PRIORITY = "optional"
LICENSE = "GPL"
DEPENDS = "libmikey libopie1"

SRC_URI = "http://minisip.org/source/minisip-0.1a.tar.gz"
S = "${WORKDIR}/minisip-0.1"

inherit autotools

EXTRA_OECONF = "--enable-qte --enable-opie"

repository: Terentyev/familiar-linux

repo_name: Terentyev/familiar-linux


text:
package metrics

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
)

const (
	// Namespace metric label
	Namespace = "namespace"
	// Status metric label
	Status = "status"
	// SourceID metric label
	SourceID = "source_id"
	// EventType metric label
	EventType = "event_type"
	// EventTypeVersion metric label
	EventTypeVersion = "event_type_version"
)

var (
	// TotalPublishedMessages metric
	TotalPublishedMessages = promauto.NewCounterVec(prometheus.CounterOpts{
		Name: "total_published_messages",
		Help: "The total number of published messages",
	}, []string{Namespace, Status, SourceID, EventType, EventTypeVersion})
)

repository: rasyidmujahid/kyma

repo_name: rasyidmujahid/kyma


text:
package workload

import (
	"context"
	"errors"

	"github.com/Masterlu1998/kube-viewer/debug"
	"github.com/Masterlu1998/kube-viewer/kScrapper/common"
	"github.com/Masterlu1998/kube-viewer/kube"
	"k8s.io/client-go/kubernetes"
)

const (
	ReplicaSetListScrapperTypes = "ReplicaSetListScrapper"
	ReplicaSetResourceTypes     = "ReplicaSet"
)

type ReplicaSetScrapper struct {
	*common.CommonScrapper
}

func NewReplicaSetListScrapper(lister *kube.KubeLister, client *kubernetes.Clientset, dc *debug.DebugCollector) *ReplicaSetScrapper {
	return &ReplicaSetScrapper{
		CommonScrapper: common.NewCommonScrapper(dc, client, lister),
	}
}

func (r *ReplicaSetScrapper) GetScrapperTypes() string {
	return ReplicaSetListScrapperTypes
}

func (r *ReplicaSetScrapper) StartScrapper(ctx context.Context, args common.ScrapperArgs) {
	r.CommonScrapper.ScrapeDataIntoChWithSource(ctx, r.scrapeDataIntoCh, args)
}

func (r *ReplicaSetScrapper) scrapeDataIntoCh(args common.ScrapperArgs) (common.KubernetesData, error) {
	listArgs, ok := args.(common.ListScrapperArgs)
	if !ok {
		return nil, errors.New("convert to common.ListScrapperArgs failed")
	}

	replicaSets, err := getWorkloads(r.KubernetesClient, r.KubernetesLister, ReplicaSetResourceTypes, listArgs.Namespace)
	if err != nil {
		return nil, err
	}

	return replicaSets, nil
}


text:
LICENSE     = "LiPS"
DESCRIPTION = "LiPS address book library."
SECTION = "gpe/libs"
PRIORITY    = "optional"
DEPENDS     = "glib-2.0 librecord2 gconf sqlite3"
PR          = "r1"

GPE_TARBALL_SUFFIX = "bz2"
inherit gpephone pkgconfig autotools

SRC_URI += "file://i18n.patch"

FILES_${PN} += "${datadir}/contact ${libdir}/lips/*.so"
FILES_${PN}-dbg += "${libdir}/lips/.debug/*.so"

do_configure_prepend () {
        mkdir "${S}/po"
        touch "${S}/po/POTFILES.in"
}

SRC_URI[md5sum] = "6bb28fc35f7589b8fae2ed73f9e5e08e"
SRC_URI[sha256sum] = "901eb2cd2b817f70ee400565f714ee95b3ebed692ba37a579b6d71a21dab1a43"

repository: overo/overo-oe-natty

repo_name: overo/overo-oe-natty

branch_name: HEAD


text:
@article{ISI:000408747900002,
 article-number = {384003},
 author = {Giavazzi, Fabio and Malinverno, Chiara and Corallino, Salvatore and
Ginelli, Francesco and Scita, Giorgio and Cerbino, Roberto},
 doi = {10.1088/1361-6463/aa7f8e},
 eissn = {1361-6463},
 issn = {0022-3727},
 journal = {JOURNAL OF PHYSICS D-APPLIED PHYSICS},
 month = {SEP 27},
 number = {38},
 orcid-numbers = {Cerbino, Roberto/0000-0003-0434-7741
Giavazzi, Fabio/0000-0003-4930-0592},
 researcherid-numbers = {Cerbino, Roberto/A-2286-2008
Giavazzi, Fabio/C-3054-2017},
 times-cited = {14},
 title = {Giant fluctuations and structural effects in a flocking epithelium},
 unique-id = {ISI:000408747900002},
 volume = {50},
 year = {2017}
}

repository: rcerbino/starter-academic

repo_name: rcerbino/starter-academic


text:
################################################################################
include(DownloadProject)

# With CMake 3.8 and above, we can hide warnings about git being in a
# detached head by passing an extra GIT_CONFIG option
if(NOT (${CMAKE_VERSION} VERSION_LESS "3.8.0"))
    set(EBW_EXTRA_OPTIONS "GIT_CONFIG advice.detachedHead=false")
else()
    set(EBW_EXTRA_OPTIONS "")
endif()

# Shortcut function
function(ebw_download_project name)
    download_project(
        PROJ         ${name}
        SOURCE_DIR   ${EBW_EXTERNAL}/${name}
        DOWNLOAD_DIR ${EBW_EXTERNAL}/.cache/${name}
        QUIET
        ${EBW_EXTRA_OPTIONS}
        ${ARGN}
    )
endfunction()

################################################################################

## Catch2 BSL 1.0 optional
function(ebw_download_catch2)
    ebw_download_project(Catch2
        GIT_REPOSITORY https://github.com/catchorg/Catch2.git
        GIT_TAG        v2.4.2
    )
endfunction()

set(EBW_EIGEN_VERSION 3.3.7 CACHE STRING "Default version of Eigen used.")
function(ebw_download_eigen)
ebw_download_project(eigen
		GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
		GIT_TAG        ${LIBIGL_EIGEN_VERSION}
		${EBW_EIGEN_VERSION}
	)
endfunction()

repository: geometryprocessing/SimpleBVH

repo_name: geometryprocessing/SimpleBVH

gha_license_id: MIT

extension: cmake


text:
DESCRIPTION = "Text processing system"
HOMEPAGE = "http://docutils.sourceforge.net"
SECTION = "devel/python"
LICENSE = "PSF"
PR = "ml0"

SRC_URI = "${SOURCEFORGE_MIRROR}/docutils/docutils-${PV}.tar.gz"
S = "${WORKDIR}/docutils-${PV}"

inherit distutils


text:
package url

import (
	"net/http"

	"github.com/adhocore/urlsh/cache"
	"github.com/adhocore/urlsh/common"
	"github.com/adhocore/urlsh/model"
	"github.com/adhocore/urlsh/orm"
	"github.com/adhocore/urlsh/request"
)

// ListURLsFilteredFromRequest gets list of urls filtered using http.Request query params
// It returns list of matching model.URL arrays and error if nothing matched.
func ListURLsFilteredFromRequest(req *http.Request) ([]model.URL, error) {
	_ = req.ParseForm()

	filter := request.URLFilter{
		ShortCode: req.Form.Get("short_code"),
		Keyword:   req.Form.Get("keyword"),
		Page:      req.Form.Get("page"),
	}

	return ListURLsFiltered(filter)
}

// ListURLsFiltered gets list of urls filtered using request.URLFilter
// It returns list of matching model.URL arrays and error if nothing matched.
func ListURLsFiltered(filter request.URLFilter) ([]model.URL, error) {
	var urls []model.URL

	limit, conn := 50, orm.Connection().Select("short_code, origin_url, hits, deleted, expires_on")
	if filter.ShortCode != "" {
		limit = 1
		conn = conn.Where("short_code = ?", filter.ShortCode)
	}

	if filter.Keyword != "" {
		conn = conn.
			Joins("LEFT JOIN url_keywords ON url_keywords.url_id = urls.id").
			Joins("LEFT JOIN keywords ON url_keywords.keyword_id = keywords.id").
			Where("keyword = ?", filter.Keyword)
	}

	if conn.Offset(filter.GetOffset(limit)).Limit(limit).Find(&urls); len(urls) == 0 {
		return urls, common.ErrNoMatchingData
	}

	return urls, nil
}

// DeleteURLFromRequest deletes url using short code from request
// It returns error on failure.
func DeleteURLFromRequest(req *http.Request) error {
	_ = req.ParseForm()

	shortCode := req.Form.Get("short_code")

	return DeleteURLByShortCode(shortCode)
}

// DeleteURLByShortCode deletes url using short code
// It returns error on failure.
func DeleteURLByShortCode(shortCode string) error {
	if shortCode == "" {
		return common.ErrShortCodeEmpty
	}

	urlModel := model.URL{ShortCode: shortCode, Deleted: true}
	result := orm.Connection().Model(urlModel).
		Where("short_code = ? AND deleted = ?", shortCode, false).
		Updates(urlModel)

	if result.RowsAffected == 0 {
		return common.ErrNoShortCode
	}

	go cache.DeactivateURL(urlModel)

	return nil
}

repository: ndiritumichael/urlsh

repo_name: ndiritumichael/urlsh


text:
/*
 * Copyright (c) 2019 uplus.io
 */

package io

import (
	"bufio"
	"os"
	"path/filepath"
	"strings"
)

type Stream struct {
	path string
	file *os.File

	reader *bufio.Reader
	writer *bufio.Writer
}

type Reader interface {
	read(length int, buffer Buffer)
}

func NewStream(path string) (*Stream) {
	stream := &Stream{path: path}
	stream.Init()
	return stream;
}

func (p *Stream) Init() {
	var err error
	p.file, err = os.OpenFile(p.path, os.O_CREATE|os.O_RDWR|os.O_SYNC, os.ModeAppend|0644)
	if err != nil {
		//log.Fatal(err)
		if os.IsNotExist(err) {
			index := strings.LastIndex(p.path, string(filepath.Separator))
			dir := string([]byte(p.path)[:index])
			os.MkdirAll(dir, os.ModePerm)
		}
	}

	reader := bufio.NewReader(p.file)
	writer := bufio.NewWriter(p.file)
	p.reader = reader
	p.writer = writer

}

func (p *Stream) Close() {
	p.writer.Flush()
	p.file.Close()
}

func (p *Stream) ReadLine(offset int64) ([]byte, error) {
	p.file.Seek(offset, 0)
	reader := p.reader
	line, _, err := reader.ReadLine()
	return line, err
}

func (p *Stream) Read(offset int64, length uint32) Buffer {
	reader := p.reader
	bytes := make([]byte, length)
	p.file.Seek(offset, 0)
	reader.Read(bytes)
	buffer := NewBigBuffer(nil)
	buffer.WriteBytes(bytes)
	reader.ReadLine()
	return *buffer
}

func (p *Stream) Write(offset int64, buffer Buffer) {
	writer := p.writer
	p.file.Seek(offset, 0)
	writer.Write(buffer.Bytes())
}

func (p *Stream) Flush() error {
	return p.writer.Flush()
}

repository: uplus-io/udb

repo_name: uplus-io/udb


text:
@Article{C2JM35601E,
author ="Zhao, Yanli and Gruner, George",
title  ="Nanonet as a scaffold with targeted functionalities",
journal  ="J. Mater. Chem.",
year  ="2012",
volume  ="22",
issue  ="48",
pages  ="24983-24991",
publisher  ="The Royal Society of Chemistry",
doi  ="10.1039/C2JM35601E",
url  ="http://dx.doi.org/10.1039/C2JM35601E",
abstract  ="A random assembly of nanoscale electrically conducting wires{,} a so called a nanonet{,} has a variety of interesting properties{,} which has been exploited for multiple applications. These nanowire networks can also be regarded as a {"}scaffold{"} onto which molecules and nanoscale objects can be attached for well defined functionalities. This review summarizes the strategies of the attachments onto the scaffold{,} the investigation of the interface between the scaffold and the functional species together with the applications in the areas of renewable energy{,} opto-electronics{,} catalysis and biotechnology. The initial work on this novel materials platform will likely followed up by development efforts so that the fundamental value propositions associated with the scaffold can be fully exploited. Initial work towards this goal is gradually emerging."}
@Article{C2JM34517J,
author ="Cai, Minzhen and Thorpe, Daniel and Adamson, Douglas H. and Schniepp, Hannes C.",
title  ="Methods of graphite exfoliation",
journal  ="J. Mater. Chem.",
year  ="2012",
volume  ="22",
issue  ="48",
pages  ="24992-25002",
publisher  ="The Royal Society of Chemistry",
doi  ="10.1039/C2JM34517J",
url  ="http://dx.doi.org/10.1039/C2JM34517J",
abstract  ="For applications of two-dimensional graphene{,} commercially viable sources are necessary. Exfoliation from bulk{,} stacked graphite is the most economical way to achieve large quantities of single layer graphene. A number of methods have been developed to achieve exfoliation of graphite{,} each with advantages and disadvantages. In this review{,} we describe current exfoliation methods and techniques used to produce single layer materials from graphite precursors."}

repository: Tinaatucsd/MPContribs

repo_name: Tinaatucsd/MPContribs


text:
require recipes-support/attr/${BPN}_2.2.52.bb
FILESEXTRAPATHS_prepend = "\
${COREBASE}/meta/recipes-support/attr/${BPN}:\
${COREBASE}/meta/recipes-support/attr/files:\
"

inherit debian-package
DEBIAN_SECTION = "utils"

DPR = "0"

LICENSE = "LGPLv2.1+ & GPLv2+"
LICENSE_${PN} = "GPLv2+"
LICENSE_lib${BPN} = "LGPLv2.1+"
LIC_FILES_CHKSUM = " \
file://doc/COPYING;md5=c781d70ed2b4d48995b790403217a249 \
file://doc/COPYING.LGPL;md5=9e9a206917f8af112da634ce3ab41764 \
"

SRC_URI += " \
file://run-ptest \
file://acl-fix-the-order-of-expected-output-of-getfacl.patch \
file://relative-libdir.patch;striplevel=0 \
file://add-missing-configure.ac.patch \
"

repository: takuya-t/meta-debian

repo_name: takuya-t/meta-debian

branch_name: refs/heads/daisy


text:
package cron_server

import (
	"strings"

	"github.com/xinliangnote/go-gin-api/internal/api/repository/db_repo/cron_task_repo"

	"github.com/spf13/cast"
)

func (s *server) AddTask(task *cron_task_repo.CronTask) {
	spec := "0 " + strings.TrimSpace(task.Spec)
	name := cast.ToString(task.Id)

	s.cron.AddFunc(spec, s.AddJob(task), name)
}

repository: pdwjun/go-gin-api

repo_name: pdwjun/go-gin-api


text:
require linux-libc-headers.inc

INHIBIT_DEFAULT_DEPS = "1"

DEPENDS += "unifdef-native"

INC_PR = "r13"
PR = "${INC_PR}.1"

DEFAULT_PREFERENCE = "-99"
DEFAULT_PREFERENCE_trihidtv = "10"

SRC_URI = "${SAMSUNG_OS_MIRROR}/LN40C550.zip"

S = "${WORKDIR}/mv_selp_kernel_1C_3B_RFS131_FSR120/kernel-2.6.18_pro500-mips24ke_nfp_be"

do_unpack2() {
        tar -xvf ${WORKDIR}/linux.tgz -C ${WORKDIR}/
        rm -f ${WORKDIR}/*.tar ${WORKDIR}/*.tgz ${WORKDIR}/*.gz
}

addtask unpack2 before do_patch after do_unpack

do_configure () {
	set_arch

	echo > .mvl_cross_compile
	echo ${ARCH} > .mvl_target_cpu
	oe_runmake allnoconfig ARCH=${ARCH}
}

do_compile(){
	:
}

STAGE_TEMP="${WORKDIR}/temp-staging"

do_stage () {
        set_arch
        echo $ARCH
        rm -rf ${STAGE_TEMP}
        mkdir -p ${STAGE_TEMP}
        oe_runmake headers_install INSTALL_HDR_PATH=${STAGE_TEMP}${exec_prefix} ARCH=$ARCH

	install ${S}/include/asm/sgi*.h ${STAGE_TEMP}${includedir}/asm/
	install ${S}/include/linux/if_addr.h ${STAGE_TEMP}${includedir}/linux/

        if [ "$ARCH" = "arm" ]; then
                cp ${WORKDIR}/procinfo.h ${STAGE_TEMP}${includedir}/asm/
        fi
        install -d ${STAGING_INCDIR}
        rm -rf ${STAGING_INCDIR}/linux ${STAGING_INCDIR}/asm ${STAGING_INCDIR}/asm-generic
        cp -pfLR ${STAGE_TEMP}${includedir}/linux ${STAGING_INCDIR}/
        cp -pfLR ${STAGE_TEMP}${includedir}/asm ${STAGING_INCDIR}/
        cp -pfLR ${STAGE_TEMP}${includedir}/asm-generic ${STAGING_INCDIR}/
}

do_install() {
        set_arch
        oe_runmake headers_install INSTALL_HDR_PATH=${D}${exec_prefix} ARCH=${ARCH}
}

repository: metalsky/SamyGO

repo_name: metalsky/SamyGO


text:
package layer5

import (
	"fmt"
	"io/ioutil"
	"testing"

	"os"
	"strings"

	adapter_integration "istio.io/istio/mixer/pkg/adapter/test"
)

func TestReport(t *testing.T) {
	adptCrBytes, err := ioutil.ReadFile("config/layer5.yaml")
	if err != nil {
		t.Fatalf("could not read file: %v", err)
	}

	operatorCfgBytes, err := ioutil.ReadFile("sample_operator_cfg.yaml")
	if err != nil {
		t.Fatalf("could not read file: %v", err)
	}
	operatorCfg := string(operatorCfgBytes)
	shutdown := make(chan error, 1)

	var outFile *os.File
	outFile, err = os.OpenFile("out.txt", os.O_RDONLY|os.O_CREATE, 0666)
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		if removeErr := os.Remove(outFile.Name()); removeErr != nil {
			t.Logf("Could not remove temporary file %s: %v", outFile.Name(), removeErr)
		}
	}()

	adapter_integration.RunTest(
		t,
		nil,
		adapter_integration.Scenario{
			Setup: func() (ctx interface{}, err error) {
				pServer, err := NewLayer5Adapter("")
				if err != nil {
					return nil, err
				}
				go func() {
					pServer.Run(shutdown)
					_ = <-shutdown
				}()
				return pServer, nil
			},
			Teardown: func(ctx interface{}) {
				s := ctx.(Server)
				s.Close()
			},
			ParallelCalls: []adapter_integration.Call{
				{
					CallKind: adapter_integration.REPORT,
					Attrs:    map[string]interface{}{"request.size": int64(555)},
				},
			},
			GetState: func(ctx interface{}) (interface{}, error) {
				// validate if the content of "out.txt" is as expected
				bytes, err := ioutil.ReadFile("out.txt")
				if err != nil {
					return nil, err
				}
				s := string(bytes)
				wantStr := `HandleMetric invoked with:
       Adapter config: &Params{FilePath:out.txt,}
       Instances: 'i1metric.instance.istio-system':
       {
           Value = 555
           Dimensions = map[response_code:200]
       }
`
				if normalize(s) != normalize(wantStr) {
					return nil, fmt.Errorf("got adapters state as : '%s'; want '%s'", s, wantStr)
				}
				return nil, nil
			},
			GetConfig: func(ctx interface{}) ([]string, error) {
				s := ctx.(Server)
				return []string{
					// CRs for built-in templates (metric is what we need for this test)
					// are automatically added by the integration test framework.
					string(adptCrBytes),
					strings.Replace(operatorCfg, "{ADDRESS}", s.Addr(), 1),
				}, nil
			},
			Want: `
     {
      "AdapterState": null,
      "Returns": [
       {
        "Check": {
         "Status": {},
         "ValidDuration": 0,
         "ValidUseCount": 0
        },
        "Quota": null,
        "Error": null
       }
      ]
     }`,
		},
	)
}

func normalize(s string) string {
	s = strings.TrimSpace(s)
	s = strings.Replace(s, "\t", "", -1)
	s = strings.Replace(s, "\n", "", -1)
	s = strings.Replace(s, " ", "", -1)
	return s
}

gha_language: Go


text:
SUMMARY = "CANweb production image"

#IMAGE_FEATURES += "dev-pkgs ssh-server-openssh tools-debug debug-tweaks tools-sdk"
IMAGE_FEATURES += "ssh-server-openssh dev-pkgs tools-sdk"

LICENSE = "MIT"

inherit core-image

CORE_IMAGE_EXTRA_INSTALL = " \
	ncurses bluez5 libqmi libmbim openvpn kernel-modules \
	i2c-tools fbset evtest apache2 mod-wsgi canweb-customize python3-pip vim \
	python-sse python-pybluez python-cantools \
"

IMAGE_INSTALL = "\
    packagegroup-core-boot \
    packagegroup-core-full-cmdline \
    ${CORE_IMAGE_EXTRA_INSTALL} \
    "

inherit extrausers
EXTRA_USERS_PARAMS = "usermod -P canweb123 root;"

cleanup_rootfs() {
#	rm ${IMAGE_ROOTFS}/etc/nginx/sites-enabled/default_server
	install -d ${IMAGE_ROOTFS}/usr/lib/apache2/modules
#	cp ${IMAGE_ROOTFS}/usr/libexec/apache2/modules/mod_wsgi.so ${IMAGE_ROOTFS}/usr/lib/apache2/modules/mod_wsgi.so
}

ROOTFS_POSTPROCESS_COMMAND += "cleanup_rootfs;"

repository: bcran/meta-canweb

repo_name: bcran/meta-canweb


text:
package crdhandler

import (
	"errors"
	"fmt"
	"strings"

	"github.com/munnerz/goautoneg"
	"k8s.io/apimachinery/pkg/runtime"
)

var defaultMediaType = mediaType{
	Type:    "application",
	SubType: "json",
}

type mediaType struct {
	Type, SubType string
}

func (h Handler) getInputSerializer(contentType string) (runtime.Serializer, error) {
	parts := strings.SplitN(contentType, "/", 2)
	if len(parts) != 2 {
		return nil, errors.New(fmt.Sprintf("invalid content-type %#q", contentType))
	}
	return h.serializers[mediaType{parts[0], parts[1]}], nil
}

func (h Handler) getOutputSerializer(accept string) (runtime.Serializer, error) {
	if len(accept) == 0 {
		return h.serializers[defaultMediaType], nil
	}

	clauses := goautoneg.ParseAccept(accept)
	for _, clause := range clauses {
		for k, v := range h.serializers {
			switch {
			case clause.Type == "*" && clause.SubType == "*":
				return h.serializers[defaultMediaType], nil
			case clause.Type == k.Type && clause.SubType == k.SubType,
				clause.Type == k.Type && clause.SubType == "*":
				return v, nil
			}
		}
	}

	return nil, errors.New("unknown type")
}


text:
DESCRIPTION = "Very high-quality data compression program"
SECTION = "console/utils"
PR = "r0"

LICENSE = "bzip2"
FILESPATHPKG =. "bzip2-${PV}:"
SRC_URI = "http://www.bzip.org/${PV}/bzip2-${PV}.tar.gz \
	   file://bzip2-1.0.5-autoconfiscated.patch"
SRC_URI[md5sum] = "00b516f4704d4a7cb50a1d97e6e8e15b"
SRC_URI[sha256sum] = "a2848f34fcd5d6cf47def00461fcb528a0484d8edef8208d6d2e2909dc61d9cd"

S = "${WORKDIR}/bzip2-${PV}"

inherit autotools pkgconfig native

do_configure_prepend () {
	if test -f LICENSE ; then sh ./autogen.sh ; fi
}


text:
inherit core-image

require recipes-chicken/images/all-chicken-egg-recipes.inc

CORE_IMAGE_EXTRA_INSTALL += "\
    chicken \
    ${ALL_CHICKEN_EGG_RECIPES} \
"

TOOLCHAIN_HOST_TASK_append = " chicken-cross-canadian-${TRANSLATED_TARGET_ARCH}"

repository: Adellica/meta-chicken

repo_name: Adellica/meta-chicken


text:
@article{art_aa99,
 author = {A. Ferrari and F.-X. Schmider and G. Alengrin},
 journal = {Astronomy and Astrophysics},
 pages = {177--185},
 title = {Parametric representation of helioseismic data. Part 1: a
method for estimation of mode correlation},
 volume = {138},
 year = {1999}
}

repository: andferrari/academic-kickstart

repo_name: andferrari/academic-kickstart


text:
//Thomaz
package funcao

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

func E1_FuncoesAnonimas() {
	soma := func(a int, b int) int {
		return a + b
	}

	fmt.Println(soma(10, 20))

	func() {
		fmt.Println("Função anônima #1")
	}()

	func(msg string) {
		fmt.Println(msg)
	}("Função anônima #2")
}

func E2_ComoParametro_Filter(lista []int, filtro func(x int) bool) []int {
	var newList []int

	for item := range lista {
		if (filtro(item)) {
			newList = append(newList, item)
		}
	}

	return newList
}

func E3_ComoParametro_Map(lista []int, mapper func(x int) int) []int {
	var newList []int

	for item := range lista {
		newList = append(newList, mapper(item))
	}

	return newList
}

type Response struct {
	Cep string `json:"cep"`
	Logradouro string `json:"logradouro"`
	Complemento string `json:"complemento"`
	Bairro string `json:"bairro"`
	Localidade string `json:"localidade"`
	Uf string `json:"uf"`
	Ibge string `json:"ibge"`
	Gia string `json:"gia"`
	Ddd string `json:"ddd"`
	Siafi string `json:"siafi"`
}

func E4_ComoParametro_Correio(cep string, callback func(x Response)) {
	client := &http.Client{} //Criei um client para ter controle sobre cabaçalhos HTTP, política de redirecionamento e outras configs
	//se não não querer fazer esse controle http.Get (" http://example.com/ ") já resolveria o problema
	req, err := http.NewRequest("GET", "https://viacep.com.br/ws/"+ strings.TrimSpace(cep)+"/json/", nil) //Monta a requisição

	if (err != nil) {
		fmt.Println(err.Error())
	}
	//Adicionando cabeçalhos
	req.Header.Add("Accept", "application/json") //The Accept header is used to inform the server by the client that which content type is understandable by the client expressed as MIME-types
	req.Header.Add("Content-Type", "application/json")

	//Enviando requisição
	resp, err := client.Do(req)

	if (err != nil) {
		fmt.Println(err.Error())
	}

	defer resp.Body.Close()
	bodyBytes, err := ioutil.ReadAll(resp.Body)

	if (err != nil) {
		fmt.Println(err.Error())
	}

	var response Response
	json.Unmarshal(bodyBytes, &response) //& antes de uma variável significa algo como "use o endereço dessa variável para a ação x". Fazendo isso, afetará diretamente a variável

	defer callback(response)
}

repository: thomazreimberg/Go-Patterns-TFR

repo_name: thomazreimberg/Go-Patterns-TFR


text:
package ratelimiter

import "github.com/go-redis/redis/v8"

// redis 中执行的 lua 脚本判断 key 是否应该被限频
// 返回 json 字符串： is_limited false:无需限频，true:需限频
var tokenBucketRedisLuaIsLimitedScript = redis.NewScript(`
    -- 兼容低版本 redis 手动打开允许随机写入 （执行 TIME 指令获取时间）
    -- 避免报错 Write commands not allowed after non deterministic commands. Call redis.replicate_commands() at the start of your script in order to switch to single commands         replication mode.
    -- Redis 出于数据一致性考虑，要求脚本必须是纯函数的形式，也就是说对于一段 Lua 脚本给定相同的参数，重复执行其结果都是相同的。
    -- 这个限制的原因是 Redis 不仅仅是单机版的内存数据库，它还支持主从复制和持久化，执行过的 Lua 脚本会复制给 slave 以及持久化到磁盘，如果重复执行得到结果不同，那么就会出现内存、磁盘、 slave 之间的数据不一致，在 failover 或者重启之后造成数据错乱影响业务。
    -- 如果执行过非确定性命令（也就是 TIME ，因为时间是随机的）， Redis 就不允许执行写命令，以此来保证数据一致性。
    -- 在 Redis 中 time 命令是一个随机命令（时间是变化的），在 Lua 脚本中调用了随机命令之后禁止再调用写命令， Redis 中一共有 10 个随机类命令：
    -- spop 、 srandmember 、 sscan 、 zscan 、 hscan 、 randomkey 、 scan 、 lastsave 、 pubsub 、 time
    -- 在执行 redis.replicate_commands() 之后， Redis 就不再是把整个 Lua 脚本同步给 slave 和持久化，而是只把脚本中的写命令使用 multi/exec 包裹后直接去做复制，那么 slave 和持久化只复制了写命名，而写入的也是确定的结果。
    redis.replicate_commands()

    redis.log(redis.LOG_DEBUG, "------------ ratelimiter script begin ------------")
    -- 获取参数
    local p_key = KEYS[1]
    local p_bucket_capacity = tonumber(ARGV[1])
    local p_fill_count = tonumber(ARGV[2])
    local p_interval_microsecond = tonumber(ARGV[3])
    local p_expire_second = tonumber(ARGV[4])

    -- 返回结果
    local result = {}
    result['p_key'] = p_key
    result['p_fill_count'] = p_fill_count
    result['p_bucket_capacity'] = p_bucket_capacity
    result['p_interval_microsecond'] = p_interval_microsecond
    result['p_expire_second'] = p_expire_second

    -- 每次填充 token 数为 0 或 令牌桶容量为 0 则表示限制该请求 直接返回 无需操作 redis
    if p_fill_count <= 0 or p_bucket_capacity <= 0 then
        result['msg'] = "be limited by p_fill_count or p_bucket_capacity"
        result['is_limited'] = true
        return cjson.encode(result)
    end

    -- 判断桶是否存在
    local exists = redis.call("EXISTS", p_key)
    redis.log(redis.LOG_DEBUG, "ratelimiter: key:" .. p_key .. ", exists:" .. exists)

    -- 桶不存在则在 redis 中创建桶 并消耗当前 token
    if exists == 0 then
        -- 本次填充时间戳
        local now_timestamp_array = redis.call("TIME")
        -- 微秒级时间戳
        local last_consume_timestamp = tonumber(now_timestamp_array[1]) * 1000000 + tonumber(now_timestamp_array[2])
        redis.log(redis.LOG_DEBUG, "ratelimiter: last_consume_timestamp:" .. last_consume_timestamp .. ", remain_token_count:" .. p_bucket_capacity)
        -- 首次请求 默认为满桶  消耗一个 token
        local remain_token_count = p_bucket_capacity - 1

        -- 将当前秒级时间戳和剩余 token 数保存到 redis
        redis.call("HMSET", p_key, "last_consume_timestamp", last_consume_timestamp, "remain_token_count", remain_token_count)
        -- 设置 redis 的过期时间
        redis.call("EXPIRE", p_key, p_expire_second)
        redis.log(redis.LOG_DEBUG, "ratelimiter: call HMSET for creating bucket")
        redis.log(redis.LOG_DEBUG, "------------ ratelimiter script end ------------")

        -- 保存 result 信息
        result['msg'] = "key not exists in redis"
        -- string format 避免科学计数法
        result['last_consume_timestamp'] = string.format("%18.0f", last_consume_timestamp)
        result['remain_token_count'] = remain_token_count
        result['is_limited'] = false

        return cjson.encode(result)
    end

    -- 桶存在时，重新计算填充 token
    -- 获取 redis 中保存的上次填充时间和剩余 token 数
    local array = redis.call("HMGET", p_key, "last_consume_timestamp", "remain_token_count")
    if array == nil then
        redis.log(redis.LOG_WARNING, "ratelimiter: HMGET return nil for key:" .. p_key)
        redis.log(redis.LOG_DEBUG, "------------ ratelimiter script end ------------")

        -- 保存 result 信息
        result['msg'] = "err:HMGET data return nil"
        result['is_limited'] = false

        return cjson.encode(result)
    end
    local last_consume_timestamp, remain_token_count = tonumber(array[1]), tonumber(array[2])
    redis.log(redis.LOG_DEBUG, "ratelimiter: last_consume_timestamp:" .. last_consume_timestamp .. ", remain_token_count:" .. remain_token_count)

    -- 计算当前时间距离上次填充 token 过了多少微秒
    local now_timestamp_array = redis.call("TIME")
    local now_timestamp = tonumber(now_timestamp_array[1]) * 1000000 + tonumber(now_timestamp_array[2])
    local duration_microsecond = math.max(now_timestamp - last_consume_timestamp, 0)
    -- 根据配置计算 token 的填充速率: x token/μs
    local fill_rate = p_fill_count / p_interval_microsecond
    redis.log(redis.LOG_DEBUG, "ratelimiter: now_timestamp:" .. now_timestamp .. ", duration_microsecond:" .. duration_microsecond .. ", fill_rate:" .. fill_rate)
    -- 计算在这段时间内产生了多少 token , 浮点数向下取整
    local fill_token_count = math.floor(fill_rate * duration_microsecond)
    -- 计算桶内当前时间应有的 token 总数，总数不超过桶的容量
    local now_token_count = math.min(remain_token_count + fill_token_count, p_bucket_capacity)
    redis.log(redis.LOG_DEBUG, "ratelimiter: fill_token_count:" .. fill_token_count .. ", now_token_count:" .. now_token_count)

    -- 保存 debug 信息
    result['last_consume_timestamp'] = string.format("%18.0f", last_consume_timestamp)
    result['remain_token_count'] = remain_token_count
    result['now_timestamp'] = string.format("%18.0f", now_timestamp)
    result['duration_microsecond'] = string.format("%18.0f", duration_microsecond)
    result['fill_rate'] = string.format("%18.9f", fill_rate)
    result['fill_token_count'] = fill_token_count
    result['now_token_count'] = now_token_count

    -- 无可用 token
    if now_token_count <= 0 then
        -- 更新 redis 中的数据，被限流不消耗 now_token_count
        redis.call("HMSET", p_key, "last_consume_timestamp", last_consume_timestamp, "remain_token_count", now_token_count)
        -- 设置 redis 的过期时间
        redis.call("EXPIRE", p_key, p_expire_second)
        redis.log(redis.LOG_DEBUG, "ratelimiter: call HMSET for updating bucket")
        redis.log(redis.LOG_DEBUG, "------------ ratelimiter script end ------------")
        result['msg'] = "limit"
        result['is_limited'] = true
        return cjson.encode(result)
    end

    -- 更新 redis 中的数据, 消耗一个 token
    redis.call("HMSET", p_key, "last_consume_timestamp", now_timestamp, "remain_token_count", now_token_count - 1)
    -- 设置 redis 的过期时间
    redis.call("EXPIRE", p_key, p_expire_second)
    redis.log(redis.LOG_DEBUG, "ratelimiter: call HMSET for updating bucket")
    redis.log(redis.LOG_DEBUG, "------------ ratelimiter script end ------------")
    result['msg'] = "pass"
    result['is_limited'] = false
    return cjson.encode(result)
`)

repository: uhonliu/go-ratelimiter

repo_name: uhonliu/go-ratelimiter


text:
package zhttp

import (
	"github.com/Greyh4t/dnscache"
)

var dnsCache *dnscache.Resolver

func SetDnsCache(resolver *dnscache.Resolver) {
	dnsCache = resolver
}

func Request(method, url string, ro *RequestOptions) (*Response, error) {
	return doRequest(method, url, ro, nil)
}

func Get(url string, ro *RequestOptions) (*Response, error) {
	return doRequest("GET", url, ro, nil)
}

func Delete(url string, ro *RequestOptions) (*Response, error) {
	return doRequest("DELETE", url, ro, nil)
}

func Head(url string, ro *RequestOptions) (*Response, error) {
	return doRequest("HEAD", url, ro, nil)
}

func Patch(url string, ro *RequestOptions) (*Response, error) {
	return doRequest("PATCH", url, ro, nil)
}

func Post(url string, ro *RequestOptions) (*Response, error) {
	return doRequest("POST", url, ro, nil)
}

func Put(url string, ro *RequestOptions) (*Response, error) {
	return doRequest("PUT", url, ro, nil)
}

func Options(url string, ro *RequestOptions) (*Response, error) {
	return doRequest("OPTIONS", url, ro, nil)
}

repository: zlmfslx/zhttp

repo_name: zlmfslx/zhttp


text:
require hyperion-image-base.inc

DESCRIPTION = "A minimal console-only image with hyperion and debug tweaks"

IMAGE_FEATURES += " \
    package-management \
    "

EXTRA_IMAGE_FEATURES = " \
    debug-tweaks \
    "

IMAGE_INSTALL += " \
    hyperion-dbg \
    "

repository: ThomArmax/meta-hyperion

repo_name: ThomArmax/meta-hyperion

gha_language: SourcePawn


text:
SECTION = "devel/chicken"
DESCRIPTION = "Automatically read and write s-expressions to a level implementation"
LICENSE = "BSD"
LIC_FILES_CHKSUM = "file://level-sexp.meta;md5=a4d57118e018660ae95578904e2498ec"
DEPENDS_append_class-cross = " chicken-egg-level-cross chicken-egg-interfaces-cross chicken-egg-lazy-seq-cross"
DEPENDS_append_class-crosssdk = " chicken-egg-level-crosssdk chicken-egg-interfaces-crosssdk chicken-egg-lazy-seq-crosssdk"
DEPENDS_append_class-nativesdk = " nativesdk-chicken-egg-level nativesdk-chicken-egg-interfaces nativesdk-chicken-egg-lazy-seq chicken-egg-level-crosssdk chicken-egg-interfaces-crosssdk chicken-egg-lazy-seq-crosssdk"
DEPENDS = " chicken-egg-level chicken-egg-interfaces chicken-egg-lazy-seq chicken-egg-level-cross chicken-egg-interfaces-cross chicken-egg-lazy-seq-cross"
RDEPENDS_${PN} = " chicken-egg-level chicken-egg-interfaces chicken-egg-lazy-seq"

SRC_URI = "http://code.call-cc.org/egg-tarballs/4/level-sexp/level-sexp-${PV}.tar.gz"

SRC_URI[md5sum] = "1ba633fd9fa074a446ffe51cb73c3147"
SRC_URI[sha256sum] = "35d205b61d038ff3f0aa6bcb5e4e3823f4d40226ef41ab1880609f198e0a66c5"

S = "${WORKDIR}/level-sexp-${PV}"

inherit chicken_install

BBCLASSEXTEND = "cross crosssdk nativesdk"

repository: OSSystems/meta-chicken

repo_name: OSSystems/meta-chicken


text:
@article{Liu2012Optimum,
 abstract = {This paper designs the closed-form precoding matrices for non-regenerative MIMO relay system with the direct link. A multiple power constrained non-convex optimization problem is formulated by using the minimum-mean-squared error (MMSE) criterion. We decompose the original problem into two sub-problems. The relay transceiver Wiener filter structure is first rigorously derived, then the source transmit and destination receive matrices are jointly designed by solving an equivalent dual problem. Through our proposed joint iterative algorithm, the closed-form solutions can be finally obtained. The effectiveness of our proposed scheme is validated by simulations with comparison to some of the existing schemes.},
 author = {Liu, Fan and Xu, Hongbo and Li, Jun and Xia, Hongxing},
 journal = {Ieice Trans Commun},
 keywords = {direct link;joint precoding;MIMO relay;non-regenerative},
 number = {9},
 pages = {2878-2883},
 title = {Optimum Linear Precoding Design for Non-Regenerative MIMO Relay System with Direct Link},
 volume = {95},
 year = {2012}
}

repository: sean-xia/academic-kickstart

repo_name: sean-xia/academic-kickstart


text:
package config

import (
	"os"
	"strconv"
)

type Config interface {
	GetAgentServerAddress() string
	GetMonitoringServerAddress() string
	GetPort() int32
	GetStorageServerAddress() string
	GetApplicationMonitoringAddress() string
	GetIncidentServerAddress() string
	GetNotificationServerAddress() string
}

type cfg struct {
	port                        int32
	agentServer                 string
	monitoringServer            string
	storageServer               string
	applicationMonitoringServer string
	incidentServer              string
	notificationServer          string
}

func (c *cfg) GetNotificationServerAddress() string {
	return c.notificationServer
}

func (c *cfg) GetIncidentServerAddress() string {
	return c.incidentServer
}

func (c *cfg) GetApplicationMonitoringAddress() string {
	return c.applicationMonitoringServer
}

func (c *cfg) GetStorageServerAddress() string {
	return c.storageServer
}

func (c *cfg) GetAgentServerAddress() string {
	return c.agentServer
}

func (c *cfg) GetMonitoringServerAddress() string {
	return c.monitoringServer
}

func (c *cfg) GetPort() int32 {
	return c.port
}

const (
	ENV_PORT                          = "PORT"
	ENV_AGENT_SERVER                  = "AGENT_SERVER_HOST"
	ENV_MONITORING_SERVER             = "MONITORING_SERVER_HOST"
	ENV_STORAGE_SERVER                = "STORAGE_SERVER_HOST"
	ENV_APPLICATION_MONITORING_SERVER = "APPLICATION_MONITORING_SERVER_HOST"
	ENV_INCIDENT_SERVER               = "INCIDENT_SERVER_HOST"
	ENV_NOTIFICATION_SERVER           = "NOTIFICATION_SERVER_HOST"

	defaultPort int32 = 8080
)

func New() Config {
	portValue := os.Getenv(ENV_PORT)
	port := defaultPort
	if portValue != "" {
		i, err := strconv.ParseInt(portValue, 10, 32)
		if err == nil {
			port = int32(i)
		}
	}
	return &cfg{
		port:                        port,
		agentServer:                 os.Getenv(ENV_AGENT_SERVER),
		monitoringServer:            os.Getenv(ENV_MONITORING_SERVER),
		storageServer:               os.Getenv(ENV_STORAGE_SERVER),
		applicationMonitoringServer: os.Getenv(ENV_APPLICATION_MONITORING_SERVER),
		incidentServer:              os.Getenv(ENV_INCIDENT_SERVER),
		notificationServer:          os.Getenv(ENV_NOTIFICATION_SERVER),
	}
}

repository: squzy/squzy

repo_name: squzy/squzy

branch_name: refs/heads/develop


text:
package app

import (
	"database/sql"

	"github.com/google/uuid"
)

const (
	queryIns = "INSERT INTO users VALUES (?, ?, ?)"
	queryOut = "SELECT id, name, created_at FROM users WHERE id = ?"
)

// Repository is
type Repository interface {
	Get(id string) (*User, error)
	Set(user *User) error
}

// UserRepository is
type UserRepository struct {
	db      *sql.DB
	stmtIns *sql.Stmt
	stmtOut *sql.Stmt
}

// NewUserRepository is
func NewUserRepository(db *sql.DB) (*UserRepository, error) {
	ins, err := db.Prepare(queryIns)
	if err != nil {
		return nil, err
	}

	out, err := db.Prepare(queryOut)
	if err != nil {
		return nil, err
	}

	return &UserRepository{
		db:      db,
		stmtIns: ins,
		stmtOut: out,
	}, nil
}

// Get is
func (u *UserRepository) Get(id string) (*User, error) {
	user := &User{}
	if err := u.stmtOut.QueryRow(id).Scan(&user.ID, &user.Name, &user.CreatedAt); err != nil {
		return nil, err
	}
	return user, nil
}

// Set is
func (u *UserRepository) Set(user *User) error {
	// get version 4 UUID
	id, err := uuid.NewRandom()
	if err != nil {
		return err
	}

	//now := time.Now()
	if _, err := u.stmtIns.Exec(id, user.Name, user.CreatedAt); err != nil {
		return err
	}
	user.ID = id.String()
	//user.CreatedAt = now
	return nil
}

// Close is
func (u *UserRepository) Close() {
	u.db.Close()
	u.stmtIns.Close()
	u.stmtOut.Close()
}

repository: bassaer/go-user-api

repo_name: bassaer/go-user-api


text:
require rsyslog.inc
PR = "${INC_PR}.0"

SRC_URI[md5sum] = "37562d0e71a24938a9ed7f242bd32d35"
SRC_URI[sha256sum] = "e034b02473fd7e5313522173ec2f6c57e6fbcaec2c6b289edb968b1d64ae3ffd"

repository: linkessence/Galileo

repo_name: linkessence/Galileo


text:
package fw

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"strings"
	"testing"
)

func Test_MatchOneOfPatterns_Partial(t *testing.T) {
	// Arrange
	ass := assert.New(t)
	var tests = []struct {
		patterns []string
		input    string
		result   bool
	}{
		{[]string{"xxx", "yyy", "zzz"}, "yyyyy", true},
		{[]string{"xxx", "yyy", "zzz"}, "YYYYY", true},
		{[]string{"xxx", "yyy", "zzz"}, "yyy", true},
		{[]string{"xxx", "yyy", "zzz"}, "yyyzzz", true},
		{[]string{"xxx", "yyy", "zzz"}, "cccyyybbb", true},
		{[]string{"xxx", "yyy", "zzz"}, "CCCYYYBBB", true},
		{[]string{"xxx", "yyy", "zzz"}, "aaa", false},
	}

	for _, test := range tests {
		t.Run(test.input, func(t *testing.T) {
			// Act
			m, _ := NewPartialMatcher(test.patterns, strings.ToUpper)
			result := m.Match(test.input)

			// Assert
			ass.Equal(test.result, result)
		})
	}
}

func Test_SearchOneOfPatterns(t *testing.T) {
	// Arrange
	ass := assert.New(t)
	var tests = []struct {
		patterns []string
		input    string
		result   []string
	}{
		{[]string{"xxx", "yyy", "zzz"}, "yyyyy", []string{"YYY", "YYY", "YYY"}},
		{[]string{"xxx", "yyy", "zzz"}, "YYYYY", []string{"YYY", "YYY", "YYY"}},
		{[]string{"xxx", "yyy", "zzz"}, "yyy", []string{"YYY"}},
		{[]string{"xxx", "yyy", "zzz"}, "yyyzzz", []string{"YYY", "ZZZ"}},
		{[]string{"xxx", "yyy", "zzz"}, "cccyyybbb", []string{"YYY"}},
		{[]string{"xxx", "yyy", "zzz"}, "CCCYYYBBB", []string{"YYY"}},
		{[]string{"xxx", "yyy", "zzz"}, "aaa", []string{}},
	}

	for _, test := range tests {
		t.Run(test.input, func(t *testing.T) {
			// Act
			m, _ := NewPartialMatcher(test.patterns, strings.ToUpper)
			result := m.Search(test.input)

			// Assert
			ass.Equal(test.result, result)
		})
	}
}

func TestNewPartialMatcher_EmptyMatches(t *testing.T) {
	// Arrange
	ass := assert.New(t)

	// Act
	m, err := NewPartialMatcher([]string{}, strings.ToUpper)

	// Assert
	ass.Error(err)
	ass.Nil(m)
}

func Test_MatchOneOfPatterns_Exact(t *testing.T) {
	// Arrange
	ass := assert.New(t)
	var tests = []struct {
		patterns []string
		input    string
		result   bool
	}{
		{[]string{"xxx", "yyy", "zzz"}, "yyyyy", false},
		{[]string{"xxx", "yyy", "zzz"}, "yyy", true},
		{[]string{"xxx", "yyy", "zzz"}, "yyyzzz", false},
		{[]string{"xxx", "yyy", "zzz"}, "cccyyybbb", false},
		{[]string{"xxx", "yyy", "zzz"}, "aaa", false},
	}

	for _, test := range tests {
		t.Run(test.input, func(t *testing.T) {
			// Act
			m := NewExactMatch(test.patterns)
			result := m.Match(test.input)

			// Assert
			ass.Equal(test.result, result)
		})
	}
}

func Test_MatchOneOfPatternsWithBloomEnabled_Exact(t *testing.T) {
	// Arrange
	ass := assert.New(t)
	var tests = []struct {
		patterns []string
		input    string
		result   bool
	}{
		{[]string{"xxx", "yyy", "zzz"}, "yyyyy", false},
		{[]string{"xxx", "yyy", "zzz"}, "yyy", true},
		{[]string{"xxx", "yyy", "zzz"}, "yyyzzz", false},
		{[]string{"xxx", "yyy", "zzz"}, "cccyyybbb", false},
		{[]string{"xxx", "yyy", "zzz"}, "aaa", false},
	}

	for _, test := range tests {
		t.Run(test.input, func(t *testing.T) {
			// Act
			b := NewBloomFilter(test.patterns)
			m := NewExactMatch(test.patterns)
			c := NewMatchAll(b, m)
			result := c.Match(test.input)

			// Assert
			ass.Equal(test.result, result)
		})
	}
}

func Test_MatchAnyOneOfPatterns_Exact(t *testing.T) {
	// Arrange
	ass := assert.New(t)
	var tests = []struct {
		name     string
		patterns []string
		input    []string
		result   bool
	}{
		{"one matched", []string{"xxx", "yyy", "zzz"}, []string{"yyy"}, true},
		{"all matched", []string{"xxx", "yyy", "zzz"}, []string{"xxx", "yyy", "zzz"}, true},
		{"all not matched", []string{"eee", "rr", "dd"}, []string{"xxx", "yyy", "zzz"}, false},
		{"one not matched", []string{"xxx", "yyy", "zzz"}, []string{"eee"}, false},
		{"empty", []string{"xxx", "yyy", "zzz"}, []string{}, false},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			// Act
			m := NewExactMatch(test.patterns)
			result := MatchAny(test.input, m)

			// Assert
			ass.Equal(test.result, result)
		})
	}
}

func Test_Filter(t *testing.T) {
	// Arrange
	ass := assert.New(t)
	m := NewExactMatch([]string{"xxx", "yyy", "zzz"})

	var tests = []struct {
		name     string
		input    []string
		expected []string
	}{
		{"one matched", []string{"aa", "yyy", "bb"}, []string{"yyy"}},
		{"all matched", []string{"xxx", "yyy", "zzz"}, []string{"xxx", "yyy", "zzz"}},
		{"all not matched", []string{"eee", "rr", "dd"}, []string{}},
		{"one not matched", []string{"xxx", "yyy", "ee"}, []string{"xxx", "yyy"}},
		{"empty", []string{}, []string{}},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			// Act
			result := Filter(test.input, m)

			// Assert
			ass.Equal(test.expected, result)
		})
	}
}

func Test_Filter_Nil(t *testing.T) {
	// Arrange
	ass := assert.New(t)

	// Act
	result := Filter([]string{"a"}, nil)

	// Assert
	ass.Equal([]string{}, result)
}

func Test_BloomFilter_estimate(t *testing.T) {
	// Arrange
	n := uint(100000)
	data := generateRandomStringSlice(int(n), 50)
	b := NewBloomFilter(data).(*bloomFilter)

	// Act
	r := b.estimate(n)

	// Assert
	fmt.Printf("%f\n", r)
}

repository: aegoroff/solt

repo_name: aegoroff/solt


text:
package awserrors

import (
	"fmt"
	"testing"

	"github.com/aws/aws-sdk-go/aws/awserr"
	errors "github.com/segmentio/errors-go"
)

func TestAdapt(t *testing.T) {
	tests := []struct {
		scenario string
		function func(*testing.T)
	}{
		{
			scenario: "adapting errors that don't come from AWS simply returns the original error",
			function: testAdaptNonAwsError,
		},

		{
			scenario: "adapting errors exposes the correct code, message, and causes",
			function: testAdaptError,
		},

		{
			scenario: "adapting batch errors exposes the correct code, message, and causes",
			function: testAdaptBatchError,
		},

		{
			scenario: "adapting request errors exposes the correct code, message, and causes",
			function: testAdaptRequestError,
		},
	}

	for _, test := range tests {
		t.Run(test.scenario, test.function)
	}
}

func testAdaptNonAwsError(t *testing.T) {
	e1 := errors.New("some error")
	e2, ok := Adapt(e1)

	if ok {
		t.Error("adapting non-AWS errors must return false to indicate that the error was not recognized")
	}

	if e1 != e2 {
		t.Error("adapting non-AWS errors must return the original error unchanged")
	}
}

func testAdaptError(t *testing.T) {
	e0 := errors.New("base error")
	e1 := &singleError{code: "ThrottlingException", msg: "too many requests", orig: e0}
	e2, ok := Adapt(e1)

	if !ok {
		t.Error("adapting AWS errors must return true to indicate that the error was recognized")
	}

	s1 := e1.Error()
	s2 := e2.Error()

	if s1 != s2 {
		t.Error("the adapted AWS error must preserve the original error string")
		t.Log("expected:", s1)
		t.Log("found:   ", s2)
	}

	c1 := code(e1)
	c2 := code(e2)

	if c1 != c2 {
		t.Error("the adapted AWS error must preserve the original error code")
		t.Log("expected:", c1)
		t.Log("found:   ", c2)
	}

	m1 := message(e1)
	m2 := message(e2)

	if m1 != m2 {
		t.Error("the adapted AWS error must preserve the original error message")
		t.Log("expected:", m1)
		t.Log("found:   ", m2)
	}

	if cause := errors.Cause(e2); cause != e0 {
		t.Error("the adapted AWS error must expose the original error as a cause")
		t.Log("expected:", e0)
		t.Log("found:   ", cause)
	}

	if causes := errors.Causes(e2); !equalErrors(causes, []error{e0}) {
		t.Error("the adapted AWS error must expose the original error as causes")
		t.Log("expected:", []error{e0})
		t.Log("found:   ", causes)
	}

	if id := requestID(e2); id != "" {
		t.Error("unexpected request ID on AWS error:", id)
	}

	if sc := statusCode(e2); sc != 0 {
		t.Error("unexpected status code on AWS error:", sc)
	}
}

func testAdaptBatchError(t *testing.T) {
	e0 := []error{
		errors.New("base error 1"),
		errors.New("base error 2"),
		errors.New("base error 3"),
	}
	e1 := &batchError{code: "ThrottlingException", msg: "too many requests", orig: e0}
	e2, ok := Adapt(e1)

	if !ok {
		t.Error("adapting AWS errors must return true to indicate that the error was recognized")
	}

	s1 := e1.Error()
	s2 := e2.Error()

	if s1 != s2 {
		t.Error("the adapted AWS error must preserve the original error string")
		t.Log("expected:", s1)
		t.Log("found:   ", s2)
	}

	c1 := code(e1)
	c2 := code(e2)

	if c1 != c2 {
		t.Error("the adapted AWS error must preserve the original error code")
		t.Log("expected:", c1)
		t.Log("found:   ", c2)
	}

	m1 := message(e1)
	m2 := message(e2)

	if m1 != m2 {
		t.Error("the adapted AWS error must preserve the original error message")
		t.Log("expected:", m1)
		t.Log("found:   ", m2)
	}

	if causes := errors.Causes(e2); !equalErrors(causes, e0) {
		t.Error("the adapted AWS error must expose the original error as causes")
		t.Log("expected:", e0)
		t.Log("found:   ", causes)
	}
}

func testAdaptRequestError(t *testing.T) {
	e1 := &requestError{code: "ThrottlingException", msg: "too many requests", id: "1234567890", status: 429}
	e2, ok := Adapt(e1)

	if !ok {
		t.Error("adapting AWS errors must return true to indicate that the error was recognized")
	}

	s1 := e1.Error()
	s2 := e2.Error()

	if s1 != s2 {
		t.Error("the adapted AWS error must preserve the original error string")
		t.Log("expected:", s1)
		t.Log("found:   ", s2)
	}

	c1 := code(e1)
	c2 := code(e2)

	if c1 != c2 {
		t.Error("the adapted AWS error must preserve the original error code")
		t.Log("expected:", c1)
		t.Log("found:   ", c2)
	}

	m1 := message(e1)
	m2 := message(e2)

	if m1 != m2 {
		t.Error("the adapted AWS error must preserve the original error message")
		t.Log("expected:", m1)
		t.Log("found:   ", m2)
	}

	id1 := requestID(e1)
	id2 := requestID(e2)

	if id1 != id2 {
		t.Error("the adapted AWS error must preserve the original request ID")
		t.Log("expected:", id1)
		t.Log("found:   ", id2)
	}

	sc1 := statusCode(e1)
	sc2 := statusCode(e2)

	if sc1 != sc2 {
		t.Error("the adapted AWS error must preserve the original status code")
		t.Log("expected:", sc1)
		t.Log("found:   ", sc2)
	}
}

func code(err error) string {
	e, ok := err.(interface {
		Code() string
	})
	if ok {
		return e.Code()
	}
	return ""
}

func message(err error) string {
	e, ok := err.(interface {
		Message() string
	})
	if ok {
		return e.Message()
	}
	return ""
}

func requestID(err error) string {
	e, ok := err.(interface {
		RequestID() string
	})
	if ok {
		return e.RequestID()
	}
	return ""
}

func statusCode(err error) int {
	e, ok := err.(interface {
		StatusCode() int
	})
	if ok {
		return e.StatusCode()
	}
	return 0
}

func equalErrors(errs1 []error, errs2 []error) bool {
	if len(errs1) != len(errs2) {
		return false
	}

	for i := range errs1 {
		if errs1[i] != errs2[i] {
			return false
		}
	}

	return true
}

type singleError struct {
	code string
	msg  string
	orig error
}

func (e *singleError) Error() string   { return awserr.SprintError(e.code, e.msg, "", e.orig) }
func (e *singleError) Code() string    { return e.code }
func (e *singleError) Message() string { return e.msg }
func (e *singleError) OrigErr() error  { return e.orig }

type batchError struct {
	code string
	msg  string
	orig []error
}

func (e *batchError) Error() string {
	return awserr.SprintError(e.code, e.msg, "", errors.Join(e.orig...))
}
func (e *batchError) Code() string      { return e.code }
func (e *batchError) Message() string   { return e.msg }
func (e *batchError) OrigErrs() []error { return e.orig }

type requestError struct {
	code   string
	msg    string
	id     string
	status int
}

func (e *requestError) Error() string {
	return awserr.SprintError(e.code, e.msg, fmt.Sprintf("id=%s, status=%d", e.id, e.status), nil)
}
func (e *requestError) Code() string      { return e.code }
func (e *requestError) Message() string   { return e.msg }
func (e *requestError) OrigErr() error    { return nil }
func (e *requestError) StatusCode() int   { return e.status }
func (e *requestError) RequestID() string { return e.id }

repository: fossabot/errors-go

repo_name: fossabot/errors-go

gha_license_id: NOASSERTION


text:
package data

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/evergreen-ci/evergreen/model/task"
	"github.com/evergreen-ci/evergreen/units"
	"github.com/mongodb/amboy"
	"github.com/mongodb/grip"
	"github.com/mongodb/grip/message"
	"github.com/pkg/errors"
)

type GenerateConnector struct{}

// GenerateTasks parses JSON files for `generate.tasks` and creates the new builds and tasks.
func (gc *GenerateConnector) GenerateTasks(ctx context.Context, taskID string, jsonBytes []json.RawMessage, group amboy.QueueGroup) error {
	t, err := task.FindOneId(taskID)
	if err != nil {
		return errors.Wrapf(err, "problem finding task %s", taskID)
	}

	// in the future this operation should receive a context tied
	// to the lifetime of the application (e.g. env.Context())
	// rather than a context tied to the lifetime of the request
	// (e.g. ctx above.)
	q, err := group.Get(context.TODO(), t.Version)
	if err != nil {
		return errors.Wrapf(err, "problem getting queue for version %s", t.Version)
	}
	err = q.Put(ctx, units.NewGenerateTasksJob(taskID, jsonBytes))
	grip.Debug(message.WrapError(err, message.Fields{
		"message": "problem saving new generate tasks job for task",
		"task_id": taskID}))
	return nil
}

func (gc *GenerateConnector) GeneratePoll(ctx context.Context, taskID string, group amboy.QueueGroup) (bool, []string, error) {
	t, err := task.FindOneId(taskID)
	if err != nil {
		return false, nil, errors.Wrapf(err, "problem finding task %s", taskID)
	}

	// in the future this operation should receive a context tied
	// to the lifetime of the application (e.g. env.Context())
	// rather than a context tied to the lifetime of the request
	// (e.g. ctx above.)
	q, err := group.Get(context.TODO(), t.Version)
	if err != nil {
		return false, nil, errors.Wrapf(err, "problem getting queue for version %s", t.Version)
	}
	jobID := fmt.Sprintf("generate-tasks-%s", taskID)
	j, exists := q.Get(ctx, jobID)
	if !exists {
		return false, nil, errors.Errorf("task %s not in queue", taskID)
	}
	return j.Status().Completed, j.Status().Errors, nil
}

type MockGenerateConnector struct{}

func (gc *MockGenerateConnector) GenerateTasks(ctx context.Context, taskID string, jsonBytes []json.RawMessage, group amboy.QueueGroup) error {
	return nil
}

func (gc *MockGenerateConnector) GeneratePoll(ctx context.Context, taskID string, queue amboy.QueueGroup) (bool, []string, error) {
	// no task
	if taskID == "0" {
		return false, nil, errors.New("No task called '0'")
	}
	// finished
	if taskID == "1" {
		return true, nil, nil
	}
	// not yet finished
	return false, nil, nil
}

repository: HubBucket-Team/evergreen

repo_name: HubBucket-Team/evergreen


text:
// Code generated by private/model/cli/gen-api/main.go. DO NOT EDIT.

package alexaforbusiness

import (
	"context"
	"fmt"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/internal/awsutil"
)

type SearchContactsInput struct {
	_ struct{} `type:"structure"`

	// The filters to use to list a specified set of address books. The supported
	// filter keys are DisplayName, FirstName, LastName, and AddressBookArns.
	Filters []Filter `type:"list"`

	// The maximum number of results to include in the response. If more results
	// exist than the specified MaxResults value, a token is included in the response
	// so that the remaining results can be retrieved.
	MaxResults *int64 `min:"1" type:"integer"`

	// An optional token returned from a prior request. Use this token for pagination
	// of results from this action. If this parameter is specified, the response
	// only includes results beyond the token, up to the value specified by MaxResults.
	NextToken *string `min:"1" type:"string"`

	// The sort order to use in listing the specified set of contacts. The supported
	// sort keys are DisplayName, FirstName, and LastName.
	SortCriteria []Sort `type:"list"`
}

// String returns the string representation
func (s SearchContactsInput) String() string {
	return awsutil.Prettify(s)
}

// Validate inspects the fields of the type to determine if they are valid.
func (s *SearchContactsInput) Validate() error {
	invalidParams := aws.ErrInvalidParams{Context: "SearchContactsInput"}
	if s.MaxResults != nil && *s.MaxResults < 1 {
		invalidParams.Add(aws.NewErrParamMinValue("MaxResults", 1))
	}
	if s.NextToken != nil && len(*s.NextToken) < 1 {
		invalidParams.Add(aws.NewErrParamMinLen("NextToken", 1))
	}
	if s.Filters != nil {
		for i, v := range s.Filters {
			if err := v.Validate(); err != nil {
				invalidParams.AddNested(fmt.Sprintf("%s[%v]", "Filters", i), err.(aws.ErrInvalidParams))
			}
		}
	}
	if s.SortCriteria != nil {
		for i, v := range s.SortCriteria {
			if err := v.Validate(); err != nil {
				invalidParams.AddNested(fmt.Sprintf("%s[%v]", "SortCriteria", i), err.(aws.ErrInvalidParams))
			}
		}
	}

	if invalidParams.Len() > 0 {
		return invalidParams
	}
	return nil
}

type SearchContactsOutput struct {
	_ struct{} `type:"structure"`

	// The contacts that meet the specified set of filter criteria, in sort order.
	Contacts []ContactData `type:"list"`

	// The token returned to indicate that there is more data available.
	NextToken *string `min:"1" type:"string"`

	// The total number of contacts returned.
	TotalCount *int64 `type:"integer"`
}

// String returns the string representation
func (s SearchContactsOutput) String() string {
	return awsutil.Prettify(s)
}

const opSearchContacts = "SearchContacts"

// SearchContactsRequest returns a request value for making API operation for
// Alexa For Business.
//
// Searches contacts and lists the ones that meet a set of filter and sort criteria.
//
//    // Example sending a request using SearchContactsRequest.
//    req := client.SearchContactsRequest(params)
//    resp, err := req.Send(context.TODO())
//    if err == nil {
//        fmt.Println(resp)
//    }
//
// Please also see https://docs.aws.amazon.com/goto/WebAPI/alexaforbusiness-2017-11-09/SearchContacts
func (c *Client) SearchContactsRequest(input *SearchContactsInput) SearchContactsRequest {
	op := &aws.Operation{
		Name:       opSearchContacts,
		HTTPMethod: "POST",
		HTTPPath:   "/",
		Paginator: &aws.Paginator{
			InputTokens:     []string{"NextToken"},
			OutputTokens:    []string{"NextToken"},
			LimitToken:      "MaxResults",
			TruncationToken: "",
		},
	}

	if input == nil {
		input = &SearchContactsInput{}
	}

	req := c.newRequest(op, input, &SearchContactsOutput{})

	return SearchContactsRequest{Request: req, Input: input, Copy: c.SearchContactsRequest}
}

// SearchContactsRequest is the request type for the
// SearchContacts API operation.
type SearchContactsRequest struct {
	*aws.Request
	Input *SearchContactsInput
	Copy  func(*SearchContactsInput) SearchContactsRequest
}

// Send marshals and sends the SearchContacts API request.
func (r SearchContactsRequest) Send(ctx context.Context) (*SearchContactsResponse, error) {
	r.Request.SetContext(ctx)
	err := r.Request.Send()
	if err != nil {
		return nil, err
	}

	resp := &SearchContactsResponse{
		SearchContactsOutput: r.Request.Data.(*SearchContactsOutput),
		response:             &aws.Response{Request: r.Request},
	}

	return resp, nil
}

// NewSearchContactsRequestPaginator returns a paginator for SearchContacts.
// Use Next method to get the next page, and CurrentPage to get the current
// response page from the paginator. Next will return false, if there are
// no more pages, or an error was encountered.
//
// Note: This operation can generate multiple requests to a service.
//
//   // Example iterating over pages.
//   req := client.SearchContactsRequest(input)
//   p := alexaforbusiness.NewSearchContactsRequestPaginator(req)
//
//   for p.Next(context.TODO()) {
//       page := p.CurrentPage()
//   }
//
//   if err := p.Err(); err != nil {
//       return err
//   }
//
func NewSearchContactsPaginator(req SearchContactsRequest) SearchContactsPaginator {
	return SearchContactsPaginator{
		Pager: aws.Pager{
			NewRequest: func(ctx context.Context) (*aws.Request, error) {
				var inCpy *SearchContactsInput
				if req.Input != nil {
					tmp := *req.Input
					inCpy = &tmp
				}

				newReq := req.Copy(inCpy)
				newReq.SetContext(ctx)
				return newReq.Request, nil
			},
		},
	}
}

// SearchContactsPaginator is used to paginate the request. This can be done by
// calling Next and CurrentPage.
type SearchContactsPaginator struct {
	aws.Pager
}

func (p *SearchContactsPaginator) CurrentPage() *SearchContactsOutput {
	return p.Pager.CurrentPage().(*SearchContactsOutput)
}

// SearchContactsResponse is the response type for the
// SearchContacts API operation.
type SearchContactsResponse struct {
	*SearchContactsOutput

	response *aws.Response
}

// SDKResponseMetdata returns the response metadata for the
// SearchContacts request.
func (r *SearchContactsResponse) SDKResponseMetdata() *aws.Response {
	return r.response
}


text:
package register

import (
	"context"
	"fmt"
	"io/ioutil"
	"testing"
	"time"

	"github.com/newrelic/infrastructure-agent/internal/instrumentation"
	"github.com/newrelic/infrastructure-agent/pkg/backend/backoff"
	log "github.com/sirupsen/logrus"
	"github.com/sirupsen/logrus/hooks/test"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/newrelic/infrastructure-agent/pkg/backend/identityapi"
	"github.com/newrelic/infrastructure-agent/pkg/entity"
	"github.com/newrelic/infrastructure-agent/pkg/fwrequest"
	"github.com/newrelic/infrastructure-agent/pkg/integrations/v4/protocol"
)

// Payload size.
const MB = 1000 * 1000

func newClientReturning(ids ...entity.ID) identityapi.RegisterClient {
	return &fakeClient{
		ids: ids,
	}
}

const (
	agentVersion = "testVersion"
)

type fakeClient struct {
	ids       []entity.ID
	collector [][]string
	err       error
}

func (c *fakeClient) RegisterBatchEntities(agentEntityID entity.ID, entities []entity.Fields) (r []identityapi.RegisterEntityResponse, err error) {
	if c.err != nil {
		return nil, c.err
	}
	r = []identityapi.RegisterEntityResponse{}
	for i, id := range c.ids {
		var name string
		if len(entities) > i {
			name = entities[i].Name
			// Simulate an entity that generates error
			if name == "error" {
				r = append(r, identityapi.RegisterEntityResponse{Name: name, ErrorMsg: "Invalid entityName"})
			} else if name == "warnings" {
				r = append(r, identityapi.RegisterEntityResponse{
					Name: name,
					Warnings: []string{
						"Too many metadata, dropping ...",
						"Invalid metadata: ...",
					},
				})
			} else {
				r = append(r, identityapi.RegisterEntityResponse{Name: name, ID: id})
			}
		}
	}

	names := make([]string, len(entities))
	for i, e := range entities {
		names[i] = e.Name
	}
	c.collector = append(c.collector, names)

	return
}

func (c *fakeClient) RegisterEntity(agentEntityID entity.ID, entity entity.Fields) (r identityapi.RegisterEntityResponse, err error) {
	// won't be called
	return
}

func (c *fakeClient) RegisterEntitiesRemoveMe(agentEntityID entity.ID, entities []identityapi.RegisterEntity) (r []identityapi.RegisterEntityResponse, t time.Duration, err error) {
	// won't be called
	return
}

func (c *fakeClient) assertRecordedData(t *testing.T, names [][]string) {
	require.Equal(t, len(names), len(c.collector))

	for i, n := range names {
		assert.ElementsMatch(t, n, c.collector[i])
	}
}

func TestWorker_Run_SendsWhenBatchLimitIsReached(t *testing.T) {
	testCases := []struct {
		name                 string
		maxBatchSize         int
		maxBatchByteSize     int
		expectedBatchesCount int
		givenEntitiesCount   int
		expectedEntityName   [][]string //TODO fix naming
		batchDuration        time.Duration
		timeout              time.Duration
	}{
		{
			name:                 "given_a_batch_limits_of_5_entities_when_5_entities_are_submitted_then_register_is_called_in_a_single_batch",
			maxBatchSize:         5,
			maxBatchByteSize:     MB,
			givenEntitiesCount:   5,
			expectedBatchesCount: 1,
			expectedEntityName:   [][]string{{"test-0", "test-1", "test-2", "test-3", "test-4"}},
			batchDuration:        50 * time.Millisecond,
			timeout:              100 * time.Millisecond,
		},
		{
			name:                 "given_a_batch_limit_of_2_entities_when_4_entities_are_submitted_then_register_is_called_in_2_batches",
			maxBatchSize:         2,
			maxBatchByteSize:     MB,
			givenEntitiesCount:   4,
			expectedBatchesCount: 2,
			expectedEntityName:   [][]string{{"test-0", "test-1"}, {"test-2", "test-3"}},
			batchDuration:        50 * time.Millisecond,
			timeout:              100 * time.Millisecond,
		},
		{
			name:                 "given_a_batch_limit_of_2_entities_when_3_entities_are_submitted_then_register_is_called_with_1_batch_by_limit_and_other_batch_by_timer",
			maxBatchSize:         2,
			maxBatchByteSize:     MB,
			givenEntitiesCount:   3,
			expectedBatchesCount: 1,
			expectedEntityName:   [][]string{{"test-0", "test-1"}, {"test-2"}},
			batchDuration:        50 * time.Millisecond,
			timeout:              100 * time.Millisecond,
		},
		{
			name:                 "given_a_batch_byte_limit_of_2_entities_size_when_2_entities_are_submitted_then_register_is_called_in_a_single_batch",
			maxBatchSize:         10,                                              // high number to prevent send by this setting
			maxBatchByteSize:     (&entity.Fields{Name: "test-1"}).JsonSize() * 2, //calc based on number of entities per batch
			givenEntitiesCount:   2,
			expectedBatchesCount: 1,
			expectedEntityName:   [][]string{{"test-0", "test-1"}},
			batchDuration:        time.Second,
			timeout:              100 * time.Millisecond,
		},
		{
			name:                 "given_a_batch_byte_limit_of_2_entities_size_when_3_entities_are_submitted_then_register_is_called_with_1_batch_by_limit_and_other_batch_by_timer",
			maxBatchSize:         10,                                              // high number to prevent send by this setting
			maxBatchByteSize:     (&entity.Fields{Name: "test-1"}).JsonSize() * 2, //calc based on number of entities per batch
			givenEntitiesCount:   3,
			expectedBatchesCount: 1,
			expectedEntityName:   [][]string{{"test-0", "test-1"}, {"test-2"}},
			batchDuration:        50 * time.Millisecond,
			timeout:              100 * time.Millisecond,
		},
	}

	for _, test := range testCases {
		t.Run(test.name, func(t *testing.T) {
			reqsToRegisterQueue := make(chan fwrequest.EntityFwRequest, test.givenEntitiesCount)
			registeredQueue := make(chan fwrequest.EntityFwRequest, test.givenEntitiesCount)

			agentIdentity := func() entity.Identity {
				return entity.Identity{ID: 13}
			}

			ids := make([]entity.ID, test.maxBatchSize)
			for i := 0; i < test.maxBatchSize; i++ {
				ids[i] = entity.ID(i)
			}

			config := WorkerConfig{
				MaxBatchSize:      test.maxBatchSize,
				MaxBatchSizeBytes: test.maxBatchByteSize,
				MaxBatchDuration:  test.batchDuration,
				MaxRetryBo:        0,
			}

			client := &fakeClient{
				ids:       ids,
				collector: make([][]string, 0),
			}

			w := NewWorker(agentIdentity, client, backoff.NewDefaultBackoff(), reqsToRegisterQueue, registeredQueue, config, instrumentation.NoopMeasure)

			ctx, cancel := context.WithCancel(context.Background())
			defer cancel()

			go w.Run(ctx)

			for i := 0; i < test.givenEntitiesCount; i++ {
				reqsToRegisterQueue <- fwrequest.NewEntityFwRequest(protocol.Dataset{Entity: entity.Fields{
					Name: fmt.Sprintf("test-%d", i),
				}}, entity.EmptyID, fwrequest.FwRequestMeta{}, protocol.IntegrationMetadata{}, agentVersion)
			}

			// wait for the register to finish all the requests
			for i := 0; i < test.givenEntitiesCount; i++ {
				select {
				case <-registeredQueue:
				case <-time.After(test.timeout):
					assert.Fail(t, "timeout exceeded waiting for register")
				}
			}

			client.assertRecordedData(t, test.expectedEntityName)
		})
	}
}

func TestWorker_Run_EntityGraterThanMaxByteSize(t *testing.T) {
	reqsToRegisterQueue := make(chan fwrequest.EntityFwRequest, 0)
	registeredQueue := make(chan fwrequest.EntityFwRequest, 1)

	agentIdentity := func() entity.Identity {
		return entity.Identity{ID: 13}
	}
	config := WorkerConfig{
		MaxBatchSize:      2,
		MaxBatchSizeBytes: (&entity.Fields{Name: "test-1"}).JsonSize(),
		MaxBatchDuration:  50 * time.Millisecond,
		MaxRetryBo:        0,
	}

	client := &fakeClient{
		ids: []entity.ID{1},
	}

	w := NewWorker(agentIdentity, client, backoff.NewDefaultBackoff(), reqsToRegisterQueue, registeredQueue, config, instrumentation.NoopMeasure)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	go w.Run(ctx)

	reqsToRegisterQueue <- fwrequest.NewEntityFwRequest(protocol.Dataset{Entity: entity.Fields{
		Name: "this-entity-should-not-pass",
	}}, entity.EmptyID, fwrequest.FwRequestMeta{}, protocol.IntegrationMetadata{}, agentVersion)

	reqsToRegisterQueue <- fwrequest.NewEntityFwRequest(protocol.Dataset{Entity: entity.Fields{
		Name: "test-1",
	}}, entity.EmptyID, fwrequest.FwRequestMeta{}, protocol.IntegrationMetadata{}, agentVersion)

	select {
	case req := <-registeredQueue:
		assert.Equal(t, "test-1", req.Data.Entity.Name)
	}
}

func TestWorker_registerEntitiesWithRetry_OnError_RetryBackoff(t *testing.T) {
	reqsToRegisterQueue := make(chan fwrequest.EntityFwRequest, 0)
	reqsRegisteredQueue := make(chan fwrequest.EntityFwRequest, 0)

	agentIdentity := func() entity.Identity {
		return entity.Identity{ID: 13}
	}

	client := &fakeClient{
		// StatusCodeLimitExceed is an e.g. of error for which we should retry with backoff.
		err: identityapi.NewRegisterEntityError("err", identityapi.StatusCodeLimitExceed, fmt.Errorf("err")),
	}

	backoff := backoff.NewDefaultBackoff()
	backoffCh := make(chan time.Duration)
	backoff.GetBackoffTimer = func(d time.Duration) *time.Timer {
		select {
		case backoffCh <- d:
		default:
		}
		return time.NewTimer(0)
	}

	config := WorkerConfig{
		MaxBatchSize:      1,
		MaxBatchSizeBytes: MB,
		MaxBatchDuration:  50 * time.Millisecond,
		MaxRetryBo:        0,
	}
	w := NewWorker(agentIdentity, client, backoff, reqsToRegisterQueue, reqsRegisteredQueue, config, instrumentation.NoopMeasure)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	result := make(chan []identityapi.RegisterEntityResponse, 1)
	go func() {
		result <- w.registerEntitiesWithRetry(ctx, []entity.Fields{{Name: "test"}})
	}()

	select {
	case <-backoffCh: // Success
	case <-result:
		t.Error("registerEntitiesWithRetry should retry")
	case <-time.NewTimer(200 * time.Millisecond).C:
		t.Error("Backoff not called")
	}

	cancel()
	select {
	case actual := <-result:
		var expected []identityapi.RegisterEntityResponse = nil
		assert.Equal(t, expected, actual)
	case <-time.NewTimer(200 * time.Millisecond).C:
		t.Error("registerEntitiesWithRetry should stop")
	}
}

func TestWorker_registerEntitiesWithRetry_OnError_Discard(t *testing.T) {
	reqsToRegisterQueue := make(chan fwrequest.EntityFwRequest, 0)
	reqsRegisteredQueue := make(chan fwrequest.EntityFwRequest, 0)

	agentIdentity := func() entity.Identity {
		return entity.Identity{ID: 13}
	}

	client := &fakeClient{
		// 400 is an e.g. of an error for which we should discard data.
		err: identityapi.NewRegisterEntityError("err", 400, fmt.Errorf("err")),
	}

	backoff := backoff.NewDefaultBackoff()
	backoffCh := make(chan time.Duration)
	backoff.GetBackoffTimer = func(d time.Duration) *time.Timer {
		select {
		case backoffCh <- d:
		default:
		}
		return time.NewTimer(0)
	}

	config := WorkerConfig{
		MaxBatchSize:      1,
		MaxBatchSizeBytes: MB,
		MaxBatchDuration:  50 * time.Millisecond,
		MaxRetryBo:        0,
	}
	w := NewWorker(agentIdentity, client, backoff, reqsToRegisterQueue, reqsRegisteredQueue, config, instrumentation.NoopMeasure)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	response := make(chan []identityapi.RegisterEntityResponse, 1)
	go func() {
		response <- w.registerEntitiesWithRetry(ctx, []entity.Fields{{Name: "test"}})
	}()

	select {
	case actual := <-response:
		var expected []identityapi.RegisterEntityResponse = nil
		assert.Equal(t, expected, actual)
	case <-backoffCh:
		t.Error("backoff should not be called")
	case <-time.NewTimer(200 * time.Millisecond).C:
		t.Error("registerEntitiesWithRetry should stop")
	}
}

func TestWorker_registerEntitiesWithRetry_Success(t *testing.T) {
	reqsToRegisterQueue := make(chan fwrequest.EntityFwRequest, 0)
	reqsRegisteredQueue := make(chan fwrequest.EntityFwRequest, 0)

	agentIdentity := func() entity.Identity {
		return entity.Identity{ID: 13}
	}

	client := &fakeClient{
		ids: []entity.ID{13},
		// no err from backend.
		err: nil,
	}

	backoff := backoff.NewDefaultBackoff()
	backoffCh := make(chan time.Duration)
	backoff.GetBackoffTimer = func(d time.Duration) *time.Timer {
		select {
		case backoffCh <- d:
		default:
		}
		return time.NewTimer(0)
	}

	config := WorkerConfig{
		MaxBatchSize:      1,
		MaxBatchSizeBytes: MB,
		MaxBatchDuration:  50 * time.Millisecond,
		MaxRetryBo:        0,
	}
	w := NewWorker(agentIdentity, client, backoff, reqsToRegisterQueue, reqsRegisteredQueue, config, instrumentation.NoopMeasure)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	response := make(chan []identityapi.RegisterEntityResponse, 1)
	go func() {
		response <- w.registerEntitiesWithRetry(ctx, []entity.Fields{{Name: "test"}})
	}()
	select {
	case actual := <-response:
		assert.Equal(t, 1, len(actual))
		assert.Equal(t, "13", actual[0].ID.String())
	case <-backoffCh:
		t.Error("backoff should not be called")
	case <-time.NewTimer(200 * time.Millisecond).C:
		t.Error("registerEntitiesWithRetry should stop")
	}
}

func TestWorker_send_Logging_VerboseEnabled(t *testing.T) {
	expectedErrs := []string{
		"Invalid entityName",
	}
	expectedWarnings := []string{
		"Too many metadata, dropping ...",
		"Invalid metadata: ...",
	}

	// When the request is successful but some entities fail, we log only when verbose is enabled.
	reqsToRegisterQueue := make(chan fwrequest.EntityFwRequest, 0)
	reqsRegisteredQueue := make(chan fwrequest.EntityFwRequest, 0)

	agentIdentity := func() entity.Identity {
		return entity.Identity{ID: 12}
	}

	client := &fakeClient{
		ids: []entity.ID{13, 14},
		// no err from backend.
		err: nil,
	}

	config := WorkerConfig{
		VerboseLogLevel: 1,
	}
	w := NewWorker(agentIdentity, client, backoff.NewDefaultBackoff(), reqsToRegisterQueue, reqsRegisteredQueue, config, instrumentation.NoopMeasure)

	batch := map[entity.Key]fwrequest.EntityFwRequest{
		entity.Key("error"): {
			Data: protocol.Dataset{
				Entity: entity.Fields{Name: "error"},
			},
		},
		entity.Key("warnings"): {
			Data: protocol.Dataset{
				Entity: entity.Fields{Name: "warnings"},
			},
		},
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	go func() {
		for {
			select {
			case <-ctx.Done():
				return
			case <-reqsRegisteredQueue:
				// empty the registered queue to process new requests.
			case <-time.After(5 * time.Second):
				cancel()
				t.Error("Timeout while executing the test")
			}
		}
	}()

	hook := new(test.Hook)
	log.AddHook(hook)
	log.SetOutput(ioutil.Discard)

	batchSizeBytes := 10000
	w.send(ctx, batch, &batchSizeBytes)

	searchLogEntries := func(expectedMessages []string, level log.Level) (found bool) {
		for _, expectedMsg := range expectedMessages {
			for i, entry := range hook.AllEntries() {
				if entry.Level != level {
					continue
				}
				if val, ok := hook.AllEntries()[i].Data["error"]; ok {
					errStr := val.(error).Error()
					if errStr == expectedMsg {
						found = true
					}
				}
			}
		}
		return
	}

	assert.Eventually(t, func() bool {
		ok := searchLogEntries(expectedErrs, log.ErrorLevel)
		return ok
	}, time.Second, 10*time.Millisecond,
		"expected to find error messages: %s", expectedErrs)

	assert.Eventually(t, func() bool {
		ok := searchLogEntries(expectedWarnings, log.WarnLevel)
		return ok
	}, time.Second, 10*time.Millisecond,
		"expected to find warning messages: %s \n", expectedWarnings)
}

func TestWorker_send_Logging_VerboseDisabled(t *testing.T) {
	// When the request is successful but some entities fail, we log only when verbose is enabled.
	reqsToRegisterQueue := make(chan fwrequest.EntityFwRequest, 0)
	reqsRegisteredQueue := make(chan fwrequest.EntityFwRequest, 0)

	agentIdentity := func() entity.Identity {
		return entity.Identity{ID: 12}
	}

	client := &fakeClient{
		ids: []entity.ID{13, 14},
		// no err from backend.
		err: nil,
	}

	config := WorkerConfig{
		VerboseLogLevel: 0,
	}
	w := NewWorker(agentIdentity, client, backoff.NewDefaultBackoff(), reqsToRegisterQueue, reqsRegisteredQueue, config, instrumentation.NoopMeasure)

	batch := map[entity.Key]fwrequest.EntityFwRequest{
		entity.Key("error"): {
			Data: protocol.Dataset{
				Entity: entity.Fields{Name: "error"},
			},
		},
		entity.Key("warnings"): {
			Data: protocol.Dataset{
				Entity: entity.Fields{Name: "warnings"},
			},
		},
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	go func() {
		for {
			select {
			case <-ctx.Done():
				return
			case <-reqsRegisteredQueue:
				// empty the registered queue to process new requests.
			case <-time.After(5 * time.Second):
				cancel()
				t.Error("Timeout while executing the test")
			}
		}
	}()

	hook := new(test.Hook)
	log.AddHook(hook)
	log.SetOutput(ioutil.Discard)

	batchSizeBytes := 10000
	w.send(ctx, batch, &batchSizeBytes)

	assert.Empty(t, hook.AllEntries())
}

repository: greenszpila/infrastructure-agent

repo_name: greenszpila/infrastructure-agent


text:
package terminal

import (
	"fmt"
	"io"
	"sort"
	"time"

	"gonum.org/v1/gonum/stat"

	outputs "gitlab.com/devopsworks/tools/dw-query-digest/outputs"
)

// Display show report in the terminal
func Display(servermeta outputs.ServerInfo, s outputs.QueryStatsSlice, w io.Writer) {
	fmt.Fprintf(w, "\n# Server Info\n\n")
	fmt.Fprintf(w, "  Binary             : %s\n", servermeta.Binary)
	fmt.Fprintf(w, "  VersionShort       : %s\n", servermeta.VersionShort)
	fmt.Fprintf(w, "  Version            : %s\n", servermeta.Version)
	fmt.Fprintf(w, "  VersionDescription : %s\n", servermeta.VersionDescription)
	fmt.Fprintf(w, "  TCPPort            : %d\n", servermeta.TCPPort)
	fmt.Fprintf(w, "  UnixSocket         : %s\n", servermeta.UnixSocket)

	fmt.Fprintf(w, "\n# Internal Analyzer Statistics\n\n")
	// fmt.Fprintf(w, "  Start     : %s\n", servermeta.AnalysisStart)
	fmt.Fprintf(w, "  Duration  : %14.3fs\n", servermeta.AnalysisDuration)
	fmt.Fprintf(w, "  Log lines : %14.3fM (%d)\n", float64(servermeta.CumLines)/1000000.0, servermeta.CumLines)
	fmt.Fprintf(w, "  Lines/s   : %14.3f\n", servermeta.AnalysedLinesPerSecond)
	fmt.Fprintf(w, "  Bytes/s   : %14.3f\n", servermeta.AnalysedBytesPerSecond)
	fmt.Fprintf(w, "  Queries/s : %14.3f\n", servermeta.AnalysedQueriesPerSecond)

	fmt.Fprintf(w, "\n# Global Statistics\n\n")
	fmt.Fprintf(w, "  Total queries      : %.3fM (%d)\n", float64(servermeta.QueryCount)/1000000.0, servermeta.QueryCount)
	fmt.Fprintf(w, "  Total bytes        : %.3fM (%d)\n", float64(servermeta.CumBytes)/1000000.0, servermeta.CumBytes)
	fmt.Fprintf(w, "  Total fingerprints : %d\n", servermeta.UniqueQueries)
	fmt.Fprintf(w, "  Capture start      : %s\n", servermeta.Start)
	fmt.Fprintf(w, "  Capture end        : %s\n", servermeta.End)
	fmt.Fprintf(w, "  Duration           : %s (%d s)\n", servermeta.End.Sub(servermeta.Start), servermeta.End.Sub(servermeta.Start)/time.Second)
	fmt.Fprintf(w, "  QPS                : %.0f\n", float64(time.Second)*(float64(servermeta.QueryCount)/float64(servermeta.End.Sub(servermeta.Start))))

	fmt.Fprintf(w, "\n# Queries\n")

	ffactor := 100.0 * float64(time.Second) / float64(servermeta.End.Sub(servermeta.Start))
	for idx, val := range s {
		val.Concurrency = val.CumQueryTime * ffactor
		sort.Float64s(val.QueryTime)
		fmt.Fprintf(w, "\n# Query #%d: %x\n\n", idx+1, val.Hash[0:5])
		fmt.Fprintf(w, "  Fingerprint     : %s\n", val.FingerPrint)
		fmt.Fprintf(w, "  Schema          : %s\n", val.Schema)
		fmt.Fprintf(w, "  Calls           : %d\n", val.Count)
		fmt.Fprintf(w, "  CumErrored      : %d\n", val.CumErrored)
		fmt.Fprintf(w, "  CumKilled       : %d\n", val.CumKilled)
		fmt.Fprintf(w, "  CumQueryTime    : %s\n", fsecsToDuration(val.CumQueryTime))
		fmt.Fprintf(w, "  CumLockTime     : %s\n", fsecsToDuration(val.CumLockTime))
		fmt.Fprintf(w, "  CumRowsSent     : %d\n", val.CumRowsSent)
		fmt.Fprintf(w, "  CumRowsExamined : %d\n", val.CumRowsExamined)
		fmt.Fprintf(w, "  CumRowsAffected : %d\n", val.CumRowsAffected)
		fmt.Fprintf(w, "  CumBytesSent    : %d\n", val.CumBytesSent)
		fmt.Fprintf(w, "  Concurrency     : %2.2f%%\n", val.Concurrency)
		fmt.Fprintf(w, "  min / max time  : %s / %s\n", fsecsToDuration(val.QueryTime[0]), fsecsToDuration(val.QueryTime[len(val.QueryTime)-1]))
		fmt.Fprintf(w, "  mean time       : %s\n", fsecsToDuration(stat.Mean(val.QueryTime, nil)))
		fmt.Fprintf(w, "  p50 time        : %s\n", fsecsToDuration(stat.Quantile(0.5, 1, val.QueryTime, nil)))
		fmt.Fprintf(w, "  p95 time        : %s\n", fsecsToDuration(stat.Quantile(0.95, 1, val.QueryTime, nil)))
		fmt.Fprintf(w, "  stddev time     : %s\n", fsecsToDuration(stat.StdDev(val.QueryTime, nil)))
		// fmt.Fprintf(w, "\tmax time        : %.2f\n", stat.Max(0.95, 1, val.QueryTime, nil))

	}

}

// fsecsToDuration converts float seconds to time.Duration
// Since we have float64 seconds durations
// We first convert to µs (* 1e6) then to duration
func fsecsToDuration(d float64) time.Duration {
	return time.Duration(d*1e6) * time.Microsecond
}

func init() {
	outputs.Add("terminal", Display)
}

repository: devops-works/dw-query-digest

repo_name: devops-works/dw-query-digest