def optimalWriteOrder():
 while MetricCache:
  (metric, datapoints) = MetricCache.drain_metric()
  dbFileExists = state.database.exists(metric)
  if ((not dbFileExists) and CREATE_BUCKET):
   if CREATE_BUCKET.drain(1):
    (yield (metric, datapoints, dbFileExists))
   continue
  (yield (metric, datapoints, dbFileExists))

def optimalWriteOrder ( ) : while MetricCache : ( metric , datapoints ) = MetricCache . drain_metric ( ) dbFileExists = state . database . exists ( metric ) if ( ( not dbFileExists ) and CREATE_BUCKET ) : if CREATE_BUCKET . drain ( 1 ) : ( yield ( metric , datapoints , dbFileExists ) ) continue ( yield ( metric , datapoints , dbFileExists ) )
generates metrics with the most cached values first and applies a soft rate limit on new metrics .