Update app.py
Browse files
app.py
CHANGED
@@ -12,22 +12,13 @@ def machine_id(id):
|
|
12 |
return (id >> 12) & 0b1111111111
|
13 |
|
14 |
|
15 |
-
def server_id(id):
|
16 |
-
return (id >> 12) & 0b11111
|
17 |
-
|
18 |
-
|
19 |
-
def datacenter_id(id):
|
20 |
-
return (id >> 17) & 0b11111
|
21 |
-
|
22 |
-
|
23 |
def creation_time(id):
|
24 |
return id >> 22
|
25 |
|
26 |
|
27 |
def snow_to_components(snowflake_id: int):
|
28 |
timestamp_ms = creation_time(snowflake_id) + SNOWFLAKE_EPOCH
|
29 |
-
datacenter_id_val = datacenter_id(snowflake_id)
|
30 |
-
server_id_val = server_id(snowflake_id)
|
31 |
machine_id_val = machine_id(snowflake_id)
|
32 |
sequence_id_val = sequence_id(snowflake_id)
|
33 |
readable_time = time.strftime(
|
@@ -36,8 +27,6 @@ def snow_to_components(snowflake_id: int):
|
|
36 |
return (
|
37 |
readable_time,
|
38 |
timestamp_ms,
|
39 |
-
datacenter_id_val,
|
40 |
-
server_id_val,
|
41 |
machine_id_val,
|
42 |
sequence_id_val,
|
43 |
)
|
@@ -45,49 +34,30 @@ def snow_to_components(snowflake_id: int):
|
|
45 |
|
46 |
def components_to_snow(
|
47 |
timestamp_ms: int,
|
48 |
-
datacenter_id_val: int,
|
49 |
-
server_id_val: int,
|
50 |
machine_id_val: int,
|
51 |
sequence_id_val: int,
|
52 |
):
|
53 |
-
timestamp_bits = bin(
|
54 |
-
|
55 |
-
|
56 |
-
machine_id_bits = bin(machine_id_val)[2:].zfill(10)
|
57 |
-
sequence_bits = bin(sequence_id_val)[2:].zfill(12)
|
58 |
-
binary_snowflake = (
|
59 |
-
timestamp_bits
|
60 |
-
+ machine_id_bits
|
61 |
-
+ server_id_bits
|
62 |
-
+ datacenter_id_bits
|
63 |
-
+ sequence_bits
|
64 |
-
)
|
65 |
-
snowflake_id = int(binary_snowflake, 2)
|
66 |
-
return snowflake_id
|
67 |
|
68 |
|
69 |
def update_components(snowflake_input_str):
|
70 |
if snowflake_input_str:
|
71 |
snowflake_input = int(snowflake_input_str)
|
72 |
return snow_to_components(snowflake_input)
|
73 |
-
return None, None, None, None, None
|
74 |
|
75 |
|
76 |
def update_snowflake(
|
77 |
_,
|
78 |
timestamp_input,
|
79 |
-
datacenter_input,
|
80 |
-
server_input,
|
81 |
machine_input,
|
82 |
sequence_input,
|
83 |
):
|
84 |
-
if all(
|
85 |
-
[timestamp_input, datacenter_input, server_input, machine_input, sequence_input]
|
86 |
-
):
|
87 |
snowflake_id = components_to_snow(
|
88 |
int(timestamp_input),
|
89 |
-
int(datacenter_input),
|
90 |
-
int(server_input),
|
91 |
int(machine_input),
|
92 |
int(sequence_input),
|
93 |
)
|
@@ -101,21 +71,17 @@ with gr.Blocks() as demo:
|
|
101 |
snowflake = gr.Textbox(label="Snowflake ID", interactive=True)
|
102 |
with gr.Accordion("Snowflake ID Components", open=False):
|
103 |
readable_time = gr.Textbox(label="Timestamp (Readable)", interactive=False)
|
104 |
-
timestamp = gr.Number(label="Timestamp (ms)", interactive=True)
|
105 |
-
datacenter = gr.Number(0, label="Datacenter ID", step=1, interactive=True)
|
106 |
-
server = gr.Number(0, label="Server ID", step=1, interactive=True)
|
107 |
machine = gr.Number(0, label="Machine ID", step=1, interactive=True)
|
108 |
sequence = gr.Number(0, label="Sequence ID", step=1, interactive=True)
|
109 |
id_components = [
|
110 |
readable_time,
|
111 |
timestamp,
|
112 |
-
datacenter,
|
113 |
-
server,
|
114 |
machine,
|
115 |
sequence,
|
116 |
]
|
117 |
snowflake.input(update_components, inputs=snowflake, outputs=id_components)
|
118 |
-
for component in [timestamp,
|
119 |
component.input(update_snowflake, inputs=id_components, outputs=snowflake)
|
120 |
|
121 |
-
demo.launch()
|
|
|
12 |
return (id >> 12) & 0b1111111111
|
13 |
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def creation_time(id):
|
16 |
return id >> 22
|
17 |
|
18 |
|
19 |
def snow_to_components(snowflake_id: int):
|
20 |
timestamp_ms = creation_time(snowflake_id) + SNOWFLAKE_EPOCH
|
21 |
+
# datacenter_id_val = datacenter_id(snowflake_id)
|
|
|
22 |
machine_id_val = machine_id(snowflake_id)
|
23 |
sequence_id_val = sequence_id(snowflake_id)
|
24 |
readable_time = time.strftime(
|
|
|
27 |
return (
|
28 |
readable_time,
|
29 |
timestamp_ms,
|
|
|
|
|
30 |
machine_id_val,
|
31 |
sequence_id_val,
|
32 |
)
|
|
|
34 |
|
35 |
def components_to_snow(
|
36 |
timestamp_ms: int,
|
|
|
|
|
37 |
machine_id_val: int,
|
38 |
sequence_id_val: int,
|
39 |
):
|
40 |
+
# timestamp_bits = bin()[2:].zfill(41)
|
41 |
+
time_component = bin(int(timestamp_ms) - SNOWFLAKE_EPOCH)
|
42 |
+
return int(time_component[2:].zfill(42) + bin(machine_id_val)[2:].zfill(10) + bin(sequence_id_val)[2:].zfill(12),2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
|
45 |
def update_components(snowflake_input_str):
|
46 |
if snowflake_input_str:
|
47 |
snowflake_input = int(snowflake_input_str)
|
48 |
return snow_to_components(snowflake_input)
|
49 |
+
return None, None, None, None, None
|
50 |
|
51 |
|
52 |
def update_snowflake(
|
53 |
_,
|
54 |
timestamp_input,
|
|
|
|
|
55 |
machine_input,
|
56 |
sequence_input,
|
57 |
):
|
58 |
+
if all([timestamp_input, machine_input, sequence_input]):
|
|
|
|
|
59 |
snowflake_id = components_to_snow(
|
60 |
int(timestamp_input),
|
|
|
|
|
61 |
int(machine_input),
|
62 |
int(sequence_input),
|
63 |
)
|
|
|
71 |
snowflake = gr.Textbox(label="Snowflake ID", interactive=True)
|
72 |
with gr.Accordion("Snowflake ID Components", open=False):
|
73 |
readable_time = gr.Textbox(label="Timestamp (Readable)", interactive=False)
|
74 |
+
timestamp = gr.Number(0,label="Timestamp (ms)", interactive=True)
|
|
|
|
|
75 |
machine = gr.Number(0, label="Machine ID", step=1, interactive=True)
|
76 |
sequence = gr.Number(0, label="Sequence ID", step=1, interactive=True)
|
77 |
id_components = [
|
78 |
readable_time,
|
79 |
timestamp,
|
|
|
|
|
80 |
machine,
|
81 |
sequence,
|
82 |
]
|
83 |
snowflake.input(update_components, inputs=snowflake, outputs=id_components)
|
84 |
+
for component in [timestamp, machine, sequence]:
|
85 |
component.input(update_snowflake, inputs=id_components, outputs=snowflake)
|
86 |
|
87 |
+
demo.launch()
|