database

package
v0.0.0-...-42c6bbb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2020 License: GPL-2.0, GPL-3.0 Imports: 18 Imported by: 6

README

database

Contains database handlers, defines structs for storing data in the database, and functions for tracking entities that interact with the platform (users, companies, national/sub-national actors, etc.).

Folder structure

  • cities.go: Contains functions to create, save, or retrieve cities from db
  • companies.go: Contains functions to create, save, or retrieve companies from db
  • countries.go: Contains functions to create, save, or retrieve countries from db
  • database_test.go: Tests the creation and retrieval of users.
  • db.go: defines boltDB buckets and functions to handle DB
  • landing.go:
  • populate.go: Populates the local test database with static test data.
  • static.go:
  • regions.go: Contains functions to create, save, or retrieve regions from db
  • users.go:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AssetBucket = []byte("Assets")
View Source
var CityBucket = []byte("Cities")
View Source
var CompanyBucket = []byte("Companies")
View Source
var CountryBucket = []byte("Countries")
View Source
var CountryIds map[int]string
View Source
var OversightBucket = []byte("Oversight")
View Source
var PledgeBucket = []byte("Pledges")
View Source
var RegionBucket = []byte("Regions")
View Source
var RequestBucket = []byte("Requests")
View Source
var StateBucket = []byte("States")
View Source
var USStateCities map[string][]string
View Source
var USStates []string
View Source
var UserBucket = []byte("Users")

Functions

func CreateHomeDir

func CreateHomeDir() error

CreateHomeDir creates a home directory

func DeleteKeyFromBucket

func DeleteKeyFromBucket(key int, bucketName []byte) error

DeleteKeyFromBucket deletes a given key from the bucket bucketName but doesn not shift indices of elements succeeding the deleted element's index

func FlushDB

func FlushDB() error

func InitUSStates

func InitUSStates() map[int]string

func NewRequest

func NewRequest(request ConnectRequest) error

func OpenDB

func OpenDB() (*bolt.DB, error)

OpenDB opens the db

func Populate

func Populate()

func Save

func Save(dir string, bucketName []byte, x BucketItem) error

func UpdateAsset

func UpdateAsset(key int, info Asset) error

func UpdatePledge

func UpdatePledge(key int, updated Pledge) error

Types

type Actor

type Actor interface {
	GetPledges() ([]Pledge, error)
	AddPledges(pledgeIDs ...int) error
	UpdateMRV(MRV string)
	GetID() int
}

func RetrieveActor

func RetrieveActor(actorType string, actorID int) (Actor, error)

Given the type of actor (company, city, state, region, country, etc.) and the ID of the actor, return the entity (all actor types implement the Actor interface, so the function returns the interface).

type Asset

type Asset struct {
	Index      int
	Title      string
	Subtitle   string
	CompanyID  int
	Name       string
	Location   string
	State      string
	Type       string
	ActionType []string
	Capacity   float64

	Reports []ReportsByYear
}

func NewAsset

func NewAsset(name string, companyID int, location string, state string, type_ string) (Asset, error)

func RetrieveAllAssets

func RetrieveAllAssets() ([]Asset, error)

RetrieveAllAssets gets a list of all assets in the database

func RetrieveAsset

func RetrieveAsset(key int) (Asset, error)

Given a key of type int, retrieves the corresponding asset object from the database assets bucket.

func RetrieveAssetByName

func RetrieveAssetByName(name string, companyID int) (Asset, error)

Given a name and company, retrieves the corresponding asset object from the database assets bucket.

func (*Asset) GetID

func (x *Asset) GetID() int

func (*Asset) ReportAssetData

func (a *Asset) ReportAssetData(year int, gwh int, mitOut int) error

func (*Asset) Save

func (x *Asset) Save() error

Puts asset object in assets bucket. Called by NewAsset

func (*Asset) SetID

func (x *Asset) SetID(id int)

type BucketItem

type BucketItem interface {
	SetID(id int)
	GetID() int
	Save() error
}

type Certificate

type Certificate struct {
	ID            int
	CertificateID int
	Type          string
	Unit          string
	Status        string
}

