Program to monitor web traffic – day and week basis « Ajeet Singh
We need to design a function to gives real time statistics of our web traffic, like count for the day's website visit,count one week's website visit.
I am targeting an algorithm for O(1) complexity with minimum required memory. So after spending some time in design of a fancy data structure I finalize this simplest approach.
-- have to be synchronized.
-- Find better solution.
Read full article from Program to monitor web traffic – day and week basis « Ajeet Singh
We need to design a function to gives real time statistics of our web traffic, like count for the day's website visit,count one week's website visit.
I am targeting an algorithm for O(1) complexity with minimum required memory. So after spending some time in design of a fancy data structure I finalize this simplest approach.
-- have to be synchronized.
-- Find better solution.
Class CyclicBuffer { private final int [] buffer; private final int weeksCounter = 0 ; private int lastDay; public CyclicBuffer(){ buffer = new int [ 7 ]; } public void increaseHitsForTheDay( int day){ if (lastDay+ 1 == day){ weeksCounter = weeksCounter - buffer[day]; buffer[day] = 0 ; } ++buffer[day]; ++weeksCounter; lastDay = day; } public int getHistForTheDay( int day){ return buffer[day]; } public int getHistForCurrentWeek(){ return weeksCounter; } } |