Ejemplo n.º 1
0
// LogoutAll will not return any HTTP response, but will simply prepare a
// response for logging a user out completely from all providers. If a user
// should log out of just a specific OAuth2 provider, use the Logout method
// on the associated ProviderHandler.
func (g *ProviderGroup) LogoutAll(ctx context.Context,
	w webhelp.ResponseWriter) error {
	var errs errors.ErrorGroup
	for _, handler := range g.handlers {
		errs.Add(handler.Logout(ctx, w))
	}
	return errs.Finalize()
}
Ejemplo n.º 2
0
// Tokens will return a map of all the currently valid OAuth2 tokens
func (g *ProviderGroup) Tokens(ctx context.Context) (map[string]*oauth2.Token,
	error) {
	rv := make(map[string]*oauth2.Token)
	var errs errors.ErrorGroup
	for name, handler := range g.handlers {
		token, err := handler.Token(ctx)
		errs.Add(err)
		if err == nil && token != nil {
			rv[name] = token
		}
	}
	return rv, errs.Finalize()
}
Ejemplo n.º 3
0
func (d *Data) search(proj_id int64,
	scoreFunc func(sample_id int64) (score float64, err error)) (
	SearchResults, error) {

	var samples []Sample
	err := d.db.Where("project_id = ?", proj_id).Find(&samples).Error
	if err != nil {
		return nil, Err.Wrap(err)
	}

	var wg sync.WaitGroup
	var result_mtx sync.Mutex
	result := make(SearchResults, 0, len(samples))
	var result_errs errors.ErrorGroup
	samples_ch := make(chan Sample)

	wg.Add(*searchParallelism)
	for i := 0; i < *searchParallelism; i++ {
		go func() {
			defer wg.Done()
			for sample := range samples_ch {
				val, err := scoreFunc(sample.Id)
				result_mtx.Lock()
				result_errs.Add(err)
				if err == nil {
					result = append(result, SearchResult{Sample: sample, Score: val})
				}
				result_mtx.Unlock()
			}
		}()
	}

	for _, sample := range samples {
		samples_ch <- sample
	}
	close(samples_ch)
	wg.Wait()

	err = result_errs.Finalize()
	if err != nil {
		return nil, err
	}

	sort.Sort(result)
	return result, nil
}
Ejemplo n.º 4
0
func (d *Data) CreateDB() error {
	var errs errors.ErrorGroup
	tx := d.db.Begin()

	errs.Add(tx.Exec(`CREATE TABLE
	  api_keys (
  	  user_id character varying(255) NOT NULL,
  	  key character varying(255) NOT NULL
	  );`).Error)
	errs.Add(tx.Exec(`CREATE INDEX
	  idx_api_keys_user_id ON api_keys(user_id);`).Error)
	errs.Add(tx.Exec(`CREATE UNIQUE INDEX
	  idx_api_keys_key ON api_keys(key);`).Error)

	errs.Add(tx.Exec(`CREATE SEQUENCE projects_id_seq;`).Error)
	errs.Add(tx.Exec(`CREATE TABLE
    projects (
      id bigint NOT NULL DEFAULT nextval('projects_id_seq'),
      created_at timestamp with time zone NOT NULL,
      user_id character varying(255) NOT NULL,
      name character varying(255) NOT NULL,
      public boolean NOT NULL
    );`).Error)
	errs.Add(tx.Exec(`CREATE UNIQUE INDEX
	  idx_projects_user_id_name ON projects(user_id, name);`).Error)
	errs.Add(tx.Exec(`CREATE INDEX
	  idx_projects_public ON projects(public);`).Error)

	errs.Add(tx.Exec(`CREATE SEQUENCE dimensions_id_seq;`).Error)
	errs.Add(tx.Exec(`CREATE TABLE
    dimensions (
      id bigint NOT NULL DEFAULT nextval('dimensions_id_seq'),
      project_id bigint NOT NULL,
      name character varying(255) NOT NULL
    );`).Error)
	errs.Add(tx.Exec(`CREATE UNIQUE INDEX
	  idx_dimensions_project_id_name ON dimensions(project_id, name);`).Error)

	errs.Add(tx.Exec(`CREATE SEQUENCE samples_id_seq;`).Error)
	errs.Add(tx.Exec(`CREATE TABLE
    samples (
      id bigint NOT NULL DEFAULT nextval('samples_id_seq'),
      control_id bigint NOT NULL,
      created_at timestamp with time zone NOT NULL,
      project_id bigint NOT NULL,
      name character varying(255) NOT NULL
    );`).Error)
	errs.Add(tx.Exec(`CREATE UNIQUE INDEX
	  idx_samples_project_id_name ON samples(project_id, name);`).Error)

	errs.Add(tx.Exec(`CREATE TABLE
    sample_values (
      sample_id bigint NOT NULL,
      dimension_id bigint NOT NULL,

      rank integer NOT NULL,
      rank_diff integer NOT NULL,
      abs_rank_diff integer NOT NULL,

      value real NOT NULL,
      value_diff real NOT NULL,
      abs_value_diff real NOT NULL,

      primary key(sample_id, dimension_id)
    );`).Error)
	errs.Add(tx.Exec(`CREATE INDEX
	  idx_sample_values_sample_id_abs_rank_diff ON
	      sample_values(sample_id, abs_rank_diff);`).Error)
	errs.Add(tx.Exec(`CREATE INDEX
	  idx_sample_values_sample_id_rank_diff ON
	      sample_values(sample_id, rank_diff);`).Error)
	errs.Add(tx.Exec(`CREATE INDEX
	  idx_sample_values_sample_id_abs_value_diff ON
	      sample_values(sample_id, abs_value_diff);`).Error)

	errs.Add(tx.Exec(`CREATE SEQUENCE controls_id_seq;`).Error)
	errs.Add(tx.Exec(`CREATE TABLE
    controls (
      id bigint NOT NULL DEFAULT nextval('controls_id_seq'),
      created_at timestamp with time zone NOT NULL,
      project_id bigint NOT NULL,
      name character varying(255) NOT NULL
    );`).Error)
	errs.Add(tx.Exec(`CREATE UNIQUE INDEX
	  idx_controls_project_id_name ON controls(project_id, name);`).Error)

	errs.Add(tx.Exec(`CREATE TABLE
    control_values (
      control_id bigint NOT NULL,
      dimension_id bigint NOT NULL,
      rank integer NOT NULL,
      value real NOT NULL,
      primary key(control_id, dimension_id)
    );`).Error)
	errs.Add(tx.Exec(`CREATE INDEX
	  idx_control_values_control_id_rank ON
	      control_values(control_id, rank);`).Error)

	err := errs.Finalize()
	if err != nil {
		tx.Rollback()
		return err
	}

	tx.Commit()
	return nil
}