type City

type City struct {

	// Identifying info
	Index   int
	Name    string
	Region  string
	Country string

	// Contextual data
	Area        float64
	Iso         string
	Population  int
	Latitude    float64
	Longitude   float64
	Revenue     float64
	CompanySize int
	HQ          string

	MRV string

	Children []string

	Pledges []int

	Emissions  map[string]string // accept whatever emissions the frontend passes
	Mitigation map[string]string
	Adaptation map[string]string

	LastUpdated string
}

Our definition of "City" includes cities, municipalities, towns, shires, villages, communes, etc. The following struct defines the relevant fields.

func NewCity

func NewCity(name string, region string, country string) (City, error)

Function that creates a new city object given its name, region, and country and saves the object in the countries bucket.

func RetrieveAllCities

func RetrieveAllCities() ([]City, error)

Retrieves all countries from the countries bucket.

func RetrieveCity

func RetrieveCity(key int) (City, error)

Given a key of type int, retrieves the corresponding city object from the database cities bucket.

func RetrieveCityByName

func RetrieveCityByName(name string, region string) (City, error)

Given a name and region, retrieves the corresponding city object from the database cities bucket.

func SearchCity

func SearchCity(name string) ([]City, error)

func (*City) AddPledges

func (c *City) AddPledges(pledgeIDs ...int) error

func (*City) GetID

func (x *City) GetID() int

func (City) GetPledges

func (c City) GetPledges() ([]Pledge, error)

func (*City) Save

func (x *City) Save() error

Saves city object in cities bucket. Called by NewCity

func (*City) SetID

func (x *City) SetID(id int)

func (*City) UpdateMRV

func (c *City) UpdateMRV(MRV string)

type ClimateReport

type ClimateReport struct {
	ID       int
	Name     string
	Scope    string
	Date     string
	Verified bool
	// contains filtered or unexported fields
}

type Company

type Company struct {

	// Identifying info
	Index       int
	Name        string `json:"shortname"`
	FullName    string `json:"name"`
	Description string `json:"description"`

	Locations      []Location           `json:"locations"`
	Accountability []DistributionRecord `json:"accountability"`

	Country string
	Address string

	UserIDs []int

	// Contextual data
	Area       float64
	Iso        string
	Population int
	// Latitude    float64
	// Longitude   float64
	Revenue     float64
	CompanySize int
	HQ          string

	MultiNational []string // an array of all the countries a company is in; if not an MNC, leave empty
	ForProfit     bool
	Industry      bool

	MRV string // the company's chosen MRV reporting methodology

	Pledges []int

	// Nested scopes
	States    []int
	Regions   []int
	Countries []int

	// The entity IDs of all the company's physical assets
	Assets []int

	// IDs of all the company's financial/regulatory assets (e.g. RECs, climate bonds, etc.)
	Certificates []Certificate

	ClimateReports []ClimateReport

	LastUpdated string
	Files       []string // list of ipfs hashes to be stored for verification or something similar
}

Our definition of "Company" includes .... The following struct defines the relevant fields.

func NewCompany

func NewCompany(name string, country string) (Company, error)

Function that creates a new company object given its name and country and saves the object in the countries bucket.

func RetrieveAllCompanies

func RetrieveAllCompanies() ([]Company, error)

RetrieveAllCompanies gets a list of all companies in the database

func RetrieveAllMultiNationals

func RetrieveAllMultiNationals() ([]Company, error)

func RetrieveCompany

func RetrieveCompany(key int) (Company, error)

Given a key of type int, retrieves the corresponding company object from the database companies bucket.

func RetrieveCompanyByName

func RetrieveCompanyByName(name string, country string) (Company, error)

Given a name and country, retrieves the corresponding company object from the database companies bucket.

func SearchCompany

func SearchCompany(name string) ([]Company, error)

func (*Company) AddAssets

func (c *Company) AddAssets(assetIDs ...int) error

func (*Company) AddCountries

func (c *Company) AddCountries(countryIDs ...int) error

func (*Company) AddPledges

func (c *Company) AddPledges(pledgeIDs ...int) error

func (*Company) AddRegions

