Skip to content

Commit 0692f25

Browse files
committed
update script
1 parent a973469 commit 0692f25

File tree

7 files changed

+756
-16
lines changed

7 files changed

+756
-16
lines changed

src/broadcast_stage.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,4 @@ mod tests {
426426
assert_eq!(highest_index, 2 * leader_rotation_interval - 1);
427427
}
428428
}
429+

src/loopfiles.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,38 @@ def loopfile():
1313
file_object.seek(0)
1414
# If file is not empty then append '\n'
1515
data = file_object.read(100)
16-
if len(data) > 0 :
17-
file_object.write("\n")
16+
17+
for last_line in file_object:
18+
pass
19+
print(filename)
20+
print(last_line)
21+
22+
# If the file is updated in the last cycle
23+
if last_line == "999":
24+
remove_999()
25+
else:
26+
append_999()
27+
#if len(data) > 0 :
28+
# file_object.write("\n")
1829
# Append text at the end of file
19-
#file_object.write("hello hi")
30+
#file_object.write("9371")
2031

2132
else:
2233
continue
2334

35+
def remove_999():
36+
print("Remove last line with 999")
37+
38+
def append_999():
39+
print("Append last line with 999")
40+
2441
def commit():
2542
os.system('git commit -a -m "merge and update" > /dev/null 2>&1')
2643

2744
def set_sys_time(year, month, day):
2845
os.system('date -s %04d%02d%02d' % (year, month, day))
2946

3047
if __name__ == '__main__':
31-
set_sys_time(2017, 1, 1)
48+
# set_sys_time(2017, 1, 1)
3249
loopfile()
33-
commit()
50+
# commit()

src/update.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os
33

44

5-
65
def modify():
76
file = open('zero.md', 'r')
87
flag = int(file.readline()) == 0
@@ -22,18 +21,49 @@ def loopfile():
2221
print(os.path.join(directory, filename))
2322

2423
with open(filename, "a+") as file_object:
25-
# Move read cursor to the start of file.
26-
file_object.seek(0)
27-
# If file is not empty then append '\n'
28-
data = file_object.read(100)
29-
if len(data) > 0 :
30-
file_object.write("\n")
31-
# Append text at the end of file
32-
#file_object.write("hello hi")
33-
24+
# Move read cursor to the start of file.
25+
file_object.seek(0)
26+
# If file is not empty then append '\n'
27+
data = file_object.read(100)
28+
29+
for last_line in file_object:
30+
pass
31+
print(filename)
32+
print(last_line)
33+
file_object.close()
34+
# If the file is updated in the last cycle
35+
if last_line == " 01":
36+
remove_999(filename)
37+
# else:
38+
# append_999(filename)
39+
3440
else:
3541
continue
3642

43+
44+
def remove_999(filename):
45+
print("Remove last line with 999")
46+
fd = open(filename, "r")
47+
d = fd.read()
48+
fd.close()
49+
m = d.split("\n")
50+
s = "\n".join(m[:-1])
51+
fd = open(filename, "w+")
52+
for i in range(len(s)):
53+
fd.write(s[i])
54+
fd.close()
55+
56+
57+
def append_999(filename):
58+
print("Append last line with 999")
59+
with open(filename, "a+") as fil:
60+
fil.seek(0)
61+
data = fil.read(100)
62+
if len(data) > 0:
63+
fil.write("\n//999")
64+
fil.close()
65+
66+
3767
def commit():
3868
os.system('git commit -a -m "merge and update" > /dev/null 2>&1')
3969

@@ -55,4 +85,5 @@ def daily_commit(start_date, end_date):
5585

5686

5787
if __name__ == '__main__':
58-
daily_commit(datetime.date(2017, 8, 31), datetime.date(2017, 12, 28))
88+
# daily_commit(datetime.date(2017, 8, 31), datetime.date(2017, 12, 28))
89+
loopfile()

