PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/app/main.rb

https://bitbucket.org/sulab/dashboard
Ruby | 233 lines | 105 code | 32 blank | 96 comment | 1 complexity | 278982423ab8ba16b082d27b71b7a486 MD5 | raw file
  1. require 'rubygems'
  2. require 'sinatra'
  3. require 'dm-core'
  4. require 'dm-aggregates'
  5. require 'dm-serializer'
  6. require 'yaml'
  7. require 'slim'
  8. require 'json'
  9. require 'date'
  10. require 'hipchat'
  11. require 'awesome_print'
  12. require 'active_support'
  13. ## CONFIGURATION
  14. $yaml = YAML.load_file("settings.yaml")
  15. configure do
  16. set :port, 6699
  17. set :public_folder, '../public'
  18. DataMapper.setup(:default, {
  19. :adapter => 'mysql',
  20. :host => $yaml["mysql"]["host"],
  21. :username => $yaml["mysql"]["username"],
  22. :password => $yaml["mysql"]["password"],
  23. :database => $yaml["mysql"]["database"]
  24. })
  25. # DataMapper::Logger.new(STDOUT, :debug)
  26. end
  27. ### MODELS
  28. class Log
  29. include DataMapper::Resource
  30. property :id, Serial
  31. property :application,Integer
  32. property :version, String, :length => 12
  33. property :status, Integer
  34. property :header, String, :length => 255
  35. property :body, Text
  36. property :timestamp, DateTime
  37. end
  38. class Commit
  39. include DataMapper::Resource
  40. property :id, Serial
  41. property :author, String, :length => 255
  42. property :repo, String, :length => 255
  43. property :message, Text
  44. property :timestamp, DateTime
  45. # property :hash, String, :length => 40
  46. end
  47. DataMapper.finalize
  48. ### CONTROLLER ACTIONS
  49. before do
  50. end
  51. not_found do
  52. slim:'404'
  53. end
  54. get '/' do
  55. yaml.to_s
  56. # slim:'index'
  57. end
  58. # Remote code status report submissions
  59. post '/submit' do
  60. Log.create( :application => params['application'],
  61. :version => params['version'],
  62. :status => params['status'],
  63. :header => params['page_title'],
  64. :body => params['message'],
  65. :timestamp => DateTime.now )
  66. end
  67. get '/gen_report' do
  68. content_type :json
  69. report_generation.to_json
  70. end
  71. get '/weekly_report' do
  72. report = weekly_report[:pygenewiki]
  73. rate = (( report[:warning] + report[:error] ) / report[:total].to_f * 100 ).round(4)
  74. msg = "A total of #{ report[:total] } pages were updated. With a error percentage of #{ rate }% where #{ report[:warning] } give us some notification warning and where #{ report[:error] } threw a complete error."
  75. if rate < 1.0 then
  76. hipchat_sender(msg, 'green')
  77. else
  78. hipchat_sender(msg, 'red')
  79. end
  80. end
  81. private
  82. def weekly_report(weeks=1)
  83. now = DateTime.now
  84. week_ago = now - (7*weeks)
  85. {
  86. :pygenewiki => {
  87. :total => Log.all(:application => 0, :timestamp.gte => week_ago ).count,
  88. :success => Log.all(:application => 0, :status => 0, :timestamp.gte => week_ago ).count,
  89. :warning => Log.all(:application => 0, :status => 1, :timestamp.gte => week_ago ).count,
  90. :error => Log.all(:application => 0, :status => 2, :timestamp.gte => week_ago ).count,
  91. :log => Log.all(:application => 0, :status.not => 0, :order => [:timestamp.desc], :limit => 40, :timestamp.gte => week_ago) }
  92. }
  93. end
  94. def report_generation
  95. {
  96. :pygenewiki => {
  97. :total => Log.all(:application => 0).count,
  98. :success => Log.all(:application => 0, :status => 0).count,
  99. :warning => Log.all(:application => 0, :status => 1).count,
  100. :error => Log.all(:application => 0, :status => 2).count,
  101. # :log => Log.all(:application => 0, :status.not => 0, :order => [:timestamp.desc], :limit => 40) }
  102. :log => Log.all(:application => 0, :order => [:timestamp.desc], :limit => 4000) }
  103. }
  104. end
  105. def get_rand
  106. o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten;
  107. string = (0..50).map{ o[rand(o.length)] }.join;
  108. Digest::MD5.hexdigest(DateTime.now.to_s+string)
  109. end
  110. def hipchat_sender(msg, color=green)
  111. client = HipChat::Client.new( $yaml["etc"]["hipchat_api"] )
  112. client[ $yaml["etc"]["hipchat_room"] ].send('Dashy', msg, :color => color)
  113. end
  114. # //Cron Jobs
  115. # public function _writecommits()
  116. # {
  117. # $crud_model = new Model_Crud();
  118. # //Get list of all public sulab repos
  119. # $request = Request::factory("https://api.bitbucket.org/1.0/users/sulab/")->method('GET')->execute();
  120. # $response = json_decode( $request->body() );
  121. # //krumo($response->repositories);
  122. # if(isset($response)) {
  123. # foreach($response->repositories as $repo):
  124. # //For each of the repos, get the latest commits
  125. # $request = Request::factory("https://api.bitbucket.org/1.0/repositories/sulab/".$repo->slug."/changesets")->method('GET')->execute();
  126. # $response = json_decode( $request->body() );
  127. # if(isset($response)) {
  128. # foreach($response->changesets as $commit):
  129. # //krumo($commit);
  130. # $crud_model->insertNewest("commits", array(
  131. # "author" => $commit->author,
  132. # "repo" => $repo->slug,
  133. # "message" => $commit->message,
  134. # "timestamp" => $commit->utctimestamp,
  135. # "hash" => $commit->raw_node ), "hash" );
  136. # endforeach;
  137. # }
  138. # endforeach;
  139. # }
  140. # }
  141. # public function _writeTwitterMembers()
  142. # {
  143. # $crud_model = new Model_Crud();
  144. # $lab_members = Kohana::$config->load('site')->get('twitter_accounts');
  145. # foreach($lab_members as $twitter_account):
  146. # $request = Request::factory("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=".$twitter_account)->method('GET')->execute();
  147. # //krumo($request->body());
  148. # $response = json_decode( $request->body() );
  149. # if(isset($response)) {
  150. # foreach($response as $status_update):
  151. # $post = array();
  152. # $post["status_id"] = $status_update->id_str;
  153. # $datetime = new DateTime($status_update->timestamp);
  154. # $datetime->setTimezone(new DateTimeZone('PST'));
  155. # $post["timestamp"] = $datetime->format('Y-m-d H:i:s');
  156. # $post["message"] = $status_update->text;
  157. # $post["person"] = $status_update->user->name;
  158. # $post["picture"] = $status_update->user->profile_image_url;
  159. # $crud_model->insertNewest("lab_twitter", $post, "status_id");
  160. # endforeach;
  161. # }
  162. # endforeach;
  163. # }
  164. # public function action_update()
  165. # {
  166. # $this->_writecommits();
  167. # $this->_writeTwitterMembers();
  168. # $this->action_submit();
  169. # }
  170. # //Getter methods
  171. # private function _commits()
  172. # {
  173. # //$site_passphrase = Kohana::$config->load('site')->get('passphrase');
  174. # //$bit_bucket_users = Kohana::$config->load('site')->get('bitbucket_users');
  175. # $crud_model = new Model_Crud();
  176. # return $crud_model->getRecentCommits("commits", 6);
  177. # }
  178. # private function _pygenewiki()
  179. # {
  180. # $crud_model = new Model_Crud();
  181. # return $crud_model->getRecent("pygenewiki", 6);
  182. # }
  183. # private function _lab_twitter()
  184. # {
  185. # $crud_model = new Model_Crud();
  186. # return $crud_model->getRecent("lab_twitter", 6, "timestamp");
  187. # }
  188. # private function _at_lab_twitter()
  189. # {
  190. # $crud_model = new Model_Crud();
  191. # return $crud_model->getRecent("at_lab_twitter", 6, "id");
  192. # }
  193. # public function action_poll()
  194. # {
  195. # $results = array();
  196. # $results["bitbucket"] = $this->_commits();
  197. # $results["pygenewiki"] = $this->_pygenewiki();
  198. # $results["lab_twitter"] = $this->_lab_twitter();
  199. # $results["at_lab_twitter"] = $this->_at_lab_twitter();
  200. # $this->response->headers('content-type', 'application/json')->body(json_encode($results));
  201. # }