"""filter all the duplicate downloads per user for each hour intervall"""
forkinkeys:
id_bibdocs=run_sql("select distinct id_bibdoc from rnkDOWNLOADS where id_bibrec=%s"%k)
forbibdocinid_bibdocs:
values=run_sql("""select DATE_FORMAT(download_time,"%%Y-%%m-%%d %%H"), client_host from rnkDOWNLOADS where id_bibrec=%s and id_bibdoc=%s and download_time >=\"%s\";"""%(k,bibdoc[0],last_updated))
forvalinvalues:
date_res=val[0]
date1="%s:00:00"%(date_res)
date2=compute_next_hour(date_res)
duplicates=(run_sql("select count(*) from rnkDOWNLOADS where id_bibrec=%s and id_bibdoc=%s and download_time>='%s' and download_time<'%s' and client_host=%s;"%(k,bibdoc[0],date1,date2,val[1]))[0][0])-1
run_sql("delete from rnkDOWNLOADS where id_bibrec=%s and id_bibdoc=%s and download_time>='%s' and download_time<'%s' and client_host=%s limit %s;"%(k,bibdoc[0],date1,date2,val[1],duplicates))
deffilter_downloads_per_hour(keys,last_updated):
"""filter all the duplicate downloads per user for each hour intervall"""
forkinkeys:
values=run_sql("""select DATE_FORMAT(download_time,"%%Y-%%m-%%d %%H"), client_host from rnkDOWNLOADS where id_bibrec=%s and download_time >=\"%s\";"""%(k,last_updated))
forvalinvalues:
date_res=val[0]
date1="%s:00:00"%(date_res)
date2=compute_next_hour(date_res)
duplicates=(run_sql("select count(*) from rnkDOWNLOADS where id_bibrec=%s and download_time>='%s' and download_time<'%s' and client_host=%s;"%(k,date1,date2,val[1]))[0][0])-1
run_sql("delete from rnkDOWNLOADS where id_bibrec=%s and download_time>='%s' and download_time<'%s' and client_host=%s limit %s;"%(k,date1,date2,val[1],duplicates))
defcompute_next_hour(date_res):
"""treat the change of the year, of (special)month etc.. and return the date in database format"""
res2=run_sql("select id_bibrec,count(*) from rnkDOWNLOADS where client_host in %s and id_bibrec in %s and id_bibrec != %s group by id_bibrec;"%(tuple_client_host,tuple_string_id_bibrec_list,j))#0.0023 par requete
list_tuple.append(list(res2))
dic_result[j]=list_tuple[0]
#merge new values with old dictionnary
returnmerge_with_old_dictionnary(dic,dic_result)
defmerge_with_old_dictionnary(old_dic,new_dic):
"""For each key id_bibrec in new_dic add the old values contained in old_dic