Design Log Storage System in Python

PythonServer Side ProgrammingProgramming

Suppose we have some logs, that each log contains a unique id and timestamp. The Timestamp is a string that has the format: Year:Month:Day:Hour:Minute: Second, for example,2019:01:01:23:59:59. All domains are zero-padded decimal numbers.

We have to design a log storage system to implement the following functions −

  • void Put(int id, string timestamp): This will take the log's unique id and timestamp, and it stores the log in the storage system.

  • int[] Retrieve(String start, String end, String granularity): This will return the id of logs whose timestamps are within the range from start to end parameters. The granularity parameter indicates the time level for consideration. For example, start = "2019:01:01:23:59:59", end = "2019:01:02:23:59:59", and granularity = "Day", it means that we need to find the logs within the range from Jan. 1st 2019 to Jan. 2nd 2019.

So, if the input is like

  • put(1, "2019:01:01:23:59:59");

  • put(2, "2019:01:01:22:59:59");

  • put(3, "2018:01:01:00:00:00");

  • retrieve("2018:01:01:01:01:01","2019:01:01:23:00:00","Year");

  • retrieve("2018:01:01:01:01:01","2019:01:01:23:00:00","Hour");

then the output will be [1,2,3] because we need to return all logs within range 2018 and 2019 and [1,2], because we need to return all logs start from 2018:01:01:01 to 2019:01:01:23, where log 3 is left outside the range.

To solve this, we will follow these steps −

  • Define the initializer.

  • logs := a new list

  • Define a function put(). This will take id, timestamp

  • insert id, timestamp at the end of logs

  • Define a function retrieve(). This will take s, e, gra

  • index := a map like {'Year':5, 'Month' : 8, 'Day' : 11, 'Hour' : 14, 'Minute' : 17, 'Second' :20}[gra]

  • start := s[from index 0 to index]

  • end := e[from index 0 to index]

  • return (tid for each tid, timestamp in logs if start <= timestamp[from index 0 to index] <= end)

Example

Let us see the following implementation to get better understanding −

 Live Demo

class LogSystem(object):
   def __init__(self):
      self.logs = []
   def put(self, id, timestamp):
      self.logs.append((id, timestamp))
   def retrieve(self, s, e, gra):
      index = {'Year':5, 'Month' : 8, 'Day' : 11, 'Hour' : 14, 'Minute' : 17, 'Second' :20}[gra]
      start = s[:index]
      end = e[:index]
      return (tid for tid, timestamp in self.logs if start <= timestamp[:index] <= end)
ob = LogSystem()
ob.put(1, "2019:01:01:23:59:59")
ob.put(2, "2019:01:01:22:59:59")
ob.put(3, "2018:01:01:00:00:00")
print(list(ob.retrieve("2018:01:01:01:01:01","2019:01:01:23:00:00","Year")))
print(list(ob.retrieve("2018:01:01:01:01:01","2019:01:01:23:00:00","Hour")))

Input

ob.put(1, "2019:01:01:23:59:59")
ob.put(2, "2019:01:01:22:59:59")
ob.put(3, "2018:01:01:00:00:00")
ob.retrieve("2018:01:01:01:01:01","2019:01:01:23:00:00","Year")
ob.retrieve("2018:01:01:01:01:01","2019:01:01:23:00:00","Hour")

Output

[1, 2, 3]
[1, 2]
raja
Published on 16-Nov-2020 14:31:35
Advertisements