func (c *Company) AddRegions(regionIDs ...int) error

func (*Company) AddStates

func (c *Company) AddStates(stateIDs ...int) error

func (*Company) GetAssetsByState

func (c *Company) GetAssetsByState(state string) ([]Asset, error)

func (*Company) GetCountries

func (c *Company) GetCountries() ([]Country, error)

func (*Company) GetID

func (x *Company) GetID() int
BucketItem interface method:

A getter method for structs that implement the BucketItem interface. The method allows you to retrieve the ID of structs that implement the BucketItem interface methods, even if you don't know specifically which struct you are workign with.

func (Company) GetPledges

func (c Company) GetPledges() ([]Pledge, error)

func (*Company) GetRegions

func (c *Company) GetRegions() ([]Region, error)

func (*Company) GetStates

func (c *Company) GetStates() ([]State, error)

func (*Company) Save

func (x *Company) Save() error

Saves company object in companies bucket. Called by NewCompany

func (*Company) SetID

func (x *Company) SetID(id int)
BucketItem interface method:

SetID() is a common method between all structs that qualify as bucket items that allow them to implement the BucketItem interface. SetID() is a simple setter method that allows the updating of the bucket item's ID. The function's only use should be in the Save() function; otherwise, the ID should not be modified.

func (*Company) UpdateMRV

func (c *Company) UpdateMRV(MRV string)

Actor Interface method:

Allows for the updating of the chosen reporting methodology for any of the climate actor types that implement the Actor interface.

type ConnectRequest

type ConnectRequest struct {
	Index   int
	DBName  string
	OrgName string

	DBActorTypes  []string // what type of actors does the DB cover?
	DBActionTypes []string // what type of actions does the DB track?

	ContactInfo string
	Links       []string // links with more info
}

func RetrieveAllRequests

func RetrieveAllRequests() ([]ConnectRequest, error)

func (*ConnectRequest) GetID

func (x *ConnectRequest) GetID() int

func (*ConnectRequest) Save

func (x *ConnectRequest) Save() error

func (*ConnectRequest) SetID

func (x *ConnectRequest) SetID(id int)

type Country

type Country struct {

	// Identifying info
	Index       int
	Name        string `json:"name"`
	FullName    string `json:"full_name"`
	Description string `json:"description"`

	// Contextual data
	Area        float64
	Iso         string
	Population  int
	Latitude    float64
	Longitude   float64
	Revenue     float64
	CompanySize int
	HQ          string

	MRV string

	// For countries: children = regions
	Children       []string
	Credits        []int
	Pledges        []int                `json:"pledges"`
	Accountability []DistributionRecord `json:"accountability"`

	Emissions  map[string]string `json:"emissions"` // accept whatever emissions data the frontend passes
	Mitigation map[string]string
	Adaptation map[string]string

	LastUpdated string
	Files       []string // an a rray of all the necessary documents to validate this specific country
}

Nation-states, countries

func NewCountry

func NewCountry(name string) (Country, error)

Function that creates a new country object given its name and saves the object in the countries bucket.

func RetrieveAllCountries

func RetrieveAllCountries() ([]Country, error)

Retrieves all countries from the countries bucket.

func RetrieveCountry

func RetrieveCountry(key int) (Country, error)

Given a key of type int, retrieves the corresponding country object from the database countries bucket.

func RetrieveCountryByName

func RetrieveCountryByName(name string) (Country, error)

Given the name of the country, retrieves the corresponding country object from the database countries bucket.

func (*Country) AddPledges

func (c *Country) AddPledges(pledgeIDs ...int) error

func (*Country) GetID

func (x *Country) GetID() int

func (Country) GetPledges

func (c Country) GetPledges() ([]Pledge, error)

func (*Country) Save

func (x *Country) Save() error

Saves country object in countries bucket. Called by NewCountry

func (*Country) SetID

func (x *Country) SetID(id int)

func (*Country) UpdateMRV

func (c *Country) UpdateMRV(MRV string)

type DistributionRecord

type DistributionRecord struct {
	Title  string
	Weight int
}

type EthWallet