test_ground/dummyfile1.rs

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
use influx_db_client as influxdb;
2+
use metrics;
3+
use std::env;
4+
use std::sync::atomic::{AtomicUsize, Ordering};
5+
use timing;
6+
7+
const DEFAULT_METRICS_RATE: usize = 100;
8+
9+
pub struct Counter {
10+
pub name: &'static str,
11+
12+
pub counts: AtomicUsize,
13+
pub times: AtomicUsize,
14+
15+
pub lastlog: AtomicUsize,
16+
pub lograte: AtomicUsize,
17+
}
18+
19+
macro_rules! create_counter {
20+
($name:expr, $lograte:expr) => {
21+
Counter {
22+
name: $name,
23+
counts: AtomicUsize::new(0),
24+
times: AtomicUsize::new(0),
25+
lastlog: AtomicUsize::new(0),
26+
lograte: AtomicUsize::new($lograte),
27+
}
28+
};
29+
}
30+
31+
macro_rules! inc_counter {
32+
($name:expr, $count:expr) => {
33+
unsafe { $name.inc($count) };
34+
};
35+
}
36+
37+
macro_rules! inc_new_counter_info {
38+
($name:expr, $count:expr) => {{
39+
inc_new_counter!($name, $count, Level::Info, 0);
40+
}};
41+
($name:expr, $count:expr, $lograte:expr) => {{
42+
inc_new_counter!($name, $count, Level::Info, $lograte);
43+
}};
44+
}
45+
46+
macro_rules! inc_new_counter {
47+
($name:expr, $count:expr, $level:expr, $lograte:expr) => {{
48+
if log_enabled!($level) {
49+
static mut INC_NEW_COUNTER: Counter = create_counter!($name, $lograte);
50+
inc_counter!(INC_NEW_COUNTER, $count);
51+
}
52+
}};
53+
}
54+
55+
impl Counter {
56+
fn default_log_rate() -> usize {
57+
let v = env::var("XPZ_DEFAULT_METRICS_RATE")
58+
.map(|x| x.parse().unwrap_or(DEFAULT_METRICS_RATE))
59+
.unwrap_or(DEFAULT_METRICS_RATE);
60+
if v == 0 {
61+
DEFAULT_METRICS_RATE
62+
} else {
63+
v
64+
}
65+
}
66+
pub fn inc(&mut self, events: usize) {
67+
let counts = self.counts.fetch_add(events, Ordering::Relaxed);
68+
let times = self.times.fetch_add(1, Ordering::Relaxed);
69+
let mut lograte = self.lograte.load(Ordering::Relaxed);
70+
if lograte == 0 {
71+
lograte = Counter::default_log_rate();
72+
self.lograte.store(lograte, Ordering::Relaxed);
73+
}
74+
if times % lograte == 0 && times > 0 {
75+
let lastlog = self.lastlog.load(Ordering::Relaxed);
76+
info!(
77+
"COUNTER:{{\"name\": \"{}\", \"counts\": {}, \"samples\": {}, \"now\": {}, \"events\": {}}}",
78+
self.name,
79+
counts + events,
80+
times,
81+
timing::timestamp(),
82+
events,
83+
);
84+
metrics::submit(
85+
influxdb::Point::new(&format!("counter-{}", self.name))
86+
.add_field(
87+
"count",
88+
influxdb::Value::Integer(counts as i64 - lastlog as i64),
89+
).to_owned(),
90+
);
91+
self.lastlog
92+
.compare_and_swap(lastlog, counts, Ordering::Relaxed);
93+
}
94+
}
95+
}
96+
#[cfg(test)]
97+
mod tests {
98+
use counter::{Counter, DEFAULT_METRICS_RATE};
99+
use log::Level;
100+
use std::env;
101+
use std::sync::atomic::{AtomicUsize, Ordering};
102+
use std::sync::{Once, RwLock, ONCE_INIT};
103+
104+
fn get_env_lock() -> &'static RwLock<()> {
105+
static mut ENV_LOCK: Option<RwLock<()>> = None;
106+
static INIT_HOOK: Once = ONCE_INIT;
107+
108+
unsafe {
109+
INIT_HOOK.call_once(|| {
110+
ENV_LOCK = Some(RwLock::new(()));
111+
});
112+
&ENV_LOCK.as_ref().unwrap()
113+
}
114+
}
115+
116+
#[test]
117+
fn test_counter() {
118+
let _readlock = get_env_lock().read();
119+
static mut COUNTER: Counter = create_counter!("test", 100);
120+
let count = 1;
121+
inc_counter!(COUNTER, count);
122+
unsafe {
123+
assert_eq!(COUNTER.counts.load(Ordering::Relaxed), 1);
124+
assert_eq!(COUNTER.times.load(Ordering::Relaxed), 1);
125+
assert_eq!(COUNTER.lograte.load(Ordering::Relaxed), 100);
126+
assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 0);
127+
assert_eq!(COUNTER.name, "test");
128+
}
129+
for _ in 0..199 {
130+
inc_counter!(COUNTER, 2);
131+
}
132+
unsafe {
133+
assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 199);
134+
}
135+
inc_counter!(COUNTER, 2);
136+
unsafe {
137+
assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 399);
138+
}
139+
}
140+
#[test]
141+
fn test_inc_new_counter() {
142+
let _readlock = get_env_lock().read();
143+
inc_new_counter_info!("counter-1", 1);
144+
inc_new_counter_info!("counter-2", 1, 2);
145+
}
146+
#[test]
147+
fn test_lograte() {
148+
let _readlock = get_env_lock().read();
149+
assert_eq!(
150+
Counter::default_log_rate(),
151+
DEFAULT_METRICS_RATE,
152+
"default_log_rate() is {}, expected {}, XPZ_DEFAULT_METRICS_RATE environment variable set?",
153+
Counter::default_log_rate(),
154+
DEFAULT_METRICS_RATE,
155+
);
156+
static mut COUNTER: Counter = create_counter!("test_lograte", 0);
157+
inc_counter!(COUNTER, 2);
158+
unsafe {
159+
assert_eq!(
160+
COUNTER.lograte.load(Ordering::Relaxed),
161+
DEFAULT_METRICS_RATE
162+
);
163+
}
164+
}
165+
166+
#[test]
167+
fn test_lograte_env() {
168+
assert_ne!(DEFAULT_METRICS_RATE, 0);
169+
let _writelock = get_env_lock().write();
170+
static mut COUNTER: Counter = create_counter!("test_lograte_env", 0);
171+
env::set_var("XPZ_DEFAULT_METRICS_RATE", "50");
172+
inc_counter!(COUNTER, 2);
173+
unsafe {
174+
assert_eq!(COUNTER.lograte.load(Ordering::Relaxed), 50);
175+
}
176+
177+
static mut COUNTER2: Counter = create_counter!("test_lograte_env", 0);
178+
env::set_var("XPZ_DEFAULT_METRICS_RATE", "0");
179+
inc_counter!(COUNTER2, 2);
180+
unsafe {
181+
assert_eq!(
182+
COUNTER2.lograte.load(Ordering::Relaxed),
183+
DEFAULT_METRICS_RATE
184+
);
185+
}
186+
}
187+
}
188+
189+
01

0 commit comments

Comments
 (0)