{"metadata":{"id":"9147869f-b51e-4e80-9467-8016fe5bbf65","name":"rateControllerPR","user_save_timestamp":"1970-01-01T01:00:00.000Z","auto_save_timestamp":"1970-01-01T01:00:00.000Z","language_info":{"name":"scala","file_extension":"scala","codemirror_mode":"text/x-scala"},"trusted":true,"sparkNotebook":null,"customLocalRepo":null,"customRepos":null,"customDeps":null,"customImports":null,"customArgs":null,"customSparkConf":null,"customVars":null},"cells":[{"metadata":{"trusted":true,"input_collapsed":false,"collapsed":false,"id":"BA8B01A097484D07BD15EFB283AD9B5D"},"cell_type":"code","source":["/** Calculate the end value we will emit at the time `seconds`. */\n"," def valueAtSecond(seconds: Long, rowsPerSecond: Long, rampUpTimeSeconds: Long): Long = {\n"," // E.g., rampUpTimeSeconds = 4, rowsPerSecond = 10\n"," // Then speedDeltaPerSecond = 2\n"," //\n"," // seconds = 0 1 2 3 4 5 6\n"," // speed = 0 2 4 6 8 10 10 (speedDeltaPerSecond * seconds)\n"," // end value = 0 2 6 12 20 30 40 (0 + speedDeltaPerSecond * seconds) * (seconds + 1) / 2\n"," val speedDeltaPerSecond = rowsPerSecond / (rampUpTimeSeconds + 1)\n"," if (seconds <= rampUpTimeSeconds) {\n"," // Calculate \"(0 + speedDeltaPerSecond * seconds) * (seconds + 1) / 2\" in a special way to\n"," // avoid overflow\n"," if (seconds % 2 == 1) {\n"," (seconds + 1) / 2 * speedDeltaPerSecond * seconds\n"," } else {\n"," seconds / 2 * speedDeltaPerSecond * (seconds + 1)\n"," }\n"," } else {\n"," // rampUpPart is just a special case of the above formula: rampUpTimeSeconds == seconds\n"," val rampUpPart = valueAtSecond(rampUpTimeSeconds, rowsPerSecond, rampUpTimeSeconds)\n"," rampUpPart + (seconds - rampUpTimeSeconds) * rowsPerSecond\n"," }\n"," }\n"," "],"outputs":[{"name":"stdout","output_type":"stream","text":"valueAtSecond: (seconds: Long, rowsPerSecond: Long, rampUpTimeSeconds: Long)Long\n"},{"metadata":{},"data":{"text/html":""},"output_type":"execute_result","execution_count":1,"time":"Took: 1.163s, at 2018-05-02 20:57"}]},{"metadata":{"trusted":true,"input_collapsed":false,"collapsed":false,"id":"9353595D8F1947C0887B723B612BB8CF"},"cell_type":"code","source":[" def valueAtSecond2(seconds: Long, rowsPerSecond: Long, rampUpTimeSeconds: Long): Long = {\n"," // E.g., rampUpTimeSeconds = 4, rowsPerSecond = 10\n"," // Then speedDeltaPerSecond = 2\n"," //\n"," // seconds = 0 1 2 3 4 5 6\n"," // speed = 0 2 4 6 8 10 10 (speedDeltaPerSecond * seconds)\n"," // end value = 0 2 6 12 20 30 40 (0 + speedDeltaPerSecond * seconds) * (seconds + 1) / 2\n"," val speedDeltaPerSecond = math.max(1, rowsPerSecond / (rampUpTimeSeconds + 1))\n","\n"," // If rowsPerSecond is smaller than rampUpTimeSeconds, speed will exceed the rowsPerSecond in\n"," // the rampUpTimes, so we should delay the ramp up.\n"," // E.g., rampUpTimeSeconds = 10, rowsPerSecond = 6\n"," // Then speedDeltaPerSecond = 1\n"," //\n"," // seconds = 0 1 2 3 4 5 6 7 8 9 10 11\n"," // speed = 0 0 0 0 0 1 2 3 4 5 6 6\n"," // end value = 0 0 0 0 0 1 3 6 10 15 21 27\n"," val validSeconds = math.max(0, math.min(seconds, seconds - (rampUpTimeSeconds - rowsPerSecond)))\n","\n"," if (seconds <= rampUpTimeSeconds) {\n"," // Calculate \"(0 + speedDeltaPerSecond * seconds) * (seconds + 1) / 2\" in a special way to\n"," // avoid overflow\n"," if (validSeconds % 2 == 1) {\n"," (validSeconds + 1) / 2 * speedDeltaPerSecond * validSeconds\n"," } else {\n"," validSeconds / 2 * speedDeltaPerSecond * (validSeconds + 1)\n"," }\n"," } else {\n"," // rampUpPart is just a special case of the above formula: rampUpTimeSeconds == seconds\n"," val rampUpPart = valueAtSecond2(rampUpTimeSeconds, rowsPerSecond, rampUpTimeSeconds)\n"," rampUpPart + (seconds - rampUpTimeSeconds) * rowsPerSecond\n"," }\n"," }"],"outputs":[{"name":"stdout","output_type":"stream","text":"valueAtSecond2: (seconds: Long, rowsPerSecond: Long, rampUpTimeSeconds: Long)Long\n"},{"metadata":{},"data":{"text/html":""},"output_type":"execute_result","execution_count":2,"time":"Took: 1.017s, at 2018-05-02 20:57"}]},{"metadata":{"trusted":true,"input_collapsed":false,"collapsed":false,"presentation":{"tabs_state":"{\n \"tab_id\": \"#tab662667508-2\"\n}","pivot_chart_state":"{\n \"hiddenAttributes\": [],\n \"menuLimit\": 200,\n \"cols\": [],\n \"rows\": [],\n \"vals\": [],\n \"exclusions\": {},\n \"inclusions\": {},\n \"unusedAttrsVertical\": 85,\n \"autoSortUnusedAttrs\": false,\n \"inclusionsInfo\": {},\n \"aggregatorName\": \"Count\",\n \"rendererName\": \"Table\"\n}"},"id":"FDC3BF78C58A4C2B888C751A573401EE"},"cell_type":"code","source":["(0 to 101).map(x=> (x, valueAtSecond(x, rowsPerSecond = 10, rampUpTimeSeconds=100)))"],"outputs":[{"name":"stdout","output_type":"stream","text":"res9: scala.collection.immutable.IndexedSeq[(Int, Long)] = Vector((0,0), (1,0), (2,0), (3,0), (4,0), (5,0), (6,0), (7,0), (8,0), (9,0), (10,0), (11,0), (12,0), (13,0), (14,0), (15,0), (16,0), (17,0), (18,0), (19,0), (20,0), (21,0), (22,0), (23,0), (24,0), (25,0), (26,0), (27,0), (28,0), (29,0), (30,0), (31,0), (32,0), (33,0), (34,0), (35,0), (36,0), (37,0), (38,0), (39,0), (40,0), (41,0), (42,0), (43,0), (44,0), (45,0), (46,0), (47,0), (48,0), (49,0), (50,0), (51,0), (52,0), (53,0), (54,0), (55,0), (56,0), (57,0), (58,0), (59,0), (60,0), (61,0), (62,0), (63,0), (64,0), (65,0), (66,0), (67,0), (68,0), (69,0), (70,0), (71,0), (72,0), (73,0), (74,0), (75,0), (76,0), (77,0), (78,0), (79,0), (80,0), (81,0), (82,0), (83,0), (84,0), (85,0), (86,0), (87,0), (88,0), (89,0), (90,0), (91,0), (92,0..."},{"metadata":{},"data":{"text/html":"
\n \n
\n
\n
  • \n \n
  • \n \n
  • \n \n
  • \n \n
  • \n \n
\n\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n
"},"output_type":"execute_result","execution_count":6,"time":"Took: 1.018s, at 2018-05-02 20:58"}]},{"metadata":{"trusted":true,"input_collapsed":false,"collapsed":false,"presentation":{"tabs_state":"{\n \"tab_id\": \"#tab1187879623-2\"\n}","pivot_chart_state":"{\n \"hiddenAttributes\": [],\n \"menuLimit\": 200,\n \"cols\": [],\n \"rows\": [],\n \"vals\": [],\n \"exclusions\": {},\n \"inclusions\": {},\n \"unusedAttrsVertical\": 85,\n \"autoSortUnusedAttrs\": false,\n \"inclusionsInfo\": {},\n \"aggregatorName\": \"Count\",\n \"rendererName\": \"Table\"\n}"},"id":"9D4C1304A63F410985A434FEF2643570"},"cell_type":"code","source":["(0 to 101).map(x=> (x, valueAtSecond2(x, rowsPerSecond = 10, rampUpTimeSeconds=100)))"],"outputs":[{"name":"stdout","output_type":"stream","text":"res11: scala.collection.immutable.IndexedSeq[(Int, Long)] = Vector((0,0), (1,0), (2,0), (3,0), (4,0), (5,0), (6,0), (7,0), (8,0), (9,0), (10,0), (11,0), (12,0), (13,0), (14,0), (15,0), (16,0), (17,0), (18,0), (19,0), (20,0), (21,0), (22,0), (23,0), (24,0), (25,0), (26,0), (27,0), (28,0), (29,0), (30,0), (31,0), (32,0), (33,0), (34,0), (35,0), (36,0), (37,0), (38,0), (39,0), (40,0), (41,0), (42,0), (43,0), (44,0), (45,0), (46,0), (47,0), (48,0), (49,0), (50,0), (51,0), (52,0), (53,0), (54,0), (55,0), (56,0), (57,0), (58,0), (59,0), (60,0), (61,0), (62,0), (63,0), (64,0), (65,0), (66,0), (67,0), (68,0), (69,0), (70,0), (71,0), (72,0), (73,0), (74,0), (75,0), (76,0), (77,0), (78,0), (79,0), (80,0), (81,0), (82,0), (83,0), (84,0), (85,0), (86,0), (87,0), (88,0), (89,0), (90,0), (91,1), (92,..."},{"metadata":{},"data":{"text/html":"
\n \n
\n
\n
  • \n \n
  • \n \n
  • \n \n
  • \n \n
  • \n \n
\n\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n
"},"output_type":"execute_result","execution_count":7,"time":"Took: 0.968s, at 2018-05-02 20:59"}]},{"metadata":{"trusted":true,"input_collapsed":false,"collapsed":false,"presentation":{"tabs_state":"{\n \"tab_id\": \"#tab473456626-2\"\n}","pivot_chart_state":"{\n \"hiddenAttributes\": [],\n \"menuLimit\": 200,\n \"cols\": [],\n \"rows\": [],\n \"vals\": [],\n \"exclusions\": {},\n \"inclusions\": {},\n \"unusedAttrsVertical\": 85,\n \"autoSortUnusedAttrs\": false,\n \"inclusionsInfo\": {},\n \"aggregatorName\": \"Count\",\n \"rendererName\": \"Table\"\n}"},"id":"67248A15E4644C148E19D38E01DE6EEA"},"cell_type":"code","source":["(0 to 101).map(x=> (x, valueAtSecond2(x, rowsPerSecond = 101, rampUpTimeSeconds=100)))"],"outputs":[{"name":"stdout","output_type":"stream","text":"res13: scala.collection.immutable.IndexedSeq[(Int, Long)] = Vector((0,0), (1,1), (2,3), (3,6), (4,10), (5,15), (6,21), (7,28), (8,36), (9,45), (10,55), (11,66), (12,78), (13,91), (14,105), (15,120), (16,136), (17,153), (18,171), (19,190), (20,210), (21,231), (22,253), (23,276), (24,300), (25,325), (26,351), (27,378), (28,406), (29,435), (30,465), (31,496), (32,528), (33,561), (34,595), (35,630), (36,666), (37,703), (38,741), (39,780), (40,820), (41,861), (42,903), (43,946), (44,990), (45,1035), (46,1081), (47,1128), (48,1176), (49,1225), (50,1275), (51,1326), (52,1378), (53,1431), (54,1485), (55,1540), (56,1596), (57,1653), (58,1711), (59,1770), (60,1830), (61,1891), (62,1953), (63,2016), (64,2080), (65,2145), (66,2211), (67,2278), (68,2346), (69,2415), (70,2485), (71,2556), (72,2628), ..."},{"metadata":{},"data":{"text/html":"
\n \n
\n
\n
  • \n \n
  • \n \n
  • \n \n
  • \n \n
  • \n \n
\n\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n \n
\n

entries total
\n

\n
\n
\n
\n
\n
\n
"},"output_type":"execute_result","execution_count":8,"time":"Took: 0.921s, at 2018-05-02 21:07"}]},{"metadata":{"trusted":true,"input_collapsed":false,"collapsed":true,"id":"65B34096F2AA458D82A5041496E038A0"},"cell_type":"code","source":[""],"outputs":[]}],"nbformat":4}