1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798(*————————————————————————————————————————————————————————————————————————————
Copyright (c) 2020–2021 Craig Ferguson <me@craigfe.io>
Distributed under the MIT license. See terms at the end of this file.
————————————————————————————————————————————————————————————————————————————*)open!Importtype'at={data:'aarray;timestamps:Mtime.tarray;max_length:int;mutablemost_recently_added:int;mutablelength:int;get_time:unit->Mtime.t;elt:(moduleInteger.Swithtypet='a)}letcreate(typea)~clock:get_time~size~elt:at=ifsize<=0thenFmt.invalid_arg"Flow_meter.create: non-positive size %d"size;(* We need [n + 1] timestamp samples to integrate over [n] values. *)letmax_length=size+1inletstart_time=get_time()in{get_time;data=Array.makemax_length(Obj.magicNone);timestamps=Array.makemax_lengthstart_time;max_length;most_recently_added=-1;length=0;elt}letis_emptyt=t.most_recently_added=-1letpusht~key~data=t.data.(key)<-data;t.timestamps.(key)<-t.get_time()letrecordtdata=ift.length=t.max_lengththen((* Buffer is full. Overwrite the oldest value. *)letnext=(t.most_recently_added+1)modt.lengthinpusht~key:next~data;t.most_recently_added<-next)else((* Increase the buffer size *)pusht~key:t.length~data;t.most_recently_added<-t.length;t.length<-succt.length)letoldest_indext=ift.length=t.max_lengththen(t.most_recently_added+1)modt.lengthelse0letfold=letrecauxdatafacc=function|-1->acc|n->auxdataf(faccdata.(n))(n-1)infunt~f~init->auxt.datafinit(t.length-1)letper_second:typea.at->float=funt->let(moduleInteger)=t.eltinifis_emptytthenFloat.zeroelse(* Sum all values in the window {i except the first one} and divide by the
time interval. We can think of the first value as representing work done
just {i before} the time interval starts, so using a half-open sample
correctly avoids over-reporting the flow-rate. *)letoldest_index=oldest_indextinletsum=Integer.sub(foldt~f:Integer.add~init:Integer.zero)t.data.(oldest_index)inletinterval=letstart_time=t.timestamps.(oldest_index)inletend_time=t.timestamps.(t.most_recently_added)inMtime.span_to_s(Mtime.spanstart_timeend_time)inifFloat.compareintervalFloat.epsilon<0thenFloat.zeroelseInteger.to_floatsum/.interval(*————————————————————————————————————————————————————————————————————————————
Copyright (c) 2020–2021 Craig Ferguson <me@craigfe.io>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
————————————————————————————————————————————————————————————————————————————*)