Copyright 2017, OpenCensus Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

package view

import (
	
	
	
	

	
	
	
)

type command interface {
	handleCommand(w *worker)
}
getViewByNameReq is the command to get a view given its name.
type getViewByNameReq struct {
	name string
	c    chan *getViewByNameResp
}

type getViewByNameResp struct {
	v *View
}

func ( *getViewByNameReq) ( *worker) {
	 := .views[.name]
	if  == nil {
		.c <- &getViewByNameResp{nil}
		return
	}
	.c <- &getViewByNameResp{.view}
}
registerViewReq is the command to register a view.
type registerViewReq struct {
	views []*View
	err   chan error
}

func ( *registerViewReq) ( *worker) {
	for ,  := range .views {
		if  := .canonicalize();  != nil {
			.err <- 
			return
		}
	}
	var  []string
	for ,  := range .views {
		,  := .tryRegisterView()
		if  != nil {
			 = append(, fmt.Sprintf("%s: %v", .Name, ))
			continue
		}
		internal.SubscriptionReporter(.Measure.Name())
		.subscribe()
	}
	if len() > 0 {
		.err <- errors.New(strings.Join(, "\n"))
	} else {
		.err <- nil
	}
}
unregisterFromViewReq is the command to unregister to a view. Has no impact on the data collection for client that are pulling data from the library.
type unregisterFromViewReq struct {
	views []string
	done  chan struct{}
}

func ( *unregisterFromViewReq) ( *worker) {
	for ,  := range .views {
		,  := .views[]
		if ! {
			continue
		}
Report pending data for this view before removing it.
		.reportView()

		.unsubscribe()
this was the last subscription and view is not collecting anymore. The collected data can be cleared.
			.clearRows()
		}
		.unregisterView()
	}
	.done <- struct{}{}
}
retrieveDataReq is the command to retrieve data for a view.
type retrieveDataReq struct {
	now time.Time
	v   string
	c   chan *retrieveDataResp
}

type retrieveDataResp struct {
	rows []*Row
	err  error
}

func ( *retrieveDataReq) ( *worker) {
	.mu.Lock()
	defer .mu.Unlock()
	,  := .views[.v]
	if ! {
		.c <- &retrieveDataResp{
			nil,
			fmt.Errorf("cannot retrieve data; view %q is not registered", .v),
		}
		return
	}

	if !.isSubscribed() {
		.c <- &retrieveDataResp{
			nil,
			fmt.Errorf("cannot retrieve data; view %q has no subscriptions or collection is not forcibly started", .v),
		}
		return
	}
	.c <- &retrieveDataResp{
		.collectedRows(),
		nil,
	}
}
recordReq is the command to record data related to multiple measures at once.
type recordReq struct {
	tm          *tag.Map
	ms          []stats.Measurement
	attachments map[string]interface{}
	t           time.Time
}

func ( *recordReq) ( *worker) {
	.mu.Lock()
	defer .mu.Unlock()
	for ,  := range .ms {
		if ( == stats.Measurement{}) { // not registered
			continue
		}
		 := .getMeasureRef(.Measure().Name())
		for  := range .views {
			.addSample(.tm, .Value(), .attachments, .t)
		}
	}
}
setReportingPeriodReq is the command to modify the duration between reporting the collected data to the registered clients.
type setReportingPeriodReq struct {
	d time.Duration
	c chan bool
}

func ( *setReportingPeriodReq) ( *worker) {
	.timer.Stop()
	if .d <= 0 {
		.timer = time.NewTicker(defaultReportingDuration)
	} else {
		.timer = time.NewTicker(.d)
	}
	.c <- true