// WatchModelLives returns a StringsWatcher that notifies of changes
// to any model life values. The watcher will not send any more events
// for a model after it has been observed to be Dead.
965118
func (st *State) WatchModelLives() StringsWatcher {
	return newLifecycleWatcher(st, modelsC, nil, nil, nil)
}
1272640
func (st *State) WatchRemoteRelations() StringsWatcher {
	// Use a no-op transform func to record the known ids.
	known := make(map[interface{}]bool)
	tr := func(id string) string {
		known[id] = true
		return id
	}

	filter := func(id interface{}) bool {
		id, err := st.strictLocalID(id.(string))
		if err != nil {
			return false
		}

		// Gather the remote app names.
		remoteApps, closer := st.db().GetCollection(remoteApplicationsC)
		defer closer()

		type remoteAppDoc struct {
			Name string
		}
		remoteAppNameField := bson.D{{"name", 1}}
		var apps []remoteAppDoc
		err = remoteApps.Find(nil).Select(remoteAppNameField).All(&apps)
		if err != nil {
			watchLogger.Errorf("could not lookup remote application names: %v", err)
			return false
		}
		remoteAppNames := set.NewStrings()
		for _, a := range apps {
			remoteAppNames.Add(a.Name)
		}

		// Run a query to pickup any relations to those remote apps.
		relations, closer := st.db().GetCollection(relationsC)
		defer closer()

		query := bson.D{
			{"key", id},
			{"endpoints.applicationname", bson.D{{"$in", remoteAppNames.Values()}}},
		}
		num, err := relations.Find(query).Count()
		if err != nil {
			watchLogger.Errorf("could not lookup remote relations: %v", err)
			return false
		}
		// The relation (or remote app) may have been deleted, but if it has been
		// seen previously, return true.
		if num == 0 {
			_, seen := known[id]
			delete(known, id)
			return seen
		}
		return num > 0
	}
	return newRelationLifeSuspendedWatcher(st, nil, filter, tr)
}
377279
func (st *State) WatchModelMachines() StringsWatcher {
	members := bson.D{{"$or", []bson.D{
		{{"containertype", ""}},
		{{"containertype", bson.D{{"$exists", false}}}},
	}}}
	filter := func(id interface{}) bool {
		k, err := st.strictLocalID(id.(string))
		if err != nil {
			return false
		}
		return !strings.Contains(k, "/")
	}
	return newLifecycleWatcher(st, machinesC, members, filter, nil)
}
904210
func (v *ViewModel) Watch(expression string, callback func(newVal *js.Object)) (unwatcher func()) {
	obj := v.Call("$watch", expression, callback)
	return func() {
		obj.Invoke()
	}
}
1271405
func (m *Machine) WatchUnits() (watcher.StringsWatcher, error) {
	var results params.StringsWatchResults
	args := params.Entities{
		Entities: []params.Entity{{Tag: m.tag.String()}},
	}
	err := m.facade.FacadeCall("WatchUnits", args, &results)
	if err != nil {
		return nil, err
	}
	if len(results.Results) != 1 {
		return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results))
	}
	result := results.Results[0]
	if result.Error != nil {
		return nil, result.Error
	}
	w := apiwatcher.NewStringsWatcher(m.facade.RawAPICaller(), result)
	return w, nil
}
982488
func (c *Client) WatchAllModels() (*api.AllWatcher, error) {
	var info params.AllWatcherId
	if err := c.facade.FacadeCall("WatchAllModels", nil, &info); err != nil {
		return nil, err
	}
	return api.NewAllModelWatcher(c.facade.RawAPICaller(), &info.AllWatcherId), nil
}
1275909
func (a *Application) WatchUnits() StringsWatcher {
	members := bson.D{{"application", a.doc.Name}}
	prefix := a.doc.Name + "/"
	filter := func(unitDocID interface{}) bool {
		unitName, err := a.st.strictLocalID(unitDocID.(string))
		if err != nil {
			return false
		}
		return strings.HasPrefix(unitName, prefix)
	}
	return newLifecycleWatcher(a.st, unitsC, members, filter, nil)
}
1088517
func (e *ModelMachinesWatcher) WatchModelMachines() (params.StringsWatchResult, error) {
	result := params.StringsWatchResult{}
	if !e.authorizer.AuthController() {
		return result, ErrPerm
	}
	watch := e.st.WatchModelMachines()
	// Consume the initial event and forward it to the result.
	if changes, ok := <-watch.Changes(); ok {
		result.StringsWatcherId = e.resources.Register(watch)
		result.Changes = changes
	} else {
		err := watcher.EnsureErr(watch)
		return result, fmt.Errorf("cannot obtain initial model machines: %v", err)
	}
	return result, nil
}
410230
func Watch(facade base.FacadeCaller, method string, tag names.Tag) (watcher.NotifyWatcher, error) {
	var results params.NotifyWatchResults
	args := params.Entities{
		Entities: []params.Entity{{Tag: tag.String()}},
	}
	err := facade.FacadeCall(method, args, &results)
	if err != nil {
		return nil, err
	}
	if len(results.Results) != 1 {
		return nil, fmt.Errorf("expected 1 result, got %d", len(results.Results))
	}
	result := results.Results[0]
	if result.Error != nil {
		return nil, result.Error
	}
	return apiwatcher.NewNotifyWatcher(facade.RawAPICaller(), result), nil
}
215351
func (a API) WatchUnitAssignments() (watcher.StringsWatcher, error) {
	var result params.StringsWatchResult
	err := a.facade.FacadeCall("WatchUnitAssignments", nil, &result)
	if err != nil {
		return nil, err
	}
	if result.Error != nil {
		return nil, result.Error
	}
	w := apiwatcher.NewStringsWatcher(a.facade.RawAPICaller(), result)
	return w, nil
}
1167062
func (f *Facade) WatchUnits(args params.Entities) (params.StringsWatchResults, error) {
	results := params.StringsWatchResults{
		Results: make([]params.StringsWatchResult, len(args.Entities)),
	}
	for i, arg := range args.Entities {
		id, changes, err := f.watchUnits(arg.Tag)
		if err != nil {
			results.Results[i].Error = common.ServerError(err)
			continue
		}
		results.Results[i].StringsWatcherId = id
		results.Results[i].Changes = changes
	}
	return results, nil
}
783367
func (m *MockMachineProvisioner) WatchContainers(arg0 instance.ContainerType) (watcher.StringsWatcher, error) {
	ret := m.ctrl.Call(m, "WatchContainers", arg0)
	ret0, _ := ret[0].(watcher.StringsWatcher)
	ret1, _ := ret[1].(error)
	return ret0, ret1
}
1063007
func (c *Client) WatchExternalControllers() (watcher.StringsWatcher, error) {
	var results params.StringsWatchResults
	err := c.facade.FacadeCall("WatchExternalControllers", nil, &results)
	if err != nil {
		return nil, err
	}
	if n := len(results.Results); n != 1 {
		return nil, errors.Errorf("expected 1 result, got %d", n)
	}
	result := results.Results[0]
	if result.Error != nil {
		return nil, result.Error
	}
	w := apiwatcher.NewStringsWatcher(c.facade.RawAPICaller(), result)
	return w, nil
}
956526
func (fw Watchers) WatchMachineManagedFilesystems(m names.MachineTag) state.StringsWatcher {
	w := &hostFilesystemsWatcher{
		stringsWatcherBase:     stringsWatcherBase{out: make(chan []string)},
		backend:                fw.Backend,
		changes:                set.NewStrings(),
		hostFilesystems:        fw.Backend.WatchMachineFilesystems(m),
		modelFilesystems:       fw.Backend.WatchModelFilesystems(),
		modelVolumeAttachments: fw.Backend.WatchModelVolumeAttachments(),
		modelVolumesAttached:   names.NewSet(),
		modelVolumeFilesystems: make(map[names.VolumeTag]names.FilesystemTag),
		hostMatch: func(tag names.Tag) (bool, error) {
			return tag == m, nil
		},
	}
	w.tomb.Go(func() error {
		defer watcher.Stop(w.hostFilesystems, &w.tomb)
		defer watcher.Stop(w.modelFilesystems, &w.tomb)
		defer watcher.Stop(w.modelVolumeAttachments, &w.tomb)
		return w.loop()
	})
	return w
}
339584
func (m *Machine) WatchContainers() (watcher.StringsWatcher, error) {
	var result params.StringsWatchResult
	arg := params.Entity{Tag: m.tag.String()}
	err := m.facade.FacadeCall("WatchContainers", arg, &result)
	if err != nil {
		return nil, errors.Trace(err)
	}
	if result.Error != nil {
		return nil, result.Error
	}
	return apiwatcher.NewStringsWatcher(m.facade.RawAPICaller(), result), nil
}
281213
func (c *ControllerAPI) WatchAllModels() (params.AllWatcherId, error) {
	if err := c.checkHasAdmin(); err != nil {
		return params.AllWatcherId{}, errors.Trace(err)
	}
	w := c.state.WatchAllModels(c.statePool)
	return params.AllWatcherId{
		AllWatcherId: c.resources.Register(w),
	}, nil
}
1054840
func (fw Watchers) WatchUnitManagedFilesystems(app names.ApplicationTag) state.StringsWatcher {
	w := &hostFilesystemsWatcher{
		stringsWatcherBase:     stringsWatcherBase{out: make(chan []string)},
		backend:                fw.Backend,
		changes:                set.NewStrings(),
		hostFilesystems:        fw.Backend.WatchUnitFilesystems(app),
		modelFilesystems:       fw.Backend.WatchModelFilesystems(),
		modelVolumeAttachments: fw.Backend.WatchModelVolumeAttachments(),
		modelVolumesAttached:   names.NewSet(),
		modelVolumeFilesystems: make(map[names.VolumeTag]names.FilesystemTag),
		hostMatch: func(tag names.Tag) (bool, error) {
			entity, err := names.UnitApplication(tag.Id())
			if err != nil {
				return false, errors.Trace(err)
			}
			return app.Id() == entity, nil
		},
	}
	w.tomb.Go(func() error {
		defer watcher.Stop(w.hostFilesystems, &w.tomb)
		defer watcher.Stop(w.modelFilesystems, &w.tomb)
		defer watcher.Stop(w.modelVolumeAttachments, &w.tomb)
		return w.loop()
	})
	return w
}