type EthWallet struct {
	EncryptedPrivateKey string
	PublicKey           string
	Address             string
}

EthWallet contains the structures needed for an ethereum wallet

type Location

type Location struct {
	Name            string
	Latitude        string
	Longitude       string
	NationStaeID    int
	RegionalStateID int
}

type Oversight

type Oversight struct {
	Index int
	Name  string

	UserIDs []int

	// options:
	// - "international organization"
	// - "UNFCCC Body"
	// - "Civil Society"
	// - "Financial Institution"
	// - "Research Organization"
	// - "Independent Reviewer"
	// - "Oracle System"
	OrgType string

	MRV string

	Scope  string // where does the actor operate?
	Weight int    // their weight, based on the organization's reputation

}

func NewOsOrg

func NewOsOrg(name string) (Oversight, error)

func RetrieveAllOsOrgs

func RetrieveAllOsOrgs() ([]Oversight, error)

func RetrieveOsOrg

func RetrieveOsOrg(key int) (Oversight, error)

func RetrieveOsOrgByName

func RetrieveOsOrgByName(name string) (Oversight, error)

func (*Oversight) AddPledges

func (os *Oversight) AddPledges(pledgeIDs ...int) error

func (*Oversight) GetID

func (x *Oversight) GetID() int

func (Oversight) GetPledges

func (os Oversight) GetPledges() ([]Pledge, error)

func (*Oversight) Save

func (x *Oversight) Save() error

func (*Oversight) SetID

func (x *Oversight) SetID(id int)

func (*Oversight) UpdateMRV

func (c *Oversight) UpdateMRV(MRV string)

type Pledge

type Pledge struct {
	ID        int
	ActorType string
	ActorID   int
	Coop      bool

	/*
		Pledges can be:
		emissions reductions,
		mitigation actions (energy efficiency, renewables, etc.),
		or adaptation actions
	*/
	PledgeType string

	BaseYear float64

	TargetYear float64

	Goal float64

	// is this goal determined by a regulator, or voluntarily
	// adopted by the climate actor?
	Regulatory bool
}

func NewPledge

func NewPledge(pledgeType string, baseYear float64, targetYear float64, goal float64,
	regulatory bool, actorType string, actorID int) (Pledge, error)

func RetrieveAllPledges

func RetrieveAllPledges() ([]Pledge, error)

func RetrievePledge

func RetrievePledge(key int) (Pledge, error)

func (*Pledge) GetID

func (x *Pledge) GetID() int

func (*Pledge) Save

func (x *Pledge) Save() error

func (*Pledge) SetID

func (x *Pledge) SetID(id int)

type Region

type Region struct {

	// Identifying info
	Index   int
	Name    string
	Country string

	// Contextual data
	Area        float64
	Iso         string
	Population  int
	Latitude    float64
	Longitude   float64
	Revenue     float64
	CompanySize int
	HQ          string

	MRV string

	Pledges []int

	//	For regions: children = companies (divided by region)
	Children []string

	Emissions  map[string]string // accept whatever emissions the frontend passes
	Mitigation map[string]string
	Adaptation map[string]string

	LastUpdated string
}

Our definition of "Region" includes regions, areas, etc. The following struct defines the relevant fields.

func NewRegion

func NewRegion(name string, country string) (Region, error)

Function that creates a new region object given its name and country and saves the object in the regions bucket.

func RetrieveAllRegions

func RetrieveAllRegions() ([]Region, error)

Retrieves all regions from the regions bucket.

func RetrieveRegion

func RetrieveRegion(key int) (Region, error)

Given a key of type int, retrieves the corresponding region object from the database regions bucket.

func RetrieveRegionByName

func RetrieveRegionByName(name string, country string) (Region, error)

Given the name and country of the region, retrieves the corresponding region object from the database regions bucket.

func SearchRegion

func SearchRegion(name string) ([]Region, error)

func (*Region) AddPledges

func (c *Region) AddPledges(pledgeIDs ...int) error

func (*Region) GetID

func (x *Region) GetID() int

func (Region) GetPledges

func (c Region) GetPledges() ([]Pledge, error)

func (*Region) Save

