{"action":"created","comment":{"url":"https://api.github.com/repos/alphagov/paas-billing/pulls/comments/197082654","pull_request_review_id":130753342,"id":197082654,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDE5NzA4MjY1NA==","diff_hunk":"@@ -0,0 +1,204 @@\n+package cfstore\n+\n+import (\n+\t\"context\"\n+\t\"database/sql\"\n+\t\"time\"\n+\n+\t\"code.cloudfoundry.org/lager\"\n+\tcfclient \"github.com/cloudfoundry-community/go-cfclient\"\n+)\n+\n+const (\n+\tDefaultInitTimeout = 5 * time.Minute\n+)\n+\n+type Config struct {\n+\t// CFClient config\n+\tClientConfig *cfclient.Config\n+\t// Client for communicating with cf\n+\tClient CFDataClient\n+\t// Database connection\n+\tDB *sql.DB\n+\t// Logger overrides the default logger\n+\tLogger lager.Logger\n+\t// Collection delay\n+\tSchedule time.Duration\n+}\n+\n+type Store struct {\n+\tclient CFDataClient\n+\tdb     *sql.DB\n+\tlogger lager.Logger\n+}\n+\n+func (s *Store) Init() error {\n+\ts.logger.Info(\"initializing\")\n+\tctx, cancel := context.WithTimeout(context.Background(), DefaultInitTimeout)\n+\tdefer cancel()\n+\ttx, err := s.db.BeginTx(ctx, nil)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tdefer tx.Rollback()\n+\tif err := s.collectServices(tx); err != nil {\n+\t\treturn err\n+\t}\n+\tif err := s.collectServicePlans(tx); err != nil {\n+\t\treturn err\n+\t}\n+\ts.logger.Info(\"initialized\")\n+\treturn tx.Commit()\n+}\n+\n+func (s *Store) CollectServicePlans() error {\n+\tctx, cancel := context.WithTimeout(context.Background(), DefaultInitTimeout)\n+\tdefer cancel()\n+\ttx, err := s.db.BeginTx(ctx, nil)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tdefer tx.Rollback()\n+\tif err := s.collectServicePlans(tx); err != nil {\n+\t\treturn err\n+\t}\n+\treturn tx.Commit()\n+}\n+\n+func (s *Store) collectServicePlans(tx *sql.Tx) error {\n+\tplans, err := s.client.ListServicePlans()\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tfor _, plan := range plans {\n+\t\tvalidFrom := plan.UpdatedAt\n+\t\tvar planCount int\n+\t\terr := tx.QueryRow(`\n+\t\t\tselect count(*)\n+\t\t\tfrom service_plans\n+\t\t\twhere guid = $1\n+\t\t`, plan.Guid).Scan(&planCount)\n+\t\tif err != nil {\n+\t\t\treturn err\n+\t\t}\n+\t\tif planCount == 0 {\n+\t\t\tvalidFrom = plan.CreatedAt\n+\t\t}\n+\n+\t\tvar serviceValidFrom *time.Time\n+\t\terr = tx.QueryRow(`\n+\t\t\tselect valid_from\n+\t\t\tfrom services\n+\t\t\twhere guid = $1\n+\t\t\torder by valid_from desc\n+\t\t\tlimit 1\n+\t\t`, plan.ServiceGuid).Scan(&serviceValidFrom)\n+\t\tif err == sql.ErrNoRows {\n+\t\t\tpanic(\"meh dunno what to do\")","path":"cfstore/cfstore.go","position":97,"original_position":97,"commit_id":"7363ace474b9d95dd95066cdb77e4a70265fb007","original_commit_id":"7363ace474b9d95dd95066cdb77e4a70265fb007","user":{"login":"paroxp","id":2418945,"node_id":"MDQ6VXNlcjI0MTg5NDU=","avatar_url":"https://avatars3.githubusercontent.com/u/2418945?v=4","gravatar_id":"","url":"https://api.github.com/users/paroxp","html_url":"https://github.com/paroxp","followers_url":"https://api.github.com/users/paroxp/followers","following_url":"https://api.github.com/users/paroxp/following{/other_user}","gists_url":"https://api.github.com/users/paroxp/gists{/gist_id}","starred_url":"https://api.github.com/users/paroxp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paroxp/subscriptions","organizations_url":"https://api.github.com/users/paroxp/orgs","repos_url":"https://api.github.com/users/paroxp/repos","events_url":"https://api.github.com/users/paroxp/events{/privacy}","received_events_url":"https://api.github.com/users/paroxp/received_events","type":"User","site_admin":false},"body":"Do we want to panic here? Can we not:\r\n\r\n```\r\nif err != nil {\r\n  return err\r\n}\r\n```\r\n\r\nfor the error to be handled properly later on and not kill the application?","created_at":"2018-06-21T10:20:13Z","updated_at":"2018-06-21T10:43:51Z","html_url":"https://github.com/alphagov/paas-billing/pull/36#discussion_r197082654","pull_request_url":"https://api.github.com/repos/alphagov/paas-billing/pulls/36","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/alphagov/paas-billing/pulls/comments/197082654"},"html":{"href":"https://github.com/alphagov/paas-billing/pull/36#discussion_r197082654"},"pull_request":{"href":"https://api.github.com/repos/alphagov/paas-billing/pulls/36"}}},"pull_request":{"url":"https://api.github.com/repos/alphagov/paas-billing/pulls/36","id":195099190,"node_id":"MDExOlB1bGxSZXF1ZXN0MTk1MDk5MTkw","html_url":"https://github.com/alphagov/paas-billing/pull/36","diff_url":"https://github.com/alphagov/paas-billing/pull/36.diff","patch_url":"https://github.com/alphagov/paas-billing/pull/36.patch","issue_url":"https://api.github.com/repos/alphagov/paas-billing/issues/36","number":36,"state":"open","locked":false,"title":"[#157438891] Historical service data","user":{"login":"LeePorte","id":922815,"node_id":"MDQ6VXNlcjkyMjgxNQ==","avatar_url":"https://avatars0.githubusercontent.com/u/922815?v=4","gravatar_id":"","url":"https://api.github.com/users/LeePorte","html_url":"https://github.com/LeePorte","followers_url":"https://api.github.com/users/LeePorte/followers","following_url":"https://api.github.com/users/LeePorte/following{/other_user}","gists_url":"https://api.github.com/users/LeePorte/gists{/gist_id}","starred_url":"https://api.github.com/users/LeePorte/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LeePorte/subscriptions","organizations_url":"https://api.github.com/users/LeePorte/orgs","repos_url":"https://api.github.com/users/LeePorte/repos","events_url":"https://api.github.com/users/LeePorte/events{/privacy}","received_events_url":"https://api.github.com/users/LeePorte/received_events","type":"User","site_admin":false},"body":"What\r\n----\r\n\r\nThis commit adds the new `cfstore` package to the billing app. The\r\npurpose of this package is to enable the collection of service and\r\nservice plan data from Cloud Foundry and storing this data within the\r\nbilling database.\r\n\r\nThe consumers of this are the pricing calculator and the billing app\r\nitself. Due to the names of the services and plans being used\r\nas a method of identifying which plan to use, this was clearly a fragile\r\napproach and when a plan was renamed the result would be the GUID being\r\ndisplayed which has very little meaning to our users.\r\n\r\nIn order to aid local testing two make targets have been added, these\r\nstart the docker postgres container and clean up the resulting\r\ncontainer.\r\n\r\nHow to review\r\n-----\r\n\r\n- Code review\r\n- See in action\r\n\r\nWho can review\r\n-----\r\n\r\nNeither @chrisfarms nor @LeePorte \r\n","created_at":"2018-06-15T07:42:10Z","updated_at":"2018-06-21T10:43:51Z","closed_at":null,"merged_at":null,"merge_commit_sha":"63e0d28bbf5140f7debdfd34858ee871b51d433c","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/alphagov/paas-billing/pulls/36/commits","review_comments_url":"https://api.github.com/repos/alphagov/paas-billing/pulls/36/comments","review_comment_url":"https://api.github.com/repos/alphagov/paas-billing/pulls/comments{/number}","comments_url":"https://api.github.com/repos/alphagov/paas-billing/issues/36/comments","statuses_url":"https://api.github.com/repos/alphagov/paas-billing/statuses/7363ace474b9d95dd95066cdb77e4a70265fb007","head":{"label":"alphagov:historical_service_data-157438891","ref":"historical_service_data-157438891","sha":"7363ace474b9d95dd95066cdb77e4a70265fb007","user":{"login":"alphagov","id":596977,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU5Njk3Nw==","avatar_url":"https://avatars0.githubusercontent.com/u/596977?v=4","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"repo":{"id":106835027,"node_id":"MDEwOlJlcG9zaXRvcnkxMDY4MzUwMjc=","name":"paas-billing","full_name":"alphagov/paas-billing","owner":{"login":"alphagov","id":596977,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU5Njk3Nw==","avatar_url":"https://avatars0.githubusercontent.com/u/596977?v=4","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/alphagov/paas-billing","description":"A Go app for polling the CF API for usage events","fork":false,"url":"https://api.github.com/repos/alphagov/paas-billing","forks_url":"https://api.github.com/repos/alphagov/paas-billing/forks","keys_url":"https://api.github.com/repos/alphagov/paas-billing/keys{/key_id}","collaborators_url":"https://api.github.com/repos/alphagov/paas-billing/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/alphagov/paas-billing/teams","hooks_url":"https://api.github.com/repos/alphagov/paas-billing/hooks","issue_events_url":"https://api.github.com/repos/alphagov/paas-billing/issues/events{/number}","events_url":"https://api.github.com/repos/alphagov/paas-billing/events","assignees_url":"https://api.github.com/repos/alphagov/paas-billing/assignees{/user}","branches_url":"https://api.github.com/repos/alphagov/paas-billing/branches{/branch}","tags_url":"https://api.github.com/repos/alphagov/paas-billing/tags","blobs_url":"https://api.github.com/repos/alphagov/paas-billing/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/alphagov/paas-billing/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/alphagov/paas-billing/git/refs{/sha}","trees_url":"https://api.github.com/repos/alphagov/paas-billing/git/trees{/sha}","statuses_url":"https://api.github.com/repos/alphagov/paas-billing/statuses/{sha}","languages_url":"https://api.github.com/repos/alphagov/paas-billing/languages","stargazers_url":"https://api.github.com/repos/alphagov/paas-billing/stargazers","contributors_url":"https://api.github.com/repos/alphagov/paas-billing/contributors","subscribers_url":"https://api.github.com/repos/alphagov/paas-billing/subscribers","subscription_url":"https://api.github.com/repos/alphagov/paas-billing/subscription","commits_url":"https://api.github.com/repos/alphagov/paas-billing/commits{/sha}","git_commits_url":"https://api.github.com/repos/alphagov/paas-billing/git/commits{/sha}","comments_url":"https://api.github.com/repos/alphagov/paas-billing/comments{/number}","issue_comment_url":"https://api.github.com/repos/alphagov/paas-billing/issues/comments{/number}","contents_url":"https://api.github.com/repos/alphagov/paas-billing/contents/{+path}","compare_url":"https://api.github.com/repos/alphagov/paas-billing/compare/{base}...{head}","merges_url":"https://api.github.com/repos/alphagov/paas-billing/merges","archive_url":"https://api.github.com/repos/alphagov/paas-billing/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/alphagov/paas-billing/downloads","issues_url":"https://api.github.com/repos/alphagov/paas-billing/issues{/number}","pulls_url":"https://api.github.com/repos/alphagov/paas-billing/pulls{/number}","milestones_url":"https://api.github.com/repos/alphagov/paas-billing/milestones{/number}","notifications_url":"https://api.github.com/repos/alphagov/paas-billing/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/alphagov/paas-billing/labels{/name}","releases_url":"https://api.github.com/repos/alphagov/paas-billing/releases{/id}","deployments_url":"https://api.github.com/repos/alphagov/paas-billing/deployments","created_at":"2017-10-13T14:31:13Z","updated_at":"2018-06-19T08:28:16Z","pushed_at":"2018-06-21T10:06:35Z","git_url":"git://github.com/alphagov/paas-billing.git","ssh_url":"git@github.com:alphagov/paas-billing.git","clone_url":"https://github.com/alphagov/paas-billing.git","svn_url":"https://github.com/alphagov/paas-billing","homepage":null,"size":12383,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":4,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":0,"open_issues":4,"watchers":0,"default_branch":"master"}},"base":{"label":"alphagov:master","ref":"master","sha":"6a8e94f78d286cc253eb2e1676b302e86da3e1b1","user":{"login":"alphagov","id":596977,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU5Njk3Nw==","avatar_url":"https://avatars0.githubusercontent.com/u/596977?v=4","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"repo":{"id":106835027,"node_id":"MDEwOlJlcG9zaXRvcnkxMDY4MzUwMjc=","name":"paas-billing","full_name":"alphagov/paas-billing","owner":{"login":"alphagov","id":596977,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU5Njk3Nw==","avatar_url":"https://avatars0.githubusercontent.com/u/596977?v=4","gravatar_id":"","url":"https://api.github.com/users/alphagov","html_url":"https://github.com/alphagov","followers_url":"https://api.github.com/users/alphagov/followers","following_url":"https://api.github.com/users/alphagov/following{/other_user}","gists_url":"https://api.github.com/users/alphagov/gists{/gist_id}","starred_url":"https://api.github.com/users/alphagov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alphagov/subscriptions","organizations_url":"https://api.github.com/users/alphagov/orgs","repos_url":"https://api.github.com/users/alphagov/repos","events_url":"https://api.github.com/users/alphagov/events{/privacy}","received_events_url":"https://api.github.com/users/alphagov/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/alphagov/paas-billing","description":"A Go app for polling the CF API for usage events","fork":false,"url":"https://api.github.com/repos/alphagov/paas-billing","forks_url":"https://api.github.com/repos/alphagov/paas-billing/forks","keys_url":"https://api.github.com/repos/alphagov/paas-billing/keys{/key_id}","collaborators_url":"https://api.github.com/repos/alphagov/paas-billing/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/alphagov/paas-billing/teams","hooks_url":"https://api.github.com/repos/alphagov/paas-billing/hooks","issue_events_url":"https://api.github.com/repos/alphagov/paas-billing/issues/events{/number}","events_url":"https://api.github.com/repos/alphagov/paas-billing/events","assignees_url":"https://api.github.com/repos/alphagov/paas-billing/assignees{/user}","branches_url":"https://api.github.com/repos/alphagov/paas-billing/branches{/branch}","tags_url":"https://api.github.com/repos/alphagov/paas-billing/tags","blobs_url":"https://api.github.com/repos/alphagov/paas-billing/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/alphagov/paas-billing/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/alphagov/paas-billing/git/refs{/sha}","trees_url":"https://api.github.com/repos/alphagov/paas-billing/git/trees{/sha}","statuses_url":"https://api.github.com/repos/alphagov/paas-billing/statuses/{sha}","languages_url":"https://api.github.com/repos/alphagov/paas-billing/languages","stargazers_url":"https://api.github.com/repos/alphagov/paas-billing/stargazers","contributors_url":"https://api.github.com/repos/alphagov/paas-billing/contributors","subscribers_url":"https://api.github.com/repos/alphagov/paas-billing/subscribers","subscription_url":"https://api.github.com/repos/alphagov/paas-billing/subscription","commits_url":"https://api.github.com/repos/alphagov/paas-billing/commits{/sha}","git_commits_url":"https://api.github.com/repos/alphagov/paas-billing/git/commits{/sha}","comments_url":"https://api.github.com/repos/alphagov/paas-billing/comments{/number}","issue_comment_url":"https://api.github.com/repos/alphagov/paas-billing/issues/comments{/number}","contents_url":"https://api.github.com/repos/alphagov/paas-billing/contents/{+path}","compare_url":"https://api.github.com/repos/alphagov/paas-billing/compare/{base}...{head}","merges_url":"https://api.github.com/repos/alphagov/paas-billing/merges","archive_url":"https://api.github.com/repos/alphagov/paas-billing/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/alphagov/paas-billing/downloads","issues_url":"https://api.github.com/repos/alphagov/paas-billing/issues{/number}","pulls_url":"https://api.github.com/repos/alphagov/paas-billing/pulls{/number}","milestones_url":"https://api.github.com/repos/alphagov/paas-billing/milestones{/number}","notifications_url":"https://api.github.com/repos/alphagov/paas-billing/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/alphagov/paas-billing/labels{/name}","releases_url":"https://api.github.com/repos/alphagov/paas-billing/releases{/id}","deployments_url":"https://api.github.com/repos/alphagov/paas-billing/deployments","created_at":"2017-10-13T14:31:13Z","updated_at":"2018-06-19T08:28:16Z","pushed_at":"2018-06-21T10:06:35Z","git_url":"git://github.com/alphagov/paas-billing.git","ssh_url":"git@github.com:alphagov/paas-billing.git","clone_url":"https://github.com/alphagov/paas-billing.git","svn_url":"https://github.com/alphagov/paas-billing","homepage":null,"size":12383,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":4,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":0,"open_issues":4,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/alphagov/paas-billing/pulls/36"},"html":{"href":"https://github.com/alphagov/paas-billing/pull/36"},"issue":{"href":"https://api.github.com/repos/alphagov/paas-billing/issues/36"},"comments":{"href":"https://api.github.com/repos/alphagov/paas-billing/issues/36/comments"},"review_comments":{"href":"https://api.github.com/repos/alphagov/paas-billing/pulls/36/comments"},"review_comment":{"href":"https://api.github.com/repos/alphagov/paas-billing/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/alphagov/paas-billing/pulls/36/commits"},"statuses":{"href":"https://api.github.com/repos/alphagov/paas-billing/statuses/7363ace474b9d95dd95066cdb77e4a70265fb007"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"paroxp"}}