├── LICENSE ├── PITCHME.md ├── PITCHME.yaml ├── README.md ├── assets ├── css │ └── PITCHME.css ├── down-arrow.png └── img │ ├── logo.png │ └── pro-code-step-and-zoom.gif └── src ├── elixir └── monitor.ex ├── go └── server.go ├── javascript └── config.js └── python └── time.py /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /PITCHME.md: -------------------------------------------------------------------------------- 1 | @title[Code Presenting] 2 | ## Code 3 | ## Presenting 4 | 5 | --- 6 | @title[Fenced Code Blocks] 7 | 8 | ### `Fenced Code Blocks` 9 | #### Snippets embedded in your Markdown 10 | 11 | @fa[arrow-down] 12 | 13 | +++?color=#272c34 14 | @title[Sample Code Block] 15 | 16 | ```python 17 | from time import localtime 18 | 19 | activities = {8: 'Sleeping', 9: 'Commuting', 17: 'Working', 20 | 18: 'Commuting', 20: 'Eating', 22: 'Resting' } 21 | 22 | time_now = localtime() 23 | hour = time_now.tm_hour 24 | 25 | for activity_time in sorted(activities.keys()): 26 | if hour < activity_time: 27 | print activities[activity_time] 28 | break 29 | else: 30 | print 'Unknown, AFK or sleeping!' 31 | ``` 32 | 33 | @snap[south span-100 text-06] 34 | Fenced code blocks let you present **code snippets** with auto-syntax highlighting. 35 | @snapend 36 | 37 | --- 38 | 39 | ### `Fenced Code Blocks` 40 | ### Code Presenting 41 | 42 | @fa[arrow-down] 43 | 44 | +++?color=#272c34 45 | @title[Sample Code Presenting] 46 | 47 | ```go 48 | package main 49 | 50 | import ( 51 | "fmt" 52 | "golang.org/x/net/websocket" 53 | "log" 54 | "net/http" 55 | ) 56 | 57 | func echoHandler(ws *websocket.Conn) { 58 | msg := make([]byte, 512) 59 | n, err := ws.Read(msg) 60 | if err != nil { 61 | log.Fatal(err) 62 | } 63 | fmt.Printf("Receive: %s\n", msg[:n]) 64 | 65 | m, err := ws.Write(msg[:n]) 66 | if err != nil { 67 | log.Fatal(err) 68 | } 69 | fmt.Printf("Send: %s\n", msg[:m]) 70 | } 71 | ``` 72 | 73 | 74 | @[1] 75 | @[3-8] 76 | @[10-24] 77 | 78 | @snap[south span-100 text-06] 79 | Use code-presenting to *step-into* code directly on your slides. 80 | @snapend 81 | 82 | 83 | --- 84 | 85 | ### `Fenced Code Blocks` 86 | ### Code Presenting 87 | ### With Annotations 88 | 89 | @fa[arrow-down] 90 | 91 | +++?color=#272c34 92 | @title[Sample With Annotations] 93 | 94 | ```python 95 | from time import localtime 96 | 97 | activities = {8: 'Sleeping', 9: 'Commuting', 17: 'Working', 98 | 18: 'Commuting', 20: 'Eating', 22: 'Resting' } 99 | 100 | time_now = localtime() 101 | hour = time_now.tm_hour 102 | 103 | for activity_time in sorted(activities.keys()): 104 | if hour < activity_time: 105 | print activities[activity_time] 106 | break 107 | else: 108 | print 'Unknown, AFK or sleeping!' 109 | ``` 110 | 111 | @snap[south span-100 text-09] 112 | @[1](Use code presenting annotations to document indivudual lines of code.) 113 | @[6-7](Or focus your audience's attention on entire blocks of code.) 114 | @[9-14](You can even *step-into* loops to demonstrate logical code flow.) 115 | @snapend 116 | 117 | --- 118 | @title[Code Delimiters] 119 | 120 | ### `Code Delimiters` 121 | #### For any file in your Repo 122 | 123 | ``` 124 | ---?code=path/to/source.file 125 | ``` 126 | 127 | @fa[arrow-down] 128 | 129 | +++?code=src/elixir/monitor.ex&lang=elixir&color=#272c34 130 | @title[Sample With Annotations] 131 | 132 | @snap[south span-100 text-08] 133 | @[11-14](Code delimters deliver powerful code presenting features on your slides.) 134 | @[22-28](You can *step-into* any repo source file and focus on any line or block of code.) 135 | @[179-185](With optional annotations to help guide or educate your audience.) 136 | @snapend 137 | 138 | --- 139 | @title[Pro Code Step-and-Zoom] 140 | 141 | @snap[west span-45 text-08] 142 | For even more powerful GitPitch code presenting features learn 143 | about
[Pro Code Step-and-Zoom @fa[external-link]](https://gitpitch.com/docs/code-features/pro-code-zoom) 144 | @snapend 145 | 146 | @img[east span-45](assets/img/pro-code-step-and-zoom.gif) 147 | 148 | --- 149 | 150 | ### Learn By Example 151 | #### View The [Presentation Markdown](https://github.com/gitpitch/code-presenting/blob/master/PITCHME.md) 152 | 153 | @snap[south span-100 text-06] 154 | For complete Code Presenting documentation, see the [GitPitch Docs @fa[external-link]](https://gitpitch.com/docs/code-features) 155 | @snapend 156 | -------------------------------------------------------------------------------- /PITCHME.yaml: -------------------------------------------------------------------------------- 1 | theme: template 2 | highlight: tomorrow 3 | logo: assets/img/logo.png 4 | published : true 5 | footnote: "GitPitch Live Code Presenting Feature Demo" 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![GitPitch](https://gitpitch.com/assets/badge.svg)](https://gitpitch.com/gitpitch/code-presenting) 2 | 3 | # Blog Post - Let Your Code Do The Talking 4 | 5 | This repository contains the companion presentation for the GitPitch [Let Your Code Do The Talking](https://medium.com/@gitpitch/let-your-code-do-the-talking-983906a3a587) blog post on [Medium](https://medium.com/@gitpitch/let-your-code-do-the-talking-983906a3a587). -------------------------------------------------------------------------------- /assets/css/PITCHME.css: -------------------------------------------------------------------------------- 1 | /* Slide Deck Custom CSS Style Rules */ 2 | -------------------------------------------------------------------------------- /assets/down-arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gitpitch/code-presenting/9f18278e5042a0aa8f0f0ad7543df1f42005362c/assets/down-arrow.png -------------------------------------------------------------------------------- /assets/img/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gitpitch/code-presenting/9f18278e5042a0aa8f0f0ad7543df1f42005362c/assets/img/logo.png -------------------------------------------------------------------------------- /assets/img/pro-code-step-and-zoom.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gitpitch/code-presenting/9f18278e5042a0aa8f0f0ad7543df1f42005362c/assets/img/pro-code-step-and-zoom.gif -------------------------------------------------------------------------------- /src/elixir/monitor.ex: -------------------------------------------------------------------------------- 1 | defmodule GenMetrics.GenStage.Monitor do 2 | use GenServer 3 | alias GenMetrics.GenStage.Manager 4 | alias GenMetrics.GenStage.Monitor 5 | alias GenMetrics.GenStage.Pipeline 6 | alias GenMetrics.GenStage.Window 7 | alias GenMetrics.Reporter 8 | alias GenMetrics.Utils.Runtime 9 | 10 | @moduledoc false 11 | @handle_demand :handle_demand 12 | @handle_events :handle_events 13 | @handle_call :handle_call 14 | @handle_cast :handle_cast 15 | 16 | defstruct pipeline: %Pipeline{}, metrics: nil, start: 0, duration: 0 17 | 18 | def start_link(pipeline) do 19 | GenServer.start_link(__MODULE__, pipeline) 20 | end 21 | 22 | def init(pipeline) do 23 | with {:ok, _} <- validate_modules(pipeline), 24 | {:ok, _} <- validate_behaviours(pipeline), 25 | {:ok, _} <- activate_tracing(pipeline), 26 | state <- initialize_monitor(pipeline), 27 | do: start_monitor(state) 28 | end 29 | 30 | # 31 | # Handlers for intercepting :erlang.trace/3 and :erlang.trace_pattern/2 32 | # callbacks for modules registered on the pipeline. 33 | # 34 | 35 | def handle_info({:trace_ts, pid, :call, {mod, fun, [demand | _]}, ts}, state) do 36 | {:noreply, 37 | do_intercept_call_request(state, pid, {mod, fun}, demand, ts)} 38 | end 39 | 40 | # Intercept {:noreply, [event], new_state} response. 41 | def handle_info({:trace_ts, pid, :return_from, {mod, _, _}, 42 | {:noreply, events, _}, ts}, state) do 43 | {:noreply, 44 | do_intercept_call_response(state, mod, pid, length(events), ts)} 45 | end 46 | 47 | # Intercept {:noreply, [event], new_state, :hibernate} response. 48 | def handle_info({:trace_ts, pid, :return_from, {mod, _, _}, 49 | {:noreply, events, _, _}, ts}, state) do 50 | {:noreply, 51 | do_intercept_call_response(state, mod, pid, length(events), ts)} 52 | end 53 | 54 | # Intercept {:reply, _reply, [event], new_state} response. 55 | def handle_info({:trace_ts, pid, :return_from, {mod, _, _}, 56 | {:reply, _, events, _}, ts}, state) do 57 | {:noreply, 58 | do_intercept_call_response(state, mod, pid, length(events), ts)} 59 | end 60 | 61 | # Intercept {:reply, _reply, [event], new_state, :hibernate} response. 62 | def handle_info({:trace_ts, pid, :return_from, {mod, _, _}, 63 | {:noreply, _, events, _, _}, ts}, state) do 64 | {:noreply, 65 | do_intercept_call_response(state, mod, pid, length(events), ts)} 66 | end 67 | 68 | # Intercept {:stop, reason, new_state} response. 69 | def handle_info({:trace_ts, pid, :return_from, {mod, _, _}, 70 | {:stop, _, _}, ts}, state) do 71 | {:noreply, 72 | do_intercept_call_response(state, mod, pid, 0, ts)} 73 | end 74 | 75 | # Report and rollover metrics window. 76 | def handle_info(:rollover_metrics_window, state) do 77 | now = :erlang.system_time 78 | state = %Monitor{state | duration: Runtime.nano_to_milli(now - state.start)} 79 | window = Manager.as_window(state.metrics, 80 | Runtime.statistics?(state.pipeline), Runtime.sample_rate(state.pipeline)) 81 | window = %Window{window | pipeline: state.pipeline, 82 | start: state.start, duration: state.duration} 83 | Reporter.push(GenMetrics.GenStage.Reporter, window) 84 | Process.send_after(self(), 85 | :rollover_metrics_window, Runtime.window_interval(state.pipeline)) 86 | if Runtime.sampling?(state.pipeline) do 87 | activate_tracing(state.pipeline) 88 | Process.send_after(self(), 89 | :silence_metrics_window, Runtime.sample_interval(state.pipeline)) 90 | end 91 | {:noreply, initialize_monitor(state.pipeline, state.metrics)} 92 | end 93 | 94 | # Sampling window is closed for current metrics windows 95 | # so temporarily silence tracing. 96 | def handle_info(:silence_metrics_window, state) do 97 | activate_tracing(state.pipeline, true) 98 | {:noreply, state} 99 | end 100 | 101 | # Catch-all for calls not intercepted by monitor. 102 | def handle_info(_msg, state), do: {:noreply, state} 103 | 104 | # 105 | # Private utility functions follow. 106 | # 107 | 108 | # Initialize GenServer state for monitor. 109 | defp initialize_monitor(pipeline, metrics \\ nil) do 110 | if metrics do 111 | %Monitor{pipeline: pipeline, 112 | metrics: Manager.reinitialize(metrics), 113 | start: :erlang.system_time} 114 | else 115 | %Monitor{pipeline: pipeline, 116 | metrics: Manager.initialize(), 117 | start: :erlang.system_time} 118 | end 119 | end 120 | 121 | # Initialize periodic callback for metrics reporting and window rollover. 122 | defp start_monitor(state) do 123 | Process.send_after(self(), 124 | :rollover_metrics_window, Runtime.window_interval(state.pipeline)) 125 | if Runtime.sampling?(state.pipeline) do 126 | Process.send_after(self(), 127 | :silence_metrics_window, Runtime.sample_interval(state.pipeline)) 128 | end 129 | {:ok, state} 130 | end 131 | 132 | # Activate tracing for stages within pipeline. 133 | defp activate_tracing(pipeline, silent \\ false) do 134 | 135 | if silent do 136 | :erlang.trace(:processes, false, [:call, :monotonic_timestamp]) 137 | else 138 | :erlang.trace(:processes, true, [:call, :monotonic_timestamp]) 139 | 140 | for pmod <- pipeline.producer do 141 | :erlang.trace_pattern({pmod, :handle_demand, 2}, 142 | [{:_, [], [{:return_trace}]}]) 143 | :erlang.trace_pattern({pmod, :handle_cast, 2}, 144 | [{:_, [], [{:return_trace}]}]) 145 | if Runtime.synchronous?(pipeline) do 146 | :erlang.trace_pattern({pmod, :handle_call, 3}, 147 | [{:_, [], [{:return_trace}]}]) 148 | end 149 | end 150 | 151 | for pcmod <- pipeline.producer_consumer do 152 | :erlang.trace_pattern({pcmod, :handle_events, 3}, 153 | [{:_, [], [{:return_trace}]}]) 154 | :erlang.trace_pattern({pcmod, :handle_cast, 2}, 155 | [{:_, [], [{:return_trace}]}]) 156 | if Runtime.synchronous?(pipeline) do 157 | :erlang.trace_pattern({pcmod, :handle_call, 3}, 158 | [{:_, [], [{:return_trace}]}]) 159 | end 160 | end 161 | 162 | for cmod <- pipeline.consumer do 163 | :erlang.trace_pattern({cmod, :handle_events, 3}, 164 | [{:_, [], [{:return_trace}]}]) 165 | end 166 | end 167 | 168 | {:ok, pipeline} 169 | end 170 | 171 | # Validate pipeline modules can be loaded or report failures. 172 | defp validate_modules(pipeline) do 173 | case require_modules(pipeline) do 174 | [] -> {:ok, pipeline} 175 | errs -> {:stop, {:bad_pipeline, errs}} 176 | end 177 | end 178 | 179 | # Ensure pipeline modules are available and can be loaded. 180 | defp require_modules(pipeline) do 181 | [pipeline.producer, pipeline.producer_consumer, pipeline.consumer] 182 | |> Enum.flat_map(fn(modules) -> modules end) 183 | |> Enum.uniq 184 | |> Runtime.require_modules 185 | end 186 | 187 | # Validate pipeline modules implement GenStage or report failures. 188 | defp validate_behaviours(pipeline) do 189 | case require_behaviour(pipeline, GenStage) do 190 | [] -> {:ok, pipeline} 191 | errs -> {:stop, {:bad_pipeline, errs}} 192 | end 193 | end 194 | 195 | # Ensure pipeline modules implement GenStage behaviour. 196 | defp require_behaviour(pipeline, behaviour) do 197 | [pipeline.producer, pipeline.producer_consumer, pipeline.consumer] 198 | |> Enum.flat_map(fn(modules) -> modules end) 199 | |> Enum.uniq 200 | |> Runtime.require_behaviour(behaviour) 201 | end 202 | 203 | defp do_intercept_call_request(state, pid, {mod, fun}, demand, ts) do 204 | case fun do 205 | @handle_demand -> do_open_metric(state, mod, pid, demand, ts) 206 | @handle_events -> do_open_metric(state, mod, pid, length(demand), ts) 207 | @handle_call -> do_open_metric(state, mod, pid, 0, ts) 208 | @handle_cast -> do_open_metric(state, mod, pid, 0, ts) 209 | _ -> state 210 | end 211 | end 212 | 213 | defp do_intercept_call_response(state, mod, pid, events, ts) do 214 | do_close_metric(state, mod, pid, events, ts) 215 | end 216 | 217 | # Open partial metric on handle_ function call trace. 218 | defp do_open_metric(state, mod, pid, demand, ts) do 219 | metrics = 220 | Manager.open_summary_metric(state.metrics, mod, pid, demand, ts) 221 | state = %Monitor{state | metrics: metrics} 222 | 223 | if Runtime.statistics?(state.pipeline) do 224 | metrics = 225 | Manager.open_stats_metric(state.metrics, {mod, pid, demand, ts}) 226 | %Monitor{state | metrics: metrics} 227 | else 228 | state 229 | end 230 | end 231 | 232 | # Close complete metric on handle_ function return trace. 233 | defp do_close_metric(state, mod, pid, events, ts) do 234 | metrics = Manager.close_summary_metric(state.metrics, mod, pid, events, ts) 235 | state = %Monitor{state | metrics: metrics} 236 | 237 | if Runtime.statistics?(state.pipeline) do 238 | metrics = Manager.close_stats_metric(state.pipeline, 239 | state.metrics, {mod, pid, events, ts}) 240 | %Monitor{state | metrics: metrics} 241 | else 242 | state 243 | end 244 | end 245 | 246 | end 247 | -------------------------------------------------------------------------------- /src/go/server.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "golang.org/x/net/websocket" 6 | "log" 7 | "net/http" 8 | ) 9 | 10 | func echoHandler(ws *websocket.Conn) { 11 | msg := make([]byte, 512) 12 | n, err := ws.Read(msg) 13 | if err != nil { 14 | log.Fatal(err) 15 | } 16 | fmt.Printf("Receive: %s\n", msg[:n]) 17 | 18 | m, err := ws.Write(msg[:n]) 19 | if err != nil { 20 | log.Fatal(err) 21 | } 22 | fmt.Printf("Send: %s\n", msg[:m]) 23 | } 24 | 25 | func main() { 26 | http.Handle("/echo", websocket.Handler(echoHandler)) 27 | err := http.ListenAndServe(":8080", nil) 28 | if err != nil { 29 | panic("ListenAndServe: " + err.Error()) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/javascript/config.js: -------------------------------------------------------------------------------- 1 | var url = require('url'); 2 | var config = module.exports = {}; 3 | var redisToGoConfig; 4 | 5 | config.server = { 6 | host: '0.0.0.0', 7 | port: process.env.PORT || 3000 8 | }; 9 | 10 | config.redis = { 11 | host: 'localhost', 12 | port: 6379, 13 | options: { 14 | 15 | } 16 | }; 17 | 18 | if (process.env.REDISTOGO_URL) { 19 | redisToGoConfig = url.parse(process.env.REDISTOGO_URL); 20 | config.redis.port = redisToGoConfig.port; 21 | config.redis.host = redisToGoConfig.hostname; 22 | config.redis.options.auth_pass = 23 | redisToGoConfig.auth.split(':')[1]; 24 | } 25 | -------------------------------------------------------------------------------- /src/python/time.py: -------------------------------------------------------------------------------- 1 | from time import localtime 2 | 3 | activities = {8: 'Sleeping', 9: 'Commuting', 17: 'Working', 4 | 18: 'Commuting', 20: 'Eating', 22: 'Resting' } 5 | 6 | time_now = localtime() 7 | hour = time_now.tm_hour 8 | 9 | for activity_time in sorted(activities.keys()): 10 | if hour < activity_time: 11 | print activities[activity_time] 12 | break 13 | else: 14 | print 'Unknown, AFK or sleeping!' 15 | --------------------------------------------------------------------------------