Permalink
Browse files

go rewrite

  • Loading branch information...
1 parent 547dd93 commit 8f92d79b23c075ed0c4e1fb8428d6e1d5fb6a15e ♠ ace hacker committed Jan 3, 2013
View
1 .godir
@@ -0,0 +1 @@
+l2met
View
17 Gemfile
@@ -1,17 +0,0 @@
-source :rubygems
-
-gem 'mongrel', '1.2.0.pre2'
-gem 'sinatra'
-gem 'excon'
-gem 'rack-ssl-enforcer'
-gem 'rack-cors'
-gem 'yajl-ruby'
-gem 'atomic'
-gem 'librato-metrics'
-gem 'redis'
-gem 'aws-sdk'
-gem 'scrolls', git: 'https://github.com/asenchi/scrolls.git'
-gem 'sinatra-google-auth'
-gem 'lock-smith', '0.0.8'
-gem 'turn'
-gem 'minitest'
View
@@ -1,100 +0,0 @@
-GIT
- remote: https://github.com/asenchi/scrolls.git
- revision: 4cdffb5833177d406858df2b9aecf2323347463d
- specs:
- scrolls (0.2.5.pre)
-
-GEM
- remote: http://rubygems.org/
- specs:
- aggregate (0.2.2)
- ansi (1.4.3)
- atomic (1.0.1)
- aws-sdk (1.7.1)
- httparty (~> 0.7)
- json (~> 1.4)
- nokogiri (>= 1.4.4)
- uuidtools (~> 2.1)
- daemons (1.0.10)
- excon (0.16.7)
- faraday (0.8.4)
- multipart-post (~> 1.1)
- gem_plugin (0.2.3)
- hashie (1.2.0)
- httparty (0.9.0)
- multi_json (~> 1.0)
- multi_xml
- json (1.7.5)
- librato-metrics (0.7.4)
- aggregate (~> 0.2.2)
- faraday (~> 0.7)
- multi_json
- lock-smith (0.0.8)
- minitest (4.2.0)
- mongrel (1.2.0.pre2)
- daemons (~> 1.0.10)
- gem_plugin (~> 0.2.3)
- multi_json (1.3.7)
- multi_xml (0.5.1)
- multipart-post (1.1.5)
- nokogiri (1.5.5)
- oa-core (0.3.2)
- oa-openid (0.3.2)
- oa-core (= 0.3.2)
- rack-openid (~> 1.3.1)
- ruby-openid-apps-discovery (~> 1.2.0)
- omniauth (1.1.1)
- hashie (~> 1.2)
- rack
- omniauth-google-apps (0.0.2)
- omniauth (~> 1.0)
- omniauth-openid (~> 1.0)
- ruby-openid-apps-discovery (~> 1.2.0)
- omniauth-openid (1.0.1)
- omniauth (~> 1.0)
- rack-openid (~> 1.3.1)
- rack (1.4.1)
- rack-cors (0.2.7)
- rack
- rack-openid (1.3.1)
- rack (>= 1.1.0)
- ruby-openid (>= 2.1.8)
- rack-protection (1.2.0)
- rack
- rack-ssl-enforcer (0.2.4)
- redis (3.0.2)
- ruby-openid (2.2.2)
- ruby-openid-apps-discovery (1.2.0)
- ruby-openid (>= 2.1.7)
- sinatra (1.3.3)
- rack (~> 1.3, >= 1.3.6)
- rack-protection (~> 1.2)
- tilt (~> 1.3, >= 1.3.3)
- sinatra-google-auth (1.0.0)
- oa-openid
- omniauth-google-apps
- tilt (1.3.3)
- turn (0.9.6)
- ansi
- uuidtools (2.1.3)
- yajl-ruby (1.1.0)
-
-PLATFORMS
- ruby
-
-DEPENDENCIES
- atomic
- aws-sdk
- excon
- librato-metrics
- lock-smith (= 0.0.8)
- minitest
- mongrel (= 1.2.0.pre2)
- rack-cors
- rack-ssl-enforcer
- redis
- scrolls!
- sinatra
- sinatra-google-auth
- turn
- yajl-ruby
View
@@ -1,2 +1,2 @@
-web: bundle exec bin/web
-outlet: bundle exec bin/dboutlet
+web: l2met
+librato: librato
View
@@ -1,8 +0,0 @@
-#!/usr/bin/env ruby
-$:.unshift(File.expand_path(File.join(File.dirname($0), '../lib')))
-require 'l2met/register'
-require 'l2met/outlet'
-$stdout.sync = true
-Thread.abort_on_exception = true
-L2met::Register.start
-L2met::Outlet.start
View
10 bin/web
@@ -1,10 +0,0 @@
-#!/usr/bin/env ruby
-$:.unshift(File.expand_path(File.join(File.dirname($0), '../lib')))
-require 'l2met/heartbeat'
-require 'l2met/register'
-require 'l2met/web'
-$stdout.sync = true
-Thread.abort_on_exception = true
-L2met::Heartbeat.start
-L2met::Register.start
-L2met::Web.start
View
@@ -0,0 +1,50 @@
+package db
+
+import (
+ "database/sql"
+ "fmt"
+ "github.com/bmizerany/pq"
+ "os"
+ "sync"
+)
+
+var (
+ PGLocker sync.Mutex
+ PGRLocker sync.Mutex
+ PG *sql.DB
+ PGR *sql.DB
+)
+
+func init() {
+ url := os.Getenv("DATABASE_URL")
+ if len(url) == 0 {
+ fmt.Printf("at=error error=\"must set DATABASE_URL\"\n")
+ os.Exit(1)
+ }
+ str, err := pq.ParseURL(url)
+ if err != nil {
+ fmt.Printf("at=error error=\"unable to parse DATABASE_URL\"\n")
+ os.Exit(1)
+ }
+ PG, err = sql.Open("postgres", str)
+ if err != nil {
+ fmt.Printf("at=error error=%s\n", err)
+ os.Exit(1)
+ }
+
+ rurl := os.Getenv("DATABASE_READ_URL")
+ if len(url) == 0 {
+ fmt.Printf("at=error error=\"must set DATABASE_READ_URL\"\n")
+ os.Exit(1)
+ }
+ rstr, err := pq.ParseURL(rurl)
+ if err != nil {
+ fmt.Printf("at=error error=\"unable to parse DATABASE_READ_URL\"\n")
+ os.Exit(1)
+ }
+ PGR, err = sql.Open("postgres", rstr)
+ if err != nil {
+ fmt.Printf("at=error error=%s\n", err)
+ os.Exit(1)
+ }
+}
View
@@ -0,0 +1,58 @@
+package encoding
+
+import (
+ "regexp"
+ "strconv"
+ "strings"
+)
+
+var (
+ msgPat = regexp.MustCompile(`( *)([a-zA-Z0-9\_\-\.]+)=?(([a-zA-Z0-9\.\-\_\.]+)|("([^\"]+)"))?`)
+)
+
+func ParseMsgData(msg []byte) (map[string]string, error) {
+ d := make(map[string]string)
+ pairs := msgPat.FindAllStringSubmatch(string(msg), -1)
+ for i := range pairs {
+ k := pairs[i][2]
+ v := pairs[i][4]
+ v2 := pairs[i][6]
+ if len(v) == 0 {
+ v = v2
+ }
+ d[k] = v
+ }
+ return d, nil
+}
+
+func DecodeArray(b []byte, dest *[]float64) {
+ // pq returns something like: {1.0, 2.0}
+ // let us remove the { and the }
+ trimed := b[1:(len(b) - 1)]
+ // Assuming the numbers are seperated by commas.
+ numbers := strings.Split(string(trimed), ",")
+ // Showing that we can do cool things with floats.
+ for _, x := range numbers {
+ f, err := strconv.ParseFloat(x, 64)
+ if err == nil {
+ *dest = append(*dest, f)
+ }
+ }
+}
+
+func EncodeArray(arr []float64) []byte {
+ // Convert our array into a string that looks like: {1,2,3.0}
+ var d []byte
+ d = append(d, '{')
+ for i, f := range arr {
+ st := strconv.FormatFloat(f, 'f', 5, 64)
+ for _, s := range st {
+ d = append(d, byte(s))
+ }
+ if i != (len(arr) - 1) {
+ d = append(d, ',')
+ }
+ }
+ d = append(d, '}')
+ return d
+}
View
@@ -1,30 +0,0 @@
-module L2met
- module Config
- extend self
-
- def env(key)
- ENV[key]
- end
-
- def env!(key)
- env(key) || raise("missing #{key}")
- end
-
- def env?(key)
- !env(key).nil?
- end
-
- def app_name; env!("APP_NAME"); end
- def app_url; env!("APP_URL"); end
- def port; env!("PORT").to_i; end
- def l2met_consumer; env!("L2MET_CONSUMER"); end
- def aws_id; env!("AWS_ID"); end
- def aws_secret; env!("AWS_SECRET"); end
- def num_outlets; env!("NUM_OUTLETS").to_i; end
- def outlet_delay; env!("OUTLET_DELAY").to_i; end
- def redis_url; env!("REDISGREEN_URL"); end
- def m2pg_url; env("M2PG_URL"); end
- def enable_m2pg?; env("ENABLE_M2PG") || false; end
- def enable_librato?; env("ENABLE_LIBRATO") || false; end
- end
-end
View
@@ -1,38 +0,0 @@
-require 'l2met/db'
-require 'securerandom'
-
-module L2met
- module Consumer
- extend self
-
- def all(user)
- DB["consumers"].select.select do |item|
- item.attributes["created_by"] == user
- end.map {|i| api_vals(i.attributes)}
- end
-
- def put(user, data)
- if data[:id].to_s.length.zero?
- data[:id] = SecureRandom.uuid
- end
- d = {id: data[:id],
- email: data[:email],
- token: data[:token],
- created_by: user}
- c = DB["consumers"].create(d)
- api_vals(c.attributes.to_h)
- end
-
- def get(id)
- c = DB["consumers"].at(id)
- api_vals(c.attributes.to_h)
- end
-
- def api_vals(data)
- {id: data['id'],
- drain_url: [Config.app_url, 'consumers', data['id'], 'logs'].join('/'),
- email: data["email"],
- token: data["token"]}
- end
- end
-end
View
@@ -1,62 +0,0 @@
-require 'thread'
-require 'aws/dynamo_db'
-require 'scrolls'
-
-require 'l2met/config'
-require 'l2met/heartbeat'
-require 'l2met/utils'
-
-module L2met
- module DB
- extend self
- @dynamo_lock = Mutex.new
- @table_lock = Mutex.new
-
- def metrics(name, from, to)
- f = Utils.trunc_time(Time.at(Integer(from)))
- t = Utils.trunc_time(Time.at(Integer(to)))
- result = {}
- while f < t
- q = {hash_value: f += 60, range_begins_with: name}
- items = DB["#{Config.app_name}.chart-data"].query(q)
- data = items.map(&:attributes).map(&:to_h)
- data.each do |metric|
- n, t = metric.delete('name'), metric.delete('time')
- metric.each do |stat, val|
- result[[n, stat].join('.')] ||= []
- result[[n, stat].join('.')] << {x: Integer(t), y: Float(val)}
- end
- end
- end
- result.reduce([]) do |memo, metric|
- memo << {name: metric.first, data: metric.last}
- end
- end
-
- def [](table)
- @table_lock.synchronize do
- tables[table].items
- end
- end
-
- private
-
- def tables
- @tables ||= dynamo.tables.
- map {|t| t.load_schema}.
- reduce({}) {|h, t| h[t.name] = t; h}
- end
-
- def dynamo
- @dynamo_lock.synchronize do
- @db ||= AWS::DynamoDB.new(access_key_id: Config.aws_id,
- secret_access_key: Config.aws_secret)
- end
- end
-
- def log(data, &blk)
- Scrolls.log({ns: "db"}.merge(data), &blk)
- end
-
- end
-end
Oops, something went wrong.

0 comments on commit 8f92d79

Please sign in to comment.