func (x *Region) Save() error

Saves region object in regions bucket. Called by NewRegion

func (*Region) SetID

func (x *Region) SetID(id int)

func (*Region) UpdateMRV

func (c *Region) UpdateMRV(MRV string)

type ReportsByYear

type ReportsByYear struct {
	Year               int
	GWh                int
	MitigationOutcomes int
}

type State

type State struct {

	// Identifying info
	Index   int
	Name    string
	Country string

	// Contextual data
	Area        float64
	Iso         string
	Population  int
	Latitude    float64
	Longitude   float64
	Revenue     float64
	CompanySize int
	HQ          string

	MRV string

	Pledges []int

	//	For states: children = companies (divided by state)
	Children []string

	Emissions  map[string]string // accept whatever emissions the frontend passes
	Mitigation map[string]string
	Adaptation map[string]string

	LastUpdated string
	Files       []string
}

Our definition of "State" includes states, provinces, prefectures, etc. The following struct defines the relevant fields.

func FilterStatesByCountry

func FilterStatesByCountry(country string) ([]State, error)

Retrieves and filters state by country.

func NewState

func NewState(name string, country string) (State, error)

Function that creates a new state object given its name and country and saves the object in the states bucket.

func RetrieveAllStates

func RetrieveAllStates() ([]State, error)

Retrieves all states from the states bucket.

func RetrieveState

func RetrieveState(key int) (State, error)

Given a key of type int, retrieves the corresponding state object from the database states bucket.

func RetrieveStateByName

func RetrieveStateByName(name string, country string) (State, error)

Given the name and country of the state, retrieves the corresponding state object from the database states bucket.

func SearchState

func SearchState(name string) ([]State, error)

func (*State) AddPledges

func (c *State) AddPledges(pledgeIDs ...int) error

func (*State) GetID

func (x *State) GetID() int

func (State) GetPledges

func (c State) GetPledges() ([]Pledge, error)

func (*State) Save

func (x *State) Save() error

Saves state object in states bucket. Called by NewState

func (*State) SetID

func (x *State) SetID(id int)

func (*State) UpdateMRV

func (c *State) UpdateMRV(MRV string)

type User

type User struct {
	Index int

	FirstName string
	LastName  string
	Username  string
	Email     string
	Pwhash    string
	EIN       string

	AccessToken string // the access token used to authenticate with the platform for future requests

	EntityType string // choices are: individual, company, city, state, region, country, oversight
	EntityID   int    // index of the entity the user is associated with
	Verified   bool   // if the user is a verified member of the entity they purport to be a part of
	Admin      bool   // is the user an admin for its entity?

	EthereumWallet EthWallet
	Liked          []string // array of liked projects
	NotVisible     []string // array of visible projects

}

func NewUser

func NewUser(username string, pwhash string, email string, entityType string, entityName string, entityParent string) (User, error)

NewUser creates a new user

func RetrieveAllUsers

func RetrieveAllUsers() ([]User, error)

func RetrieveUser

func RetrieveUser(key int) (User, error)

RetrieveUser retrieves a particular User indexed by key from the database

func RetrieveUserByUsername

func RetrieveUserByUsername(username string) (User, error)

func ValidateAccessToken

func ValidateAccessToken(username string, accessToken string) (User, error)

func ValidateUser

func ValidateUser(username string, pwhash string) (User, error)

ValidateUser validates a particular user

func (*User) AddPledge

func (u *User) AddPledge(pledge Pledge)

Empty function, simply allows User to match "Actor" interface methods

func (*User) GenAccessToken

func (a *User) GenAccessToken() (string, error)

func (*User) GenEthKeys

func (a *User) GenEthKeys(seedpwd string) error

func (*User) GetID

func (x *User) GetID() int

func (*User) RetrieveUserEntity

func (user *User) RetrieveUserEntity() (Actor, error)

func (*User) Save

func (x *User) Save() error

Save inserts a passed User object into the database

func (*User) SendEthereumTx

func (a *User) SendEthereumTx(address string, amount big.Int) (string, error)

func (*User) SetID

func (x *User) SetID(id int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL